|
| 1 | +# Copyright (C) British Crown (Met Office) & Contributors. |
| 2 | +# This file is part of Rose, a framework for meteorological suites. |
| 3 | +# |
| 4 | +# Rose is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# Rose is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with Rose. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +# ----------------------------------------------------------------------------- |
| 17 | + |
| 18 | +import hashlib |
| 19 | +from pathlib import Path |
| 20 | +import pytest |
| 21 | + |
| 22 | +from metomi.rose.checksum import get_checksum |
| 23 | + |
| 24 | +HELLO_WORLD = hashlib.md5(b'Hello World').hexdigest() |
| 25 | +HELLO_JUPITER = hashlib.md5(b'Hello Jupiter').hexdigest() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope='module') |
| 29 | +def checksums_setup(tmp_path_factory): |
| 30 | + """provide some exemplars for checksum to work on.""" |
| 31 | + tmp_path = tmp_path_factory.getbasetemp() |
| 32 | + (tmp_path / 'foo').write_text('Hello World') |
| 33 | + (tmp_path / 'bar').mkdir() |
| 34 | + (tmp_path / 'bar/baz').write_text('Hello Jupiter') |
| 35 | + (tmp_path / 'goodlink').symlink_to('foo') |
| 36 | + (tmp_path / 'badlink').symlink_to('bad') |
| 37 | + yield tmp_path |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture(scope='module') |
| 41 | +def checksums_dir(checksums_setup): |
| 42 | + yield ( |
| 43 | + {a: (b, c) for a, b, c in get_checksum(str(checksums_setup))}, |
| 44 | + checksums_setup |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def test_checksum_single_file(checksums_setup): |
| 49 | + res = get_checksum(str(checksums_setup / 'foo'))[0][1] |
| 50 | + assert res == HELLO_WORLD |
| 51 | + |
| 52 | + |
| 53 | +def test_checksum_custom_checksum_function(checksums_setup): |
| 54 | + res = get_checksum( |
| 55 | + str(checksums_setup / 'foo'), |
| 56 | + # Last 3 letters of the reversed filename: |
| 57 | + checksum_func=lambda x, _: x[-1:-4:-1] |
| 58 | + )[0][1] |
| 59 | + assert res == 'oof' |
| 60 | + |
| 61 | + |
| 62 | +def test_get_checksum_for_all_files(checksums_dir): |
| 63 | + assert len(checksums_dir[0]) == 7 |
| 64 | + |
| 65 | + |
| 66 | +def test_get_checksum_for_goodlink(checksums_dir): |
| 67 | + assert checksums_dir[0]['goodlink'][0] == HELLO_WORLD |
| 68 | + |
| 69 | + |
| 70 | +def test_get_checksum_for_badlink(checksums_dir): |
| 71 | + assert checksums_dir[0]['badlink'][0] == str(checksums_dir[1] / 'bad') |
| 72 | + |
| 73 | + |
| 74 | +def test_get_checksum_for_subdir_file(checksums_dir): |
| 75 | + assert checksums_dir[0]['bar/baz'][0] == HELLO_JUPITER |
| 76 | + |
| 77 | + |
| 78 | +def test_get_checksum_for_non_path(): |
| 79 | + unlikely = '/var/tmp/ariuaibvnoijunhoiujoiuj' |
| 80 | + assert not Path(unlikely).exists() |
| 81 | + with pytest.raises(FileNotFoundError, match=unlikely): |
| 82 | + get_checksum(unlikely) |
0 commit comments