blob: c0032bed01eb57153a7d1d653f8fcaf4af8f4a94 [file] [log] [blame]
Rob Mohr5dd297c2024-08-19 16:43:24 +00001# Copyright 2024 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7# https://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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Test API for bazel_roll."""
15
Rob Mohr3eef3552024-11-22 18:31:25 +000016from PB.recipe_modules.pigweed.bazel_roll.cipd_package import BazelCipdPackage
Rob Mohr5dd297c2024-08-19 16:43:24 +000017from PB.recipe_modules.pigweed.bazel_roll.git_repository import GitRepository
18from recipe_engine import recipe_test_api
19
20
Rob Mohr1e134db2024-09-30 17:48:02 +000021TEST_WORKSPACE_FILE = """
22git_repository(
23 name = "other-repo"
24 remote = "https://pigweed.googlesource.com/other/repo.git",
25 commit = "invalid commit line won't be found",
26)
27
28git_repository(
29 module_name = "pigweed",
30 # ROLL: Multiple
31 # ROLL: roll
32 # ROLL: comment
33 # ROLL: lines!
34 commit = "1111111111111111111111111111111111111111",
35 remote = "https://pigweed.googlesource.com/pigweed/pigweed.git",
36 git_repository_attribute_test = "ignored",
37 strip_prefix = "pw_toolchain_bazel",
38)
39
40git_repository(
41 name = "missing final quote/comma so will miss this line
42 remote = "https://pigweed.googlesource.com/third/repo.git",
43 commit = "2222222222222222222222222222222222222222",
44)
Rob Mohr3eef3552024-11-22 18:31:25 +000045
46cipd_repository(
47 name = "llvm_toolchain",
48 build_file = "//pw_toolchain/build_external:llvm_clang.BUILD",
49 path = "fuchsia/third_party/clang/${os}-${arch}",
50 tag = "git_revision:8280651ad57cb9fb24a404cec2401040c28dec98",
51)
Rob Mohr1e134db2024-09-30 17:48:02 +000052"""
53
54MULTIPLE_ROLL_WORKSPACE_FILE = """
55git_repository(
56 name = "pigweed",
57 # ROLL: Comment line 1.
58 commit = "1111111111111111111111111111111111111111",
59 remote = "https://pigweed.googlesource.com/pigweed/pigweed.git",
60)
61
62# Filler
63# lines
64# to
65# pad
66# out
67# the
68# file.
69
70git_override(
71 module_name = "pw_toolchain",
72 # ROLL: Comment line 2.
73 commit = "1111111111111111111111111111111111111111",
74 remote = "https://pigweed.googlesource.com/pigweed/pigweed",
75 strip_prefix = "pw_toolchain_bazel",
76)
77"""
78
79MULTIPLE_ROLL_WORKSPACE_FILE_2 = """
80git_repository(
81 name = "pigweed",
82 # ROLL: Comment line 1.
83 # ROLL: Comment line 2.
84 # ROLL: Comment line 3.
85 # ROLL: Comment line 4.
86 # ROLL: Comment line 5.
87 # ROLL: Comment line 6.
88 # ROLL: Comment line 7.
89 commit = "1111111111111111111111111111111111111111",
90 remote = "https://pigweed.googlesource.com/pigweed/pigweed.git",
91)
92
93git_repository(
94 name = "pw_toolchain",
95 commit = "1111111111111111111111111111111111111111",
96 remote = "https://pigweed.googlesource.com/pigweed/pigweed",
97 strip_prefix = "pw_toolchain_bazel",
98)
99"""
100
101
Rob Mohr5dd297c2024-08-19 16:43:24 +0000102class BazelRollTestApi(recipe_test_api.RecipeTestApi):
103 """Test API for bazel_roll."""
104
Rob Mohr1e134db2024-09-30 17:48:02 +0000105 TEST_WORKSPACE_FILE = TEST_WORKSPACE_FILE
106 MULTIPLE_ROLL_WORKSPACE_FILE = MULTIPLE_ROLL_WORKSPACE_FILE
107 MULTIPLE_ROLL_WORKSPACE_FILE_2 = MULTIPLE_ROLL_WORKSPACE_FILE_2
108
Rob Mohr3eef3552024-11-22 18:31:25 +0000109 def cipd_entry(
110 self,
111 spec: str,
112 name: str | None = None,
113 ref: str = 'latest',
114 tag: str = 'git_revision',
115 comments: int = 2,
116 **kwargs,
117 ):
118 if not name: # pragma: no cover
119 parts = spec.split('/')
120 while '{' in parts[-1] or parts[-1].endswith(('-amd64', '-arm64')):
121 parts.pop()
122 name = parts[-1]
123
124 if ':' not in tag:
125 tag = f'{tag}:1.2.3' # pragma: no cover
126
127 result = []
128 result.append('cipd_repository(')
129 result.append(f' name = "{name}",')
130 result.append(f' path = "{spec}",')
131 result.append(f' ref = "{ref}",')
132 for i in range(comments):
133 result.append(f' # ROLL: Comment line {i}.')
134 result.append(f' tag = "{tag}",')
135 for key, value in kwargs.items():
136 if isinstance(value, str):
137 value = f'"{value}"'
138 assert isinstance(value, (str, int))
139 result.append(f' {key} = {value},')
140 result.append(')')
141 return '\n'.join(result)
142
143 def git_entry(
144 self,
145 remote: str,
146 revision: str = '3' * 40,
147 branch: str | None = None,
148 name: str | None = None,
149 comments: int = 2,
150 **kwargs,
151 ):
152 name = name or remote.split('/')[-1].removesuffix('.git')
153
154 result = []
155 result.append('git_override(')
156 if branch:
157 result.append(f' branch = "{branch}",') # pragma: no cover
158 result.append(f' name = "{name}",')
159 result.append(f' remote = "{remote}",')
160 for i in range(comments):
161 result.append(f' # ROLL: Comment line {i}.')
162 result.append(f' commit = "{revision}",')
163 for key, value in kwargs.items():
164 if isinstance(value, str):
165 value = f'"{value}"'
166 assert isinstance(value, (str, int))
167 result.append(f' {key} = {value},')
168 result.append(')')
169 return '\n'.join(result)
170
171 def workspace_file(
172 self,
173 prefix: str,
174 *entries: BazelCipdPackage | GitRepository,
175 ):
176 if prefix:
177 prefix = f'{prefix.rstrip(".")}.'
178 return self.override_step_data(
179 f'{prefix}read old WORKSPACE',
180 self.m.file.read_text('\n\n'.join(entries)),
181 )
182
183 def cipd_package(
184 self,
185 *,
186 name='',
187 workspace_path='',
188 spec='fuchsia/third_party/clang/${os}-${arch}',
189 ref='latest',
190 tag='git_revision',
191 allow_mismatched_refs=False,
192 ):
193 return BazelCipdPackage(
194 workspace_path=workspace_path,
195 name=name,
196 spec=spec,
197 ref=ref,
198 tag=tag,
199 allow_mismatched_refs=allow_mismatched_refs,
200 )
201
Rob Mohr5dd297c2024-08-19 16:43:24 +0000202 def git_repo(
203 self,
204 *,
Rob Mohr4a87fc52024-09-18 15:46:22 +0000205 name='',
Rob Mohr5dd297c2024-08-19 16:43:24 +0000206 workspace_path='',
Rob Mohr4a87fc52024-09-18 15:46:22 +0000207 remote='https://pigweed.googlesource.com/pigweed/pigweed',
Rob Mohr5dd297c2024-08-19 16:43:24 +0000208 branch='main',
209 ):
210 return GitRepository(
211 workspace_path=workspace_path,
212 name=name,
Rob Mohr4a87fc52024-09-18 15:46:22 +0000213 remote=remote,
Rob Mohr5dd297c2024-08-19 16:43:24 +0000214 branch=branch,
215 )