Skip to content

Commit 8c1f8c3

Browse files
committed
Fix 6 failing CLI error handling tests
The tests expected ValueError to propagate from exec_command(), but cliff framework catches exceptions and returns exit codes instead. Updated tests to check: - Return code equals 1 (error) - Error message appears in stderr Fixed tests: - test_account_create_w_parameters_from_bad_file_format_fail - test_group_create_w_parameters_from_bad_file_format_fail - test_change_create_bad_file_format_fail - test_project_create_w_parameters_from_bad_file_format_fail - test_project_configuration_set_from_bad_file_format_fail - test_plugin_install_w_wrong_identifier_fail All 202 tests now pass.
1 parent f1f184b commit 8c1f8c3

5 files changed

Lines changed: 40 additions & 19 deletions

File tree

gerritclient/tests/unit/cli/test_account.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,21 @@ def test_account_create_w_parameters_from_file(self):
115115
self.m_client.create.assert_called_once_with(username, data=test_data)
116116

117117
@mock.patch("gerritclient.common.utils.file_exists", mock.Mock(return_value=True))
118-
def test_account_create_w_parameters_from_bad_file_format_fail(self):
118+
@mock.patch("sys.stderr")
119+
def test_account_create_w_parameters_from_bad_file_format_fail(self, mocked_stderr):
119120
username = "fake-user"
120121
test_data = {}
121122
expected_path = "/tmp/fakes/bad_file.format"
122123
args = f"account create {username} --file {expected_path}"
123124

