33import tempfile
44import time
55import logging
6- from unittest .mock import patch , MagicMock
6+ from unittest .mock import patch
77from struct_module .utils import FileItem , validate_configuration , create_structure
88
9+
910# Mock the environment variables for OpenAI
1011@pytest .fixture (autouse = True )
1112def mock_env_vars (monkeypatch ):
1213 monkeypatch .setenv ("OPENAI_API_KEY" , "test-api-key" )
1314 monkeypatch .setenv ("OPENAI_MODEL" , "gpt-3.5-turbo" )
1415
16+
1517# Test for FileItem.fetch_content
16- @patch ('struct_module.requests.get' )
18+ @patch ('struct_module.file_item. requests.get' )
1719def test_fetch_remote_content (mock_get ):
1820 mock_get .return_value .status_code = 200
1921 mock_get .return_value .text = "Mocked content"
@@ -24,6 +26,7 @@ def test_fetch_remote_content(mock_get):
2426 assert file_item .content == "Mocked content"
2527 mock_get .assert_called_once_with ("https://example.com/mock" )
2628
29+
2730# Test for FileItem.apply_template_variables
2831def test_apply_template_variables ():
2932 file_item = FileItem ({"name" : "README.md" , "content" : "Hello, ${name}!" })
@@ -33,6 +36,7 @@ def test_apply_template_variables():
3336
3437 assert file_item .content == "Hello, World!"
3538
39+
3640# Test for validate_configuration
3741def test_validate_configuration ():
3842 valid_structure = [
@@ -55,6 +59,7 @@ def test_validate_configuration():
5559 with pytest .raises (ValueError ):
5660 validate_configuration (invalid_structure )
5761
62+
5863# Test for FileItem.create with different strategies
5964def test_create_file ():
6065 structure = [
@@ -87,6 +92,7 @@ def test_create_file():
8792 assert f .read () == "#!/bin/bash\n echo 'Hello, World!'"
8893 assert oct (os .stat (script_path ).st_mode )[- 3 :] == '777'
8994
95+
9096# Test for dry run
9197def test_dry_run (caplog ):
9298 structure = [
@@ -102,10 +108,12 @@ def test_dry_run(caplog):
102108 create_structure (tmpdirname , structure , dry_run = True )
103109
104110 assert not os .path .exists (os .path .join (tmpdirname , "README.md" ))
105- assert any ("[DRY RUN] Would create file:" in message for message in caplog .messages )
111+ assert any ("[DRY RUN] Would create file:" in message
112+ for message in caplog .messages )
113+
106114
107115# Mocking requests.get for testing fetch_remote_content within create_structure
108- @patch ('struct_module.requests.get' )
116+ @patch ('struct_module.file_item. requests.get' )
109117def test_create_structure_with_remote_content (mock_get ):
110118 mock_get .return_value .status_code = 200
111119 mock_get .return_value .text = "Remote content"
@@ -126,6 +134,7 @@ def test_create_structure_with_remote_content(mock_get):
126134 with open (license_path , 'r' ) as f :
127135 assert f .read () == "Remote content"
128136
137+
129138# Test for backup strategy
130139def test_backup_strategy ():
131140 structure = [
@@ -144,12 +153,16 @@ def test_backup_strategy():
144153 backup_path = os .path .join (tmpdirname , "backup" )
145154 os .makedirs (backup_path )
146155
147- create_structure (tmpdirname , structure , backup_path = backup_path , file_strategy = 'backup' )
156+ create_structure (tmpdirname ,
157+ structure ,
158+ backup_path = backup_path ,
159+ file_strategy = 'backup' )
148160
149161 assert os .path .exists (os .path .join (backup_path , "README.md" ))
150162 with open (readme_path , 'r' ) as f :
151163 assert f .read () == "This is a README file."
152164
165+
153166# Test for skip strategy
154167def test_skip_strategy ():
155168 structure = [
@@ -170,6 +183,7 @@ def test_skip_strategy():
170183 with open (readme_path , 'r' ) as f :
171184 assert f .read () == "Existing content"
172185
186+
173187# Test for append strategy
174188def test_append_strategy ():
175189 structure = [
@@ -190,6 +204,7 @@ def test_append_strategy():
190204 with open (readme_path , 'r' ) as f :
191205 assert f .read () == "Existing content. Appended content."
192206
207+
193208# Test for rename strategy
194209def test_rename_strategy ():
195210 structure = [
@@ -212,6 +227,7 @@ def test_rename_strategy():
212227 with open (readme_path , 'r' ) as f :
213228 assert f .read () == "This is a new README file."
214229
230+
215231# Test for directory creation
216232def test_create_structure_creates_directory ():
217233 structure = [
0 commit comments