at 25.11-pre 3.8 kB view raw
1let 2 lib = import ./..; 3 4 inherit (lib) 5 fakeHash 6 fakeSha256 7 fakeSha512 8 flip 9 functionArgs 10 runTests 11 ; 12 inherit (lib.fetchers) normalizeHash withNormalizedHash; 13 14 testingThrow = expr: { 15 expr = with builtins; tryEval (seq expr "didn't throw"); 16 expected = { 17 success = false; 18 value = false; 19 }; 20 }; 21 22 # hashes of empty 23 sri256 = "sha256-d6xi4mKdjkX2JFicDIv5niSzpyI0m/Hnm8GGAIU04kY="; 24 sri512 = "sha512-AXFyVo7jiZ5we10fxZ5E9qfPjSfqkizY2apCzORKFVYZaNhCIVbooY+J4cYST00ztLf0EjivIBPPdtIYFUMfzQ=="; 25 26 unionOfDisjoints = lib.foldl lib.attrsets.unionOfDisjoint { }; 27 28 genTests = n: f: { 29 "test${n}AlreadyNormalized" = { 30 expr = f { } { 31 outputHash = ""; 32 outputHashAlgo = "md42"; 33 }; 34 expected = { 35 outputHash = ""; 36 outputHashAlgo = "md42"; 37 }; 38 }; 39 40 "test${n}EmptySha256" = { 41 expr = f { } { sha256 = ""; }; 42 expected = { 43 outputHash = fakeSha256; 44 outputHashAlgo = "sha256"; 45 }; 46 }; 47 48 "test${n}EmptySha512" = { 49 expr = f { hashTypes = [ "sha512" ]; } { sha512 = ""; }; 50 expected = { 51 outputHash = fakeSha512; 52 outputHashAlgo = "sha512"; 53 }; 54 }; 55 56 "test${n}EmptyHash" = { 57 expr = f { } { hash = ""; }; 58 expected = { 59 outputHash = fakeHash; 60 outputHashAlgo = null; 61 }; 62 }; 63 64 "test${n}Sri256" = { 65 expr = f { } { hash = sri256; }; 66 expected = { 67 outputHash = sri256; 68 outputHashAlgo = null; 69 }; 70 }; 71 72 "test${n}Sri512" = { 73 expr = f { } { hash = sri512; }; 74 expected = { 75 outputHash = sri512; 76 outputHashAlgo = null; 77 }; 78 }; 79 80 "test${n}PreservesAttrs" = { 81 expr = f { } { 82 hash = "aaaa"; 83 destination = "Earth"; 84 }; 85 expected = { 86 outputHash = "aaaa"; 87 outputHashAlgo = null; 88 destination = "Earth"; 89 }; 90 }; 91 92 "test${n}RejectsSha1ByDefault" = testingThrow (f { } { sha1 = ""; }); 93 "test${n}RejectsSha512ByDefault" = testingThrow (f { } { sha512 = ""; }); 94 95 "test${n}ThrowsOnMissing" = testingThrow (f { } { gibi = false; }); 96 }; 97in 98runTests (unionOfDisjoints [ 99 (genTests "NormalizeHash" normalizeHash) 100 (genTests "WithNormalized" ( 101 flip withNormalizedHash ({ outputHash, outputHashAlgo, ... }@args: args) 102 )) 103 { 104 testNormalizeNotRequiredEquivalent = { 105 expr = normalizeHash { required = false; } { 106 hash = ""; 107 prof = "shadoko"; 108 }; 109 expected = normalizeHash { } { 110 hash = ""; 111 prof = "shadoko"; 112 }; 113 }; 114 115 testNormalizeNotRequiredPassthru = { 116 expr = normalizeHash { required = false; } { "ga bu" = "zo meu"; }; 117 expected."ga bu" = "zo meu"; 118 }; 119 120 testOptionalArg = { 121 expr = withNormalizedHash { } ( 122 { 123 outputHash ? "", 124 outputHashAlgo ? null, 125 ... 126 }@args: 127 args 128 ) { author = "Jacques Rouxel"; }; 129 expected.author = "Jacques Rouxel"; 130 }; 131 132 testOptionalArgMetadata = { 133 expr = functionArgs ( 134 withNormalizedHash { } ( 135 { 136 outputHash ? "", 137 outputHashAlgo ? null, 138 }: 139 { } 140 ) 141 ); 142 expected.hash = true; 143 }; 144 145 testPreservesArgsMetadata = { 146 expr = functionArgs ( 147 withNormalizedHash { } ( 148 { 149 outputHash, 150 outputHashAlgo, 151 pumping ? true, 152 }: 153 { } 154 ) 155 ); 156 expected = { 157 hash = false; 158 pumping = true; 159 }; 160 }; 161 162 testRejectsMissingHashArg = testingThrow (withNormalizedHash { } ({ outputHashAlgo }: { })); 163 testRejectsMissingAlgoArg = testingThrow (withNormalizedHash { } ({ outputHash }: { })); 164 } 165])