|
23 | 23 | import unittest.mock |
24 | 24 | import pyfakefs.fake_filesystem_unittest as pyfakefs |
25 | 25 |
|
26 | | -from pysonar_scanner.utils import Arch, Os, get_arch, get_os, remove_trailing_slash, calculate_checksum |
| 26 | +from pysonar_scanner.utils import Arch, Os, get_arch, get_os, remove_trailing_slash, calculate_checksum, extract_tar |
27 | 27 |
|
28 | 28 |
|
29 | 29 | class TestUtils(unittest.TestCase): |
@@ -132,3 +132,31 @@ def test_calculate_checksum(self): |
132 | 132 | calculate_checksum(BytesIO(b"test test")), |
133 | 133 | "03ffdf45276dd38ffac79b0e9c6c14d89d9113ad783d5922580f4c66a3305591", |
134 | 134 | ) |
| 135 | + |
| 136 | + |
| 137 | +class TestExtractTar(unittest.TestCase): |
| 138 | + def setUp(self): |
| 139 | + self.test_path = pathlib.Path("/fake/path/archive.tar.gz") |
| 140 | + self.test_target_dir = pathlib.Path("/fake/target/dir") |
| 141 | + |
| 142 | + @unittest.mock.patch("tarfile.open") |
| 143 | + @unittest.mock.patch("sys.version_info", (3, 12, 0)) |
| 144 | + def test_extract_tar_python_3_12_or_higher(self, mock_open): |
| 145 | + mock_tar = unittest.mock.MagicMock() |
| 146 | + mock_open.return_value.__enter__.return_value = mock_tar |
| 147 | + |
| 148 | + extract_tar(self.test_path, self.test_target_dir) |
| 149 | + |
| 150 | + mock_open.assert_called_once_with(self.test_path, "r:gz") |
| 151 | + mock_tar.extractall.assert_called_once_with(self.test_target_dir, filter="data") |
| 152 | + |
| 153 | + @unittest.mock.patch("tarfile.open") |
| 154 | + @unittest.mock.patch("sys.version_info", (3, 11, 0)) |
| 155 | + def test_extract_tar_python_older_than_3_12(self, mock_open): |
| 156 | + mock_tar = unittest.mock.MagicMock() |
| 157 | + mock_open.return_value.__enter__.return_value = mock_tar |
| 158 | + |
| 159 | + extract_tar(self.test_path, self.test_target_dir) |
| 160 | + |
| 161 | + mock_open.assert_called_once_with(self.test_path, "r:gz") |
| 162 | + mock_tar.extractall.assert_called_once_with(self.test_target_dir) |
0 commit comments