Skip to content

Draft: Add command behaviour tests#60

Open
MJGaughran wants to merge 28 commits into
mainfrom
add-command-behaviour-tests
Open

Draft: Add command behaviour tests#60
MJGaughran wants to merge 28 commits into
mainfrom
add-command-behaviour-tests

Conversation

@MJGaughran

@MJGaughran MJGaughran commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

These test the parts of functionality that are not adequately covered by the golden master tests, such as errors.

Code changes are added for:

  • Consistency in error handling
  • Removal of dead code (to identify coverage issues, indicated with comments)
  • Changed from_scratch -> allow_all in a function where original naming was incorrect

@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.48%. Comparing base (22028aa) to head (df673cc).

Additional details and impacted files
@@                       Coverage Diff                        @@
##           improve-golden-master-tests      #60       +/-   ##
================================================================
+ Coverage                        83.59%   99.48%   +15.89%     
================================================================
  Files                               27       27               
  Lines                              957      977       +20     
================================================================
+ Hits                               800      972      +172     
+ Misses                             157        5      -152     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

DEFAULT_VERSION_FILENAME = ".version"

DEPLOYMENT_SNAPSHOT_FILENAME = "deployment.yaml"
PREVIOUS_DEPLOYMENT_SNAPSHOT_FILENAME = "previous-deployment.yaml"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code (now uses git refs)


It should be the Module's name and version as /<config_folder>/<name>/<version>.yaml
"""
if version_path.is_dir() and version_path.suffix == YAML_FILE_SUFFIX:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code as _load_release() already checks if path.is_dir()

@MJGaughran MJGaughran force-pushed the add-command-behaviour-tests branch from e346540 to 5730578 Compare June 30, 2026 14:10
This covers cases that aren't easily tested with golden master tests.
Tests relating to non-existent output dir use the compare function as
otherwise the Typer configuration prevents the exception being raised.
This feature was replaced with the git ref option to the compare
command.
@MJGaughran MJGaughran force-pushed the add-command-behaviour-tests branch from 5730578 to df673cc Compare June 30, 2026 15:56
@MJGaughran MJGaughran changed the title Draft: Add command behaviour tests Add command behaviour tests Jun 30, 2026
@ptsOSL

ptsOSL commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Is the target branch supposed to be improve-golden-master-tests?

@ptsOSL ptsOSL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reviewed it as best I can. I will say there are over 1300 lines of code in this PR and I was losing concentration a bit towards the end. It might have been better to split up some of the tests into their own PRs.

f"Deployment snapshot not found at git ref:\n{ref}"
) from exc

with io.BytesIO(ref_snapshot.data_stream.read()) as snapshot_f: # type: ignore

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need # type: ignore ?

@MJGaughran MJGaughran Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although GitPython has type hints, it appears gitdb (a dependency) does not. I'll add a minor edit to remove the type ignore statement.

This is also just an indentation change.

Comment thread src/deploy_tools/sync.py
Comment on lines +67 to +69
with repo:
logger.info("Creating snapshot")
create_snapshot(deployment, layout)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this "with" needed now when it wasnt before? Does it actually do anything here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used in Python for context managers, and allows us to automatically handle the closing of the object. I'll probably split off the code improvements as it seems there were a few too many in this PR.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that its to handle context, I just havnt seen it used in a situation like this where the "repo" object has already been initialised earlier and already exists in the outer context.

Is the with section just opening a connection using the already created Repo instance and then after the "with" section, is the connection is closed? I would have expected something more like
with repo.open_connetion() as con

@MJGaughran MJGaughran Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just guaranteeing that repo.close() is called whenever you leave that block. The reason it can't do it the way you're describing is that the initialisation has an if/else block

I've looked at the options, and it seems this is the more common approach. You can, of course, create a function like "init_or_load_existing_repo" but that obviously has its own issues.

Comment thread tests/test_binary.py
Comment thread tests/test_cli.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name of this file is perhaps a bit confusing. This isn't the only test file which tests the cli, but its name suggests that its singular purpose is to test the cli. I suppose the difference is that in this file we call it with subprocess, while elsewhere we use the typer interface to call the cli. Maybe it could be renamed to clarify that? Also is there really any difference between using subprocess and the typer interface for the purpose of the tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference between subprocess and typer is that typer.testing.CLIRunner doesn't actually spawn a separate process, and uses the typer app directly. Since the error handling logic is in main(), we need to use subprocess to verify the output.

Although many other (but not all) tests consider the cli commands, this file includes the two that are generic across all commands. One checks the version info, and the other that the exceptions are being properly handled.

Do you have a recommendation for an alternative name?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like test_cli_configuration or test_cli_setup. In my mind the version check is just to check that the cli is setup correctly and the exception test is to check that the cli is setup with exception handling. I would also be happy with test_cli_basic just to make it obvious its not the only cli test file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rename to test_cli_common.py

Comment thread tests/test_compare.py
Comment thread tests/test_compare.py
)
deprecated_link.parent.mkdir(parents=True, exist_ok=True)
deprecated_link.touch()
with pytest.raises(ComparisonError, match="Duplicate modulefiles"):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these error messages report which modulefiles are duplicated, or does the user just have to figure this out?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed the tests to check for more of the error message, including e.g. the names of the modulefiles. It makes them a bit more brittle, but that's reasonable.

Comment thread tests/test_load.py
Comment on lines +13 to +14
("name-mismatch", "Module name .* does not match path"),
("version-mismatch", "Module version .* does not match path"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the .* in these messages to account for variables in string names? If so how comes you dont do that elsewhere? I have seen other error messages you check for where you only check for part of the error message. I assumed this was to avoid unresolved variables, but could you use .* in these cases as well? I may be misunderstanding something here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now done more consistently. .* is now used for e.g. the tmp dirs instead

@MJGaughran MJGaughran changed the title Add command behaviour tests Draft: Add command behaviour tests Jul 4, 2026
@MJGaughran

Copy link
Copy Markdown
Contributor Author

I've swapped it back to draft, I'll try and split this up into at least a couple separate PRs. I'll cover all the comments you raise here first, though

@MJGaughran MJGaughran removed the request for review from MichaelStubbings July 4, 2026 10:59
Base automatically changed from improve-golden-master-tests to main July 7, 2026 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants