Skip to content

Commit 5df4292

Browse files
feat: pylint errors
1 parent e2e2866 commit 5df4292

File tree

5 files changed

+109
-19
lines changed

5 files changed

+109
-19
lines changed

tests/test_api.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Unit tests for Bitbucket API logic in manage_bitbucket_env.py."""
12
import unittest
23
from unittest.mock import patch, Mock
34
import sys
@@ -6,7 +7,16 @@
67
from manage_bitbucket_env import get_environment_uuid, get_variables
78

89
class TestAPI(unittest.TestCase):
10+
"""Test API-related functions."""
11+
logger = None
12+
auth = None
13+
workspace = None
14+
repo_slug = None
15+
deployment_name = None
16+
env_uuid = None
17+
918
def setUp(self):
19+
"""Set up test fixtures."""
1020
self.logger = Mock()
1121
self.auth = Mock()
1222
self.workspace = 'ws'
@@ -16,31 +26,49 @@ def setUp(self):
1626

1727
@patch('manage_bitbucket_env.requests.get')
1828
def test_get_environment_uuid_found(self, mock_get):
29+
"""Test successful environment UUID retrieval."""
1930
mock_resp = Mock()
20-
mock_resp.json.return_value = {"values": [{"name": "env", "uuid": "uuid-123"}]}
31+
mock_resp.json.return_value = {
32+
"values": [{"name": "env", "uuid": "uuid-123"}]
33+
}
2134
mock_resp.raise_for_status.return_value = None
2235
mock_get.return_value = mock_resp
23-
uuid = get_environment_uuid(self.workspace, self.repo_slug, self.deployment_name, self.auth, self.logger)
36+
uuid = get_environment_uuid(
37+
self.workspace, self.repo_slug, self.deployment_name,
38+
self.auth, self.logger
39+
)
2440
self.assertEqual(uuid, 'uuid-123')
2541
mock_get.assert_called_once()
2642

2743
@patch('manage_bitbucket_env.requests.get')
2844
def test_get_environment_uuid_not_found(self, mock_get):
45+
"""Test environment UUID retrieval when environment not found."""
2946
mock_resp = Mock()
30-
mock_resp.json.return_value = {"values": [{"name": "other", "uuid": "uuid-999"}]}
47+
mock_resp.json.return_value = {
48+
"values": [{"name": "other", "uuid": "uuid-999"}]
49+
}
3150
mock_resp.raise_for_status.return_value = None
3251
mock_get.return_value = mock_resp
3352
with self.assertRaises(ValueError):
34-
get_environment_uuid(self.workspace, self.repo_slug, self.deployment_name, self.auth, self.logger)
53+
get_environment_uuid(
54+
self.workspace, self.repo_slug, self.deployment_name,
55+
self.auth, self.logger
56+
)
3557

3658
@patch('manage_bitbucket_env.requests.get')
3759
def test_get_variables(self, mock_get):
60+
"""Test successful variable retrieval."""
3861
mock_resp = Mock()
39-
mock_resp.json.return_value = {"values": [{"key": "A", "value": "1", "secured": False}]}
62+
mock_resp.json.return_value = {
63+
"values": [{"key": "A", "value": "1", "secured": False}]
64+
}
4065
mock_resp.raise_for_status.return_value = None
4166
mock_get.return_value = mock_resp
42-
vars = get_variables(self.workspace, self.repo_slug, self.env_uuid, self.auth, self.logger)
43-
self.assertEqual(vars[0]["key"], "A")
67+
result_vars = get_variables(
68+
self.workspace, self.repo_slug, self.env_uuid,
69+
self.auth, self.logger
70+
)
71+
self.assertEqual(result_vars[0]["key"], "A")
4472

4573
if __name__ == '__main__':
4674
unittest.main()

tests/test_arg_parser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Unit tests for argument parsing in manage_bitbucket_env.py."""
12
import unittest
23
from unittest.mock import patch
34
import sys
@@ -6,7 +7,10 @@
67
from manage_bitbucket_env import arg_parser
78

89
class TestArgParser(unittest.TestCase):
10+
"""Test argument parsing functionality."""
11+
912
def test_export_args(self):
13+
"""Test parsing arguments for exporting variables."""
1014
test_args = [
1115
'prog', '-w', 'ws', '-r', 'repo', '-d', 'env', '-o', 'out.json'
1216
]
@@ -18,6 +22,7 @@ def test_export_args(self):
1822
self.assertEqual(args.output, 'out.json')
1923

2024
def test_import_args(self):
25+
"""Test parsing arguments for importing variables."""
2126
test_args = [
2227
'prog', '-w', 'ws', '-r', 'repo', '-d', 'env', '-i', 'in.json'
2328
]
@@ -26,6 +31,7 @@ def test_import_args(self):
2631
self.assertEqual(args.import_file, 'in.json')
2732

2833
def test_mutually_exclusive(self):
34+
"""Test that mutually exclusive arguments raise an error."""
2935
test_args = [
3036
'prog', '-w', 'ws', '-r', 'repo', '-d', 'env'
3137
]

