-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test suite for CLI and enhance TODO tags
Introduced a comprehensive test suite for the CLI functionality using `pytest` and `unittest.mock.patch` to ensure robustness. Enhanced TODO tags with issue numbers for improved tracking and organization.
- Loading branch information
Showing
6 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from pathlib import Path | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from project_forge.cli import build | ||
from click.testing import CliRunner | ||
|
||
|
||
@pytest.fixture | ||
def runner(): | ||
"""The CLI runner.""" | ||
return CliRunner() | ||
|
||
|
||
@pytest.fixture | ||
def composition_path(tmp_path: Path): | ||
"""The path to a composition file.""" | ||
path = tmp_path / "composition.yaml" | ||
path.touch(exist_ok=True) | ||
return path | ||
|
||
|
||
class TestBuildCommand: | ||
""" | ||
Tests for the cli.build command. | ||
Doesn't test the implementation, just the CLI interface. | ||
""" | ||
|
||
@patch("project_forge.commands.build.build_project") | ||
def test_use_defaults_passed_to_build_project(self, mock_build_project, runner: CliRunner, composition_path: Path): | ||
"""The `use-defaults` flag is correctly passed to the `build_project` function.""" | ||
|
||
result = runner.invoke(build, [str(composition_path), "--use-defaults"]) | ||
|
||
if result.exit_code != 0: | ||
print(result.output) | ||
|
||
assert result.exit_code == 0 | ||
mock_build_project.assert_called_once_with( | ||
composition_path, output_dir=Path.cwd(), use_defaults=True, initial_context={} | ||
) | ||
|
||
@patch("project_forge.commands.build.build_project") | ||
def test_custom_output_dir_passed_to_build_project( | ||
self, mock_build_project, runner: CliRunner, composition_path: Path, tmp_path: Path | ||
): | ||
"""The `output-dir` option is correctly passed to the `build_project` function.`""" | ||
output_dir = tmp_path / "custom-dir" | ||
output_dir.mkdir(parents=True, exist_ok=True) | ||
|
||
result = runner.invoke(build, [str(composition_path), "--output-dir", str(output_dir)]) | ||
|
||
if result.exit_code != 0: | ||
print(result.output) | ||
|
||
assert result.exit_code == 0 | ||
mock_build_project.assert_called_once_with( | ||
composition_path, output_dir=output_dir, use_defaults=False, initial_context={} | ||
) | ||
|
||
@patch("project_forge.commands.build.build_project") | ||
@patch("project_forge.cli.parse_file") | ||
def test_data_file_adds_to_initial_context( | ||
self, mock_parse_file, mock_build_project, runner: CliRunner, composition_path: Path, tmp_path: Path | ||
): | ||
"""The parsed results of a data file are added to the initial context.""" | ||
data_file_path = tmp_path / "data.yaml" | ||
data_file_path.touch(exist_ok=True) | ||
mock_parse_file.return_value = {"key": "value"} | ||
|
||
result = runner.invoke(build, [str(composition_path), "--data-file", str(data_file_path)]) | ||
|
||
if result.exit_code != 0: | ||
print("OUTPUT:", result.output, result) | ||
|
||
assert result.exit_code == 0 | ||
|
||
mock_parse_file.assert_called_once_with(data_file_path) | ||
mock_build_project.assert_called_once_with( | ||
composition_path, output_dir=Path.cwd(), use_defaults=False, initial_context={"key": "value"} | ||
) | ||
|
||
@patch("project_forge.commands.build.build_project") | ||
def test_data_options_adds_to_initial_context(self, mock_build_project, runner: CliRunner, composition_path: Path): | ||
"""Data options are added to the initial context.""" | ||
|
||
result = runner.invoke(build, [str(composition_path), "-d", "key", "value"]) | ||
|
||
if result.exit_code != 0: | ||
print(result.output) | ||
|
||
assert result.exit_code == 0 | ||
mock_build_project.assert_called_once_with( | ||
composition_path, output_dir=Path.cwd(), use_defaults=False, initial_context={"key": "value"} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters