-
-
Notifications
You must be signed in to change notification settings - Fork 679
feat(runfiles): create pathlib api for runfiles library #3694
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
+270
−9
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
35530d8
basic impl and tests
rickeylev 5fe5e25
add changelog ref
rickeylev 1bc33b0
use !r in repr
rickeylev d1318d9
Merge branch 'main' of https://github.com/bazel-contrib/rules_python …
rickeylev 5369ad0
snake case tets and fixed changelog
rickeylev 9fcfd51
Merge branch 'runfiles.pathlib' of https://github.com/rickeylev/rules…
rickeylev c342cee
cleanup changelog text, stupid ai slop description
rickeylev 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,105 @@ | ||
| import os | ||
| import pathlib | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| from python.runfiles import runfiles | ||
|
|
||
|
|
||
| class PathlibTest(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| self.tmpdir = tempfile.TemporaryDirectory(dir=os.environ.get("TEST_TMPDIR")) | ||
| # Runfiles paths are expected to be posix paths internally when we construct the strings for assertions | ||
| self.root_dir = pathlib.Path(self.tmpdir.name).as_posix() | ||
|
|
||
| def tearDown(self) -> None: | ||
| self.tmpdir.cleanup() | ||
|
|
||
| def test_path_api(self) -> None: | ||
| r = runfiles.Create({"RUNFILES_DIR": self.root_dir}) | ||
| assert r is not None | ||
| root = r.root() | ||
|
|
||
| # Test basic joining | ||
| p = root / "repo/pkg/file.txt" | ||
| self.assertEqual(str(p), f"{self.root_dir}/repo/pkg/file.txt") | ||
|
|
||
| # Test PurePath API | ||
| self.assertEqual(p.name, "file.txt") | ||
| self.assertEqual(p.suffix, ".txt") | ||
| self.assertEqual(p.parent.name, "pkg") | ||
| self.assertEqual(p.parts, ("repo", "pkg", "file.txt")) | ||
| self.assertEqual(p.stem, "file") | ||
| self.assertEqual(p.suffixes, [".txt"]) | ||
|
|
||
| # Test multiple joins | ||
| p2 = root / "repo" / "pkg" / "file.txt" | ||
| self.assertEqual(p, p2) | ||
|
|
||
| # Test joins with pathlib objects | ||
| p3 = root / pathlib.PurePath("repo/pkg/file.txt") | ||
| self.assertEqual(p, p3) | ||
|
|
||
| def test_root(self) -> None: | ||
| r = runfiles.Create({"RUNFILES_DIR": self.root_dir}) | ||
| assert r is not None | ||
| self.assertEqual(str(r.root()), self.root_dir) | ||
|
|
||
| def test_runfiles_root_method(self) -> None: | ||
| r = runfiles.Create({"RUNFILES_DIR": self.root_dir}) | ||
| assert r is not None | ||
| p = r.root() / "foo/bar" | ||
| self.assertEqual(p.runfiles_root(), r.root()) | ||
| self.assertEqual(str(p.runfiles_root()), self.root_dir) | ||
|
|
||
| def test_os_path_like(self) -> None: | ||
| r = runfiles.Create({"RUNFILES_DIR": self.root_dir}) | ||
| assert r is not None | ||
| p = r.root() / "foo" | ||
| self.assertEqual(os.fspath(p), f"{self.root_dir}/foo") | ||
|
|
||
| def test_equality_and_hash(self) -> None: | ||
| r = runfiles.Create({"RUNFILES_DIR": self.root_dir}) | ||
| assert r is not None | ||
| p1 = r.root() / "foo" | ||
| p2 = r.root() / "foo" | ||
| p3 = r.root() / "bar" | ||
|
|
||
| self.assertEqual(p1, p2) | ||
| self.assertNotEqual(p1, p3) | ||
| self.assertEqual(hash(p1), hash(p2)) | ||
|
|
||
| def test_join_path(self) -> None: | ||
| r = runfiles.Create({"RUNFILES_DIR": self.root_dir}) | ||
| assert r is not None | ||
| p = r.root().joinpath("repo", "file") | ||
| self.assertEqual(str(p), f"{self.root_dir}/repo/file") | ||
|
|
||
| def test_parents(self) -> None: | ||
| r = runfiles.Create({"RUNFILES_DIR": self.root_dir}) | ||
| assert r is not None | ||
| p = r.root() / "a/b/c" | ||
| parents = list(p.parents) | ||
| self.assertEqual(len(parents), 3) | ||
| self.assertEqual(str(parents[0]), f"{self.root_dir}/a/b") | ||
| self.assertEqual(str(parents[1]), f"{self.root_dir}/a") | ||
| self.assertEqual(str(parents[2]), self.root_dir) | ||
|
|
||
| def test_with_methods(self) -> None: | ||
| r = runfiles.Create({"RUNFILES_DIR": self.root_dir}) | ||
| assert r is not None | ||
| p = r.root() / "foo/bar.txt" | ||
| self.assertEqual(str(p.with_name("baz.py")), f"{self.root_dir}/foo/baz.py") | ||
| self.assertEqual(str(p.with_suffix(".dat")), f"{self.root_dir}/foo/bar.dat") | ||
|
|
||
| def test_match(self) -> None: | ||
| r = runfiles.Create({"RUNFILES_DIR": self.root_dir}) | ||
| assert r is not None | ||
| p = r.root() / "foo/bar.txt" | ||
| self.assertTrue(p.match("*.txt")) | ||
| self.assertTrue(p.match("foo/*.txt")) | ||
| self.assertFalse(p.match("bar/*.txt")) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.