Skip to content

Commit 8b45bf7

Browse files
authored
Fix error assertions and cleanup dependencies (#450)
## 📝 Description replaced all try and except blocks with pytest.raises for cleaner and proper assertion since mock is part of python std lib, we can import it directly from unittest ## ✔️ How to Test **What are the steps to reproduce the issue or verify the changes?** **How do I run the relevant unit/integration tests?** ## 📷 Preview **If applicable, include a screenshot or code snippet of this change. Otherwise, please remove this section.**
1 parent fcaaf50 commit 8b45bf7

File tree

7 files changed

+73
-71
lines changed

7 files changed

+73
-71
lines changed

.github/workflows/unit-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
python-version: '3.x'
2121

2222
- name: Install Python wheel
23-
run: pip install wheel boto3 mock
23+
run: pip install wheel boto3
2424

2525
- name: Update cert
2626
run: pip install certifi -U

tests/unit/test_api_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import io
77
import json
88
from types import SimpleNamespace
9+
from unittest.mock import Mock, patch
910

1011
import requests
11-
from mock import Mock, patch
1212

1313
from linodecli import api_request
1414

tests/unit/test_completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Unit tests for linodecli.completion
44
"""
55

6-
from mock import mock_open, patch
6+
from unittest.mock import mock_open, patch
77

88
from linodecli import completion
99

tests/unit/test_configuration.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import io
88
import os
99
import sys
10+
from unittest.mock import call, mock_open, patch
1011

1112
import pytest
1213
import requests_mock
13-
from mock import call, mock_open, patch
1414

1515
from linodecli import configuration
1616

@@ -73,12 +73,12 @@ def test_set_user(self):
7373

7474
f = io.StringIO()
7575

76-
try:
76+
with pytest.raises(SystemExit) as err:
7777
with contextlib.redirect_stdout(f):
7878
conf.set_user("bad_user")
79-
except SystemExit as err:
80-
assert err.code == 1
81-
assert "not configured" in f.getvalue()
79+
80+
assert err.value.code == 1
81+
assert "not configured" in f.getvalue()
8282

8383
conf.set_user("cli-dev2")
8484
assert conf.username == "cli-dev2"
@@ -91,12 +91,12 @@ def test_remove_user(self):
9191

9292
f = io.StringIO()
9393

94-
try:
94+
with pytest.raises(SystemExit) as err:
9595
with contextlib.redirect_stdout(f):
9696
conf.remove_user("cli-dev")
97-
except SystemExit as err:
98-
assert err.code == 1
99-
assert "default user!" in f.getvalue()
97+
98+
assert "default user!" in f.getvalue()
99+
assert err.value.code == 1
100100

101101
with patch("linodecli.configuration.open", mock_open()):
102102
conf.remove_user("cli-dev2")
@@ -110,12 +110,12 @@ def test_print_users(self):
110110

111111
f = io.StringIO()
112112

113-
try:
113+
with pytest.raises(SystemExit) as err:
114114
with contextlib.redirect_stdout(f):
115115
conf.print_users()
116-
except SystemExit as err:
117-
assert err.code == 0
118-
assert "* cli-dev" in f.getvalue()
116+
117+
assert err.value.code == 0
118+
assert "* cli-dev" in f.getvalue()
119119

120120
def test_set_default_user(self):
121121
"""
@@ -124,12 +124,12 @@ def test_set_default_user(self):
124124
conf = self._build_test_config()
125125

126126
f = io.StringIO()
127-
try:
127+
with pytest.raises(SystemExit) as err:
128128
with contextlib.redirect_stdout(f):
129129
conf.set_default_user("bad_user")
130-
except SystemExit as err:
131-
assert err.code == 1
132-
assert "not configured" in f.getvalue()
130+
131+
assert err.value.code == 1
132+
assert "not configured" in f.getvalue()
133133

134134
with patch("linodecli.configuration.open", mock_open()):
135135
conf.set_default_user("cli-dev2")

tests/unit/test_plugin_image_upload.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import importlib
22
from unittest.mock import patch
33

4+
import pytest
45
from pytest import CaptureFixture
56

67
from linodecli.plugins import PluginContext
@@ -10,26 +11,26 @@
1011

1112

1213
def test_print_help(capsys: CaptureFixture):
13-
try:
14+
with pytest.raises(SystemExit) as err:
1415
plugin.call(["--help"], None)
15-
except SystemExit as err:
16-
assert err.code == 0
1716

1817
captured_text = capsys.readouterr().out
18+
19+
assert err.value.code == 0
1920
assert "The image file to upload" in captured_text
2021
assert "The region to upload the image to" in captured_text
2122

2223

2324
def test_no_file(mock_cli, capsys: CaptureFixture):
24-
try:
25+
with pytest.raises(SystemExit) as err:
2526
plugin.call(
2627
["--label", "cool", "blah.txt"],
2728
PluginContext("REALTOKEN", mock_cli),
2829
)
29-
except SystemExit as err:
30-
assert err.code == 2
3130

3231
captured_text = capsys.readouterr().out
32+
33+
assert err.value.code == 2
3334
assert "No file at blah.txt" in captured_text
3435

3536

@@ -39,12 +40,12 @@ def test_file_too_large(mock_cli, capsys: CaptureFixture):
3940
args = ["--label", "cool", "blah.txt"]
4041
ctx = PluginContext("REALTOKEN", mock_cli)
4142

42-
try:
43+
with pytest.raises(SystemExit) as err:
4344
plugin.call(args, ctx)
44-
except SystemExit as err:
45-
assert err.code == 2
4645

4746
captured_text = capsys.readouterr().out
47+
48+
assert err.value.code == 2
4849
assert "File blah.txt is too large" in captured_text
4950

5051

@@ -57,12 +58,12 @@ def test_unauthorized(mock_cli, capsys: CaptureFixture):
5758

5859
ctx = PluginContext("REALTOKEN", mock_cli)
5960

60-
try:
61+
with pytest.raises(SystemExit) as err:
6162
plugin.call(args, ctx)
62-
except SystemExit as err:
63-
assert err.code == 3
6463

6564
captured_text = capsys.readouterr().out
65+
66+
assert err.value.code == 3
6667
assert "Your token was not authorized to use this endpoint" in captured_text
6768

6869

@@ -75,12 +76,12 @@ def test_non_beta(mock_cli, capsys: CaptureFixture):
7576

7677
ctx = PluginContext("REALTOKEN", mock_cli)
7778

78-
try:
79+
with pytest.raises(SystemExit) as err:
7980
plugin.call(args, ctx)
80-
except SystemExit as err:
81-
assert err.code == 4
8281

8382
captured_text = capsys.readouterr().out
83+
84+
assert err.value.code == 4
8485
assert (
8586
"It looks like you are not in the Machine Images Beta" in captured_text
8687
)
@@ -95,12 +96,12 @@ def test_non_beta(mock_cli, capsys: CaptureFixture):
9596

9697
ctx = PluginContext("REALTOKEN", mock_cli)
9798

98-
try:
99+
with pytest.raises(SystemExit) as err:
99100
plugin.call(args, ctx)
100-
except SystemExit as err:
101-
assert err.code == 4
102101

103102
captured_text = capsys.readouterr().out
103+
104+
assert err.value.code == 4
104105
assert (
105106
"It looks like you are not in the Machine Images Beta" in captured_text
106107
)
@@ -114,12 +115,12 @@ def test_failed_upload(mock_cli, capsys: CaptureFixture):
114115

115116
ctx = PluginContext("REALTOKEN", mock_cli)
116117

117-
try:
118+
with pytest.raises(SystemExit) as err:
118119
plugin.call(args, ctx)
119-
except SystemExit as err:
120-
assert err.code == 3
121120

122121
captured_text = capsys.readouterr().out
122+
123+
assert err.value.code == 3
123124
assert (
124125
"Upload failed with status 500; response was it borked :("
125126
in captured_text

tests/unit/test_plugin_kubeconfig.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656
def test_print_help():
5757
stdout_buf = io.StringIO()
5858

59-
try:
59+
with pytest.raises(SystemExit) as err:
6060
with contextlib.redirect_stdout(stdout_buf):
6161
plugin.call(["--help"], None)
62-
except SystemExit as err:
63-
assert err.code == 0
62+
63+
assert err.value.code == 0
6464

6565
assert "Path to kubeconfig file." in stdout_buf.getvalue()
6666
assert "Label for desired cluster." in stdout_buf.getvalue()
@@ -70,14 +70,14 @@ def test_print_help():
7070
def test_no_label_no_id(mock_cli):
7171
stderr_buf = io.StringIO()
7272

73-
try:
73+
with pytest.raises(SystemExit) as err:
7474
with contextlib.redirect_stderr(stderr_buf):
7575
plugin.call(
7676
[],
7777
PluginContext("REALTOKEN", mock_cli),
7878
)
79-
except SystemExit as err:
80-
assert err.code == 1
79+
80+
assert err.value.code == 1
8181

8282
assert "Either --label or --id must be used." in stderr_buf.getvalue()
8383

@@ -87,14 +87,14 @@ def test_nonexisting_label(mock_cli):
8787
stderr_buf = io.StringIO()
8888
mock_cli.call_operation = mock_call_operation
8989

90-
try:
90+
with pytest.raises(SystemExit) as err:
9191
with contextlib.redirect_stderr(stderr_buf):
9292
plugin.call(
9393
["--label", "empty_data"],
9494
PluginContext("REALTOKEN", mock_cli),
9595
)
96-
except SystemExit as err:
97-
assert err.code == 1
96+
97+
assert err.value.code == 1
9898

9999
assert (
100100
"Cluster with label empty_data does not exist." in stderr_buf.getvalue()
@@ -105,14 +105,14 @@ def test_nonexisting_label(mock_cli):
105105
def test_nonexisting_id(mock_cli):
106106
stderr_buf = io.StringIO()
107107

108-
try:
108+
with pytest.raises(SystemExit) as err:
109109
with contextlib.redirect_stderr(stderr_buf):
110110
plugin.call(
111111
["--id", "12345"],
112112
PluginContext("REALTOKEN", mock_cli),
113113
)
114-
except SystemExit as err:
115-
assert err.code == 1
114+
115+
assert err.value.code == 1
116116

117117
assert "Error retrieving kubeconfig:" in stderr_buf.getvalue()
118118

@@ -124,7 +124,7 @@ def test_improper_file(mock_cli, fake_empty_file):
124124

125125
file_path = fake_empty_file
126126

127-
try:
127+
with pytest.raises(SystemExit) as err:
128128
with contextlib.redirect_stderr(stderr_buf):
129129
plugin.call(
130130
[
@@ -135,8 +135,8 @@ def test_improper_file(mock_cli, fake_empty_file):
135135
],
136136
PluginContext("REALTOKEN", mock_cli),
137137
)
138-
except SystemExit as err:
139-
assert err.code == 1
138+
139+
assert err.value.code == 1
140140

141141
assert "Could not load file at" in stderr_buf.getvalue()
142142

0 commit comments

Comments
 (0)