Skip to content

Commit 0ab9cf6

Browse files
committed
Refactor mapping tests
1 parent 7009cef commit 0ab9cf6

File tree

1 file changed

+68
-31
lines changed

1 file changed

+68
-31
lines changed

tests/runfiles/runfiles_test.py

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -636,11 +636,10 @@ def testDirectoryBasedRlocationWithCompactRepoMappingOrderMatters(self) -> None:
636636
dir + "/lib~general/foo/file",
637637
)
638638

639-
def testRepositoryMappingImplementsMappingProtocol(self) -> None:
640-
"""Test that RepositoryMapping implements the Mapping protocol with unified lookup."""
639+
def _createTestRepositoryMapping(self):
640+
"""Helper method to create a test RepositoryMapping with both exact and prefixed mappings."""
641641
from python.runfiles.runfiles import _RepositoryMapping # buildifier: disable=bzl-visibility
642642

643-
# Create a RepositoryMapping with both exact and prefixed mappings
644643
exact_mappings = {
645644
("", "my_workspace"): "_main",
646645
("", "config_lib"): "config_lib~1.0.0",
@@ -651,27 +650,45 @@ def testRepositoryMappingImplementsMappingProtocol(self) -> None:
651650
("test_deps+", "test_lib"): "test_lib~2.1.0",
652651
}
653652

654-
repo_mapping = _RepositoryMapping(exact_mappings, prefixed_mappings)
655-
656-
# Test Mapping protocol methods
657-
658-
# Test __len__ - should count both exact and prefixed mappings
653+
return _RepositoryMapping(exact_mappings, prefixed_mappings)
654+
655+
def testRepositoryMappingLen(self) -> None:
656+
"""Test __len__ method - should count both exact and prefixed mappings."""
657+
repo_mapping = self._createTestRepositoryMapping()
659658
self.assertEqual(len(repo_mapping), 5) # 3 exact + 2 prefixed
660659

661-
# Test __getitem__ - should work for exact mappings only
660+
# Test empty mapping
661+
from python.runfiles.runfiles import _RepositoryMapping # buildifier: disable=bzl-visibility
662+
empty_mapping = _RepositoryMapping({}, {})
663+
self.assertEqual(len(empty_mapping), 0)
664+
665+
def testRepositoryMappingGetItem(self) -> None:
666+
"""Test __getitem__ method - should work for both exact and prefix-based mappings."""
667+
repo_mapping = self._createTestRepositoryMapping()
668+
669+
# Test exact lookup
662670
self.assertEqual(repo_mapping[("", "my_workspace")], "_main")
663671
self.assertEqual(repo_mapping[("", "config_lib")], "config_lib~1.0.0")
664672
self.assertEqual(repo_mapping[("deps+specific_repo", "external_dep")], "external_dep~exact")
665673

674+
# Test prefix-based lookup
675+
self.assertEqual(repo_mapping[("deps+some_repo", "external_dep")], "external_dep~prefix")
676+
677+
# Test that exact takes precedence over prefix
678+
self.assertEqual(repo_mapping[("deps+specific_repo", "external_dep")], "external_dep~exact")
679+
666680
# Test KeyError for non-existent mapping (neither exact nor prefix match)
667681
with self.assertRaises(KeyError):
668682
_ = repo_mapping[("nonexistent", "repo")]
683+
684+
def testRepositoryMappingIter(self) -> None:
685+
"""Test __iter__ method - should iterate over all mapping keys (exact first, then prefixed)."""
686+
repo_mapping = self._createTestRepositoryMapping()
669687

670-
# Test iteration - should iterate over all mapping keys (exact first, then prefixed)
671688
keys = list(repo_mapping)
672689
self.assertEqual(len(keys), 5) # 3 exact + 2 prefixed
673690

674-
# Check that exact mappings come first
691+
# Check that all expected keys are present
675692
exact_keys = [("", "my_workspace"), ("", "config_lib"), ("deps+specific_repo", "external_dep")]
676693
prefixed_keys = [("deps+", "external_dep"), ("test_deps+", "test_lib")]
677694

@@ -690,51 +707,71 @@ def testRepositoryMappingImplementsMappingProtocol(self) -> None:
690707

691708
for prefixed_key in prefixed_keys:
692709
self.assertIn(prefixed_key, last_two_keys)
710+
711+
def testRepositoryMappingContains(self) -> None:
712+
"""Test __contains__ method (in operator) - should work for both exact and prefix-based mappings."""
713+
repo_mapping = self._createTestRepositoryMapping()
693714

