|
| 1 | +import hashlib |
1 | 2 | from pathlib import Path
|
| 3 | +import random |
2 | 4 |
|
3 | 5 | import pytest
|
4 | 6 | import cloudpickle as cp
|
@@ -135,25 +137,49 @@ def test_hash_value_dir(tmpdir):
|
135 | 137 | with open(file_2, "w") as f:
|
136 | 138 | f.write("hi")
|
137 | 139 |
|
138 |
| - assert sorted(hash_value(tmpdir, tp=Directory)) == sorted( |
139 |
| - hash_value([file_1, file_2], tp=File) |
140 |
| - ) |
141 |
| - assert hash_value(tmpdir, tp=Directory) == helpers_file.hash_dir(tmpdir) |
| 140 | + test_sha = hashlib.sha256() |
| 141 | + for fx in [file_1, file_2]: |
| 142 | + test_sha.update(helpers_file.hash_file(fx).encode()) |
| 143 | + |
| 144 | + bad_sha = hashlib.sha256() |
| 145 | + for fx in [file_2, file_1]: |
| 146 | + bad_sha.update(helpers_file.hash_file(fx).encode()) |
| 147 | + |
| 148 | + orig_hash = helpers_file.hash_dir(tmpdir) |
| 149 | + |
| 150 | + assert orig_hash == test_sha.hexdigest() |
| 151 | + assert orig_hash != bad_sha.hexdigest() |
| 152 | + assert orig_hash == hash_value(tmpdir, tp=Directory) |
142 | 153 |
|
143 | 154 |
|
144 | 155 | def test_hash_value_nested(tmpdir):
|
| 156 | + hidden = tmpdir.mkdir(".hidden") |
145 | 157 | nested = tmpdir.mkdir("nested")
|
146 | 158 | file_1 = tmpdir.join("file_1.txt")
|
147 |
| - file_2 = nested.join("file_2.txt") |
148 |
| - file_3 = nested.join("file_3.txt") |
149 |
| - with open(file_1, "w") as f: |
150 |
| - f.write("hello") |
151 |
| - with open(file_2, "w") as f: |
152 |
| - f.write("hi") |
153 |
| - with open(file_3, "w") as f: |
154 |
| - f.write("hola") |
| 159 | + file_2 = hidden.join("file_2.txt") |
| 160 | + file_3 = nested.join(".file_3.txt") |
| 161 | + file_4 = nested.join("file_4.txt") |
155 | 162 |
|
156 |
| - assert hash_value(tmpdir, tp=Directory) == hash_value( |
157 |
| - [file_1, [file_2, file_3]], tp=File |
158 |
| - ) |
159 |
| - assert hash_value(tmpdir, tp=Directory) == helpers_file.hash_dir(tmpdir) |
| 163 | + test_sha = hashlib.sha256() |
| 164 | + for fx in [file_1, file_2, file_3, file_4]: |
| 165 | + with open(fx, "w") as f: |
| 166 | + f.write(str(random.randint(0, 1000))) |
| 167 | + test_sha.update(helpers_file.hash_file(fx).encode()) |
| 168 | + |
| 169 | + orig_hash = helpers_file.hash_dir(tmpdir) |
| 170 | + |
| 171 | + assert orig_hash == test_sha.hexdigest() |
| 172 | + assert orig_hash == hash_value(tmpdir, tp=Directory) |
| 173 | + |
| 174 | + nohidden_hash = helpers_file.hash_dir(tmpdir, ignore_hidden_dirs=True, ignore_hidden_files=True) |
| 175 | + nohiddendirs_hash = helpers_file.hash_dir(tmpdir, ignore_hidden_dirs=True) |
| 176 | + nohiddenfiles_hash = helpers_file.hash_dir(tmpdir, ignore_hidden_files=True) |
| 177 | + |
| 178 | + assert orig_hash != nohidden_hash |
| 179 | + assert orig_hash != nohiddendirs_hash |
| 180 | + assert orig_hash != nohiddenfiles_hash |
| 181 | + |
| 182 | + file_3.remove() |
| 183 | + assert helpers_file.hash_dir(tmpdir) == nohiddenfiles_hash |
| 184 | + hidden.remove() |
| 185 | + assert helpers_file.hash_dir(tmpdir) == nohidden_hash |
0 commit comments