|
| 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