xref: /aosp_15_r20/external/bazelbuild-rules_python/tests/runfiles/runfiles_test.py (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1*60517a1eSAndroid Build Coastguard Worker# Copyright 2018 The Bazel Authors. All rights reserved.
2*60517a1eSAndroid Build Coastguard Worker#
3*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*60517a1eSAndroid Build Coastguard Worker#
7*60517a1eSAndroid Build Coastguard Worker#    http://www.apache.org/licenses/LICENSE-2.0
8*60517a1eSAndroid Build Coastguard Worker#
9*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*60517a1eSAndroid Build Coastguard Worker# limitations under the License.
14*60517a1eSAndroid Build Coastguard Worker
15*60517a1eSAndroid Build Coastguard Workerimport os
16*60517a1eSAndroid Build Coastguard Workerimport tempfile
17*60517a1eSAndroid Build Coastguard Workerimport unittest
18*60517a1eSAndroid Build Coastguard Workerfrom typing import Any, List, Optional
19*60517a1eSAndroid Build Coastguard Worker
20*60517a1eSAndroid Build Coastguard Workerfrom python.runfiles import runfiles
21*60517a1eSAndroid Build Coastguard Worker
22*60517a1eSAndroid Build Coastguard Worker
23*60517a1eSAndroid Build Coastguard Workerclass RunfilesTest(unittest.TestCase):
24*60517a1eSAndroid Build Coastguard Worker    """Unit tests for `rules_python.python.runfiles.Runfiles`."""
25*60517a1eSAndroid Build Coastguard Worker
26*60517a1eSAndroid Build Coastguard Worker    def testRlocationArgumentValidation(self) -> None:
27*60517a1eSAndroid Build Coastguard Worker        r = runfiles.Create({"RUNFILES_DIR": "whatever"})
28*60517a1eSAndroid Build Coastguard Worker        assert r is not None  # mypy doesn't understand the unittest api.
29*60517a1eSAndroid Build Coastguard Worker        self.assertRaises(ValueError, lambda: r.Rlocation(None))  # type: ignore
30*60517a1eSAndroid Build Coastguard Worker        self.assertRaises(ValueError, lambda: r.Rlocation(""))
31*60517a1eSAndroid Build Coastguard Worker        self.assertRaises(TypeError, lambda: r.Rlocation(1))  # type: ignore
32*60517a1eSAndroid Build Coastguard Worker        self.assertRaisesRegex(
33*60517a1eSAndroid Build Coastguard Worker            ValueError, "is not normalized", lambda: r.Rlocation("../foo")
34*60517a1eSAndroid Build Coastguard Worker        )
35*60517a1eSAndroid Build Coastguard Worker        self.assertRaisesRegex(
36*60517a1eSAndroid Build Coastguard Worker            ValueError, "is not normalized", lambda: r.Rlocation("foo/..")
37*60517a1eSAndroid Build Coastguard Worker        )
38*60517a1eSAndroid Build Coastguard Worker        self.assertRaisesRegex(
39*60517a1eSAndroid Build Coastguard Worker            ValueError, "is not normalized", lambda: r.Rlocation("foo/../bar")
40*60517a1eSAndroid Build Coastguard Worker        )
41*60517a1eSAndroid Build Coastguard Worker        self.assertRaisesRegex(
42*60517a1eSAndroid Build Coastguard Worker            ValueError, "is not normalized", lambda: r.Rlocation("./foo")
43*60517a1eSAndroid Build Coastguard Worker        )
44*60517a1eSAndroid Build Coastguard Worker        self.assertRaisesRegex(
45*60517a1eSAndroid Build Coastguard Worker            ValueError, "is not normalized", lambda: r.Rlocation("foo/.")
46*60517a1eSAndroid Build Coastguard Worker        )
47*60517a1eSAndroid Build Coastguard Worker        self.assertRaisesRegex(
48*60517a1eSAndroid Build Coastguard Worker            ValueError, "is not normalized", lambda: r.Rlocation("foo/./bar")
49*60517a1eSAndroid Build Coastguard Worker        )
50*60517a1eSAndroid Build Coastguard Worker        self.assertRaisesRegex(
51*60517a1eSAndroid Build Coastguard Worker            ValueError, "is not normalized", lambda: r.Rlocation("//foobar")
52*60517a1eSAndroid Build Coastguard Worker        )
53*60517a1eSAndroid Build Coastguard Worker        self.assertRaisesRegex(
54*60517a1eSAndroid Build Coastguard Worker            ValueError, "is not normalized", lambda: r.Rlocation("foo//")
55*60517a1eSAndroid Build Coastguard Worker        )
56*60517a1eSAndroid Build Coastguard Worker        self.assertRaisesRegex(
57*60517a1eSAndroid Build Coastguard Worker            ValueError, "is not normalized", lambda: r.Rlocation("foo//bar")
58*60517a1eSAndroid Build Coastguard Worker        )
59*60517a1eSAndroid Build Coastguard Worker        self.assertRaisesRegex(
60*60517a1eSAndroid Build Coastguard Worker            ValueError,
61*60517a1eSAndroid Build Coastguard Worker            "is absolute without a drive letter",
62*60517a1eSAndroid Build Coastguard Worker            lambda: r.Rlocation("\\foo"),
63*60517a1eSAndroid Build Coastguard Worker        )
64*60517a1eSAndroid Build Coastguard Worker
65*60517a1eSAndroid Build Coastguard Worker    def testCreatesManifestBasedRunfiles(self) -> None:
66*60517a1eSAndroid Build Coastguard Worker        with _MockFile(contents=["a/b c/d"]) as mf:
67*60517a1eSAndroid Build Coastguard Worker            r = runfiles.Create(
68*60517a1eSAndroid Build Coastguard Worker                {
69*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_MANIFEST_FILE": mf.Path(),
70*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_DIR": "ignored when RUNFILES_MANIFEST_FILE has a value",
71*60517a1eSAndroid Build Coastguard Worker                    "TEST_SRCDIR": "always ignored",
72*60517a1eSAndroid Build Coastguard Worker                }
73*60517a1eSAndroid Build Coastguard Worker            )
74*60517a1eSAndroid Build Coastguard Worker            assert r is not None  # mypy doesn't understand the unittest api.
75*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(r.Rlocation("a/b"), "c/d")
76*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("foo"))
77*60517a1eSAndroid Build Coastguard Worker
78*60517a1eSAndroid Build Coastguard Worker    def testManifestBasedRunfilesEnvVars(self) -> None:
79*60517a1eSAndroid Build Coastguard Worker        with _MockFile(name="MANIFEST") as mf:
80*60517a1eSAndroid Build Coastguard Worker            r = runfiles.Create(
81*60517a1eSAndroid Build Coastguard Worker                {
82*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_MANIFEST_FILE": mf.Path(),
83*60517a1eSAndroid Build Coastguard Worker                    "TEST_SRCDIR": "always ignored",
84*60517a1eSAndroid Build Coastguard Worker                }
85*60517a1eSAndroid Build Coastguard Worker            )
86*60517a1eSAndroid Build Coastguard Worker            assert r is not None  # mypy doesn't understand the unittest api.
87*60517a1eSAndroid Build Coastguard Worker            self.assertDictEqual(
88*60517a1eSAndroid Build Coastguard Worker                r.EnvVars(),
89*60517a1eSAndroid Build Coastguard Worker                {
90*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_MANIFEST_FILE": mf.Path(),
91*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_DIR": mf.Path()[: -len("/MANIFEST")],
92*60517a1eSAndroid Build Coastguard Worker                    "JAVA_RUNFILES": mf.Path()[: -len("/MANIFEST")],
93*60517a1eSAndroid Build Coastguard Worker                },
94*60517a1eSAndroid Build Coastguard Worker            )
95*60517a1eSAndroid Build Coastguard Worker
96*60517a1eSAndroid Build Coastguard Worker        with _MockFile(name="foo.runfiles_manifest") as mf:
97*60517a1eSAndroid Build Coastguard Worker            r = runfiles.Create(
98*60517a1eSAndroid Build Coastguard Worker                {
99*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_MANIFEST_FILE": mf.Path(),
100*60517a1eSAndroid Build Coastguard Worker                    "TEST_SRCDIR": "always ignored",
101*60517a1eSAndroid Build Coastguard Worker                }
102*60517a1eSAndroid Build Coastguard Worker            )
103*60517a1eSAndroid Build Coastguard Worker            assert r is not None  # mypy doesn't understand the unittest api.
104*60517a1eSAndroid Build Coastguard Worker            self.assertDictEqual(
105*60517a1eSAndroid Build Coastguard Worker                r.EnvVars(),
106*60517a1eSAndroid Build Coastguard Worker                {
107*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_MANIFEST_FILE": mf.Path(),
108*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_DIR": (
109*60517a1eSAndroid Build Coastguard Worker                        mf.Path()[: -len("foo.runfiles_manifest")] + "foo.runfiles"
110*60517a1eSAndroid Build Coastguard Worker                    ),
111*60517a1eSAndroid Build Coastguard Worker                    "JAVA_RUNFILES": (
112*60517a1eSAndroid Build Coastguard Worker                        mf.Path()[: -len("foo.runfiles_manifest")] + "foo.runfiles"
113*60517a1eSAndroid Build Coastguard Worker                    ),
114*60517a1eSAndroid Build Coastguard Worker                },
115*60517a1eSAndroid Build Coastguard Worker            )
116*60517a1eSAndroid Build Coastguard Worker
117*60517a1eSAndroid Build Coastguard Worker        with _MockFile(name="x_manifest") as mf:
118*60517a1eSAndroid Build Coastguard Worker            r = runfiles.Create(
119*60517a1eSAndroid Build Coastguard Worker                {
120*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_MANIFEST_FILE": mf.Path(),
121*60517a1eSAndroid Build Coastguard Worker                    "TEST_SRCDIR": "always ignored",
122*60517a1eSAndroid Build Coastguard Worker                }
123*60517a1eSAndroid Build Coastguard Worker            )
124*60517a1eSAndroid Build Coastguard Worker            assert r is not None  # mypy doesn't understand the unittest api.
125*60517a1eSAndroid Build Coastguard Worker            self.assertDictEqual(
126*60517a1eSAndroid Build Coastguard Worker                r.EnvVars(),
127*60517a1eSAndroid Build Coastguard Worker                {
128*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_MANIFEST_FILE": mf.Path(),
129*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_DIR": "",
130*60517a1eSAndroid Build Coastguard Worker                    "JAVA_RUNFILES": "",
131*60517a1eSAndroid Build Coastguard Worker                },
132*60517a1eSAndroid Build Coastguard Worker            )
133*60517a1eSAndroid Build Coastguard Worker
134*60517a1eSAndroid Build Coastguard Worker    def testCreatesDirectoryBasedRunfiles(self) -> None:
135*60517a1eSAndroid Build Coastguard Worker        r = runfiles.Create(
136*60517a1eSAndroid Build Coastguard Worker            {
137*60517a1eSAndroid Build Coastguard Worker                "RUNFILES_DIR": "runfiles/dir",
138*60517a1eSAndroid Build Coastguard Worker                "TEST_SRCDIR": "always ignored",
139*60517a1eSAndroid Build Coastguard Worker            }
140*60517a1eSAndroid Build Coastguard Worker        )
141*60517a1eSAndroid Build Coastguard Worker        assert r is not None  # mypy doesn't understand the unittest api.
142*60517a1eSAndroid Build Coastguard Worker        self.assertEqual(r.Rlocation("a/b"), "runfiles/dir/a/b")
143*60517a1eSAndroid Build Coastguard Worker        self.assertEqual(r.Rlocation("foo"), "runfiles/dir/foo")
144*60517a1eSAndroid Build Coastguard Worker
145*60517a1eSAndroid Build Coastguard Worker    def testDirectoryBasedRunfilesEnvVars(self) -> None:
146*60517a1eSAndroid Build Coastguard Worker        r = runfiles.Create(
147*60517a1eSAndroid Build Coastguard Worker            {
148*60517a1eSAndroid Build Coastguard Worker                "RUNFILES_DIR": "runfiles/dir",
149*60517a1eSAndroid Build Coastguard Worker                "TEST_SRCDIR": "always ignored",
150*60517a1eSAndroid Build Coastguard Worker            }
151*60517a1eSAndroid Build Coastguard Worker        )
152*60517a1eSAndroid Build Coastguard Worker        assert r is not None  # mypy doesn't understand the unittest api.
153*60517a1eSAndroid Build Coastguard Worker        self.assertDictEqual(
154*60517a1eSAndroid Build Coastguard Worker            r.EnvVars(),
155*60517a1eSAndroid Build Coastguard Worker            {
156*60517a1eSAndroid Build Coastguard Worker                "RUNFILES_DIR": "runfiles/dir",
157*60517a1eSAndroid Build Coastguard Worker                "JAVA_RUNFILES": "runfiles/dir",
158*60517a1eSAndroid Build Coastguard Worker            },
159*60517a1eSAndroid Build Coastguard Worker        )
160*60517a1eSAndroid Build Coastguard Worker
161*60517a1eSAndroid Build Coastguard Worker    def testFailsToCreateManifestBasedBecauseManifestDoesNotExist(self) -> None:
162*60517a1eSAndroid Build Coastguard Worker        def _Run():
163*60517a1eSAndroid Build Coastguard Worker            runfiles.Create({"RUNFILES_MANIFEST_FILE": "non-existing path"})
164*60517a1eSAndroid Build Coastguard Worker
165*60517a1eSAndroid Build Coastguard Worker        self.assertRaisesRegex(IOError, "non-existing path", _Run)
166*60517a1eSAndroid Build Coastguard Worker
167*60517a1eSAndroid Build Coastguard Worker    def testFailsToCreateAnyRunfilesBecauseEnvvarsAreNotDefined(self) -> None:
168*60517a1eSAndroid Build Coastguard Worker        with _MockFile(contents=["a b"]) as mf:
169*60517a1eSAndroid Build Coastguard Worker            runfiles.Create(
170*60517a1eSAndroid Build Coastguard Worker                {
171*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_MANIFEST_FILE": mf.Path(),
172*60517a1eSAndroid Build Coastguard Worker                    "RUNFILES_DIR": "whatever",
173*60517a1eSAndroid Build Coastguard Worker                    "TEST_SRCDIR": "always ignored",
174*60517a1eSAndroid Build Coastguard Worker                }
175*60517a1eSAndroid Build Coastguard Worker            )
176*60517a1eSAndroid Build Coastguard Worker        runfiles.Create(
177*60517a1eSAndroid Build Coastguard Worker            {
178*60517a1eSAndroid Build Coastguard Worker                "RUNFILES_DIR": "whatever",
179*60517a1eSAndroid Build Coastguard Worker                "TEST_SRCDIR": "always ignored",
180*60517a1eSAndroid Build Coastguard Worker            }
181*60517a1eSAndroid Build Coastguard Worker        )
182*60517a1eSAndroid Build Coastguard Worker        self.assertIsNone(runfiles.Create({"TEST_SRCDIR": "always ignored"}))
183*60517a1eSAndroid Build Coastguard Worker        self.assertIsNone(runfiles.Create({"FOO": "bar"}))
184*60517a1eSAndroid Build Coastguard Worker
185*60517a1eSAndroid Build Coastguard Worker    def testManifestBasedRlocation(self) -> None:
186*60517a1eSAndroid Build Coastguard Worker        with _MockFile(
187*60517a1eSAndroid Build Coastguard Worker            contents=[
188*60517a1eSAndroid Build Coastguard Worker                "Foo/runfile1",
189*60517a1eSAndroid Build Coastguard Worker                "Foo/runfile2 C:/Actual Path\\runfile2",
190*60517a1eSAndroid Build Coastguard Worker                "Foo/Bar/runfile3 D:\\the path\\run file 3.txt",
191*60517a1eSAndroid Build Coastguard Worker                "Foo/Bar/Dir E:\\Actual Path\\Directory",
192*60517a1eSAndroid Build Coastguard Worker            ]
193*60517a1eSAndroid Build Coastguard Worker        ) as mf:
194*60517a1eSAndroid Build Coastguard Worker            r = runfiles.CreateManifestBased(mf.Path())
195*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(r.Rlocation("Foo/runfile1"), "Foo/runfile1")
196*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(r.Rlocation("Foo/runfile2"), "C:/Actual Path\\runfile2")
197*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
198*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("Foo/Bar/runfile3"), "D:\\the path\\run file 3.txt"
199*60517a1eSAndroid Build Coastguard Worker            )
200*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
201*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("Foo/Bar/Dir/runfile4"),
202*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory/runfile4",
203*60517a1eSAndroid Build Coastguard Worker            )
204*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
205*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("Foo/Bar/Dir/Deeply/Nested/runfile4"),
206*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory/Deeply/Nested/runfile4",
207*60517a1eSAndroid Build Coastguard Worker            )
208*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("unknown"))
209*60517a1eSAndroid Build Coastguard Worker            if RunfilesTest.IsWindows():
210*60517a1eSAndroid Build Coastguard Worker                self.assertEqual(r.Rlocation("c:/foo"), "c:/foo")
211*60517a1eSAndroid Build Coastguard Worker                self.assertEqual(r.Rlocation("c:\\foo"), "c:\\foo")
212*60517a1eSAndroid Build Coastguard Worker            else:
213*60517a1eSAndroid Build Coastguard Worker                self.assertEqual(r.Rlocation("/foo"), "/foo")
214*60517a1eSAndroid Build Coastguard Worker
215*60517a1eSAndroid Build Coastguard Worker    def testManifestBasedRlocationWithRepoMappingFromMain(self) -> None:
216*60517a1eSAndroid Build Coastguard Worker        with _MockFile(
217*60517a1eSAndroid Build Coastguard Worker            contents=[
218*60517a1eSAndroid Build Coastguard Worker                ",config.json,config.json~1.2.3",
219*60517a1eSAndroid Build Coastguard Worker                ",my_module,_main",
220*60517a1eSAndroid Build Coastguard Worker                ",my_protobuf,protobuf~3.19.2",
221*60517a1eSAndroid Build Coastguard Worker                ",my_workspace,_main",
222*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2,config.json,config.json~1.2.3",
223*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2,protobuf,protobuf~3.19.2",
224*60517a1eSAndroid Build Coastguard Worker            ]
225*60517a1eSAndroid Build Coastguard Worker        ) as rm, _MockFile(
226*60517a1eSAndroid Build Coastguard Worker            contents=[
227*60517a1eSAndroid Build Coastguard Worker                "_repo_mapping " + rm.Path(),
228*60517a1eSAndroid Build Coastguard Worker                "config.json /etc/config.json",
229*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2/foo/runfile C:/Actual Path\\protobuf\\runfile",
230*60517a1eSAndroid Build Coastguard Worker                "_main/bar/runfile /the/path/./to/other//other runfile.txt",
231*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2/bar/dir E:\\Actual Path\\Directory",
232*60517a1eSAndroid Build Coastguard Worker            ],
233*60517a1eSAndroid Build Coastguard Worker        ) as mf:
234*60517a1eSAndroid Build Coastguard Worker            r = runfiles.CreateManifestBased(mf.Path())
235*60517a1eSAndroid Build Coastguard Worker
236*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
237*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_module/bar/runfile", ""),
238*60517a1eSAndroid Build Coastguard Worker                "/the/path/./to/other//other runfile.txt",
239*60517a1eSAndroid Build Coastguard Worker            )
240*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
241*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_workspace/bar/runfile", ""),
242*60517a1eSAndroid Build Coastguard Worker                "/the/path/./to/other//other runfile.txt",
243*60517a1eSAndroid Build Coastguard Worker            )
244*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
245*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_protobuf/foo/runfile", ""),
246*60517a1eSAndroid Build Coastguard Worker                "C:/Actual Path\\protobuf\\runfile",
247*60517a1eSAndroid Build Coastguard Worker            )
248*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
249*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_protobuf/bar/dir", ""), "E:\\Actual Path\\Directory"
250*60517a1eSAndroid Build Coastguard Worker            )
251*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
252*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_protobuf/bar/dir/file", ""),
253*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory/file",
254*60517a1eSAndroid Build Coastguard Worker            )
255*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
256*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_protobuf/bar/dir/de eply/nes ted/fi~le", ""),
257*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory/de eply/nes ted/fi~le",
258*60517a1eSAndroid Build Coastguard Worker            )
259*60517a1eSAndroid Build Coastguard Worker
260*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("protobuf/foo/runfile"))
261*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("protobuf/bar/dir"))
262*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("protobuf/bar/dir/file"))
263*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("protobuf/bar/dir/dir/de eply/nes ted/fi~le"))
264*60517a1eSAndroid Build Coastguard Worker
265*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
266*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("_main/bar/runfile", ""),
267*60517a1eSAndroid Build Coastguard Worker                "/the/path/./to/other//other runfile.txt",
268*60517a1eSAndroid Build Coastguard Worker            )
269*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
270*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/foo/runfile", ""),
271*60517a1eSAndroid Build Coastguard Worker                "C:/Actual Path\\protobuf\\runfile",
272*60517a1eSAndroid Build Coastguard Worker            )
273*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
274*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/bar/dir", ""), "E:\\Actual Path\\Directory"
275*60517a1eSAndroid Build Coastguard Worker            )
276*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
277*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/bar/dir/file", ""),
278*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory/file",
279*60517a1eSAndroid Build Coastguard Worker            )
280*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
281*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/bar/dir/de eply/nes  ted/fi~le", ""),
282*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory/de eply/nes  ted/fi~le",
283*60517a1eSAndroid Build Coastguard Worker            )
284*60517a1eSAndroid Build Coastguard Worker
285*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(r.Rlocation("config.json", ""), "/etc/config.json")
286*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("_main", ""))
287*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("my_module", ""))
288*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("protobuf", ""))
289*60517a1eSAndroid Build Coastguard Worker
290*60517a1eSAndroid Build Coastguard Worker    def testManifestBasedRlocationWithRepoMappingFromOtherRepo(self) -> None:
291*60517a1eSAndroid Build Coastguard Worker        with _MockFile(
292*60517a1eSAndroid Build Coastguard Worker            contents=[
293*60517a1eSAndroid Build Coastguard Worker                ",config.json,config.json~1.2.3",
294*60517a1eSAndroid Build Coastguard Worker                ",my_module,_main",
295*60517a1eSAndroid Build Coastguard Worker                ",my_protobuf,protobuf~3.19.2",
296*60517a1eSAndroid Build Coastguard Worker                ",my_workspace,_main",
297*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2,config.json,config.json~1.2.3",
298*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2,protobuf,protobuf~3.19.2",
299*60517a1eSAndroid Build Coastguard Worker            ]
300*60517a1eSAndroid Build Coastguard Worker        ) as rm, _MockFile(
301*60517a1eSAndroid Build Coastguard Worker            contents=[
302*60517a1eSAndroid Build Coastguard Worker                "_repo_mapping " + rm.Path(),
303*60517a1eSAndroid Build Coastguard Worker                "config.json /etc/config.json",
304*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2/foo/runfile C:/Actual Path\\protobuf\\runfile",
305*60517a1eSAndroid Build Coastguard Worker                "_main/bar/runfile /the/path/./to/other//other runfile.txt",
306*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2/bar/dir E:\\Actual Path\\Directory",
307*60517a1eSAndroid Build Coastguard Worker            ],
308*60517a1eSAndroid Build Coastguard Worker        ) as mf:
309*60517a1eSAndroid Build Coastguard Worker            r = runfiles.CreateManifestBased(mf.Path())
310*60517a1eSAndroid Build Coastguard Worker
311*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
312*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf/foo/runfile", "protobuf~3.19.2"),
313*60517a1eSAndroid Build Coastguard Worker                "C:/Actual Path\\protobuf\\runfile",
314*60517a1eSAndroid Build Coastguard Worker            )
315*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
316*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf/bar/dir", "protobuf~3.19.2"),
317*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory",
318*60517a1eSAndroid Build Coastguard Worker            )
319*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
320*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf/bar/dir/file", "protobuf~3.19.2"),
321*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory/file",
322*60517a1eSAndroid Build Coastguard Worker            )
323*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
324*60517a1eSAndroid Build Coastguard Worker                r.Rlocation(
325*60517a1eSAndroid Build Coastguard Worker                    "protobuf/bar/dir/de eply/nes  ted/fi~le", "protobuf~3.19.2"
326*60517a1eSAndroid Build Coastguard Worker                ),
327*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory/de eply/nes  ted/fi~le",
328*60517a1eSAndroid Build Coastguard Worker            )
329*60517a1eSAndroid Build Coastguard Worker
330*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("my_module/bar/runfile", "protobuf~3.19.2"))
331*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("my_protobuf/foo/runfile", "protobuf~3.19.2"))
332*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("my_protobuf/bar/dir", "protobuf~3.19.2"))
333*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(
334*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_protobuf/bar/dir/file", "protobuf~3.19.2")
335*60517a1eSAndroid Build Coastguard Worker            )
336*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(
337*60517a1eSAndroid Build Coastguard Worker                r.Rlocation(
338*60517a1eSAndroid Build Coastguard Worker                    "my_protobuf/bar/dir/de eply/nes  ted/fi~le", "protobuf~3.19.2"
339*60517a1eSAndroid Build Coastguard Worker                )
340*60517a1eSAndroid Build Coastguard Worker            )
341*60517a1eSAndroid Build Coastguard Worker
342*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
343*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("_main/bar/runfile", "protobuf~3.19.2"),
344*60517a1eSAndroid Build Coastguard Worker                "/the/path/./to/other//other runfile.txt",
345*60517a1eSAndroid Build Coastguard Worker            )
346*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
347*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/foo/runfile", "protobuf~3.19.2"),
348*60517a1eSAndroid Build Coastguard Worker                "C:/Actual Path\\protobuf\\runfile",
349*60517a1eSAndroid Build Coastguard Worker            )
350*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
351*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/bar/dir", "protobuf~3.19.2"),
352*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory",
353*60517a1eSAndroid Build Coastguard Worker            )
354*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
355*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/bar/dir/file", "protobuf~3.19.2"),
356*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory/file",
357*60517a1eSAndroid Build Coastguard Worker            )
358*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
359*60517a1eSAndroid Build Coastguard Worker                r.Rlocation(
360*60517a1eSAndroid Build Coastguard Worker                    "protobuf~3.19.2/bar/dir/de eply/nes  ted/fi~le", "protobuf~3.19.2"
361*60517a1eSAndroid Build Coastguard Worker                ),
362*60517a1eSAndroid Build Coastguard Worker                "E:\\Actual Path\\Directory/de eply/nes  ted/fi~le",
363*60517a1eSAndroid Build Coastguard Worker            )
364*60517a1eSAndroid Build Coastguard Worker
365*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
366*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("config.json", "protobuf~3.19.2"), "/etc/config.json"
367*60517a1eSAndroid Build Coastguard Worker            )
368*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("_main", "protobuf~3.19.2"))
369*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("my_module", "protobuf~3.19.2"))
370*60517a1eSAndroid Build Coastguard Worker            self.assertIsNone(r.Rlocation("protobuf", "protobuf~3.19.2"))
371*60517a1eSAndroid Build Coastguard Worker
372*60517a1eSAndroid Build Coastguard Worker    def testDirectoryBasedRlocation(self) -> None:
373*60517a1eSAndroid Build Coastguard Worker        # The _DirectoryBased strategy simply joins the runfiles directory and the
374*60517a1eSAndroid Build Coastguard Worker        # runfile's path on a "/". This strategy does not perform any normalization,
375*60517a1eSAndroid Build Coastguard Worker        # nor does it check that the path exists.
376*60517a1eSAndroid Build Coastguard Worker        r = runfiles.CreateDirectoryBased("foo/bar baz//qux/")
377*60517a1eSAndroid Build Coastguard Worker        self.assertEqual(r.Rlocation("arg"), "foo/bar baz//qux/arg")
378*60517a1eSAndroid Build Coastguard Worker        if RunfilesTest.IsWindows():
379*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(r.Rlocation("c:/foo"), "c:/foo")
380*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(r.Rlocation("c:\\foo"), "c:\\foo")
381*60517a1eSAndroid Build Coastguard Worker        else:
382*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(r.Rlocation("/foo"), "/foo")
383*60517a1eSAndroid Build Coastguard Worker
384*60517a1eSAndroid Build Coastguard Worker    def testDirectoryBasedRlocationWithRepoMappingFromMain(self) -> None:
385*60517a1eSAndroid Build Coastguard Worker        with _MockFile(
386*60517a1eSAndroid Build Coastguard Worker            name="_repo_mapping",
387*60517a1eSAndroid Build Coastguard Worker            contents=[
388*60517a1eSAndroid Build Coastguard Worker                "_,config.json,config.json~1.2.3",
389*60517a1eSAndroid Build Coastguard Worker                ",my_module,_main",
390*60517a1eSAndroid Build Coastguard Worker                ",my_protobuf,protobuf~3.19.2",
391*60517a1eSAndroid Build Coastguard Worker                ",my_workspace,_main",
392*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2,config.json,config.json~1.2.3",
393*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2,protobuf,protobuf~3.19.2",
394*60517a1eSAndroid Build Coastguard Worker            ],
395*60517a1eSAndroid Build Coastguard Worker        ) as rm:
396*60517a1eSAndroid Build Coastguard Worker            dir = os.path.dirname(rm.Path())
397*60517a1eSAndroid Build Coastguard Worker            r = runfiles.CreateDirectoryBased(dir)
398*60517a1eSAndroid Build Coastguard Worker
399*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
400*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_module/bar/runfile", ""), dir + "/_main/bar/runfile"
401*60517a1eSAndroid Build Coastguard Worker            )
402*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
403*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_workspace/bar/runfile", ""), dir + "/_main/bar/runfile"
404*60517a1eSAndroid Build Coastguard Worker            )
405*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
406*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_protobuf/foo/runfile", ""),
407*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/foo/runfile",
408*60517a1eSAndroid Build Coastguard Worker            )
409*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
410*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_protobuf/bar/dir", ""), dir + "/protobuf~3.19.2/bar/dir"
411*60517a1eSAndroid Build Coastguard Worker            )
412*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
413*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_protobuf/bar/dir/file", ""),
414*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/bar/dir/file",
415*60517a1eSAndroid Build Coastguard Worker            )
416*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
417*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_protobuf/bar/dir/de eply/nes ted/fi~le", ""),
418*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/bar/dir/de eply/nes ted/fi~le",
419*60517a1eSAndroid Build Coastguard Worker            )
420*60517a1eSAndroid Build Coastguard Worker
421*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
422*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf/foo/runfile", ""), dir + "/protobuf/foo/runfile"
423*60517a1eSAndroid Build Coastguard Worker            )
424*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
425*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf/bar/dir/dir/de eply/nes ted/fi~le", ""),
426*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf/bar/dir/dir/de eply/nes ted/fi~le",
427*60517a1eSAndroid Build Coastguard Worker            )
428*60517a1eSAndroid Build Coastguard Worker
429*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
430*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("_main/bar/runfile", ""), dir + "/_main/bar/runfile"
431*60517a1eSAndroid Build Coastguard Worker            )
432*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
433*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/foo/runfile", ""),
434*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/foo/runfile",
435*60517a1eSAndroid Build Coastguard Worker            )
436*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
437*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/bar/dir", ""),
438*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/bar/dir",
439*60517a1eSAndroid Build Coastguard Worker            )
440*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
441*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/bar/dir/file", ""),
442*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/bar/dir/file",
443*60517a1eSAndroid Build Coastguard Worker            )
444*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
445*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/bar/dir/de eply/nes  ted/fi~le", ""),
446*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/bar/dir/de eply/nes  ted/fi~le",
447*60517a1eSAndroid Build Coastguard Worker            )
448*60517a1eSAndroid Build Coastguard Worker
449*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(r.Rlocation("config.json", ""), dir + "/config.json")
450*60517a1eSAndroid Build Coastguard Worker
451*60517a1eSAndroid Build Coastguard Worker    def testDirectoryBasedRlocationWithRepoMappingFromOtherRepo(self) -> None:
452*60517a1eSAndroid Build Coastguard Worker        with _MockFile(
453*60517a1eSAndroid Build Coastguard Worker            name="_repo_mapping",
454*60517a1eSAndroid Build Coastguard Worker            contents=[
455*60517a1eSAndroid Build Coastguard Worker                "_,config.json,config.json~1.2.3",
456*60517a1eSAndroid Build Coastguard Worker                ",my_module,_main",
457*60517a1eSAndroid Build Coastguard Worker                ",my_protobuf,protobuf~3.19.2",
458*60517a1eSAndroid Build Coastguard Worker                ",my_workspace,_main",
459*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2,config.json,config.json~1.2.3",
460*60517a1eSAndroid Build Coastguard Worker                "protobuf~3.19.2,protobuf,protobuf~3.19.2",
461*60517a1eSAndroid Build Coastguard Worker            ],
462*60517a1eSAndroid Build Coastguard Worker        ) as rm:
463*60517a1eSAndroid Build Coastguard Worker            dir = os.path.dirname(rm.Path())
464*60517a1eSAndroid Build Coastguard Worker            r = runfiles.CreateDirectoryBased(dir)
465*60517a1eSAndroid Build Coastguard Worker
466*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
467*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf/foo/runfile", "protobuf~3.19.2"),
468*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/foo/runfile",
469*60517a1eSAndroid Build Coastguard Worker            )
470*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
471*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf/bar/dir", "protobuf~3.19.2"),
472*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/bar/dir",
473*60517a1eSAndroid Build Coastguard Worker            )
474*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
475*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf/bar/dir/file", "protobuf~3.19.2"),
476*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/bar/dir/file",
477*60517a1eSAndroid Build Coastguard Worker            )
478*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
479*60517a1eSAndroid Build Coastguard Worker                r.Rlocation(
480*60517a1eSAndroid Build Coastguard Worker                    "protobuf/bar/dir/de eply/nes  ted/fi~le", "protobuf~3.19.2"
481*60517a1eSAndroid Build Coastguard Worker                ),
482*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/bar/dir/de eply/nes  ted/fi~le",
483*60517a1eSAndroid Build Coastguard Worker            )
484*60517a1eSAndroid Build Coastguard Worker
485*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
486*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("my_module/bar/runfile", "protobuf~3.19.2"),
487*60517a1eSAndroid Build Coastguard Worker                dir + "/my_module/bar/runfile",
488*60517a1eSAndroid Build Coastguard Worker            )
489*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
490*60517a1eSAndroid Build Coastguard Worker                r.Rlocation(
491*60517a1eSAndroid Build Coastguard Worker                    "my_protobuf/bar/dir/de eply/nes  ted/fi~le", "protobuf~3.19.2"
492*60517a1eSAndroid Build Coastguard Worker                ),
493*60517a1eSAndroid Build Coastguard Worker                dir + "/my_protobuf/bar/dir/de eply/nes  ted/fi~le",
494*60517a1eSAndroid Build Coastguard Worker            )
495*60517a1eSAndroid Build Coastguard Worker
496*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
497*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("_main/bar/runfile", "protobuf~3.19.2"),
498*60517a1eSAndroid Build Coastguard Worker                dir + "/_main/bar/runfile",
499*60517a1eSAndroid Build Coastguard Worker            )
500*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
501*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/foo/runfile", "protobuf~3.19.2"),
502*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/foo/runfile",
503*60517a1eSAndroid Build Coastguard Worker            )
504*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
505*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/bar/dir", "protobuf~3.19.2"),
506*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/bar/dir",
507*60517a1eSAndroid Build Coastguard Worker            )
508*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
509*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("protobuf~3.19.2/bar/dir/file", "protobuf~3.19.2"),
510*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/bar/dir/file",
511*60517a1eSAndroid Build Coastguard Worker            )
512*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
513*60517a1eSAndroid Build Coastguard Worker                r.Rlocation(
514*60517a1eSAndroid Build Coastguard Worker                    "protobuf~3.19.2/bar/dir/de eply/nes  ted/fi~le", "protobuf~3.19.2"
515*60517a1eSAndroid Build Coastguard Worker                ),
516*60517a1eSAndroid Build Coastguard Worker                dir + "/protobuf~3.19.2/bar/dir/de eply/nes  ted/fi~le",
517*60517a1eSAndroid Build Coastguard Worker            )
518*60517a1eSAndroid Build Coastguard Worker
519*60517a1eSAndroid Build Coastguard Worker            self.assertEqual(
520*60517a1eSAndroid Build Coastguard Worker                r.Rlocation("config.json", "protobuf~3.19.2"), dir + "/config.json"
521*60517a1eSAndroid Build Coastguard Worker            )
522*60517a1eSAndroid Build Coastguard Worker
523*60517a1eSAndroid Build Coastguard Worker    def testCurrentRepository(self) -> None:
524*60517a1eSAndroid Build Coastguard Worker        # Under bzlmod, the current repository name is the empty string instead
525*60517a1eSAndroid Build Coastguard Worker        # of the name in the workspace file.
526*60517a1eSAndroid Build Coastguard Worker        if bool(int(os.environ["BZLMOD_ENABLED"])):
527*60517a1eSAndroid Build Coastguard Worker            expected = ""
528*60517a1eSAndroid Build Coastguard Worker        else:
529*60517a1eSAndroid Build Coastguard Worker            expected = "rules_python"
530*60517a1eSAndroid Build Coastguard Worker        r = runfiles.Create({"RUNFILES_DIR": "whatever"})
531*60517a1eSAndroid Build Coastguard Worker        assert r is not None  # mypy doesn't understand the unittest api.
532*60517a1eSAndroid Build Coastguard Worker        self.assertEqual(r.CurrentRepository(), expected)
533*60517a1eSAndroid Build Coastguard Worker
534*60517a1eSAndroid Build Coastguard Worker    @staticmethod
535*60517a1eSAndroid Build Coastguard Worker    def IsWindows() -> bool:
536*60517a1eSAndroid Build Coastguard Worker        return os.name == "nt"
537*60517a1eSAndroid Build Coastguard Worker
538*60517a1eSAndroid Build Coastguard Worker
539*60517a1eSAndroid Build Coastguard Workerclass _MockFile:
540*60517a1eSAndroid Build Coastguard Worker    def __init__(
541*60517a1eSAndroid Build Coastguard Worker        self, name: Optional[str] = None, contents: Optional[List[Any]] = None
542*60517a1eSAndroid Build Coastguard Worker    ) -> None:
543*60517a1eSAndroid Build Coastguard Worker        self._contents = contents or []
544*60517a1eSAndroid Build Coastguard Worker        self._name = name or "x"
545*60517a1eSAndroid Build Coastguard Worker        self._path: Optional[str] = None
546*60517a1eSAndroid Build Coastguard Worker
547*60517a1eSAndroid Build Coastguard Worker    def __enter__(self) -> Any:
548*60517a1eSAndroid Build Coastguard Worker        tmpdir = os.environ.get("TEST_TMPDIR")
549*60517a1eSAndroid Build Coastguard Worker        self._path = os.path.join(tempfile.mkdtemp(dir=tmpdir), self._name)
550*60517a1eSAndroid Build Coastguard Worker        with open(self._path, "wt") as f:
551*60517a1eSAndroid Build Coastguard Worker            f.writelines(l + "\n" for l in self._contents)
552*60517a1eSAndroid Build Coastguard Worker        return self
553*60517a1eSAndroid Build Coastguard Worker
554*60517a1eSAndroid Build Coastguard Worker    def __exit__(
555*60517a1eSAndroid Build Coastguard Worker        self,
556*60517a1eSAndroid Build Coastguard Worker        exc_type: Any,  # pylint: disable=unused-argument
557*60517a1eSAndroid Build Coastguard Worker        exc_value: Any,  # pylint: disable=unused-argument
558*60517a1eSAndroid Build Coastguard Worker        traceback: Any,  # pylint: disable=unused-argument
559*60517a1eSAndroid Build Coastguard Worker    ) -> None:
560*60517a1eSAndroid Build Coastguard Worker        if self._path:
561*60517a1eSAndroid Build Coastguard Worker            os.remove(self._path)
562*60517a1eSAndroid Build Coastguard Worker            os.rmdir(os.path.dirname(self._path))
563*60517a1eSAndroid Build Coastguard Worker
564*60517a1eSAndroid Build Coastguard Worker    def Path(self) -> str:
565*60517a1eSAndroid Build Coastguard Worker        assert self._path is not None
566*60517a1eSAndroid Build Coastguard Worker        return self._path
567*60517a1eSAndroid Build Coastguard Worker
568*60517a1eSAndroid Build Coastguard Worker
569*60517a1eSAndroid Build Coastguard Workerif __name__ == "__main__":
570*60517a1eSAndroid Build Coastguard Worker    unittest.main()
571