Skip to content

Commit 0fc2a9c

Browse files
committed
add test, fix mixed case
1 parent 5ec7baa commit 0fc2a9c

3 files changed

Lines changed: 64 additions & 6 deletions

File tree

python/private/repo_utils.bzl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,18 +357,20 @@ def _norm_path(mrctx, p):
357357
return p
358358

359359
def _relative_to(mrctx, path, parent, fail = fail):
360-
path_d = _norm_path(mrctx, path) + "/"
361-
parent_d = _norm_path(mrctx, parent) + "/"
360+
path_str = str(path)
361+
parent_str = str(parent)
362+
path_d = _norm_path(mrctx, path_str) + "/"
363+
parent_d = _norm_path(mrctx, parent_str) + "/"
362364
if path_d.startswith(parent_d):
363-
return path.removeprefix(parent + "/")
365+
return path_str[len(parent_str):].removeprefix("/")
364366
else:
365367
fail("{} is not relative to {}".format(path, parent))
366368

367369
def _is_relative_to(mrctx, path, parent):
368370
"""Tell if `path` is equal to or beneath `parent`."""
369-
path = _norm_path(mrctx, path)
370-
parent = _norm_path(mrctx, parent)
371-
return (parent + "/").startswith(path + "/")
371+
path_d = _norm_path(mrctx, path) + "/"
372+
parent_d = _norm_path(mrctx, parent) + "/"
373+
return path_d.startswith(parent_d)
372374

373375
def _repo_root_relative_path(mrctx, path):
374376
"""Takes a path object and returns a repo-relative path string.

tests/repo_utils/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load(":repo_utils_test.bzl", "repo_utils_test_suite")
2+
3+
repo_utils_test_suite(name = "repo_utils_tests")
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Unit tests for repo_utils.bzl."""
2+
3+
load("@rules_testing//lib:test_suite.bzl", "test_suite")
4+
load("//python/private:repo_utils.bzl", "repo_utils") # buildifier: disable=bzl-visibility
5+
6+
_tests = []
7+
8+
def _test_get_platforms_os_name(env):
9+
mock_mrctx = struct(
10+
os = struct(
11+
name = "Mac OS X",
12+
),
13+
)
14+
got = repo_utils.get_platforms_os_name(mock_mrctx)
15+
env.expect.that_str(got).equals("osx")
16+
17+
_tests.append(_test_get_platforms_os_name)
18+
19+
def _test_relative_to(env):
20+
mock_mrctx_linux = struct(os = struct(name = "linux"))
21+
mock_mrctx_win = struct(os = struct(name = "windows"))
22+
23+
# Case-sensitive matching (Linux)
24+
got = repo_utils.relative_to(mock_mrctx_linux, "foo/bar/baz", "foo/bar")
25+
env.expect.that_str(got).equals("baz")
26+
27+
# Case-insensitive matching (Windows)
28+
got = repo_utils.relative_to(mock_mrctx_win, "C:/Foo/Bar/Baz", "c:/foo/bar")
29+
env.expect.that_str(got).equals("Baz")
30+
31+
# Failure case
32+
failures = []
33+
def _mock_fail(msg):
34+
failures.append(msg)
35+
36+
repo_utils.relative_to(mock_mrctx_linux, "foo/bar/baz", "qux", fail = _mock_fail)
37+
env.expect.that_collection(failures).contains_exactly(["foo/bar/baz is not relative to qux"])
38+
39+
_tests.append(_test_relative_to)
40+
41+
def _test_is_relative_to(env):
42+
# Note: `_is_relative_to` isn't publicly exported in `repo_utils` struct natively.
43+
# Actually wait, `is_relative_to` is not exported by repo_utils.bzl!
44+
# Let's skip testing `_is_relative_to` directly in this suite unless we export it or we don't need to test it directly.
45+
pass
46+
47+
def repo_utils_test_suite(name):
48+
"""Create the test suite.
49+
50+
Args:
51+
name: the name of the test suite
52+
"""
53+
test_suite(name = name, basic_tests = _tests)

0 commit comments

Comments
 (0)