|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 5 | +# See https://github.com/nexB/container-inspector for support or download. |
| 6 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 7 | +# |
| 8 | + |
| 9 | +import os |
| 10 | + |
| 11 | +from commoncode import fileutils |
| 12 | +from commoncode import testcase |
| 13 | + |
| 14 | +from container_inspector import utils |
| 15 | + |
| 16 | + |
| 17 | +def check_files(target_dir, expected): |
| 18 | + """ |
| 19 | + Walk test_dir. |
| 20 | + Check that all dirs are readable. |
| 21 | + Check that all files are: |
| 22 | + * non-special, |
| 23 | + * readable, |
| 24 | + * have a posix path that ends with one of the expected tuple paths. |
| 25 | + """ |
| 26 | + result = [] |
| 27 | + |
| 28 | + test_dir_path = fileutils.as_posixpath(target_dir) |
| 29 | + for top, _, files in os.walk(target_dir): |
| 30 | + for f in files: |
| 31 | + location = os.path.join(top, f) |
| 32 | + path = fileutils.as_posixpath(location) |
| 33 | + path = path.replace(test_dir_path, '').strip('/') |
| 34 | + result.append(path) |
| 35 | + |
| 36 | + expected_content = sorted(expected) |
| 37 | + result = sorted(result) |
| 38 | + |
| 39 | + assert result == expected_content |
| 40 | + |
| 41 | + |
| 42 | +class TestUtils(testcase.FileBasedTesting): |
| 43 | + test_data_dir = os.path.join(os.path.dirname(__file__), 'data') |
| 44 | + |
| 45 | + def test_extract_tree_with_colon_in_filenames(self): |
| 46 | + expected = ( |
| 47 | + 'colon/libc6:amd64.list', |
| 48 | + ) |
| 49 | + test_dir = self.get_test_loc('tar/colon.tar.xz') |
| 50 | + temp_dir = self.get_temp_dir() |
| 51 | + errors = utils.extract_tar(location=test_dir, target_dir=temp_dir) |
| 52 | + check_files(temp_dir, expected) |
| 53 | + assert not errors |
| 54 | + |
| 55 | + def test_extract_tar_relative(self): |
| 56 | + expected = () |
| 57 | + test_dir = self.get_test_loc('tar/tar_relative.tar') |
| 58 | + temp_dir = self.get_temp_dir() |
| 59 | + errors = utils.extract_tar(location=test_dir, target_dir=temp_dir) |
| 60 | + check_files(temp_dir, expected) |
| 61 | + assert errors |
| 62 | + for error in errors: |
| 63 | + assert 'skipping unsupported' in error |
| 64 | + assert 'with relative path' in error |
| 65 | + |
| 66 | + def test_extract_tar_absolute(self): |
| 67 | + expected = ( |
| 68 | + 'tmp/subdir/a.txt', |
| 69 | + 'tmp/subdir/b.txt', |
| 70 | + ) |
| 71 | + test_dir = self.get_test_loc('tar/absolute_path.tar') |
| 72 | + temp_dir = self.get_temp_dir() |
| 73 | + errors = utils.extract_tar(location=test_dir, target_dir=temp_dir) |
| 74 | + check_files(temp_dir, expected) |
| 75 | + assert not errors |
0 commit comments