|
| 1 | +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) |
| 2 | + |
| 3 | +include(${CPM_PATH}/CPM.cmake) |
| 4 | +include(${CPM_PATH}/testing.cmake) |
| 5 | + |
| 6 | +# Random suffix |
| 7 | +string(RANDOM LENGTH 6 ALPHABET "0123456789abcdef" tmpdir_suffix) |
| 8 | + |
| 9 | +# Seconds since epoch |
| 10 | +string(TIMESTAMP tmpdir_base "%s" UTC) |
| 11 | + |
| 12 | +set(tmp "get_shortest_hash-${tmpdir_base}-${tmpdir_suffix}") |
| 13 | + |
| 14 | +if(IS_DIRECTORY ${tmp}) |
| 15 | + message(FATAL_ERROR "Test directory ${tmp} already exists") |
| 16 | +endif() |
| 17 | + |
| 18 | +file(MAKE_DIRECTORY "${tmp}") |
| 19 | + |
| 20 | +# 1. Sanity check: none of these directories should exist yet |
| 21 | + |
| 22 | +assert_not_exists(${tmp}/cccb.hash) |
| 23 | +assert_not_exists(${tmp}/cccb77ae.hash) |
| 24 | +assert_not_exists(${tmp}/cccb77ae9609.hash) |
| 25 | +assert_not_exists(${tmp}/cccb77ae9608.hash) |
| 26 | +assert_not_exists(${tmp}/cccb77be.hash) |
| 27 | + |
| 28 | +# 2. The directory is empty, so it should get a 4-character hash |
| 29 | +cpm_get_shortest_hash(${tmp} "cccb77ae9609d2768ed80dd42cec54f77b1f1455" hash) |
| 30 | +assert_equal(${hash} "cccb") |
| 31 | +assert_contents_equal(${tmp}/cccb.hash cccb77ae9609d2768ed80dd42cec54f77b1f1455) |
| 32 | + |
| 33 | +# 3. Calling the function with a new hash that differs subtly should result |
| 34 | +# in more characters being used, enough to uniquely identify the hash |
| 35 | + |
| 36 | +cpm_get_shortest_hash(${tmp} "cccb77ae9609d2768ed80dd42cec54f77b1f1456" hash) |
| 37 | +assert_equal(${hash} "cccb77ae") |
| 38 | +assert_contents_equal(${tmp}/cccb77ae.hash cccb77ae9609d2768ed80dd42cec54f77b1f1456) |
| 39 | + |
| 40 | +cpm_get_shortest_hash(${tmp} "cccb77ae9609d2768ed80dd42cec54f77b1f1457" hash) |
| 41 | +assert_equal(${hash} "cccb77ae9609") |
| 42 | +assert_contents_equal(${tmp}/cccb77ae9609.hash cccb77ae9609d2768ed80dd42cec54f77b1f1457) |
| 43 | + |
| 44 | +cpm_get_shortest_hash(${tmp} "cccb77ae9608d2768ed80dd42cec54f77b1f1455" hash) |
| 45 | +assert_equal(${hash} "cccb77ae9608") |
| 46 | +assert_contents_equal(${tmp}/cccb77ae9608.hash cccb77ae9608d2768ed80dd42cec54f77b1f1455) |
| 47 | + |
| 48 | +cpm_get_shortest_hash(${tmp} "cccb77be9609d2768ed80dd42cec54f77b1f1456" hash) |
| 49 | +assert_equal(${hash} "cccb77be") |
| 50 | +assert_contents_equal(${tmp}/cccb77be.hash cccb77be9609d2768ed80dd42cec54f77b1f1456) |
| 51 | + |
| 52 | +# 4. The old file should still exist, and have the same content |
| 53 | +assert_contents_equal(${tmp}/cccb.hash cccb77ae9609d2768ed80dd42cec54f77b1f1455) |
| 54 | +assert_contents_equal(${tmp}/cccb77ae.hash cccb77ae9609d2768ed80dd42cec54f77b1f1456) |
| 55 | +assert_contents_equal(${tmp}/cccb77ae9609.hash cccb77ae9609d2768ed80dd42cec54f77b1f1457) |
| 56 | +assert_contents_equal(${tmp}/cccb77ae9608.hash cccb77ae9608d2768ed80dd42cec54f77b1f1455) |
| 57 | +assert_contents_equal(${tmp}/cccb77be.hash cccb77be9609d2768ed80dd42cec54f77b1f1456) |
| 58 | + |
| 59 | +# 5. Confirm idempotence: calling any of these function should produce the same hash |
| 60 | +# as before (hash lookups work correctly once the .hash files are created) |
| 61 | + |
| 62 | +cpm_get_shortest_hash(${tmp} "cccb77ae9609d2768ed80dd42cec54f77b1f1455" hash) |
| 63 | +assert_equal(${hash} "cccb") |
| 64 | +assert_contents_equal(${tmp}/cccb.hash cccb77ae9609d2768ed80dd42cec54f77b1f1455) |
| 65 | + |
| 66 | +cpm_get_shortest_hash(${tmp} "cccb77ae9609d2768ed80dd42cec54f77b1f1456" hash) |
| 67 | +assert_equal(${hash} "cccb77ae") |
| 68 | +assert_contents_equal(${tmp}/cccb77ae.hash cccb77ae9609d2768ed80dd42cec54f77b1f1456) |
| 69 | + |
| 70 | +cpm_get_shortest_hash(${tmp} "cccb77ae9609d2768ed80dd42cec54f77b1f1457" hash) |
| 71 | +assert_equal(${hash} "cccb77ae9609") |
| 72 | +assert_contents_equal(${tmp}/cccb77ae9609.hash cccb77ae9609d2768ed80dd42cec54f77b1f1457) |
| 73 | + |
| 74 | +cpm_get_shortest_hash(${tmp} "cccb77ae9608d2768ed80dd42cec54f77b1f1455" hash) |
| 75 | +assert_equal(${hash} "cccb77ae9608") |
| 76 | +assert_contents_equal(${tmp}/cccb77ae9608.hash cccb77ae9608d2768ed80dd42cec54f77b1f1455) |
| 77 | + |
| 78 | +cpm_get_shortest_hash(${tmp} "cccb77be9609d2768ed80dd42cec54f77b1f1456" hash) |
| 79 | +assert_equal(${hash} "cccb77be") |
| 80 | +assert_contents_equal(${tmp}/cccb77be.hash cccb77be9609d2768ed80dd42cec54f77b1f1456) |
| 81 | + |
| 82 | +# 6. Cleanup - remove the temporary directory that we created |
| 83 | + |
| 84 | +file(REMOVE_RECURSE ${tmp}) |
0 commit comments