diff options
author | Gavin Mak <gavinmak@google.com> | 2023-03-11 06:46:20 +0000 |
---|---|---|
committer | LUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-03-22 17:46:28 +0000 |
commit | ea2e330e43c182dc16b0111ebc69ee5a71ee4ce1 (patch) | |
tree | dc33ba0e56825b3e007d0589891756724725a465 /tests/test_git_command.py | |
parent | 1604cf255f8c1786a23388db6d5277ac7949a24a (diff) | |
download | git-repo-ea2e330e43c182dc16b0111ebc69ee5a71ee4ce1.tar.gz |
Format codebase with black and check formatting in CQ
Apply rules set by https://gerrit-review.googlesource.com/c/git-repo/+/362954/ across the codebase and fix any lingering errors caught
by flake8. Also check black formatting in run_tests (and CQ).
Bug: b/267675342
Change-Id: I972d77649dac351150dcfeb1cd1ad0ea2efc1956
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/363474
Reviewed-by: Mike Frysinger <vapier@google.com>
Tested-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Gavin Mak <gavinmak@google.com>
Diffstat (limited to 'tests/test_git_command.py')
-rw-r--r-- | tests/test_git_command.py | 222 |
1 files changed, 115 insertions, 107 deletions
diff --git a/tests/test_git_command.py b/tests/test_git_command.py index 96408a23..c4c3a4c5 100644 --- a/tests/test_git_command.py +++ b/tests/test_git_command.py | |||
@@ -19,138 +19,146 @@ import os | |||
19 | import unittest | 19 | import unittest |
20 | 20 | ||
21 | try: | 21 | try: |
22 | from unittest import mock | 22 | from unittest import mock |
23 | except ImportError: | 23 | except ImportError: |
24 | import mock | 24 | import mock |
25 | 25 | ||
26 | import git_command | 26 | import git_command |
27 | import wrapper | 27 | import wrapper |
28 | 28 | ||
29 | 29 | ||
30 | class GitCommandTest(unittest.TestCase): | 30 | class GitCommandTest(unittest.TestCase): |
31 | """Tests the GitCommand class (via git_command.git).""" | 31 | """Tests the GitCommand class (via git_command.git).""" |
32 | 32 | ||
33 | def setUp(self): | 33 | def setUp(self): |
34 | def realpath_mock(val): | ||
35 | return val | ||
34 | 36 | ||
35 | def realpath_mock(val): | 37 | mock.patch.object( |
36 | return val | 38 | os.path, "realpath", side_effect=realpath_mock |
39 | ).start() | ||
37 | 40 | ||
38 | mock.patch.object(os.path, 'realpath', side_effect=realpath_mock).start() | 41 | def tearDown(self): |
42 | mock.patch.stopall() | ||
39 | 43 | ||
40 | def tearDown(self): | 44 | def test_alternative_setting_when_matching(self): |
41 | mock.patch.stopall() | 45 | r = git_command._build_env( |
46 | objdir=os.path.join("zap", "objects"), gitdir="zap" | ||
47 | ) | ||
42 | 48 | ||
43 | def test_alternative_setting_when_matching(self): | 49 | self.assertIsNone(r.get("GIT_ALTERNATE_OBJECT_DIRECTORIES")) |
44 | r = git_command._build_env( | 50 | self.assertEqual( |
45 | objdir = os.path.join('zap', 'objects'), | 51 | r.get("GIT_OBJECT_DIRECTORY"), os.path.join("zap", "objects") |
46 | gitdir = 'zap' | 52 | ) |
47 | ) | ||
48 | 53 | ||
49 | self.assertIsNone(r.get('GIT_ALTERNATE_OBJECT_DIRECTORIES')) | 54 | def test_alternative_setting_when_different(self): |
50 | self.assertEqual(r.get('GIT_OBJECT_DIRECTORY'), os.path.join('zap', 'objects')) | 55 | r = git_command._build_env( |
56 | objdir=os.path.join("wow", "objects"), gitdir="zap" | ||
57 | ) | ||
51 | 58 | ||
52 | def test_alternative_setting_when_different(self): | 59 | self.assertEqual( |
53 | r = git_command._build_env( | 60 | r.get("GIT_ALTERNATE_OBJECT_DIRECTORIES"), |
54 | objdir = os.path.join('wow', 'objects'), | 61 | os.path.join("zap", "objects"), |
55 | gitdir = 'zap' | 62 | ) |
56 | ) | 63 | self.assertEqual( |
57 | 64 | r.get("GIT_OBJECT_DIRECTORY"), os.path.join("wow", "objects") | |
58 | self.assertEqual(r.get('GIT_ALTERNATE_OBJECT_DIRECTORIES'), os.path.join('zap', 'objects')) | 65 | ) |
59 | self.assertEqual(r.get('GIT_OBJECT_DIRECTORY'), os.path.join('wow', 'objects')) | ||
60 | 66 | ||
61 | 67 | ||
62 | class GitCallUnitTest(unittest.TestCase): | 68 | class GitCallUnitTest(unittest.TestCase): |
63 | """Tests the _GitCall class (via git_command.git).""" | 69 | """Tests the _GitCall class (via git_command.git).""" |
64 | 70 | ||
65 | def test_version_tuple(self): | 71 | def test_version_tuple(self): |
66 | """Check git.version_tuple() handling.""" | 72 | """Check git.version_tuple() handling.""" |
67 | ver = git_command.git.version_tuple() | 73 | ver = git_command.git.version_tuple() |
68 | self.assertIsNotNone(ver) | 74 | self.assertIsNotNone(ver) |
69 | 75 | ||
70 | # We don't dive too deep into the values here to avoid having to update | 76 | # We don't dive too deep into the values here to avoid having to update |
71 | # whenever git versions change. We do check relative to this min version | 77 | # whenever git versions change. We do check relative to this min |
72 | # as this is what `repo` itself requires via MIN_GIT_VERSION. | 78 | # version as this is what `repo` itself requires via MIN_GIT_VERSION. |
73 | MIN_GIT_VERSION = (2, 10, 2) | 79 | MIN_GIT_VERSION = (2, 10, 2) |
74 | self.assertTrue(isinstance(ver.major, int)) | 80 | self.assertTrue(isinstance(ver.major, int)) |
75 | self.assertTrue(isinstance(ver.minor, int)) | 81 | self.assertTrue(isinstance(ver.minor, int)) |
76 | self.assertTrue(isinstance(ver.micro, int)) | 82 | self.assertTrue(isinstance(ver.micro, int)) |
77 | 83 | ||
78 | self.assertGreater(ver.major, MIN_GIT_VERSION[0] - 1) | 84 | self.assertGreater(ver.major, MIN_GIT_VERSION[0] - 1) |
79 | self.assertGreaterEqual(ver.micro, 0) | 85 | self.assertGreaterEqual(ver.micro, 0) |
80 | self.assertGreaterEqual(ver.major, 0) | 86 | self.assertGreaterEqual(ver.major, 0) |
81 | 87 | ||
82 | self.assertGreaterEqual(ver, MIN_GIT_VERSION) | 88 | self.assertGreaterEqual(ver, MIN_GIT_VERSION) |
83 | self.assertLess(ver, (9999, 9999, 9999)) | 89 | self.assertLess(ver, (9999, 9999, 9999)) |
84 | 90 | ||
85 | self.assertNotEqual('', ver.full) | 91 | self.assertNotEqual("", ver.full) |
86 | 92 | ||
87 | 93 | ||
88 | class UserAgentUnitTest(unittest.TestCase): | 94 | class UserAgentUnitTest(unittest.TestCase): |
89 | """Tests the UserAgent function.""" | 95 | """Tests the UserAgent function.""" |
90 | 96 | ||
91 | def test_smoke_os(self): | 97 | def test_smoke_os(self): |
92 | """Make sure UA OS setting returns something useful.""" | 98 | """Make sure UA OS setting returns something useful.""" |
93 | os_name = git_command.user_agent.os | 99 | os_name = git_command.user_agent.os |
94 | # We can't dive too deep because of OS/tool differences, but we can check | 100 | # We can't dive too deep because of OS/tool differences, but we can |
95 | # the general form. | 101 | # check the general form. |
96 | m = re.match(r'^[^ ]+$', os_name) | 102 | m = re.match(r"^[^ ]+$", os_name) |
97 | self.assertIsNotNone(m) | 103 | self.assertIsNotNone(m) |
98 | 104 | ||
99 | def test_smoke_repo(self): | 105 | def test_smoke_repo(self): |
100 | """Make sure repo UA returns something useful.""" | 106 | """Make sure repo UA returns something useful.""" |
101 | ua = git_command.user_agent.repo | 107 | ua = git_command.user_agent.repo |
102 | # We can't dive too deep because of OS/tool differences, but we can check | 108 | # We can't dive too deep because of OS/tool differences, but we can |
103 | # the general form. | 109 | # check the general form. |
104 | m = re.match(r'^git-repo/[^ ]+ ([^ ]+) git/[^ ]+ Python/[0-9.]+', ua) | 110 | m = re.match(r"^git-repo/[^ ]+ ([^ ]+) git/[^ ]+ Python/[0-9.]+", ua) |
105 | self.assertIsNotNone(m) | 111 | self.assertIsNotNone(m) |
106 | 112 | ||
107 | def test_smoke_git(self): | 113 | def test_smoke_git(self): |
108 | """Make sure git UA returns something useful.""" | 114 | """Make sure git UA returns something useful.""" |
109 | ua = git_command.user_agent.git | 115 | ua = git_command.user_agent.git |
110 | # We can't dive too deep because of OS/tool differences, but we can check | 116 | # We can't dive too deep because of OS/tool differences, but we can |
111 | # the general form. | 117 | # check the general form. |
112 | m = re.match(r'^git/[^ ]+ ([^ ]+) git-repo/[^ ]+', ua) | 118 | m = re.match(r"^git/[^ ]+ ([^ ]+) git-repo/[^ ]+", ua) |
113 | self.assertIsNotNone(m) | 119 | self.assertIsNotNone(m) |
114 | 120 | ||
115 | 121 | ||
116 | class GitRequireTests(unittest.TestCase): | 122 | class GitRequireTests(unittest.TestCase): |
117 | """Test the git_require helper.""" | 123 | """Test the git_require helper.""" |
118 | 124 | ||
119 | def setUp(self): | 125 | def setUp(self): |
120 | self.wrapper = wrapper.Wrapper() | 126 | self.wrapper = wrapper.Wrapper() |
121 | ver = self.wrapper.GitVersion(1, 2, 3, 4) | 127 | ver = self.wrapper.GitVersion(1, 2, 3, 4) |
122 | mock.patch.object(git_command.git, 'version_tuple', return_value=ver).start() | 128 | mock.patch.object( |
123 | 129 | git_command.git, "version_tuple", return_value=ver | |
124 | def tearDown(self): | 130 | ).start() |
125 | mock.patch.stopall() | 131 | |
126 | 132 | def tearDown(self): | |
127 | def test_older_nonfatal(self): | 133 | mock.patch.stopall() |
128 | """Test non-fatal require calls with old versions.""" | 134 | |
129 | self.assertFalse(git_command.git_require((2,))) | 135 | def test_older_nonfatal(self): |
130 | self.assertFalse(git_command.git_require((1, 3))) | 136 | """Test non-fatal require calls with old versions.""" |
131 | self.assertFalse(git_command.git_require((1, 2, 4))) | 137 | self.assertFalse(git_command.git_require((2,))) |
132 | self.assertFalse(git_command.git_require((1, 2, 3, 5))) | 138 | self.assertFalse(git_command.git_require((1, 3))) |
133 | 139 | self.assertFalse(git_command.git_require((1, 2, 4))) | |
134 | def test_newer_nonfatal(self): | 140 | self.assertFalse(git_command.git_require((1, 2, 3, 5))) |
135 | """Test non-fatal require calls with newer versions.""" | 141 | |
136 | self.assertTrue(git_command.git_require((0,))) | 142 | def test_newer_nonfatal(self): |
137 | self.assertTrue(git_command.git_require((1, 0))) | 143 | """Test non-fatal require calls with newer versions.""" |
138 | self.assertTrue(git_command.git_require((1, 2, 0))) | 144 | self.assertTrue(git_command.git_require((0,))) |
139 | self.assertTrue(git_command.git_require((1, 2, 3, 0))) | 145 | self.assertTrue(git_command.git_require((1, 0))) |
140 | 146 | self.assertTrue(git_command.git_require((1, 2, 0))) | |
141 | def test_equal_nonfatal(self): | 147 | self.assertTrue(git_command.git_require((1, 2, 3, 0))) |
142 | """Test require calls with equal values.""" | 148 | |
143 | self.assertTrue(git_command.git_require((1, 2, 3, 4), fail=False)) | 149 | def test_equal_nonfatal(self): |
144 | self.assertTrue(git_command.git_require((1, 2, 3, 4), fail=True)) | 150 | """Test require calls with equal values.""" |
145 | 151 | self.assertTrue(git_command.git_require((1, 2, 3, 4), fail=False)) | |
146 | def test_older_fatal(self): | 152 | self.assertTrue(git_command.git_require((1, 2, 3, 4), fail=True)) |
147 | """Test fatal require calls with old versions.""" | 153 | |
148 | with self.assertRaises(SystemExit) as e: | 154 | def test_older_fatal(self): |
149 | git_command.git_require((2,), fail=True) | 155 | """Test fatal require calls with old versions.""" |
150 | self.assertNotEqual(0, e.code) | 156 | with self.assertRaises(SystemExit) as e: |
151 | 157 | git_command.git_require((2,), fail=True) | |
152 | def test_older_fatal_msg(self): | 158 | self.assertNotEqual(0, e.code) |
153 | """Test fatal require calls with old versions and message.""" | 159 | |
154 | with self.assertRaises(SystemExit) as e: | 160 | def test_older_fatal_msg(self): |
155 | git_command.git_require((2,), fail=True, msg='so sad') | 161 | """Test fatal require calls with old versions and message.""" |
156 | self.assertNotEqual(0, e.code) | 162 | with self.assertRaises(SystemExit) as e: |
163 | git_command.git_require((2,), fail=True, msg="so sad") | ||
164 | self.assertNotEqual(0, e.code) | ||