1+ """Unit tests for export/import logic in manage_bitbucket_env.py."""
2+ # pylint: disable=duplicate-code
13import unittest
24from unittest .mock import patch , Mock
35import sys
46import os
57import tempfile
68import json
79sys .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
1014class 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" )
0 commit comments