-
Notifications
You must be signed in to change notification settings - Fork 2
Look up config file in plain file dir and CWD #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,89 @@ | ||
| import os | ||
| import tempfile | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from plain2code_arguments import resolve_config_file | ||
| from plain2code_exceptions import AmbiguousConfigFileError | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def two_dirs(): | ||
| """Provide two separate temporary directories: one for the plain file, one as CWD.""" | ||
| with tempfile.TemporaryDirectory() as plain_dir: | ||
| with tempfile.TemporaryDirectory() as cwd: | ||
| yield plain_dir, cwd | ||
|
|
||
|
|
||
| def _plain_file(plain_dir): | ||
| return os.path.join(plain_dir, "module.plain") | ||
|
|
||
|
|
||
| def test_config_in_plain_file_dir_only(two_dirs): | ||
| plain_dir, cwd = two_dirs | ||
| config = Path(plain_dir) / "config.yaml" | ||
| config.write_text("verbose: true\n") | ||
|
|
||
| with patch("os.getcwd", return_value=cwd): | ||
| result = resolve_config_file("config.yaml", _plain_file(plain_dir)) | ||
|
|
||
| assert result == os.path.normpath(str(config)) | ||
|
|
||
|
|
||
| def test_config_in_cwd_only(two_dirs): | ||
| plain_dir, cwd = two_dirs | ||
| config = Path(cwd) / "config.yaml" | ||
| config.write_text("verbose: true\n") | ||
|
|
||
| with patch("os.getcwd", return_value=cwd): | ||
| result = resolve_config_file("config.yaml", _plain_file(plain_dir)) | ||
|
|
||
| assert result == os.path.normpath(str(config)) | ||
|
|
||
|
|
||
| def test_config_in_both_locations_raises(two_dirs): | ||
| plain_dir, cwd = two_dirs | ||
| (Path(plain_dir) / "config.yaml").write_text("verbose: true\n") | ||
| (Path(cwd) / "config.yaml").write_text("verbose: false\n") | ||
|
|
||
| with patch("os.getcwd", return_value=cwd): | ||
| with pytest.raises(AmbiguousConfigFileError) as exc_info: | ||
| resolve_config_file("config.yaml", _plain_file(plain_dir)) | ||
|
|
||
| assert plain_dir in str(exc_info.value) | ||
| assert cwd in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_config_not_found_returns_none(two_dirs): | ||
| plain_dir, cwd = two_dirs | ||
|
|
||
| with patch("os.getcwd", return_value=cwd): | ||
| result = resolve_config_file("config.yaml", _plain_file(plain_dir)) | ||
|
|
||
| assert result is None | ||
|
|
||
|
|
||
| def test_config_same_dir_no_error(): | ||
| """When the plain file and CWD are in the same directory, a single config file is fine.""" | ||
| with tempfile.TemporaryDirectory() as d: | ||
| config = Path(d) / "config.yaml" | ||
| config.write_text("verbose: true\n") | ||
|
|
||
| with patch("os.getcwd", return_value=d): | ||
| result = resolve_config_file("config.yaml", os.path.join(d, "module.plain")) | ||
|
|
||
| assert result == os.path.normpath(str(config)) | ||
|
|
||
|
|
||
| def test_custom_config_name_found_in_plain_file_dir(two_dirs): | ||
| """A custom --config-name is also looked up in the two locations.""" | ||
| plain_dir, cwd = two_dirs | ||
| config = Path(plain_dir) / "myconfig.yaml" | ||
| config.write_text("verbose: true\n") | ||
|
|
||
| with patch("os.getcwd", return_value=cwd): | ||
| result = resolve_config_file("myconfig.yaml", _plain_file(plain_dir)) | ||
|
|
||
| assert result == os.path.normpath(str(config)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.