1- import tempfile
21import unittest
3- from unittest .mock import mock_open , patch
2+ from unittest .mock import patch
43
54from tfblocks import main
65
@@ -42,7 +41,7 @@ def test_module_match(self):
4241 "module.my_module[0].aws_s3_bucket.test" , ["module.my_module" ], []
4342 )
4443 )
45-
44+
4645 def test_resource_type_name_match (self ):
4746 """Test matching resources by type and name across different module paths"""
4847 # Resource in a module should match the same resource type and name from a file
@@ -51,14 +50,14 @@ def test_resource_type_name_match(self):
5150 "module.my_module.aws_s3_bucket.test" , [], ["aws_s3_bucket.test" ]
5251 )
5352 )
54-
53+
5554 # Different resource name should not match
5655 self .assertFalse (
5756 main .is_resource_match (
5857 "module.my_module.aws_s3_bucket.test" , [], ["aws_s3_bucket.other" ]
5958 )
6059 )
61-
60+
6261 # Different resource type should not match
6362 self .assertFalse (
6463 main .is_resource_match (
@@ -78,18 +77,24 @@ def test_wildcard_matching(self):
7877 self .assertFalse (
7978 main .is_resource_match ("aws_lambda_function.test" , ["aws_s3_bucket.*" ], [])
8079 )
81-
80+
8281 # Match resources using wildcards in module paths
8382 self .assertTrue (
84- main .is_resource_match ("module.my_module.aws_s3_bucket.test" , ["*.aws_s3_bucket.test" ], [])
83+ main .is_resource_match (
84+ "module.my_module.aws_s3_bucket.test" , ["*.aws_s3_bucket.test" ], []
85+ )
8586 )
86-
87+
8788 # Match more complex patterns
8889 self .assertTrue (
89- main .is_resource_match ("module.my_module.aws_s3_bucket.test" , ["module.*.aws_s3_bucket.*" ], [])
90+ main .is_resource_match (
91+ "module.my_module.aws_s3_bucket.test" , ["module.*.aws_s3_bucket.*" ], []
92+ )
9093 )
9194 self .assertFalse (
92- main .is_resource_match ("other.my_module.aws_s3_bucket.test" , ["module.*.aws_s3_bucket.*" ], [])
95+ main .is_resource_match (
96+ "other.my_module.aws_s3_bucket.test" , ["module.*.aws_s3_bucket.*" ], []
97+ )
9398 )
9499
95100 def test_intersection_filter (self ):
@@ -107,7 +112,7 @@ def test_intersection_filter(self):
107112
108113
109114class TestFileProcessing (unittest .TestCase ):
110- def test_extract_resource_addresses_from_content (self ):
115+ def test_extract_addresses_from_content (self ):
111116 """Test extracting resource addresses from content"""
112117 terraform_content = """
113118 resource "aws_s3_bucket" "bucket" {
@@ -123,28 +128,12 @@ def test_extract_resource_addresses_from_content(self):
123128 }
124129 """
125130
126- addresses = main .extract_resource_addresses_from_content (terraform_content )
131+ addresses = main .extract_addresses_from_content (terraform_content )
127132
128133 self .assertEqual (len (addresses ), 3 )
129134 self .assertIn ("aws_s3_bucket.bucket" , addresses )
130135 self .assertIn ("aws_dynamodb_table.table" , addresses )
131136 self .assertIn ("module.vpc" , addresses )
132-
133- def test_file_exists (self ):
134- """Test file existence check"""
135- # Test with tempfile to avoid dependencies on filesystem
136- with tempfile .NamedTemporaryFile () as temp_file :
137- self .assertTrue (main .file_exists (temp_file .name ))
138-
139- # This path should not exist
140- self .assertFalse (main .file_exists ("/path/that/does/not/exist/file.tf" ))
141-
142- def test_extract_resource_addresses_nonexistent_file (self ):
143- """Test behavior with nonexistent file"""
144- with patch ("tfblocks.main.file_exists" , return_value = False ), \
145- patch ("sys.exit" ) as mock_exit :
146- main .extract_resource_addresses_from_file ("nonexistent.tf" )
147- mock_exit .assert_called_once_with (1 )
148137
149138 def test_filter_resources_basic (self ):
150139 """Test basic resource filtering without files"""
@@ -201,7 +190,7 @@ def test_filter_resources_basic(self):
201190 resources = main .filter_resources (test_state , ["module.test" ])
202191 self .assertEqual (len (resources ), 1 )
203192 self .assertEqual (resources [0 ]["address" ], "module.test.aws_s3_bucket.nested" )
204-
193+
205194 def test_filter_resources_with_file_filters (self ):
206195 """Test resource filtering with file filters"""
207196 # Create a test state
@@ -221,13 +210,13 @@ def test_filter_resources_with_file_filters(self):
221210 }
222211
223212 # Mock the file handling part without mocking implementation details
224- with patch ("tfblocks.main.extract_resource_addresses_from_file " ) as mock_extract :
213+ with patch ("tfblocks.main.extract_addresses_from_file " ) as mock_extract :
225214 # Case 1: File contains matching resource
226215 mock_extract .return_value = ["aws_s3_bucket.test" ]
227216 resources = main .filter_resources (test_state , [], ["matching_file.tf" ])
228217 self .assertEqual (len (resources ), 1 )
229218 self .assertEqual (resources [0 ]["address" ], "aws_s3_bucket.test" )
230-
219+
231220 # Case 2: File contains non-matching resource
232221 mock_extract .return_value = ["aws_lambda_function.not_in_state" ]
233222 resources = main .filter_resources (test_state , [], ["non_matching_file.tf" ])
@@ -250,6 +239,29 @@ def test_generate_import_block(self):
250239 self .assertIn ("to = aws_s3_bucket.test" , block )
251240 self .assertIn ("id =" , block )
252241
242+ def test_generate_import_block_for_unsupported_provider (self ):
243+ """Test generating an import block for an unsupported provider resource"""
244+ resource = {
245+ "address" : "google_storage_bucket.test" ,
246+ "type" : "google_storage_bucket" ,
247+ "values" : {"name" : "test-bucket" },
248+ }
249+
250+ schema_classes = {}
251+
252+ # By default, we should generate blocks for all providers
253+ block = main .generate_import_block (resource , schema_classes )
254+ self .assertIn ("to = google_storage_bucket.test" , block )
255+ self .assertIn ("TODO" , block )
256+ self .assertIn ("google" , block )
257+ self .assertIn ("storage_bucket" , block )
258+
259+ # When supported_providers_only=True, we should not generate a block
260+ block = main .generate_import_block (
261+ resource , schema_classes , supported_providers_only = True
262+ )
263+ self .assertIsNone (block )
264+
253265 def test_generate_removed_block (self ):
254266 """Test generating a removed block for a resource"""
255267 # Test with destroy=False (default)
@@ -303,6 +315,36 @@ def test_generate_blocks_for_command(self):
303315 # Should have 2 blocks, not 3, due to deduplication
304316 self .assertEqual (len (removed_blocks ), 2 )
305317
318+ def test_generate_blocks_with_mixed_providers (self ):
319+ """Test that supported_providers_only flag filters non-AWS resources"""
320+ resources = [
321+ {
322+ "address" : "aws_s3_bucket.test" ,
323+ "type" : "aws_s3_bucket" ,
324+ "values" : {"bucket" : "test-bucket" },
325+ },
326+ {
327+ "address" : "google_storage_bucket.test" ,
328+ "type" : "google_storage_bucket" ,
329+ "values" : {"name" : "test-bucket" },
330+ },
331+ {
332+ "address" : "azurerm_storage_account.test" ,
333+ "type" : "azurerm_storage_account" ,
334+ "values" : {"name" : "teststorage" },
335+ },
336+ ]
337+
338+ # Test with all providers (default behavior)
339+ import_blocks = main .generate_blocks_for_command (resources , "import" )
340+ self .assertEqual (len (import_blocks ), 3 ) # All resources should be included
341+
342+ # Test with supported_providers_only=True
343+ import_blocks = main .generate_blocks_for_command (
344+ resources , "import" , supported_providers_only = True
345+ )
346+ self .assertEqual (len (import_blocks ), 1 ) # Only AWS resource should be included
347+
306348
307349if __name__ == "__main__" :
308350 unittest .main ()
0 commit comments