xref: /aosp_15_r20/external/bazelbuild-rules_python/examples/bzlmod/runfiles/runfiles_test.py (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Copyright 2023 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import unittest
17
18from other_module.pkg import lib
19
20from python.runfiles import runfiles
21
22
23class RunfilesTest(unittest.TestCase):
24    # """Unit tests for `runfiles.Runfiles`."""
25    def testCurrentRepository(self):
26        self.assertEqual(runfiles.Create().CurrentRepository(), "")
27
28    def testRunfilesWithRepoMapping(self):
29        data_path = runfiles.Create().Rlocation("example_bzlmod/runfiles/data/data.txt")
30        with open(data_path) as f:
31            self.assertEqual(f.read().strip(), "Hello, example_bzlmod!")
32
33    def testRunfileWithRlocationpath(self):
34        data_rlocationpath = os.getenv("DATA_RLOCATIONPATH")
35        data_path = runfiles.Create().Rlocation(data_rlocationpath)
36        with open(data_path) as f:
37            self.assertEqual(f.read().strip(), "Hello, example_bzlmod!")
38
39    def testRunfileInOtherModuleWithOurRepoMapping(self):
40        data_path = runfiles.Create().Rlocation(
41            "our_other_module/other_module/pkg/data/data.txt"
42        )
43        with open(data_path) as f:
44            self.assertEqual(f.read().strip(), "Hello, other_module!")
45
46    def testRunfileInOtherModuleWithItsRepoMapping(self):
47        data_path = lib.GetRunfilePathWithRepoMapping()
48        with open(data_path) as f:
49            self.assertEqual(f.read().strip(), "Hello, other_module!")
50
51    def testRunfileInOtherModuleWithCurrentRepository(self):
52        data_path = lib.GetRunfilePathWithCurrentRepository()
53        with open(data_path) as f:
54            self.assertEqual(f.read().strip(), "Hello, other_module!")
55
56    def testRunfileInOtherModuleWithRlocationpath(self):
57        data_rlocationpath = os.getenv("OTHER_MODULE_DATA_RLOCATIONPATH")
58        data_path = runfiles.Create().Rlocation(data_rlocationpath)
59        with open(data_path) as f:
60            self.assertEqual(f.read().strip(), "Hello, other_module!")
61
62
63if __name__ == "__main__":
64    unittest.main()
65