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_subcmds_sync.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_subcmds_sync.py')
-rw-r--r-- | tests/test_subcmds_sync.py | 215 |
1 files changed, 121 insertions, 94 deletions
diff --git a/tests/test_subcmds_sync.py b/tests/test_subcmds_sync.py index 236d54e5..5c8e606e 100644 --- a/tests/test_subcmds_sync.py +++ b/tests/test_subcmds_sync.py | |||
@@ -23,111 +23,138 @@ import command | |||
23 | from subcmds import sync | 23 | from subcmds import sync |
24 | 24 | ||
25 | 25 | ||
26 | @pytest.mark.parametrize('use_superproject, cli_args, result', [ | 26 | @pytest.mark.parametrize( |
27 | (True, ['--current-branch'], True), | 27 | "use_superproject, cli_args, result", |
28 | (True, ['--no-current-branch'], True), | 28 | [ |
29 | (True, [], True), | 29 | (True, ["--current-branch"], True), |
30 | (False, ['--current-branch'], True), | 30 | (True, ["--no-current-branch"], True), |
31 | (False, ['--no-current-branch'], False), | 31 | (True, [], True), |
32 | (False, [], None), | 32 | (False, ["--current-branch"], True), |
33 | ]) | 33 | (False, ["--no-current-branch"], False), |
34 | (False, [], None), | ||
35 | ], | ||
36 | ) | ||
34 | def test_get_current_branch_only(use_superproject, cli_args, result): | 37 | def test_get_current_branch_only(use_superproject, cli_args, result): |
35 | """Test Sync._GetCurrentBranchOnly logic. | 38 | """Test Sync._GetCurrentBranchOnly logic. |
36 | 39 | ||
37 | Sync._GetCurrentBranchOnly should return True if a superproject is requested, | 40 | Sync._GetCurrentBranchOnly should return True if a superproject is |
38 | and otherwise the value of the current_branch_only option. | 41 | requested, and otherwise the value of the current_branch_only option. |
39 | """ | 42 | """ |
40 | cmd = sync.Sync() | 43 | cmd = sync.Sync() |
41 | opts, _ = cmd.OptionParser.parse_args(cli_args) | 44 | opts, _ = cmd.OptionParser.parse_args(cli_args) |
42 | 45 | ||
43 | with mock.patch('git_superproject.UseSuperproject', | 46 | with mock.patch( |
44 | return_value=use_superproject): | 47 | "git_superproject.UseSuperproject", return_value=use_superproject |
45 | assert cmd._GetCurrentBranchOnly(opts, cmd.manifest) == result | 48 | ): |
49 | assert cmd._GetCurrentBranchOnly(opts, cmd.manifest) == result | ||
46 | 50 | ||
47 | 51 | ||
48 | # Used to patch os.cpu_count() for reliable results. | 52 | # Used to patch os.cpu_count() for reliable results. |
49 | OS_CPU_COUNT = 24 | 53 | OS_CPU_COUNT = 24 |
50 | 54 | ||
51 | @pytest.mark.parametrize('argv, jobs_manifest, jobs, jobs_net, jobs_check', [ | 55 | |
52 | # No user or manifest settings. | 56 | @pytest.mark.parametrize( |
53 | ([], None, OS_CPU_COUNT, 1, command.DEFAULT_LOCAL_JOBS), | 57 | "argv, jobs_manifest, jobs, jobs_net, jobs_check", |
54 | # No user settings, so manifest settings control. | 58 | [ |
55 | ([], 3, 3, 3, 3), | 59 | # No user or manifest settings. |
56 | # User settings, but no manifest. | 60 | ([], None, OS_CPU_COUNT, 1, command.DEFAULT_LOCAL_JOBS), |
57 | (['--jobs=4'], None, 4, 4, 4), | 61 | # No user settings, so manifest settings control. |
58 | (['--jobs=4', '--jobs-network=5'], None, 4, 5, 4), | 62 | ([], 3, 3, 3, 3), |
59 | (['--jobs=4', '--jobs-checkout=6'], None, 4, 4, 6), | 63 | # User settings, but no manifest. |
60 | (['--jobs=4', '--jobs-network=5', '--jobs-checkout=6'], None, 4, 5, 6), | 64 | (["--jobs=4"], None, 4, 4, 4), |
61 | (['--jobs-network=5'], None, OS_CPU_COUNT, 5, command.DEFAULT_LOCAL_JOBS), | 65 | (["--jobs=4", "--jobs-network=5"], None, 4, 5, 4), |
62 | (['--jobs-checkout=6'], None, OS_CPU_COUNT, 1, 6), | 66 | (["--jobs=4", "--jobs-checkout=6"], None, 4, 4, 6), |
63 | (['--jobs-network=5', '--jobs-checkout=6'], None, OS_CPU_COUNT, 5, 6), | 67 | (["--jobs=4", "--jobs-network=5", "--jobs-checkout=6"], None, 4, 5, 6), |
64 | # User settings with manifest settings. | 68 | ( |
65 | (['--jobs=4'], 3, 4, 4, 4), | 69 | ["--jobs-network=5"], |
66 | (['--jobs=4', '--jobs-network=5'], 3, 4, 5, 4), | 70 | None, |
67 | (['--jobs=4', '--jobs-checkout=6'], 3, 4, 4, 6), | 71 | OS_CPU_COUNT, |
68 | (['--jobs=4', '--jobs-network=5', '--jobs-checkout=6'], 3, 4, 5, 6), | 72 | 5, |
69 | (['--jobs-network=5'], 3, 3, 5, 3), | 73 | command.DEFAULT_LOCAL_JOBS, |
70 | (['--jobs-checkout=6'], 3, 3, 3, 6), | 74 | ), |
71 | (['--jobs-network=5', '--jobs-checkout=6'], 3, 3, 5, 6), | 75 | (["--jobs-checkout=6"], None, OS_CPU_COUNT, 1, 6), |
72 | # Settings that exceed rlimits get capped. | 76 | (["--jobs-network=5", "--jobs-checkout=6"], None, OS_CPU_COUNT, 5, 6), |
73 | (['--jobs=1000000'], None, 83, 83, 83), | 77 | # User settings with manifest settings. |
74 | ([], 1000000, 83, 83, 83), | 78 | (["--jobs=4"], 3, 4, 4, 4), |
75 | ]) | 79 | (["--jobs=4", "--jobs-network=5"], 3, 4, 5, 4), |
80 | (["--jobs=4", "--jobs-checkout=6"], 3, 4, 4, 6), | ||
81 | (["--jobs=4", "--jobs-network=5", "--jobs-checkout=6"], 3, 4, 5, 6), | ||
82 | (["--jobs-network=5"], 3, 3, 5, 3), | ||
83 | (["--jobs-checkout=6"], 3, 3, 3, 6), | ||
84 | (["--jobs-network=5", "--jobs-checkout=6"], 3, 3, 5, 6), | ||
85 | # Settings that exceed rlimits get capped. | ||
86 | (["--jobs=1000000"], None, 83, 83, 83), | ||
87 | ([], 1000000, 83, 83, 83), | ||
88 | ], | ||
89 | ) | ||
76 | def test_cli_jobs(argv, jobs_manifest, jobs, jobs_net, jobs_check): | 90 | def test_cli_jobs(argv, jobs_manifest, jobs, jobs_net, jobs_check): |
77 | """Tests --jobs option behavior.""" | 91 | """Tests --jobs option behavior.""" |
78 | mp = mock.MagicMock() | 92 | mp = mock.MagicMock() |
79 | mp.manifest.default.sync_j = jobs_manifest | 93 | mp.manifest.default.sync_j = jobs_manifest |
80 | 94 | ||
81 | cmd = sync.Sync() | 95 | cmd = sync.Sync() |
82 | opts, args = cmd.OptionParser.parse_args(argv) | 96 | opts, args = cmd.OptionParser.parse_args(argv) |
83 | cmd.ValidateOptions(opts, args) | 97 | cmd.ValidateOptions(opts, args) |
84 | 98 | ||
85 | with mock.patch.object(sync, '_rlimit_nofile', return_value=(256, 256)): | 99 | with mock.patch.object(sync, "_rlimit_nofile", return_value=(256, 256)): |
86 | with mock.patch.object(os, 'cpu_count', return_value=OS_CPU_COUNT): | 100 | with mock.patch.object(os, "cpu_count", return_value=OS_CPU_COUNT): |
87 | cmd._ValidateOptionsWithManifest(opts, mp) | 101 | cmd._ValidateOptionsWithManifest(opts, mp) |
88 | assert opts.jobs == jobs | 102 | assert opts.jobs == jobs |
89 | assert opts.jobs_network == jobs_net | 103 | assert opts.jobs_network == jobs_net |
90 | assert opts.jobs_checkout == jobs_check | 104 | assert opts.jobs_checkout == jobs_check |
91 | 105 | ||
92 | 106 | ||
93 | class GetPreciousObjectsState(unittest.TestCase): | 107 | class GetPreciousObjectsState(unittest.TestCase): |
94 | """Tests for _GetPreciousObjectsState.""" | 108 | """Tests for _GetPreciousObjectsState.""" |
95 | 109 | ||
96 | def setUp(self): | 110 | def setUp(self): |
97 | """Common setup.""" | 111 | """Common setup.""" |
98 | self.cmd = sync.Sync() | 112 | self.cmd = sync.Sync() |
99 | self.project = p = mock.MagicMock(use_git_worktrees=False, | 113 | self.project = p = mock.MagicMock( |
100 | UseAlternates=False) | 114 | use_git_worktrees=False, UseAlternates=False |
101 | p.manifest.GetProjectsWithName.return_value = [p] | 115 | ) |
102 | 116 | p.manifest.GetProjectsWithName.return_value = [p] | |
103 | self.opt = mock.Mock(spec_set=['this_manifest_only']) | 117 | |
104 | self.opt.this_manifest_only = False | 118 | self.opt = mock.Mock(spec_set=["this_manifest_only"]) |
105 | 119 | self.opt.this_manifest_only = False | |
106 | def test_worktrees(self): | 120 | |
107 | """False for worktrees.""" | 121 | def test_worktrees(self): |
108 | self.project.use_git_worktrees = True | 122 | """False for worktrees.""" |
109 | self.assertFalse(self.cmd._GetPreciousObjectsState(self.project, self.opt)) | 123 | self.project.use_git_worktrees = True |
110 | 124 | self.assertFalse( | |
111 | def test_not_shared(self): | 125 | self.cmd._GetPreciousObjectsState(self.project, self.opt) |
112 | """Singleton project.""" | 126 | ) |
113 | self.assertFalse(self.cmd._GetPreciousObjectsState(self.project, self.opt)) | 127 | |
114 | 128 | def test_not_shared(self): | |
115 | def test_shared(self): | 129 | """Singleton project.""" |
116 | """Shared project.""" | 130 | self.assertFalse( |
117 | self.project.manifest.GetProjectsWithName.return_value = [ | 131 | self.cmd._GetPreciousObjectsState(self.project, self.opt) |
118 | self.project, self.project | 132 | ) |
119 | ] | 133 | |
120 | self.assertTrue(self.cmd._GetPreciousObjectsState(self.project, self.opt)) | 134 | def test_shared(self): |
121 | 135 | """Shared project.""" | |
122 | def test_shared_with_alternates(self): | 136 | self.project.manifest.GetProjectsWithName.return_value = [ |
123 | """Shared project, with alternates.""" | 137 | self.project, |
124 | self.project.manifest.GetProjectsWithName.return_value = [ | 138 | self.project, |
125 | self.project, self.project | 139 | ] |
126 | ] | 140 | self.assertTrue( |
127 | self.project.UseAlternates = True | 141 | self.cmd._GetPreciousObjectsState(self.project, self.opt) |
128 | self.assertFalse(self.cmd._GetPreciousObjectsState(self.project, self.opt)) | 142 | ) |
129 | 143 | ||
130 | def test_not_found(self): | 144 | def test_shared_with_alternates(self): |
131 | """Project not found in manifest.""" | 145 | """Shared project, with alternates.""" |
132 | self.project.manifest.GetProjectsWithName.return_value = [] | 146 | self.project.manifest.GetProjectsWithName.return_value = [ |
133 | self.assertFalse(self.cmd._GetPreciousObjectsState(self.project, self.opt)) | 147 | self.project, |
148 | self.project, | ||
149 | ] | ||
150 | self.project.UseAlternates = True | ||
151 | self.assertFalse( | ||
152 | self.cmd._GetPreciousObjectsState(self.project, self.opt) | ||
153 | ) | ||
154 | |||
155 | def test_not_found(self): | ||
156 | """Project not found in manifest.""" | ||
157 | self.project.manifest.GetProjectsWithName.return_value = [] | ||
158 | self.assertFalse( | ||
159 | self.cmd._GetPreciousObjectsState(self.project, self.opt) | ||
160 | ) | ||