wlvncc: unstable-2023-01-05 -> unstable-2024-11-24 (#357566)
[NixPkgs.git] / lib / tests / fetchers.nix
blobc36da35673a716576d6d200d2f59f973ecd5cbfe
1 let
2   lib = import ./..;
4   inherit (lib)
5     fakeHash
6     fakeSha256
7     fakeSha512
8     flip
9     functionArgs
10     runTests
11     ;
12   inherit (lib.fetchers) normalizeHash withNormalizedHash;
14   testingThrow = expr: {
15     expr = with builtins; tryEval (seq expr "didn't throw");
16     expected = {
17       success = false;
18       value = false;
19     };
20   };
22   # hashes of empty
23   sri256 = "sha256-d6xi4mKdjkX2JFicDIv5niSzpyI0m/Hnm8GGAIU04kY=";
24   sri512 = "sha512-AXFyVo7jiZ5we10fxZ5E9qfPjSfqkizY2apCzORKFVYZaNhCIVbooY+J4cYST00ztLf0EjivIBPPdtIYFUMfzQ==";
26   unionOfDisjoints = lib.foldl lib.attrsets.unionOfDisjoint { };
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     };
40     "test${n}EmptySha256" = {
41       expr = f { } { sha256 = ""; };
42       expected = {
43         outputHash = fakeSha256;
44         outputHashAlgo = "sha256";
45       };
46     };
48     "test${n}EmptySha512" = {
49       expr = f { hashTypes = [ "sha512" ]; } { sha512 = ""; };
50       expected = {
51         outputHash = fakeSha512;
52         outputHashAlgo = "sha512";
53       };
54     };
56     "test${n}EmptyHash" = {
57       expr = f { } { hash = ""; };
58       expected = {
59         outputHash = fakeHash;
60         outputHashAlgo = null;
61       };
62     };
64     "test${n}Sri256" = {
65       expr = f { } { hash = sri256; };
66       expected = {
67         outputHash = sri256;
68         outputHashAlgo = null;
69       };
70     };
72     "test${n}Sri512" = {
73       expr = f { } { hash = sri512; };
74       expected = {
75         outputHash = sri512;
76         outputHashAlgo = null;
77       };
78     };
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     };
92     "test${n}RejectsSha1ByDefault" = testingThrow (f { } { sha1 = ""; });
93     "test${n}RejectsSha512ByDefault" = testingThrow (f { } { sha512 = ""; });
95     "test${n}ThrowsOnMissing" = testingThrow (f { } { gibi = false; });
96   };
98 runTests (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     };
115     testNormalizeNotRequiredPassthru = {
116       expr = normalizeHash { required = false; } { "ga bu" = "zo meu"; };
117       expected."ga bu" = "zo meu";
118     };
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     };
132     testOptionalArgMetadata = {
133       expr = functionArgs (
134         withNormalizedHash { } (
135           {
136             outputHash ? "",
137             outputHashAlgo ? null,
138           }:
139           { }
140         )
141       );
142       expected.hash = true;
143     };
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     };
162     testRejectsMissingHashArg = testingThrow (withNormalizedHash { } ({ outputHashAlgo }: { }));
163     testRejectsMissingAlgoArg = testingThrow (withNormalizedHash { } ({ outputHash }: { }));
164   }