Skip to content

Commit c6ca64b

Browse files
chore: fix consumer tests (#475)
Fixing of handling bazel_dep without 'version' in them Fixing of commeting out git_override blocks
1 parent 0c0a2b9 commit c6ca64b

1 file changed

Lines changed: 35 additions & 22 deletions

File tree

src/tests/test_consumer.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -196,49 +196,62 @@ def filter_repos(repo_filter: str | None) -> list[ConsumerRepo]:
196196

197197
return filtered_repos
198198

199-
200199
def comment_out_git_override(module_content: str) -> str:
200+
201201
"""
202-
Comment out existing git_override blocks for score_docs_as_code if found
202+
Comment out existing git_override blocks for score_docs_as_code only.
203203
"""
204+
lines = module_content.splitlines()
205+
result = []
206+
i = 0
204207

205-
pattern = (
206-
r"^(git_override\s*\(\s*"
207-
r"[^)]*?module_name\s*=\s*['\"]score_docs_as_code['\"]"
208-
r"[^)]*\)\s*)"
209-
)
208+
while i < len(lines):
209+
line = lines[i]
210210

211-
def comment_out_block(match: re.Match[str]) -> str:
212-
# Comment out each line of the found block
213-
return "\n".join("# " + line for line in match.group(0).splitlines())
211+
# Check if this line starts a git_override block
212+
if re.match(r"^\s*git_override\s*\(", line):
213+
# Collect the entire block
214+
block_start = i
215+
depth = line.count("(") - line.count(")")
216+
i += 1
214217

215-
# First, comment out old override(s)
216-
out = re.sub(
217-
pattern, comment_out_block, module_content, flags=re.MULTILINE | re.DOTALL
218-
)
219-
return out.strip()
218+
while i < len(lines) and depth > 0:
219+
depth += lines[i].count("(") - lines[i].count(")")
220+
i += 1
220221

222+
# Extract the block
223+
block = lines[block_start:i]
224+
block_text = "\n".join(block)
221225

222-
def replace_bazel_dep_with_local_override(module_content: str) -> str:
223-
""" """
226+
# Comment out if it's for score_docs_as_code
227+
if 'module_name = "score_docs_as_code"' in block_text or \
228+
"module_name = 'score_docs_as_code'" in block_text:
229+
result.extend("# " + line if line.strip() else "#" for line in block)
230+
else:
231+
result.extend(block)
232+
else:
233+
result.append(line)
234+
i += 1
224235

225-
# Pattern to match the bazel_dep line
226-
pattern = r'bazel_dep\(name = "score_docs_as_code", version = "[^"]+"\)'
236+
return "\n".join(result) + ("\n" if module_content.endswith("\n") else "")
237+
238+
239+
def replace_bazel_dep_with_local_override(module_content: str) -> str:
240+
# Match bazel_dep with required name and optional version
241+
pattern = r'bazel_dep\(name = "score_docs_as_code"(?:, version = "[^"]+")?\)'
227242

228-
# Replacement with local_path_override
229243
replacement = """bazel_dep(name = "score_docs_as_code", version = "0.0.0")
230244
local_path_override(
231245
module_name = "score_docs_as_code",
232246
path = "../docs_as_code"
233247
)"""
234-
235248
return re.sub(pattern, replacement, module_content)
236249

237250

238251
def replace_bazel_dep_with_git_override(
239252
module_content: str, git_hash: str, gh_url: str
240253
) -> str:
241-
pattern = r'bazel_dep\(name = "score_docs_as_code", version = "[^"]+"\)'
254+
pattern = r'bazel_dep\(name = "score_docs_as_code"(?:, version = "[^"]+")?\)'
242255

243256
replacement = f'''bazel_dep(name = "score_docs_as_code", version = "0.0.0")
244257
git_override(

0 commit comments

Comments
 (0)