|
1 | 1 | import os
|
| 2 | +import hashlib |
2 | 3 | from pathlib import Path
|
| 4 | +import random |
3 | 5 | import platform
|
4 | 6 |
|
5 | 7 | import pytest
|
@@ -143,26 +145,54 @@ def test_hash_value_dir(tmpdir):
|
143 | 145 | with open(file_2, "w") as f:
|
144 | 146 | f.write("hi")
|
145 | 147 |
|
146 |
| - assert hash_value(tmpdir, tp=Directory) == hash_value([file_1, file_2], tp=File) |
147 |
| - assert hash_value(tmpdir, tp=Directory) == helpers_file.hash_dir(tmpdir) |
| 148 | + test_sha = hashlib.sha256() |
| 149 | + for fx in [file_1, file_2]: |
| 150 | + test_sha.update(helpers_file.hash_file(fx).encode()) |
| 151 | + |
| 152 | + bad_sha = hashlib.sha256() |
| 153 | + for fx in [file_2, file_1]: |
| 154 | + bad_sha.update(helpers_file.hash_file(fx).encode()) |
| 155 | + |
| 156 | + orig_hash = helpers_file.hash_dir(tmpdir) |
| 157 | + |
| 158 | + assert orig_hash == test_sha.hexdigest() |
| 159 | + assert orig_hash != bad_sha.hexdigest() |
| 160 | + assert orig_hash == hash_value(tmpdir, tp=Directory) |
148 | 161 |
|
149 | 162 |
|
150 | 163 | def test_hash_value_nested(tmpdir):
|
| 164 | + hidden = tmpdir.mkdir(".hidden") |
151 | 165 | nested = tmpdir.mkdir("nested")
|
152 | 166 | file_1 = tmpdir.join("file_1.txt")
|
153 |
| - file_2 = nested.join("file_2.txt") |
154 |
| - file_3 = nested.join("file_3.txt") |
155 |
| - with open(file_1, "w") as f: |
156 |
| - f.write("hello") |
157 |
| - with open(file_2, "w") as f: |
158 |
| - f.write("hi") |
159 |
| - with open(file_3, "w") as f: |
160 |
| - f.write("hola") |
| 167 | + file_2 = hidden.join("file_2.txt") |
| 168 | + file_3 = nested.join(".file_3.txt") |
| 169 | + file_4 = nested.join("file_4.txt") |
| 170 | + |
| 171 | + test_sha = hashlib.sha256() |
| 172 | + for fx in [file_1, file_2, file_3, file_4]: |
| 173 | + with open(fx, "w") as f: |
| 174 | + f.write(str(random.randint(0, 1000))) |
| 175 | + test_sha.update(helpers_file.hash_file(fx).encode()) |
| 176 | + |
| 177 | + orig_hash = helpers_file.hash_dir(tmpdir) |
161 | 178 |
|
162 |
| - assert hash_value(tmpdir, tp=Directory) == hash_value( |
163 |
| - [file_1, [file_2, file_3]], tp=File |
| 179 | + assert orig_hash == test_sha.hexdigest() |
| 180 | + assert orig_hash == hash_value(tmpdir, tp=Directory) |
| 181 | + |
| 182 | + nohidden_hash = helpers_file.hash_dir( |
| 183 | + tmpdir, ignore_hidden_dirs=True, ignore_hidden_files=True |
164 | 184 | )
|
165 |
| - assert hash_value(tmpdir, tp=Directory) == helpers_file.hash_dir(tmpdir) |
| 185 | + nohiddendirs_hash = helpers_file.hash_dir(tmpdir, ignore_hidden_dirs=True) |
| 186 | + nohiddenfiles_hash = helpers_file.hash_dir(tmpdir, ignore_hidden_files=True) |
| 187 | + |
| 188 | + assert orig_hash != nohidden_hash |
| 189 | + assert orig_hash != nohiddendirs_hash |
| 190 | + assert orig_hash != nohiddenfiles_hash |
| 191 | + |
| 192 | + file_3.remove() |
| 193 | + assert helpers_file.hash_dir(tmpdir) == nohiddenfiles_hash |
| 194 | + hidden.remove() |
| 195 | + assert helpers_file.hash_dir(tmpdir) == nohidden_hash |
166 | 196 |
|
167 | 197 |
|
168 | 198 | def test_get_available_cpus():
|
|
0 commit comments