Skip to content

Commit a2d7aba

Browse files
committed
refacator mocks to mocks
1 parent 0fc2a9c commit a2d7aba

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

tests/repo_utils/repo_utils_test.bzl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@
22

33
load("@rules_testing//lib:test_suite.bzl", "test_suite")
44
load("//python/private:repo_utils.bzl", "repo_utils") # buildifier: disable=bzl-visibility
5+
load("//tests/support:mocks.bzl", "mocks")
56

67
_tests = []
78

89
def _test_get_platforms_os_name(env):
9-
mock_mrctx = struct(
10-
os = struct(
11-
name = "Mac OS X",
12-
),
13-
)
10+
mock_mrctx = mocks.rctx(os_name = "Mac OS X")
1411
got = repo_utils.get_platforms_os_name(mock_mrctx)
1512
env.expect.that_str(got).equals("osx")
1613

1714
_tests.append(_test_get_platforms_os_name)
1815

1916
def _test_relative_to(env):
20-
mock_mrctx_linux = struct(os = struct(name = "linux"))
21-
mock_mrctx_win = struct(os = struct(name = "windows"))
17+
mock_mrctx_linux = mocks.rctx(os_name = "linux")
18+
mock_mrctx_win = mocks.rctx(os_name = "windows")
2219

2320
# Case-sensitive matching (Linux)
2421
got = repo_utils.relative_to(mock_mrctx_linux, "foo/bar/baz", "foo/bar")

tests/support/mocks.bzl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Mocks for testing."""
2+
3+
def _rctx(os_name = "linux", os_arch = "x86_64", environ = None, **kwargs):
4+
"""Creates a mock of repository_ctx or module_ctx.
5+
6+
Args:
7+
os_name: The OS name to mock (e.g., "linux", "Mac OS X", "windows").
8+
os_arch: The OS architecture to mock (e.g., "x86_64", "aarch64").
9+
environ: A dictionary representing the environment variables.
10+
**kwargs: Additional attributes to add to the mock struct.
11+
12+
Returns:
13+
A struct mocking repository_ctx.
14+
"""
15+
if environ == None:
16+
environ = {}
17+
18+
attrs = {
19+
"os": struct(
20+
name = os_name,
21+
arch = os_arch,
22+
),
23+
"getenv": environ.get,
24+
}
25+
attrs.update(kwargs)
26+
27+
return struct(**attrs)
28+
29+
mocks = struct(
30+
rctx = _rctx,
31+
)

0 commit comments

Comments
 (0)