File size: 2,639 Bytes
198fb2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3

import os
import shutil
import subprocess
import sys
import unittest

sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))

import genMetrics


class GitHeadCommitTests(unittest.TestCase):
    """Tests git_head_commit()'s fallback behavior so commit metadata
    extraction degrades gracefully instead of raising."""

    def test_returns_descriptive_string_when_git_not_on_path(self):
        """Nix builds and hermetic Bazel sandboxes can lack git; the
        helper must report that distinctly rather than masquerading as
        'not a git repo'."""
        self.assertEqual(genMetrics.git_head_commit(None, "/tmp"), "git not on PATH")

    def test_returns_na_when_folder_is_not_a_directory(self):
        """An unset/misconfigured PLATFORM_DIR must not let subprocess
        raise FileNotFoundError on cwd."""
        self.assertEqual(
            genMetrics.git_head_commit("/usr/bin/git", "/no/such/directory/orfs-test"),
            "N/A",
        )


class ShutilWhichGitConsistencyTests(unittest.TestCase):
    """Cross-checks shutil.which("git") against actually invoking git, so
    we catch any environment (e.g. a stripped Nix sandbox) where the two
    disagree and the PATH probe would give the wrong answer."""

    def test_which_matches_git_version(self):
        which_result = shutil.which("git")
        try:
            output = subprocess.check_output(
                ["git", "--version"], stderr=subprocess.STDOUT, text=True
            )
        except FileNotFoundError:
            self.assertIsNone(
                which_result,
                "shutil.which found git but invoking it raised FileNotFoundError",
            )
            return

        self.assertIsNotNone(
            which_result,
            "git --version succeeded but shutil.which returned None",
        )
        self.assertTrue(output.startswith("git version "), output)


class ShutilWhichMissingCommandTests(unittest.TestCase):
    """Validates shutil.which() returns None for a non-existent command.
    This is the primitive git_head_commit() relies on at the call site;
    if it ever returned a bogus path (in Nix, in a Bazel sandbox,
    anywhere) the missing-git branch would never fire."""

    BOGUS_CMD = "orfs-genmetrics-no-such-command-xyzzy"

    def test_which_returns_none_for_missing_command(self):
        self.assertIsNone(shutil.which(self.BOGUS_CMD))

    def test_invoking_missing_command_raises_file_not_found(self):
        with self.assertRaises(FileNotFoundError):
            subprocess.check_call([self.BOGUS_CMD])


if __name__ == "__main__":
    unittest.main()