-
Notifications
You must be signed in to change notification settings - Fork 369
test: add unit tests for rez.utils.base26 #2156
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
Open
darrenhuai
wants to merge
3
commits into
AcademySoftwareFoundation:main
Choose a base branch
from
darrenhuai:test/utils-base26-coverage
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.
+139
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # Copyright Contributors to the Rez Project | ||
|
|
||
|
|
||
| """ | ||
| test rez.utils.base26 | ||
| """ | ||
| import errno | ||
| import os.path | ||
| from unittest.mock import patch | ||
|
|
||
| from rez.utils.base26 import create_unique_base26_symlink, get_next_base26 | ||
| from rez.tests.util import TestBase | ||
|
|
||
|
|
||
| class TestGetNextBase26(TestBase): | ||
| def test_no_prev_returns_a(self) -> None: | ||
| self.assertEqual(get_next_base26(None), 'a') | ||
| self.assertEqual(get_next_base26(''), 'a') | ||
|
|
||
| def test_increments_last_letter(self) -> None: | ||
| self.assertEqual(get_next_base26('a'), 'b') | ||
| self.assertEqual(get_next_base26('y'), 'z') | ||
| self.assertEqual(get_next_base26('ab'), 'ac') | ||
|
|
||
| def test_rolls_over_to_new_letter(self) -> None: | ||
| self.assertEqual(get_next_base26('z'), 'aa') | ||
| self.assertEqual(get_next_base26('az'), 'ba') | ||
| self.assertEqual(get_next_base26('zz'), 'aaa') | ||
|
|
||
| def test_rolls_over_multiple_letters(self) -> None: | ||
| self.assertEqual(get_next_base26('azz'), 'baa') | ||
|
|
||
| def test_invalid_input_raises(self) -> None: | ||
| for bad in ('A', 'a1', 'a-b', ' a', 'a ', 'a\n', 'ab\n'): | ||
| with self.subTest(item=bad): | ||
| with self.assertRaises(ValueError): | ||
| get_next_base26(bad) | ||
|
|
||
|
|
||
| class TestCreateUniqueBase26Symlink(TestBase): | ||
| """These mock the filesystem boundary (os.listdir/os.path.islink/os.symlink) | ||
| rather than creating real symlinks, since symlink creation requires elevated | ||
| privileges on some platforms (e.g. Windows without Developer Mode). | ||
| """ | ||
|
|
||
| def test_returns_existing_symlink_if_already_pointing_at_source(self) -> None: | ||
| with patch( | ||
| "rez.utils.base26.find_matching_symlink", return_value="c" | ||
| ), patch("os.symlink") as symlink: | ||
| result = create_unique_base26_symlink("/pkgs", "/source/1.0") | ||
|
|
||
| self.assertEqual(result, os.path.join("/pkgs", "c")) | ||
| symlink.assert_not_called() | ||
|
|
||
| def test_creates_first_symlink_when_none_exist(self) -> None: | ||
| with patch( | ||
| "rez.utils.base26.find_matching_symlink", return_value=None | ||
| ), patch("os.listdir", return_value=[]), patch( | ||
| "os.symlink" | ||
| ) as symlink: | ||
| result = create_unique_base26_symlink("/pkgs", "/source/1.0") | ||
|
|
||
| symlink.assert_called_once_with("/source/1.0", result) | ||
| self.assertTrue(result.endswith('a')) | ||
|
|
||
| def test_creates_symlink_after_highest_existing(self) -> None: | ||
| with patch( | ||
| "rez.utils.base26.find_matching_symlink", return_value=None | ||
| ), patch( | ||
| "os.listdir", return_value=['a', 'b', 'c'] | ||
| ), patch( | ||
| "os.path.islink", return_value=True | ||
| ), patch("os.symlink") as symlink: | ||
| result = create_unique_base26_symlink("/pkgs", "/source/2.0") | ||
|
|
||
| symlink.assert_called_once_with("/source/2.0", result) | ||
| self.assertTrue(result.endswith('d')) | ||
|
|
||
| def test_creates_symlink_after_highest_existing_with_mixed_lengths(self) -> None: | ||
| # regression test: a naive max(names) sorts lexicographically, so 'z' | ||
| # would incorrectly be picked over 'aa' as the "highest" name | ||
| with patch( | ||
| "rez.utils.base26.find_matching_symlink", return_value=None | ||
| ), patch( | ||
| "os.listdir", return_value=['a', 'b', 'c', 'z', 'aa'] | ||
| ), patch( | ||
| "os.path.islink", return_value=True | ||
| ), patch("os.symlink") as symlink: | ||
| result = create_unique_base26_symlink("/pkgs", "/source/3.0") | ||
|
|
||
| symlink.assert_called_once_with("/source/3.0", result) | ||
| self.assertTrue(result.endswith('ab')) | ||
|
|
||
| def test_retries_on_race_condition_then_succeeds(self) -> None: | ||
| exists_error = OSError(errno.EEXIST, "File exists") | ||
|
|
||
| # the first pass finds an empty dir and loses the race for 'a'; by the | ||
| # time it retries, the winner's 'a' is on disk, so it must move to 'b' | ||
| with patch( | ||
| "rez.utils.base26.find_matching_symlink", return_value=None | ||
| ), patch("os.listdir", side_effect=[[], ['a']]), patch( | ||
| "os.path.islink", return_value=True | ||
| ), patch( | ||
| "os.symlink", side_effect=[exists_error, None] | ||
| ) as symlink: | ||
| result = create_unique_base26_symlink("/pkgs", "/source/1.0") | ||
|
|
||
| self.assertEqual(symlink.call_count, 2) | ||
| self.assertEqual( | ||
| symlink.call_args_list[0][0][1], os.path.join("/pkgs", "a")) | ||
| symlink.assert_called_with("/source/1.0", result) | ||
| self.assertTrue(result.endswith('b')) | ||
|
|
||
| def test_reraises_non_eexist_oserror(self) -> None: | ||
| other_error = OSError(errno.EACCES, "Permission denied") | ||
|
|
||
| with patch( | ||
| "rez.utils.base26.find_matching_symlink", return_value=None | ||
| ), patch("os.listdir", return_value=[]), patch( | ||
| "os.symlink", side_effect=other_error | ||
| ): | ||
| with self.assertRaises(OSError): | ||
| create_unique_base26_symlink("/pkgs", "/source/1.0") | ||
|
|
||
| def test_gives_up_after_too_much_contention(self) -> None: | ||
| exists_error = OSError(errno.EEXIST, "File exists") | ||
|
|
||
| with patch( | ||
| "rez.utils.base26.find_matching_symlink", return_value=None | ||
| ), patch("os.listdir", return_value=[]), patch( | ||
| "os.symlink", side_effect=exists_error | ||
| ): | ||
| with self.assertRaises(RuntimeError): | ||
| create_unique_base26_symlink("/pkgs", "/source/1.0") | ||
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
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.
Doing this will highlight that there's actually a bug in
create_unique_base26_symlink.max(names)should be replaced withmax(names, key=lambda x: (len(x), x)).That's because:
In other words, the tests have full coverage purely in terms of code, but not in terms of logic.