|
2 | 2 | from rpmlint.checks.SourceCheck import SourceCheck |
3 | 3 | from rpmlint.filter import Filter |
4 | 4 |
|
5 | | -from Testing import CONFIG, get_tested_package |
| 5 | +from Testing import CONFIG, get_tested_mock_package, get_tested_package |
6 | 6 |
|
7 | 7 |
|
8 | 8 | @pytest.fixture(scope='function', autouse=True) |
@@ -39,3 +39,91 @@ def test_compression_and_multispec(tmp_path, package, sourcescheck): |
39 | 39 |
|
40 | 40 | assert 'multiple-specfiles' in out |
41 | 41 | assert 'package contains multiple spec files' in out |
| 42 | + |
| 43 | + |
| 44 | +# ── Tests for _check_file_ext (compressed_fileext_magic matching) ── |
| 45 | +# |
| 46 | +# Test data: test/files/magic/ contains small compressed files, each holding |
| 47 | +# a single byte 'x'. Created with standard CLI tools: |
| 48 | +# |
| 49 | +# echo -n x | gzip -n > byte.gz # -n omits filename/timestamp |
| 50 | +# echo -n x | bzip2 > byte.bz2 |
| 51 | +# echo -n x | xz > byte.xz |
| 52 | +# echo -n x | zstd -q > byte.zst |
| 53 | +# echo -n x > 0 # zip needs a file on disk |
| 54 | +# touch -t 200001010000.00 0 # fixed timestamp for reproducibility |
| 55 | +# TZ=UTC zip -0 -j byte.zip 0 # -0 store, -j junk paths |
| 56 | +# rm 0 |
| 57 | +# |
| 58 | +# In Python (bz2 is byte-identical to CLI; others differ in internal |
| 59 | +# flags but are functionally equivalent): |
| 60 | +# gzip.compress(b'x', mtime=0) |
| 61 | +# bz2.compress(b'x') |
| 62 | +# lzma.compress(b'x', format=lzma.FORMAT_XZ) |
| 63 | +# zstandard.ZstdCompressor().compress(b'x') |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize('ext, content_file', [ |
| 67 | + ('gz', 'files/magic/byte.gz'), |
| 68 | + ('tgz', 'files/magic/byte.gz'), # tgz is gzip too |
| 69 | + ('bz2', 'files/magic/byte.bz2'), |
| 70 | + ('xz', 'files/magic/byte.xz'), |
| 71 | + ('zst', 'files/magic/byte.zst'), |
| 72 | + ('zstd', 'files/magic/byte.zst'), # .zstd is the same format |
| 73 | + ('zip', 'files/magic/byte.zip'), |
| 74 | +]) |
| 75 | +def test_file_ext_consistent(sourcescheck, ext, content_file): |
| 76 | + """Extension matches actual compression format → no warning.""" |
| 77 | + output, test = sourcescheck |
| 78 | + pkg = get_tested_mock_package( |
| 79 | + files={f'archive.{ext}': {'content-path': content_file}}, |
| 80 | + ) |
| 81 | + test.check_source(pkg) |
| 82 | + out = output.print_results(output.results) |
| 83 | + assert 'inconsistent-file-extension' not in out |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize('ext, content_file', [ |
| 87 | + ('gz', 'files/magic/byte.bz2'), |
| 88 | + ('bz2', 'files/magic/byte.gz'), |
| 89 | + ('xz', 'files/magic/byte.zst'), |
| 90 | + ('zst', 'files/magic/byte.gz'), |
| 91 | + ('zstd', 'files/magic/byte.xz'), |
| 92 | + ('zip', 'files/magic/byte.gz'), |
| 93 | +]) |
| 94 | +def test_file_ext_inconsistent(sourcescheck, ext, content_file): |
| 95 | + """Extension does NOT match actual compression format → warning.""" |
| 96 | + output, test = sourcescheck |
| 97 | + pkg = get_tested_mock_package( |
| 98 | + files={f'archive.{ext}': {'content-path': content_file}}, |
| 99 | + ) |
| 100 | + test.check_source(pkg) |
| 101 | + out = output.print_results(output.results) |
| 102 | + assert 'inconsistent-file-extension' in out |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.parametrize('fname', [ |
| 106 | + 'archive.gz', |
| 107 | + 'archive.tar', |
| 108 | + 'archive.txt', |
| 109 | +]) |
| 110 | +def test_file_ext_no_magic(sourcescheck, fname): |
| 111 | + """File with no magic string (empty content) → no crash, no warning.""" |
| 112 | + output, test = sourcescheck |
| 113 | + pkg = get_tested_mock_package( |
| 114 | + files={fname: {'metadata': {'magic': None}}}, |
| 115 | + ) |
| 116 | + test.check_source(pkg) |
| 117 | + out = output.print_results(output.results) |
| 118 | + assert 'inconsistent-file-extension' not in out |
| 119 | + |
| 120 | + |
| 121 | +def test_file_ext_unknown_extension(sourcescheck): |
| 122 | + """Extension not in compressed_fileext_magic → no warning.""" |
| 123 | + output, test = sourcescheck |
| 124 | + pkg = get_tested_mock_package( |
| 125 | + files={'archive.lz4': {'content-path': 'files/magic/byte.gz'}}, |
| 126 | + ) |
| 127 | + test.check_source(pkg) |
| 128 | + out = output.print_results(output.results) |
| 129 | + assert 'inconsistent-file-extension' not in out |
0 commit comments