-
-
Notifications
You must be signed in to change notification settings - Fork 370
r.pack: Replace traceback by error message for output issues #6346
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 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dbbe84f
r.pack: Replace traceback by error message for output issues
wenzeslaus 7fb7297
add re.escape for windows paths
petrasovaa 5a93afd
Apply suggestions from code review
echoix 93c0d94
Merge branch 'main' into r_pack-add-tests
echoix 0eb3ebe
Skip the read-only dir test for Windows. At least in GitHub Actions W…
wenzeslaus 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import os | ||
| import stat | ||
|
|
||
| import pytest | ||
|
|
||
| from grass.exceptions import CalledModuleError | ||
| from grass.tools import Tools | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("compression_flag", ["", "c"]) | ||
| @pytest.mark.parametrize( | ||
| "suffix", | ||
| [".pack", ".rpack", ".r_pack", ".grass_raster", ".grr", ".arbitrary_suffix_1"], | ||
| ) | ||
| def test_pack_different_suffixes( | ||
| xy_raster_dataset_session_for_module, tmp_path, suffix, compression_flag | ||
| ): | ||
| """Check that non-zero output is created with different suffixes | ||
|
|
||
| The compression should not change the allowed suffixes. | ||
| """ | ||
| tools = Tools(session=xy_raster_dataset_session_for_module) | ||
| output = (tmp_path / "output_1").with_suffix(suffix) | ||
| tools.r_pack(input="rows_raster", output=output, flags=compression_flag) | ||
| assert output.exists() | ||
| assert output.is_file() | ||
| assert output.stat().st_size > 0 | ||
|
|
||
|
|
||
| def test_input_does_not_exist(xy_empty_dataset_session, tmp_path): | ||
| """Check we raise when input does not exists""" | ||
| tools = Tools(session=xy_empty_dataset_session) | ||
| output = tmp_path / "output_1.rpack" | ||
| with pytest.raises(CalledModuleError, match="does_not_exist"): | ||
| tools.r_pack(input="does_not_exist", output=output) | ||
|
|
||
|
|
||
| def test_output_exists_without_overwrite( | ||
| xy_raster_dataset_session_for_module, tmp_path | ||
| ): | ||
| """Check behavior when output already exists""" | ||
| tools = Tools(session=xy_raster_dataset_session_for_module) | ||
| output = tmp_path / "output_1.rpack" | ||
| assert not output.exists() | ||
| tools.r_pack(input="rows_raster", output=output) | ||
| assert output.exists() | ||
| with pytest.raises(CalledModuleError, match=rf"(?s){output}.*[Oo]verwrite"): | ||
petrasovaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Same call repeated again. | ||
| tools.r_pack(input="rows_raster", output=output) | ||
| assert output.exists() | ||
|
|
||
|
|
||
| def test_output_exists_with_overwrite(xy_raster_dataset_session_for_module, tmp_path): | ||
| """Check behavior when output already exists and overwrite is set""" | ||
| tools = Tools(session=xy_raster_dataset_session_for_module) | ||
| output = tmp_path / "output_1.rpack" | ||
| assert not output.exists() | ||
| tools.r_pack(input="rows_raster", output=output) | ||
| assert output.exists() | ||
| # Same call repeated again but with overwrite. | ||
| tools.r_pack(input="rows_raster", output=output, overwrite=True) | ||
| assert output.exists() | ||
|
|
||
|
|
||
| def test_output_dir_does_not_exist(xy_raster_dataset_session_for_module, tmp_path): | ||
| """Check behavior when directory for output does not exist""" | ||
| tools = Tools(session=xy_raster_dataset_session_for_module) | ||
| output = tmp_path / "does_not_exist" / "output_1.rpack" | ||
| assert not output.exists() | ||
| assert not output.parent.exists() | ||
| with pytest.raises( | ||
| CalledModuleError, match=rf"(?s)'{output.parent}'.*does not exist" | ||
petrasovaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ): | ||
| tools.r_pack(input="rows_raster", output=output) | ||
|
|
||
|
|
||
| def test_output_dir_is_file(xy_raster_dataset_session_for_module, tmp_path): | ||
| """Check behavior when what should be a directory is a file""" | ||
| tools = Tools(session=xy_raster_dataset_session_for_module) | ||
| parent = tmp_path / "file_as_dir" | ||
| parent.touch() | ||
| output = parent / "output_1.rpack" | ||
| assert not output.exists() | ||
| assert output.parent.exists() | ||
| assert output.parent.is_file() | ||
| with pytest.raises( | ||
| CalledModuleError, match=rf"(?s)'{output.parent}'.*is not.*directory" | ||
petrasovaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ): | ||
| tools.r_pack(input="rows_raster", output=output) | ||
|
|
||
|
|
||
| def test_output_dir_is_not_writable(xy_raster_dataset_session_for_module, tmp_path): | ||
| """Check behavior when directory is not writable""" | ||
| tools = Tools(session=xy_raster_dataset_session_for_module) | ||
| parent = tmp_path / "parent_dir" | ||
| parent.mkdir() | ||
| parent.chmod(stat.S_IREAD) | ||
| output = parent / "output_1.rpack" | ||
| assert not os.path.exists(output) # output.exists() gives permission denied | ||
| assert output.parent.exists() | ||
| assert output.parent.is_dir() | ||
| with pytest.raises( | ||
| CalledModuleError, match=rf"(?s)'{output.parent}'.*is not.*writable" | ||
petrasovaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ): | ||
| tools.r_pack(input="rows_raster", output=output) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("compression_flag", ["", "c"]) | ||
| def test_round_trip_with_r_unpack( | ||
| xy_raster_dataset_session_mapset, tmp_path, compression_flag | ||
| ): | ||
| """Check that we get the same values with r.unpack""" | ||
| tools = Tools(session=xy_raster_dataset_session_mapset) | ||
| output = tmp_path / "output_1.rpack" | ||
| tools.r_pack(input="rows_raster", output=output, flags=compression_flag) | ||
| imported = "imported" | ||
| tools.r_unpack(input=output, output=imported) | ||
| tools.r_mapcalc(expression="diff = rows_raster - imported") | ||
| stats = tools.r_univar(map="diff", format="json") | ||
| assert stats["n"] > 1 | ||
| assert stats["sum"] == 0 | ||
| assert stats["min"] == 0 | ||
| assert stats["max"] == 0 | ||
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.