From da547b2bbeb14bf46745a362ec9861f9f0653307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Mart=C3=ADn?= Date: Tue, 2 Jul 2024 15:16:12 +0200 Subject: [PATCH] fix(test-csv): fix test using tempfile (#4356) --- test_file.txt | 0 tests/lib/outputs/csv/csv_test.py | 38 +++++++++++++++++-------------- 2 files changed, 21 insertions(+), 17 deletions(-) delete mode 100644 test_file.txt diff --git a/test_file.txt b/test_file.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/lib/outputs/csv/csv_test.py b/tests/lib/outputs/csv/csv_test.py index a6ea017215..035dff9dae 100644 --- a/tests/lib/outputs/csv/csv_test.py +++ b/tests/lib/outputs/csv/csv_test.py @@ -1,3 +1,4 @@ +import tempfile from datetime import datetime from io import StringIO, TextIOWrapper from typing import List @@ -160,29 +161,32 @@ class TestCSV: mock_output_class.batch_write_data_to_file = MagicMock() findings = [MagicMock(spec=Finding)] - file_path = "test_file.txt" - # Instantiate the mock class - output_instance = mock_output_class( - findings, create_file_descriptor=True, file_path=file_path - ) + # Create a temporary file + with tempfile.NamedTemporaryFile() as file: + file_path = file.name - # Check that transform was called once - output_instance.transform.assert_called_once_with(findings) + # Instantiate the mock class + output_instance = mock_output_class( + findings, create_file_descriptor=True, file_path=file_path + ) - # Check that create_file_descriptor was called and the file descriptor was created - assert output_instance.file_descriptor is not None + # Check that transform was called once + output_instance.transform.assert_called_once_with(findings) - # Check the type - assert isinstance(output_instance.file_descriptor, TextIOWrapper) + # Check that create_file_descriptor was called and the file descriptor was created + assert output_instance.file_descriptor is not None - # Assuming we need to call batch_write_data_to_file for this test - output_instance.batch_write_data_to_file(output_instance.file_descriptor) + # Check the type + assert isinstance(output_instance.file_descriptor, TextIOWrapper) - # Check that batch_write_data_to_file was called once - output_instance.batch_write_data_to_file.assert_called_once_with( - output_instance.file_descriptor - ) + # Assuming we need to call batch_write_data_to_file for this test + output_instance.batch_write_data_to_file(output_instance.file_descriptor) + + # Check that batch_write_data_to_file was called once + output_instance.batch_write_data_to_file.assert_called_once_with( + output_instance.file_descriptor + ) def test_write_csv_with_dict(self): headers = ["provider", "account", "check_id"]