124125
m_open = mock.mock_open(read_data=json.dumps(test_data))
125126
with mock.patch("gerritclient.common.utils.open", m_open, create=True):
126-
self.assertRaisesRegex(
127-
ValueError, "Unsupported data format", self.exec_command, args
127+
result = self.exec_command(args)
128+
self.assertEqual(1, result)
129+
stderr_output = "".join(
130+
call[0][0] for call in mocked_stderr.write.call_args_list
128131
)
132+
self.assertIn("Unsupported data format", stderr_output)
129133

130134
@mock.patch("sys.stderr")
131135
def test_account_create_fail(self, mocked_stderr):

gerritclient/tests/unit/cli/test_change.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,20 @@ def test_change_create(self):
141141
self.m_client.create.assert_called_once_with(test_data)
142142

143143
@mock.patch("gerritclient.common.utils.file_exists", mock.Mock(return_value=True))
144-
def test_change_create_bad_file_format_fail(self):
144+
@mock.patch("sys.stderr")
145+
def test_change_create_bad_file_format_fail(self, mocked_stderr):
145146
test_data = {}
146147
expected_path = "/tmp/fakes/bad_file.format"
147148
args = f"change create {expected_path}"
148149

149150
m_open = mock.mock_open(read_data=json.dumps(test_data))
150151
with mock.patch("gerritclient.common.utils.open", m_open, create=True):
151-
self.assertRaisesRegex(
152-
ValueError, "Unsupported data format", self.exec_command, args
152+
result = self.exec_command(args)
153+
self.assertEqual(1, result)
154+
stderr_output = "".join(
155+
call[0][0] for call in mocked_stderr.write.call_args_list
153156
)
157+
self.assertIn("Unsupported data format", stderr_output)
154158

155159
def test_change_delete(self):
156160
change_id = "I8473b95934b5732ac55d26311a706c9c2bde9940"

gerritclient/tests/unit/cli/test_group.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,21 @@ def test_group_create_w_parameters_from_file(self):
8484
self.m_client.create.assert_called_once_with(group_name, data=test_data)
8585

8686
@mock.patch("gerritclient.common.utils.file_exists", mock.Mock(return_value=True))
87-
def test_group_create_w_parameters_from_bad_file_format_fail(self):
87+
@mock.patch("sys.stderr")
88+
def test_group_create_w_parameters_from_bad_file_format_fail(self, mocked_stderr):
8889
group_name = "Fake-Group"
8990
test_data = {}
9091
expected_path = "/tmp/fakes/bad_file.format"
9192
args = f"group create {group_name} --file {expected_path}"
9293

9394
m_open = mock.mock_open(read_data=json.dumps(test_data))
9495
with mock.patch("gerritclient.common.utils.open", m_open, create=True):
95-
self.assertRaisesRegex(
96-
ValueError, "Unsupported data format", self.exec_command, args
96+
result = self.exec_command(args)
97+
self.assertEqual(1, result)
98+
stderr_output = "".join(
99+
call[0][0] for call in mocked_stderr.write.call_args_list
97100
)
101+
self.assertIn("Unsupported data format", stderr_output)
98102

99103
@mock.patch("sys.stderr")
100104
def test_group_project_fail(self, mocked_stderr):

gerritclient/tests/unit/cli/test_plugin.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ def test_plugin_install_w_wrong_identifier_fail(self, mocked_stderr):
111111
plugin_id = "bad-plugin-identifier"
112112
url = "http://url/path/to/plugin.jar"
113113
args = f"plugin install {plugin_id} --url {url}"
114-
self.assertRaises(ValueError, self.exec_command, args)
115-
self.assertIn(
116-
'Plugin identifier must contain ".jar" prefix',
117-
mocked_stderr.write.call_args_list[0][0][0],
114+
result = self.exec_command(args)
115+
self.assertEqual(1, result)
116+
stderr_output = "".join(
117+
call[0][0] for call in mocked_stderr.write.call_args_list
118118
)
119+
self.assertIn('Plugin identifier must contain ".jar"', stderr_output)

gerritclient/tests/unit/cli/test_project.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,21 @@ def test_project_create_w_parameters_from_file(self):
233233
self.m_client.create.assert_called_once_with(project_id, data=test_data)
234234

235235
@mock.patch("gerritclient.common.utils.file_exists", mock.Mock(return_value=True))
236-
def test_project_create_w_parameters_from_bad_file_format_fail(self):
236+
@mock.patch("sys.stderr")
237+
def test_project_create_w_parameters_from_bad_file_format_fail(self, mocked_stderr):
237238
project_id = "fakes/fake-project"
238239
test_data = {}
239240
expected_path = "/tmp/fakes/bad_file.format"
240241
args = f"project create {project_id} --file {expected_path}"
241242

242243
m_open = mock.mock_open(read_data=json.dumps(test_data))
243244
with mock.patch("gerritclient.common.utils.open", m_open, create=True):
244-
self.assertRaisesRegex(
245-
ValueError, "Unsupported data format", self.exec_command, args
245+
result = self.exec_command(args)
246+
self.assertEqual(1, result)
247+
stderr_output = "".join(
248+
call[0][0] for call in mocked_stderr.write.call_args_list
246249
)
250+
self.assertIn("Unsupported data format", stderr_output)
247251

248252
@mock.patch("sys.stderr")
249253
def test_project_create_fail(self, mocked_stderr):
@@ -651,17 +655,21 @@ def test_project_configuration_set(self):
651655
self.m_client.set_config.assert_called_once_with(project_name, data=test_data)
652656

653657
@mock.patch("gerritclient.common.utils.file_exists", mock.Mock(return_value=True))
654-
def test_project_configuration_set_from_bad_file_format_fail(self):
658+
@mock.patch("sys.stderr")
659+
def test_project_configuration_set_from_bad_file_format_fail(self, mocked_stderr):
655660
project_name = "fakes/fake-project"
656661
test_data = {}
657662
expected_path = "/tmp/fakes/bad_file.format"
658663
args = f"project configuration set {project_name} --file {expected_path}"
659664

660665
m_open = mock.mock_open(read_data=json.dumps(test_data))
661666
with mock.patch("gerritclient.common.utils.open", m_open, create=True):
662-
self.assertRaisesRegex(
663-
ValueError, "Unsupported data format", self.exec_command, args
667+
result = self.exec_command(args)
668+
self.assertEqual(1, result)
669+
stderr_output = "".join(
670+
call[0][0] for call in mocked_stderr.write.call_args_list
664671
)
672+
self.assertIn("Unsupported data format", stderr_output)
665673

666674
@mock.patch("sys.stderr")
667675
def test_project_configuration_set_fail(self, mocked_stderr):

0 commit comments

Comments
 (0)