test: parser exceptions

This commit is contained in:
Xyphuz 2022-05-19 00:34:26 +08:00
parent 431e540d5d
commit 650fc9dc46
7 changed files with 42 additions and 14 deletions

View file

@ -1,7 +1,8 @@
exclude: |
(?x)^(
README.md|
LICENSE
LICENSE|
tests/in/invalid.yaml
)
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks

0
tests/in/000000.yaml Normal file
View file

3
tests/in/invalid.yaml Normal file
View file

@ -0,0 +1,3 @@
what-is-this:
"an invalid yaml"
"test purpose"

View file

@ -0,0 +1,3 @@
what-is-this:
- "a yaml file without services"
- "test purpose"

View file

@ -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")
)

18
tests/test_parser.py Normal file
View file

@ -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")

16
tests/test_service.py Normal file
View file

@ -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")
)