Skip to content

Commit ff85225

Browse files
committed
genMetrics: address Gemini review comments on PR #4206
- Move `now = datetime.now()` back to module scope so `extract_metrics` remains importable as a library (it references `now` at line 199). Moving it under `if __name__ == "__main__":` would have broken the library path with a NameError. - In `is_git_repo()`, also short-circuit to False when `folder` is not a valid directory, so callers like the PLATFORM_DIR branch in `extract_metrics` don't surface a `FileNotFoundError` from subprocess trying to chdir to a bogus path. - Add unit tests for both: a non-directory `folder` returns False, and `genMetrics.now` is module-level so the import-time contract for `extract_metrics` callers is preserved. Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
1 parent 091ea59 commit ff85225

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎flow/util/genMetrics.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ def read_sdc(file_name):
174174
def is_git_repo(folder=None):
175175
if shutil.which("git") is None:
176176
return False
177+
if folder is not None and not os.path.isdir(folder):
178+
return False
177179
cmd = ["git", "branch"]
178180
with open(os.devnull, "w") as devnull:
179181
if folder is not None:
@@ -373,9 +375,11 @@ def extract_metrics(
373375
json.dump(metrics_dict, resultSpecfile, indent=2, sort_keys=True)
374376

375377

378+
now = datetime.now()
379+
380+
376381
if __name__ == "__main__":
377382
args = parse_args()
378-
now = datetime.now()
379383

380384
extract_metrics(
381385
os.path.join(os.path.dirname(os.path.realpath(__file__)), "../"),

‎flow/util/genMetrics_test.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
import datetime
34
import os
45
import shutil
56
import subprocess
@@ -22,6 +23,24 @@ def test_returns_false_when_git_not_on_path(self):
2223
self.assertFalse(genMetrics.is_git_repo())
2324
self.assertFalse(genMetrics.is_git_repo(folder="/tmp"))
2425

26+
def test_returns_false_when_folder_is_not_a_directory(self):
27+
"""When the caller passes a folder that doesn't exist (e.g. an
28+
unset/misconfigured PLATFORM_DIR), is_git_repo() must return False
29+
without letting subprocess raise FileNotFoundError on cwd."""
30+
with mock.patch.object(genMetrics.shutil, "which", return_value="/usr/bin/git"):
31+
self.assertFalse(
32+
genMetrics.is_git_repo(folder="/no/such/directory/orfs-test")
33+
)
34+
35+
36+
class ModuleImportableTests(unittest.TestCase):
37+
"""Guards against module-scope state that extract_metrics() depends on
38+
silently moving into the `if __name__ == "__main__"` block — that would
39+
break library imports with a NameError at call time."""
40+
41+
def test_now_is_module_level_datetime(self):
42+
self.assertIsInstance(genMetrics.now, datetime.datetime)
43+
2544

2645
class ShutilWhichGitConsistencyTests(unittest.TestCase):
2746
"""Cross-checks shutil.which("git") against actually invoking git, so

0 commit comments

Comments
 (0)