tests/test_export_import.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1+
"""Unit tests for export/import logic in manage_bitbucket_env.py."""
2+
# pylint: disable=duplicate-code
13
import unittest
24
from unittest.mock import patch, Mock
35
import sys
46
import os
57
import tempfile
68
import json
79
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
8-
from manage_bitbucket_env import export_variables, export_all_variables, export_secure_keys, import_variables
10+
from manage_bitbucket_env import (
11+
export_variables, export_all_variables, export_secure_keys, import_variables
12+
)
913

1014
class TestExportImport(unittest.TestCase):
15+
"""Test export and import functionality."""
16+
logger = None
17+
auth = None
18+
workspace = None
19+
repo_slug = None
20+
deployment_name = None
21+
env_uuid = None
22+
1123
def setUp(self):
24+
"""Set up test fixtures."""
1225
self.logger = Mock()
1326
self.auth = Mock()
1427
self.workspace = 'ws'
@@ -19,6 +32,7 @@ def setUp(self):
1932
@patch('manage_bitbucket_env.get_environment_uuid')
2033
@patch('manage_bitbucket_env.get_variables')
2134
def test_export_variables(self, mock_get_vars, mock_get_uuid):
35+
"""Test successful export of non-secured variables."""
2236
mock_get_uuid.return_value = self.env_uuid
2337
mock_get_vars.return_value = [
2438
{"key": "A", "value": "1", "secured": False},
@@ -27,8 +41,11 @@ def test_export_variables(self, mock_get_vars, mock_get_uuid):
2741
with tempfile.NamedTemporaryFile(delete=False) as tf:
2842
fname = tf.name
2943
try:
30-
export_variables(self.workspace, self.repo_slug, self.deployment_name, fname, self.auth, self.logger)
31-
with open(fname) as f:
44+
export_variables(
45+
self.workspace, self.repo_slug, self.deployment_name,
46+
fname, self.auth, self.logger
47+
)
48+
with open(fname, encoding="utf-8") as f:
3249
data = json.load(f)
3350
self.assertEqual(len(data), 1)
3451
self.assertEqual(data[0]["key"], "A")
@@ -38,6 +55,7 @@ def test_export_variables(self, mock_get_vars, mock_get_uuid):
3855
@patch('manage_bitbucket_env.get_environment_uuid')
3956
@patch('manage_bitbucket_env.get_variables')
4057
def test_export_all_variables(self, mock_get_vars, mock_get_uuid):
58+
"""Test successful export of all variables."""
4159
mock_get_uuid.return_value = self.env_uuid
4260
mock_get_vars.return_value = [
4361
{"key": "A", "value": "1", "secured": False},
@@ -46,8 +64,11 @@ def test_export_all_variables(self, mock_get_vars, mock_get_uuid):
4664
with tempfile.NamedTemporaryFile(delete=False) as tf:
4765
fname = tf.name
4866
try:
49-
export_all_variables(self.workspace, self.repo_slug, self.deployment_name, fname, self.auth, self.logger)
50-
with open(fname) as f:
67+
export_all_variables(
68+
self.workspace, self.repo_slug, self.deployment_name,
69+
fname, self.auth, self.logger
70+
)
71+
with open(fname, encoding="utf-8") as f:
5172
data = json.load(f)
5273
self.assertEqual(len(data), 2)
5374
test_var = next(v for v in data if v["key"] == "A")
@@ -62,6 +83,7 @@ def test_export_all_variables(self, mock_get_vars, mock_get_uuid):
6283
@patch('manage_bitbucket_env.get_environment_uuid')
6384
@patch('manage_bitbucket_env.get_variables')
6485
def test_export_secure_keys(self, mock_get_vars, mock_get_uuid):
86+
"""Test successful export of secure keys."""
6587
mock_get_uuid.return_value = self.env_uuid
6688
mock_get_vars.return_value = [
6789
{"key": "A", "secured": False},
@@ -71,8 +93,11 @@ def test_export_secure_keys(self, mock_get_vars, mock_get_uuid):
7193
with tempfile.NamedTemporaryFile(delete=False) as tf:
7294
fname = tf.name
7395
try:
74-
export_secure_keys(self.workspace, self.repo_slug, self.deployment_name, fname, self.auth, self.logger)
75-
with open(fname) as f:
96+
export_secure_keys(
97+
self.workspace, self.repo_slug, self.deployment_name,
98+
fname, self.auth, self.logger
99+
)
100+
with open(fname, encoding="utf-8") as f:
76101
data = json.load(f)
77102
self.assertIn("B", data)
78103
self.assertIn("C", data)
@@ -84,17 +109,21 @@ def test_export_secure_keys(self, mock_get_vars, mock_get_uuid):
84109
@patch('manage_bitbucket_env.get_variables')
85110
@patch('manage_bitbucket_env.update_vars')
86111
def test_import_variables(self, mock_update, mock_get_vars, mock_get_uuid):
112+
"""Test successful import of variables."""
87113
mock_get_uuid.return_value = self.env_uuid
88114
mock_get_vars.return_value = []
89115
test_vars = [
90116
{"key": "A", "value": "1", "secured": False},
91117
{"key": "B", "value": "2", "secured": True}
92118
]
93-
with tempfile.NamedTemporaryFile(delete=False, mode='w') as tf:
119+
with tempfile.NamedTemporaryFile(delete=False, mode='w', encoding="utf-8") as tf:
94120
json.dump(test_vars, tf)
95121
fname = tf.name
96122
try:
97-
import_variables(self.workspace, self.repo_slug, self.deployment_name, fname, False, self.auth, self.logger)
123+
import_variables(
124+
self.workspace, self.repo_slug, self.deployment_name,
125+
fname, False, self.auth, self.logger
126+
)
98127
self.assertEqual(mock_update.call_count, 1)
99128
call_args = mock_update.call_args[1]
100129
self.assertEqual(call_args['var']["key"], "A")

tests/test_main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Unit tests for main function integration in manage_bitbucket_env.py."""
12
import unittest
23
from unittest.mock import patch, Mock
34
import sys
@@ -6,15 +7,22 @@
67
from manage_bitbucket_env import main
78

89
class TestMainFunction(unittest.TestCase):
10+
"""Test main function integration."""
11+
mock_logger = None
12+
913
def setUp(self):
14+
"""Set up test fixtures."""
1015
self.mock_logger = Mock()
1116

1217
@patch('manage_bitbucket_env.arg_parser')
1318
@patch('manage_bitbucket_env.load_dotenv')
1419
@patch('os.environ.get')
1520
@patch('manage_bitbucket_env.HTTPBasicAuth')
1621
@patch('manage_bitbucket_env.export_variables')
17-
def test_main_export_variables(self, mock_export, mock_auth, mock_env_get, mock_load_dotenv, mock_arg_parser):
22+
# pylint: disable=too-many-arguments, too-many-positional-arguments
23+
def test_main_export_variables(self, mock_export, mock_auth, mock_env_get,
24+
mock_load_dotenv, mock_arg_parser):
25+
"""Test main function with export variables."""
1826
mock_args = Mock()
1927
mock_args.output = 'test_output.json'
2028
mock_args.workspace = 'test-workspace'
@@ -41,6 +49,7 @@ def test_main_export_variables(self, mock_export, mock_auth, mock_env_get, mock_
4149
@patch('manage_bitbucket_env.load_dotenv')
4250
@patch('os.environ.get')
4351
def test_main_missing_credentials(self, mock_env_get, mock_load_dotenv, mock_arg_parser):
52+
"""Test main function with missing credentials."""
4453
mock_args = Mock()
4554
mock_args.output = 'test_output.json'
4655
mock_arg_parser.return_value = mock_args

tests/test_update_vars.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Unit tests for update_vars logic in manage_bitbucket_env.py."""
2+
# pylint: disable=duplicate-code
13
import unittest
24
from unittest.mock import patch, Mock
35
import sys
@@ -6,7 +8,15 @@
68
from manage_bitbucket_env import update_vars
79

810
class TestUpdateVars(unittest.TestCase):
11+
"""Test variable update functionality."""
12+
logger = None
13+
auth = None
14+
workspace = None
15+
repo_slug = None
16+
env_uuid = None
17+
918
def setUp(self):
19+
"""Set up test fixtures."""
1020
self.logger = Mock()
1121
self.auth = Mock()
1222
self.workspace = 'ws'
@@ -15,24 +25,32 @@ def setUp(self):
1525

1626
@patch('manage_bitbucket_env.requests.put')
1727
def test_update_existing(self, mock_put):
28+
"""Test updating an existing variable."""
1829
mock_resp = Mock()
1930
mock_resp.raise_for_status.return_value = None
2031
mock_put.return_value = mock_resp
2132
existing_vars = [{"key": "A", "uuid": "u1", "value": "old", "secured": False}]
2233
new_var = {"key": "A", "value": "new", "secured": False}
23-
update_vars(self.workspace, self.repo_slug, self.env_uuid, existing_vars, new_var, self.auth, self.logger)
34+
update_vars(
35+
self.workspace, self.repo_slug, self.env_uuid,
36+
existing_vars, new_var, self.auth, self.logger
37+
)
2438
mock_put.assert_called_once()
2539
call_args = mock_put.call_args
2640
self.assertIn("u1", call_args[0][0])
2741

2842
@patch('manage_bitbucket_env.requests.post')
2943
def test_update_new(self, mock_post):
44+
"""Test creating a new variable."""
3045
mock_resp = Mock()
3146
mock_resp.raise_for_status.return_value = None
3247
mock_post.return_value = mock_resp
3348
existing_vars = []
3449
new_var = {"key": "B", "value": "val", "secured": False}
35-
update_vars(self.workspace, self.repo_slug, self.env_uuid, existing_vars, new_var, self.auth, self.logger)
50+
update_vars(
51+
self.workspace, self.repo_slug, self.env_uuid,
52+
existing_vars, new_var, self.auth, self.logger
53+
)
3654
mock_post.assert_called_once()
3755

3856
if __name__ == '__main__':

0 commit comments

Comments
 (0)