diff --git a/test/test_config.py b/test/test_config.py index c2474e3..fda6340 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -6,15 +6,65 @@ :copyright: Copyright (c) 2023 `python-escpos `_ :license: MIT """ +import pathlib + +import appdirs +import pytest + +import escpos.exceptions -def generate_dummy_config(path): +def generate_dummy_config(path, content=None): """Generate a dummy config in path""" - dummy_config_content = "printer:\n type: Dummy\n" + dummy_config_content = content + if not content: + dummy_config_content = "printer:\n type: Dummy\n" path.write_text(dummy_config_content) assert path.read_text() == dummy_config_content +def simple_printer_test(config): + """Simple test for the dummy printer.""" + p = config.printer() + p._raw(b"1234") + + assert p.output == b"1234" + + +def test_config_load_with_invalid_config(tmp_path): + """Test the loading of a config with a invalid config file.""" + # generate a dummy config + config_file = tmp_path / "config.yaml" + generate_dummy_config(config_file, content="}invalid}yaml}") + + # test the config loading + from escpos import config + + c = config.Config() + with pytest.raises(escpos.exceptions.ConfigSyntaxError): + c.load(config_path=config_file) + + +def test_config_load_from_appdir(): + """Test the loading of a config in appdir.""" + from escpos import config + + # generate a dummy config + config_file = ( + pathlib.Path(appdirs.user_config_dir(config.Config._app_name)) + / config.Config._config_file + ) + + generate_dummy_config(config_file) + + # test the config loading + c = config.Config() + c.load() + + # test the resulting printer object + simple_printer_test(c) + + def test_config_load_with_file(tmp_path): """Test the loading of a config with a config file.""" # generate a dummy config @@ -26,13 +76,9 @@ def test_config_load_with_file(tmp_path): c = config.Config() c.load(config_path=config_file) - print(c._printer_config) # test the resulting printer object - p = c.printer() - p._raw(b"1234") - - assert p.output == b"1234" + simple_printer_test(c) def test_config_load_with_path(tmp_path): @@ -48,7 +94,4 @@ def test_config_load_with_path(tmp_path): c.load(config_path=tmp_path) # test the resulting printer object - p = c.printer() - p._raw(b"1234") - - assert p.output == b"1234" + simple_printer_test(c)