694-
# Test 'in' operator (via __contains__) - works for both exact and prefix-based
715+
# Test exact lookups
695716
self.assertTrue(("", "my_workspace") in repo_mapping)
696-
self.assertFalse(("nonexistent", "repo") in repo_mapping)
697-
# Prefix-based lookups also work with 'in' operator (consistent with [])
698-
self.assertTrue(("deps+some_repo", "external_dep") in repo_mapping)
717+
self.assertTrue(("deps+specific_repo", "external_dep") in repo_mapping)
699718

700-
# Test that the mapping interface works for both exact and prefix-based
701-
# Exact lookup
702-
self.assertEqual(repo_mapping[("", "my_workspace")], "_main")
703-
704-
# Prefix-based lookup
705-
self.assertEqual(repo_mapping[("deps+some_repo", "external_dep")], "external_dep~prefix")
719+
# Test prefix-based lookups (consistent with __getitem__)
720+
self.assertTrue(("deps+some_repo", "external_dep") in repo_mapping)
706721

707-
# Exact takes precedence over prefix
708-
self.assertEqual(repo_mapping[("deps+specific_repo", "external_dep")], "external_dep~exact")
722+
# Test non-existent mapping
723+
self.assertFalse(("nonexistent", "repo") in repo_mapping)
724+
725+
def testRepositoryMappingGet(self) -> None:
726+
"""Test get() method - should work for both exact and prefix-based mappings."""
727+
repo_mapping = self._createTestRepositoryMapping()
709728

710-
# Test get() method with both exact and prefix-based
729+
# Test exact lookups
711730
self.assertEqual(repo_mapping.get(("", "my_workspace")), "_main")
731+
self.assertEqual(repo_mapping.get(("deps+specific_repo", "external_dep")), "external_dep~exact")
732+
733+
# Test prefix-based lookups
712734
self.assertEqual(repo_mapping.get(("deps+some_repo", "external_dep")), "external_dep~prefix")
735+
736+
# Test non-existent mapping
713737
self.assertIsNone(repo_mapping.get(("nonexistent", "repo")))
714738
self.assertEqual(repo_mapping.get(("nonexistent", "repo"), "default"), "default")
739+
740+
def testRepositoryMappingKeysValuesItems(self) -> None:
741+
"""Test keys(), values(), and items() methods (inherited from Mapping)."""
742+
repo_mapping = self._createTestRepositoryMapping()
715743

716-
# Test items(), keys(), values() (inherited from Mapping)
744+
# Test items()
717745
items = list(repo_mapping.items())
718746
self.assertEqual(len(items), 5) # 3 exact + 2 prefixed
719747
self.assertIn((("", "my_workspace"), "_main"), items)
720748
self.assertIn((("deps+", "external_dep"), "external_dep~prefix"), items)
721749

750+
# Test keys()
722751
keys = list(repo_mapping.keys())
723-
values = list(repo_mapping.values())
724752
self.assertEqual(len(keys), 5) # 3 exact + 2 prefixed
753+
self.assertIn(("", "my_workspace"), keys)
754+
self.assertIn(("deps+", "external_dep"), keys)
755+
756+
# Test values()
757+
values = list(repo_mapping.values())
725758
self.assertEqual(len(values), 5) # 3 exact + 2 prefixed
726759
self.assertIn("_main", values)
727760
self.assertIn("external_dep~prefix", values)
761+
762+
def testRepositoryMappingTruthiness(self) -> None:
763+
"""Test pythonic truthiness - non-empty mapping is truthy, empty mapping is falsy."""
764+
repo_mapping = self._createTestRepositoryMapping()
728765

729-
# Test pythonic truthiness (no need for is_empty method)
730-
self.assertTrue(repo_mapping) # Non-empty mapping is truthy
766+
# Test non-empty mapping is truthy
767+
self.assertTrue(repo_mapping)
731768
self.assertTrue(bool(repo_mapping))
732769

733-
# Test empty mapping
770+
# Test empty mapping is falsy
771+
from python.runfiles.runfiles import _RepositoryMapping # buildifier: disable=bzl-visibility
734772
empty_mapping = _RepositoryMapping({}, {})
735-
self.assertFalse(empty_mapping) # Empty mapping is falsy
773+
self.assertFalse(empty_mapping)
736774
self.assertFalse(bool(empty_mapping))
737-
self.assertEqual(len(empty_mapping), 0)
738775

739776
# TODO: Add manifest-based test for compact repo mapping
740777
# def testManifestBasedRlocationWithCompactRepoMapping(self) -> None:

0 commit comments

Comments
 (0)