|
| 1 | +from os import DirEntry |
| 2 | +from unittest.mock import MagicMock, call, mock_open, patch |
| 3 | +import pytest |
| 4 | +import clean_dotenv |
| 5 | + |
| 6 | + |
| 7 | +class DirEntry: |
| 8 | + def __init__(self, path, is_file=True): |
| 9 | + self.path = path |
| 10 | + self.name = path |
| 11 | + self._is_file = is_file |
| 12 | + |
| 13 | + def path(self): # pragma: no cover |
| 14 | + return self.path |
| 15 | + |
| 16 | + def is_file(self): |
| 17 | + return self._is_file |
| 18 | + |
| 19 | + |
| 20 | +@patch("os.scandir") |
| 21 | +def test_find_dotenv_files(mock_scandir): |
| 22 | + # Mock os.scandir() for files |
| 23 | + mock_scandir.return_value = [ |
| 24 | + DirEntry(filename) |
| 25 | + for filename in ["test.py", "abba", "env", "test.env", ".env"] |
| 26 | + ] |
| 27 | + assert list(clean_dotenv._find_dotenv_files(None)) == ["test.env", ".env"] |
| 28 | + |
| 29 | + # Mock os.scandir() for directories |
| 30 | + mock_scandir.return_value = [ |
| 31 | + DirEntry(filename, is_file=False) |
| 32 | + for filename in ["test.py", "abba", "env", "test.env", ".env", "env"] |
| 33 | + ] |
| 34 | + assert list(clean_dotenv._find_dotenv_files(None)) == [] |
| 35 | + |
| 36 | + |
| 37 | +@patch("dotenv.main.DotEnv") |
| 38 | +def test_clean_env_function(mock_dotenv): |
| 39 | + dotenv_dict = {"KEY1": "secret_value1", "KEY2": "secret_value2"} |
| 40 | + mock_dotenv.return_value.dict.return_value.items.return_value = dotenv_dict.items() |
| 41 | + |
| 42 | + with patch("builtins.open", mock_open()) as mock_file: |
| 43 | + clean_dotenv._clean_env("test.env") |
| 44 | + mock_file.assert_called_with("test.env.example", "w") |
| 45 | + expected_calls = [call("KEY1="), call("\n"), call("KEY2="), call("\n")] |
| 46 | + mock_file.return_value.write.assert_has_calls(expected_calls) |
| 47 | + |
| 48 | + |
| 49 | +def test_find_dotenv_files_function(): |
| 50 | + with patch("os.scandir") as mock_scandir: |
| 51 | + mock_scandir.return_value = [DirEntry("test.env")] |
| 52 | + |
| 53 | + result = list(clean_dotenv._find_dotenv_files("path_to_root")) |
| 54 | + |
| 55 | + assert result == ["test.env"] |
| 56 | + |
| 57 | + |
| 58 | +@patch("argparse.ArgumentParser.parse_args") |
| 59 | +@patch("clean_dotenv._main") |
| 60 | +def test_main(mock_main, mock_parse_args): |
| 61 | + mock_parse_args.return_value = MagicMock(root_path="test_rpath") |
| 62 | + |
| 63 | + clean_dotenv.main() |
| 64 | + |
| 65 | + mock_main.assert_called_once_with("test_rpath") |
| 66 | + |
| 67 | + |
| 68 | +def test__main(): |
| 69 | + # Mock _find_dotenv_files |
| 70 | + mm_find_dotenv = MagicMock(return_value=[".env", "test.env"]) |
| 71 | + clean_dotenv._find_dotenv_files = mm_find_dotenv |
| 72 | + |
| 73 | + # Mock _clean_env |
| 74 | + mm_clean_env = MagicMock() |
| 75 | + clean_dotenv._clean_env = mm_clean_env |
| 76 | + |
| 77 | + # Call main method |
| 78 | + clean_dotenv._main("test_directory") |
| 79 | + |
| 80 | + # Detection should be called once |
| 81 | + mm_find_dotenv.assert_called_once_with("test_directory") |
| 82 | + |
| 83 | + # The creation of new .env file should be called twice, last with "test.env" |
| 84 | + assert mm_clean_env.call_count == 2 |
| 85 | + mm_clean_env.assert_called_with("test.env") |
0 commit comments