From 650fc9dc46cb8ebdc420c298de416f7e80eb4aec Mon Sep 17 00:00:00 2001 From: Xyphuz Date: Thu, 19 May 2022 00:34:26 +0800 Subject: [PATCH] test: parser exceptions --- .pre-commit-config.yaml | 3 ++- tests/in/000000.yaml | 0 tests/in/invalid.yaml | 3 +++ tests/in/no-services.yaml | 3 +++ tests/test_extends.py | 13 ------------- tests/test_parser.py | 18 ++++++++++++++++++ tests/test_service.py | 16 ++++++++++++++++ 7 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 tests/in/000000.yaml create mode 100644 tests/in/invalid.yaml create mode 100644 tests/in/no-services.yaml create mode 100644 tests/test_parser.py create mode 100644 tests/test_service.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2fa471a..29b731d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,8 @@ exclude: | (?x)^( README.md| - LICENSE + LICENSE| + tests/in/invalid.yaml ) repos: - repo: https://github.com/pre-commit/pre-commit-hooks diff --git a/tests/in/000000.yaml b/tests/in/000000.yaml new file mode 100644 index 0000000..e69de29 diff --git a/tests/in/invalid.yaml b/tests/in/invalid.yaml new file mode 100644 index 0000000..8e7a231 --- /dev/null +++ b/tests/in/invalid.yaml @@ -0,0 +1,3 @@ +what-is-this: + "an invalid yaml" + "test purpose" diff --git a/tests/in/no-services.yaml b/tests/in/no-services.yaml new file mode 100644 index 0000000..a0bf4a1 --- /dev/null +++ b/tests/in/no-services.yaml @@ -0,0 +1,3 @@ +what-is-this: + - "a yaml file without services" + - "test purpose" diff --git a/tests/test_extends.py b/tests/test_extends.py index ffbe6f7..8b28cd1 100644 --- a/tests/test_extends.py +++ b/tests/test_extends.py @@ -1,7 +1,6 @@ import pytest from compose_viz.extends import Extends -from compose_viz.service import Service def test_extend_init(): @@ -15,15 +14,3 @@ def test_extend_init(): with pytest.raises(TypeError): Extends(from_file="tests/in/000001.yaml") # type: ignore - - -def test_service_init(): - with pytest.raises(ValueError, match=r"Both image and extends are not defined in service 'frontend', aborting."): - Service(name="frontend") - - with pytest.raises( - ValueError, match=r"Only one of image and extends can be defined in service 'frontend', aborting." - ): - Service( - name="frontend", image="image", extends=Extends(service_name="frontend", from_file="tests/in/000001.yaml") - ) diff --git a/tests/test_parser.py b/tests/test_parser.py new file mode 100644 index 0000000..e28a9f9 --- /dev/null +++ b/tests/test_parser.py @@ -0,0 +1,18 @@ +import pytest + +from compose_viz.parser import Parser + + +def test_parser_error_parsing_file(): + with pytest.raises(RuntimeError, match=r"Error parsing file 'tests/in/invalid.yaml'.*"): + Parser().parse("tests/in/invalid.yaml") + + +def test_parser_invalid_yaml(): + with pytest.raises(RuntimeError, match=r"Empty yaml file, aborting."): + Parser().parse("tests/in/000000.yaml") + + +def test_parser_no_services_found(): + with pytest.raises(RuntimeError, match=r"No services found, aborting."): + Parser().parse("tests/in/no-services.yaml") diff --git a/tests/test_service.py b/tests/test_service.py new file mode 100644 index 0000000..4006e2d --- /dev/null +++ b/tests/test_service.py @@ -0,0 +1,16 @@ +import pytest + +from compose_viz.extends import Extends +from compose_viz.service import Service + + +def test_service_init(): + with pytest.raises(ValueError, match=r"Both image and extends are not defined in service 'frontend', aborting."): + Service(name="frontend") + + with pytest.raises( + ValueError, match=r"Only one of image and extends can be defined in service 'frontend', aborting." + ): + Service( + name="frontend", image="image", extends=Extends(service_name="frontend", from_file="tests/in/000001.yaml") + )