-
Notifications
You must be signed in to change notification settings - Fork 12
Fix chdir and add tests for fsutils functions #45
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 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
56c05bd
Fix the chdir function, add fsutils tests, and fix issues found durin…
DavidHuber-NOAA ddfefc4
Restore default rmdir behavior
DavidHuber-NOAA b185b77
Improve error message
DavidHuber-NOAA b779504
Alphebetize imports
DavidHuber-NOAA eeb03d6
Allow configs to be in different order
DavidHuber-NOAA c5687cf
Assert that we come back to the original WD
DavidHuber-NOAA da60838
Add a more useful error message when a directory is missing
DavidHuber-NOAA 6524b8b
Assert that the chdir finally always executes
DavidHuber-NOAA 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| from wxflow import chdir, cp, get_gid, mkdir, rm_p, rmdir | ||
|
|
||
|
|
||
| def test_mkdir(tmp_path): | ||
| """ | ||
| Test for creating a directory: | ||
| Parameters | ||
| ---------- | ||
| tmp_path - pytest fixture | ||
| """ | ||
|
|
||
| dir_path = tmp_path / 'my_test_dir' | ||
| dir_path_bad = "/some/non-existent/path" | ||
|
|
||
| # Create the good path | ||
| mkdir(dir_path) | ||
|
|
||
| # Check if dir_path was created | ||
| assert os.path.exists(dir_path) | ||
|
|
||
| # Test that attempting to create a bad path raises an OSError | ||
| with pytest.raises(OSError): | ||
| mkdir(dir_path_bad) | ||
|
|
||
|
|
||
| def test_rmdir(tmp_path): | ||
| """ | ||
| Test for removing a directory: | ||
| Parameters | ||
| ---------- | ||
| tmp_path - pytest fixture | ||
| """ | ||
|
|
||
| dir_path = tmp_path / 'my_input_dir' | ||
| # Make and then delete the directory | ||
| mkdir(dir_path) | ||
| rmdir(dir_path) | ||
|
|
||
| # Assert that it was deleted | ||
| assert not os.path.exists(dir_path) | ||
|
|
||
| # Attempt to delete a non-existent path and ignore that it is missing | ||
| rmdir('/non-existent-path', missing_ok=True) | ||
|
|
||
| # Lastly, attempt to delete a non-existent directory and do not ignore the error | ||
| with pytest.raises(FileNotFoundError): | ||
| rmdir('/non-existent-path') | ||
|
|
||
|
|
||
| def test_chdir(tmp_path): | ||
| """ | ||
| Test for changing a directory: | ||
| Parameters | ||
| ---------- | ||
| tmp_path - pytest fixture | ||
| """ | ||
|
|
||
| dir_path = tmp_path / 'my_input_dir' | ||
| # Make the directory and navigate to it | ||
| mkdir(dir_path) | ||
|
|
||
| with chdir(dir_path): | ||
| assert os.getcwd() == os.path.abspath(dir_path) | ||
|
|
||
| # Now try to go somewhere that doesn't exist | ||
| with pytest.raises(OSError): | ||
| with chdir("/a/non-existent/path"): | ||
| raise AssertionError("Navigated to a non-existent path") | ||
|
|
||
|
|
||
| def test_rm_p(tmp_path): | ||
| """ | ||
| Test for removing a file | ||
| Parameters | ||
| ---------- | ||
| tmp_path - pytest fixture | ||
| """ | ||
|
|
||
| input_path = tmp_path / 'my_test_file.txt' | ||
| # Attempt to delete a non-existent file, ignoring any errors | ||
| rm_p(input_path) | ||
|
|
||
| # Now attempt to delete the same file but do not ignore errors | ||
| with pytest.raises(FileNotFoundError): | ||
| rm_p(input_path, missing_ok=False) | ||
|
|
||
| with open(input_path, "w") as f: | ||
| f.write("") | ||
|
|
||
| # Delete the file and assert it doesn't exist | ||
| rm_p(input_path) | ||
|
|
||
| assert not os.path.isfile(input_path) | ||
|
|
||
|
|
||
| def test_cp(tmp_path): | ||
| """ | ||
| Test copying a file: | ||
| Parameters | ||
| ---------- | ||
| tmp_path - pytest fixture | ||
| """ | ||
|
|
||
| input_path = tmp_path / 'my_test_file.txt' | ||
| output_path = tmp_path / 'my_output_file.txt' | ||
| # Attempt to copy a non-existent file | ||
| rm_p(input_path) # Delete it if present | ||
| with pytest.raises(OSError): | ||
| cp(input_path, output_path) | ||
|
|
||
| # Now create the input file and repeat | ||
| with open(input_path, "w") as f: | ||
| f.write("") | ||
|
|
||
| cp(input_path, output_path) | ||
|
|
||
| # Assert both files exist (make sure it wasn't moved). | ||
| assert os.path.isfile(output_path) | ||
| assert os.path.isfile(input_path) | ||
|
|
||
|
|
||
| def test_get_gid(): | ||
| """ | ||
| Test getting a group ID: | ||
| """ | ||
|
|
||
| # Try to change groups to a non-existent one. | ||
| with pytest.raises(KeyError): | ||
| get_gid("some-non-existent-group") | ||
|
|
||
| # Now get the root group ID (should be 0) | ||
| assert get_gid("root") == 0 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this raise this error specifically. Not sure how this works, if its trying to remove something that is not existant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
excis the originalFileNotFoundError, raisingexcis equivalent to raisingFileNotFoundError. But, raising a customFileNotFoundErrorwith an more specific error message would be useful. I'll add that in.So if the file doesn't exist, nothing will happen but
rmtreewill return aFileNotFoundError. Ifmissing_ok=False, then this Exception will be passed on. Otherwise, you get a warning for trying.