-
Notifications
You must be signed in to change notification settings - Fork 0
feat!: add read-only mode, fix XDG behavior in get_data_dir() #79
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
Draft
jsstevenson
wants to merge
5
commits into
main
Choose a base branch
from
78-getdir-readonly
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c14c5ca
feat: optionally suppress write in get_data_dir
jsstevenson 16ac3ca
feat!: Add write option/checks to get_data_dir, fix XDG behavior
jsstevenson 0409874
whew add some tests
jsstevenson ac525aa
docs and more tweaks
jsstevenson 194b431
fix up docs weirdness
jsstevenson 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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,74 @@ | ||
| import os | ||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from wags_tails.utils.storage import WagsTailsDirNotAvailableError, get_data_dir | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def env_var_teardown(): | ||
| """Make sure environment variables and directories are cleaned up after each test""" | ||
| yield | ||
| for varname in ( | ||
| "XDG_DATA_DIRS", | ||
| "XDG_DATA_HOME", | ||
| "WAGS_TAILS_DIR", | ||
| "WAGS_TAILS_READONLY", | ||
| ): | ||
| if varname in os.environ: | ||
| del os.environ[varname] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def default_data_dir() -> Path: | ||
| return Path("~/.local/share/wags_tails/").expanduser() | ||
|
|
||
|
|
||
| def test_default(default_data_dir: Path): | ||
| assert get_data_dir() == default_data_dir | ||
|
|
||
|
|
||
| def test_xdg_data_home_variable(): | ||
| with tempfile.TemporaryDirectory() as td: | ||
| xdg_data_home = Path(td) | ||
| os.environ["XDG_DATA_HOME"] = str(xdg_data_home) | ||
| assert get_data_dir() == xdg_data_home / "wags_tails" | ||
|
|
||
|
|
||
| def test_handle_create_subdirectories(): | ||
| with tempfile.TemporaryDirectory() as td: | ||
| xdg_data_home = Path(td) / "a" / "b" / "c" | ||
| os.environ["XDG_DATA_HOME"] = str(xdg_data_home) | ||
| assert get_data_dir() == xdg_data_home / "wags_tails" | ||
|
|
||
|
|
||
| def test_xdg_data_dirs_variable(default_data_dir: Path): | ||
| with tempfile.TemporaryDirectory() as td: | ||
| tempdir = Path(td) | ||
| bad_dir = tempdir / "bad_location" | ||
| bad_dir.mkdir(exist_ok=True, parents=True) | ||
| data_dirs_dir = tempdir / "my_data" | ||
| (data_dirs_dir / "wags_tails").mkdir(parents=True, exist_ok=True) | ||
| os.environ["XDG_DATA_DIRS"] = f"{bad_dir}:{data_dirs_dir}" | ||
| assert get_data_dir() == default_data_dir # should skip when writeable | ||
| assert get_data_dir(readonly=True) == data_dirs_dir / "wags_tails" | ||
|
|
||
|
|
||
| def test_wags_tails_dir_variable(): | ||
| with tempfile.TemporaryDirectory() as td: | ||
| wtd = Path(td) / "wags_tails_dir" | ||
| os.environ["WAGS_TAILS_DIR"] = str(wtd) | ||
| assert get_data_dir() == wtd | ||
|
|
||
|
|
||
| def test_readonly_mode_settings(): | ||
| with tempfile.TemporaryDirectory() as td: | ||
| new_directory = Path(td) / "fake_directory" | ||
| os.environ["WAGS_TAILS_DIR"] = str(new_directory) | ||
| with pytest.raises(WagsTailsDirNotAvailableError): | ||
| get_data_dir(readonly=True) | ||
| os.environ["WAGS_TAILS_READONLY"] = "TRUE" | ||
| with pytest.raises(WagsTailsDirNotAvailableError): | ||
| get_data_dir() |
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.
Is this extra criteria needed? Only if read-only is enabled, and only if the subdirectory already exists?
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.
Yeah, I've been chasing my tail on this a bit. When read-only is on, a nonexistent directory is useless, but I think it begs the question of what to do when
$WAGS_TAILS_READONLY=trueand$WAGS_TAILS_DIRis defined but doesn't exist. I guess a simpler way forward would be to not skip and fail once the process tries to write a file to the data subdirectory, because presumably if you can define$WAGS_TAILS_READONLYthen you can also be smart enough to point$WAGS_TAILS_DIRto a real directory.