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