Merge pull request #43 from compose-viz/dev

Fix service root KeyError
This commit is contained in:
Xyphuz 2023-01-11 01:05:18 +08:00 committed by GitHub
commit 975e39ab56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 14 deletions

View file

@ -76,7 +76,9 @@ You need to install [Graphviz](https://graphviz.org/download/) to generate graph
#### Using `pip` #### Using `pip`
`pip install compose-viz` ```
pip install compose-viz
```
#### Using `.whl` #### Using `.whl`
@ -115,13 +117,13 @@ Check out the result [here](https://github.com/compose-viz/compose-viz/blob/main
### Options ### Options
| Option | Description | | Option | Description |
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | --------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `-o, --output-filename` | Output filename for the generated visualization file. [default: compose-viz] | | `-o, --output-filename FILENAME` | Output filename for the generated visualization file. [default: compose-viz] |
| `-m, --format` | Output format for the generated visualization file. See [supported formats](https://github.com/compose-viz/compose-viz/blob/main/compose_viz/models/viz_formats.py). [default: png] | | `-m, --format FORMAT` | Output format for the generated visualization file. See [supported formats](https://github.com/compose-viz/compose-viz/blob/main/compose_viz/models/viz_formats.py). [default: png] |
| `-r, --root-service` | Root of the service tree (convenient for large compose yamls) | | `-r, --root-service SERVICE_NAME` | Root of the service tree (convenient for large compose yamls) |
| `-v, --version` | Show the version of compose-viz. | | `-v, --version` | Show the version of compose-viz. |
| `--help` | Show help and exit. | | `--help` | Show help and exit. |
<p align="right">(<a href="#top">back to top</a>)</p> <p align="right">(<a href="#top">back to top</a>)</p>

View file

@ -26,13 +26,14 @@ class Parser:
return service_depends_on return service_depends_on
@staticmethod @staticmethod
def compile_dependencies(service_name: str, compose_data: spec.ComposeSpecification) -> List[str]: def compile_dependencies(service_name: str, services: Dict[Any, spec.Service], file_path: str) -> List[str]:
assert compose_data.services assert service_name in services, f"Service '{service_name}' not found in given compose file: '{file_path}'"
dependencies = [] dependencies = []
for dependency in Parser._unwrap_depends_on(compose_data.services[service_name].depends_on): for dependency in Parser._unwrap_depends_on(services[service_name].depends_on):
if dependency: if dependency:
dependencies.append(dependency) dependencies.append(dependency)
dependencies.extend(Parser.compile_dependencies(dependency, compose_data)) dependencies.extend(Parser.compile_dependencies(dependency, services, file_path))
return dependencies return dependencies
def parse(self, file_path: str, root_service: Optional[str] = None) -> Compose: def parse(self, file_path: str, root_service: Optional[str] = None) -> Compose:
@ -47,10 +48,11 @@ class Parser:
assert compose_data.services is not None, "No services found, aborting." assert compose_data.services is not None, "No services found, aborting."
root_dependencies: List[str] = list() root_dependencies: List[str] = []
if root_service: if root_service:
root_dependencies = Parser.compile_dependencies(root_service, compose_data) root_dependencies = Parser.compile_dependencies(root_service, compose_data.services, file_path)
root_dependencies.append(root_service) root_dependencies.append(root_service)
root_dependencies = list(set(root_dependencies))
for service_name, service_data in compose_data.services.items(): for service_name, service_data in compose_data.services.items():
service_name = str(service_name) service_name = str(service_name)

View file

@ -26,6 +26,7 @@ runner = CliRunner()
"tests/ymls/volumes/docker-compose.yml", "tests/ymls/volumes/docker-compose.yml",
"examples/full-stack-node-app/docker-compose.yml", "examples/full-stack-node-app/docker-compose.yml",
"examples/non-normative/docker-compose.yml", "examples/non-normative/docker-compose.yml",
"examples/voting-app/docker-compose.yml",
], ],
) )
def test_cli(test_file_path: str) -> None: def test_cli(test_file_path: str) -> None:

View file

@ -0,0 +1,32 @@
import os
from typer.testing import CliRunner
from compose_viz import cli
runner = CliRunner()
def test_root_service() -> None:
input_path = "examples/voting-app/docker-compose.yml"
output_filename = "compose-viz-test"
default_format = "png"
result = runner.invoke(cli.app, ["-r", "vote", "-o", output_filename, input_path])
assert result.exit_code == 0
assert f"Successfully parsed {input_path}\n" in result.stdout
assert os.path.exists(f"{output_filename}.{default_format}")
os.remove(f"{output_filename}.{default_format}")
def test_root_service_key_error() -> None:
input_path = "examples/voting-app/docker-compose.yml"
output_filename = "compose-viz-test"
default_format = "png"
result = runner.invoke(cli.app, ["-r", "not_exist_service", "-o", output_filename, input_path])
assert result.exit_code == 1
assert result.exception is not None
assert result.exception.args[0] == f"Service 'not_exist_service' not found in given compose file: '{input_path}'"
assert not os.path.exists(f"{output_filename}.{default_format}")