teamspeak5_client: 5.0.0-beta77 -> 6.0.0-beta2; teamspeak refactors (#377748)
[NixPkgs.git] / pkgs / build-support / writers / test.nix
blob47c35ef5448fbc2deb517e2d879df80e43576838
2   haskellPackages,
3   lib,
4   guile-lib,
5   akkuPackages,
6   nodePackages,
7   perlPackages,
8   python3Packages,
9   runCommand,
10   testers,
11   writers,
12   writeText,
15 # If you are reading this, you can test these writers by running: nix-build . -A tests.writers
17 let
18   inherit (lib) getExe recurseIntoAttrs;
20   inherit (writers)
21     makeFSharpWriter
22     writeBash
23     writeBashBin
24     writeBabashka
25     writeBabashkaBin
26     writeDash
27     writeDashBin
28     writeFish
29     writeFishBin
30     writeFSharp
31     writeGuile
32     writeGuileBin
33     writeHaskell
34     writeHaskellBin
35     writeJS
36     writeJSBin
37     writeJSON
38     writeLua
39     writeNim
40     writeNimBin
41     writeNu
42     writePerl
43     writePerlBin
44     writePyPy3
45     writePython3
46     writePython3Bin
47     writeRuby
48     writeRust
49     writeRustBin
50     writeText
51     writeTOML
52     writeYAML
53     ;
55   expectSuccess =
56     test:
57     runCommand "run-${test.name}" { } ''
58       if [[ "$(${test})" != success ]]; then
59         echo 'test ${test.name} failed'
60         exit 1
61       fi
63       touch $out
64     '';
66   expectSuccessBin =
67     test:
68     runCommand "run-${test.name}" { } ''
69       if [[ "$(${getExe test})" != success ]]; then
70         echo 'test ${test.name} failed'
71         exit 1
72       fi
74       touch $out
75     '';
77   expectDataEqual =
78     { file, expected }:
79     let
80       expectedFile = writeText "${file.name}-expected" expected;
81     in
82     testers.testEqualContents {
83       expected = expectedFile;
84       actual = file;
85       assertion = "${file.name} matches";
86     };
88 recurseIntoAttrs {
89   bin = recurseIntoAttrs {
90     bash = expectSuccessBin (
91       writeBashBin "test-writers-bash-bin" ''
92         if [[ "test" == "test" ]]; then echo "success"; fi
93       ''
94     );
96     dash = expectSuccessBin (
97       writeDashBin "test-writers-dash-bin" ''
98         test '~' = '~' && echo 'success'
99       ''
100     );
102     fish = expectSuccessBin (
103       writeFishBin "test-writers-fish-bin" ''
104         if test "test" = "test"
105           echo "success"
106         end
107       ''
108     );
110     babashka = expectSuccessBin (
111       writeBabashkaBin "test-writers-babashka-bin" { } ''
112         (println "success")
113       ''
114     );
116     guile = expectSuccessBin (
117       writeGuileBin "test-writers-guile-bin" { } ''
118         (display "success\n")
119       ''
120     );
122     rust = expectSuccessBin (
123       writeRustBin "test-writers-rust-bin" { } ''
124         fn main(){
125           println!("success")
126         }
127       ''
128     );
130     haskell = expectSuccessBin (
131       writeHaskellBin "test-writers-haskell-bin" { libraries = [ haskellPackages.acme-default ]; } ''
132         import Data.Default
134         int :: Int
135         int = def
137         main :: IO ()
138         main = case int of
139           18871 -> putStrLn $ id "success"
140           _ -> print "fail"
141       ''
142     );
144     nim = expectSuccessBin (
145       writeNimBin "test-writers-nim-bin" { } ''
146         echo "success"
147       ''
148     );
150     js = expectSuccessBin (
151       writeJSBin "test-writers-js-bin" { libraries = [ nodePackages.semver ]; } ''
152         var semver = require('semver');
154         if (semver.valid('1.2.3')) {
155           console.log('success')
156         } else {
157           console.log('fail')
158         }
159       ''
160     );
162     perl = expectSuccessBin (
163       writePerlBin "test-writers-perl-bin" { libraries = [ perlPackages.boolean ]; } ''
164         use boolean;
165         print "success\n" if true;
166       ''
167     );
169     python3 = expectSuccessBin (
170       writePython3Bin "test-writers-python3-bin" { libraries = [ python3Packages.pyyaml ]; } ''
171         import yaml
173         y = yaml.safe_load("""
174           - test: success
175         """)
176         print(y[0]['test'])
177       ''
178     );
180     # Commented out because of this issue: https://github.com/NixOS/nixpkgs/issues/39356
182     #pypy2 = expectSuccessBin (writePyPy2Bin "test-writers-pypy2-bin" { libraries = [ pypy2Packages.enum ]; } ''
183     #  from enum import Enum
184     #
185     #  class Test(Enum):
186     #      a = "success"
187     #
188     #  print Test.a
189     #'');
191     #pypy3 = expectSuccessBin (writePyPy3Bin "test-writers-pypy3-bin" { libraries = [ pypy3Packages.pyyaml ]; } ''
192     #  import yaml
193     #
194     #  y = yaml.safe_load("""
195     #    - test: success
196     #  """)
197     #  print(y[0]['test'])
198     #'');
200     # Could not test this because of external package issues :(
201     #lua = writeLuaBin "test-writers-lua-bin" { libraries = [ pkgs.luaPackages.say ]; } ''
202     #  s = require("say")
203     #  s:set_namespace("en")
205     #  s:set('money', 'I have %s dollars')
206     #  s:set('wow', 'So much money!')
208     #  print(s('money', {1000})) -- I have 1000 dollars
210     #  s:set_namespace("fr") -- switch to french!
211     #  s:set('wow', "Tant d'argent!")
213     #  print(s('wow')) -- Tant d'argent!
214     #  s:set_namespace("en")  -- switch back to english!
215     #  print(s('wow')) -- So much money!
216     #'';
218     #ruby = expectSuccessBin (writeRubyBin "test-writers-ruby-bin" { libraries = [ rubyPackages.rubocop ]; } ''
219     #puts "This should work!"
220     #'');
221   };
223   simple = recurseIntoAttrs {
224     bash = expectSuccess (
225       writeBash "test-writers-bash" ''
226         if [[ "test" == "test" ]]; then echo "success"; fi
227       ''
228     );
230     dash = expectSuccess (
231       writeDash "test-writers-dash" ''
232         test '~' = '~' && echo 'success'
233       ''
234     );
236     fish = expectSuccess (
237       writeFish "test-writers-fish" ''
238         if test "test" = "test"
239           echo "success"
240         end
241       ''
242     );
244     nim = expectSuccess (
245       writeNim "test-writers-nim" { } ''
246         echo "success"
247       ''
248     );
250     nu = expectSuccess (
251       writeNu "test-writers-nushell" ''
252         echo "success"
253       ''
254     );
256     babashka = expectSuccess (
257       writeBabashka "test-writers-babashka" { } ''
258         (println "success")
259       ''
260     );
262     guile = expectSuccess (
263       writeGuile "test-writers-guile"
264         {
265           libraries = [ guile-lib ];
266           srfi = [ 1 ];
267         }
268         ''
269           (use-modules (unit-test))
270           (assert-true (= (second '(1 2 3))
271                        2))
272           (display "success\n")
273         ''
274     );
276     guileR6RS = expectSuccess (
277       writeGuile "test-writers-guile-r6rs"
278         {
279           r6rs = true;
280           libraries = with akkuPackages; [ r6rs-slice ];
281         }
282         ''
283           (import (rnrs base (6))
284                   (rnrs io simple (6))
285                   (slice))
286           (assert (equal? (slice '(1 2 3) 0 2)
287                           '(1 2)))
288           (display "success\n")
289         ''
290     );
292     guileR7RS = expectSuccess (
293       writeGuile "test-writers-guile-r7rs"
294         {
295           r7rs = true;
296         }
297         ''
298           (import (scheme write)
299                   (srfi 1))
300           (unless (= (second '(1 2 3))
301                      2)
302                   (error "The value should be 2."))
303           (display "success\n")
304         ''
305     );
307     haskell = expectSuccess (
308       writeHaskell "test-writers-haskell" { libraries = [ haskellPackages.acme-default ]; } ''
309         import Data.Default
311         int :: Int
312         int = def
314         main :: IO ()
315         main = case int of
316           18871 -> putStrLn $ id "success"
317           _ -> print "fail"
318       ''
319     );
321     js = expectSuccess (
322       writeJS "test-writers-js" { libraries = [ nodePackages.semver ]; } ''
323         var semver = require('semver');
325         if (semver.valid('1.2.3')) {
326           console.log('success')
327         } else {
328           console.log('fail')
329         }
330       ''
331     );
333     perl = expectSuccess (
334       writePerl "test-writers-perl" { libraries = [ perlPackages.boolean ]; } ''
335         use boolean;
336         print "success\n" if true;
337       ''
338     );
340     python3 = expectSuccess (
341       writePython3 "test-writers-python3" { libraries = [ python3Packages.pyyaml ]; } ''
342         import yaml
344         y = yaml.safe_load("""
345           - test: success
346         """)
347         print(y[0]['test'])
348       ''
349     );
351     # Commented out because of this issue: https://github.com/NixOS/nixpkgs/issues/39356
353     #pypy2 = expectSuccessBin (writePyPy2Bin "test-writers-pypy2-bin" { libraries = [ pypy2Packages.enum ]; } ''
354     #  from enum import Enum
355     #
356     #  class Test(Enum):
357     #      a = "success"
358     #
359     #  print Test.a
360     #'');
362     #pypy3 = expectSuccessBin (writePyPy3Bin "test-writers-pypy3-bin" { libraries = [ pypy3Packages.pyyaml ]; } ''
363     #  import yaml
364     #
365     #  y = yaml.safe_load("""
366     #    - test: success
367     #  """)
368     #  print(y[0]['test'])
369     #'');
371     # Commented out because fails with 'error FS0039: The value or constructor 'JsonFSharpConverter' is not defined.'
373     # fsharp = expectSuccess (makeFSharpWriter {
374     #   libraries = { fetchNuGet }: [
375     #     (fetchNuGet { pname = "FSharp.SystemTextJson"; version = "0.17.4"; sha256 = "1bplzc9ybdqspii4q28l8gmfvzpkmgq5l1hlsiyg2h46w881lwg2"; })
376     #     (fetchNuGet { pname = "System.Text.Json"; version = "4.6.0"; sha256 = "0ism236hwi0k6axssfq58s1d8lihplwiz058pdvl8al71hagri39"; })
377     #   ];
378     # } "test-writers-fsharp" ''
379     #
380     #   #r "nuget: FSharp.SystemTextJson, 0.17.4"
381     #
382     #   module Json =
383     #       open System.Text.Json
384     #       open System.Text.Json.Serialization
385     #       let options = JsonSerializerOptions()
386     #       options.Converters.Add(JsonFSharpConverter())
387     #       let serialize<'a> (o: 'a) = JsonSerializer.Serialize<'a>(o, options)
388     #       let deserialize<'a> (str: string) = JsonSerializer.Deserialize<'a>(str, options)
389     #
390     #   type Letter = A | B
391     #   let a = {| Hello = Some "World"; Letter = A |}
392     #   if a |> Json.serialize |> Json.deserialize |> (=) a
393     #   then "success"
394     #   else "failed"
395     #   |> printfn "%s"
396     # '');
398     #pypy2NoLibs = expectSuccess (writePyPy2 "test-writers-pypy2-no-libs" {} ''
399     #  print("success")
400     #'');
402     python3NoLibs = expectSuccess (
403       writePython3 "test-writers-python3-no-libs" { } ''
404         print("success")
405       ''
406     );
408     pypy3NoLibs = expectSuccess (
409       writePyPy3 "test-writers-pypy3-no-libs" { } ''
410         print("success")
411       ''
412     );
414     fsharpNoNugetDeps = expectSuccess (
415       writeFSharp "test-writers-fsharp-no-nuget-deps" ''
416         printfn "success"
417       ''
418     );
420     luaNoLibs = expectSuccess (
421       writeLua "test-writers-lua-no-libs" { } ''
422         print("success")
423       ''
424     );
426     rubyNoLibs = expectSuccess (
427       writeRuby "test-writers-ruby-no-libs" { } ''
428         puts "success"
429       ''
430     );
431   };
433   path = recurseIntoAttrs {
434     bash = expectSuccess (
435       writeBash "test-writers-bash-path" (
436         writeText "test" ''
437           if [[ "test" == "test" ]]; then echo "success"; fi
438         ''
439       )
440     );
442     haskell = expectSuccess (
443       writeHaskell "test-writers-haskell-path" { libraries = [ haskellPackages.acme-default ]; } (
444         writeText "test" ''
445           import Data.Default
447           int :: Int
448           int = def
450           main :: IO ()
451           main = case int of
452             18871 -> putStrLn $ id "success"
453             _ -> print "fail"
454         ''
455       )
456     );
457   };
459   data = {
460     json = expectDataEqual {
461       file = writeJSON "data.json" { hello = "world"; };
462       expected = ''
463         {
464           "hello": "world"
465         }
466       '';
467     };
469     toml = expectDataEqual {
470       file = writeTOML "data.toml" { hello = "world"; };
471       expected = ''
472         hello = "world"
473       '';
474     };
476     yaml = expectDataEqual {
477       file = writeYAML "data.yaml" { hello = "world"; };
478       expected = "hello: world\n";
479     };
480   };
482   wrapping = recurseIntoAttrs {
483     bash-bin = expectSuccessBin (
484       writeBashBin "test-writers-wrapping-bash-bin"
485         {
486           makeWrapperArgs = [
487             "--set"
488             "ThaigerSprint"
489             "Thailand"
490           ];
491         }
492         ''
493           if [[ "$ThaigerSprint" == "Thailand" ]]; then
494             echo "success"
495           fi
496         ''
497     );
499     bash = expectSuccess (
500       writeBash "test-writers-wrapping-bash"
501         {
502           makeWrapperArgs = [
503             "--set"
504             "ThaigerSprint"
505             "Thailand"
506           ];
507         }
508         ''
509           if [[ "$ThaigerSprint" == "Thailand" ]]; then
510             echo "success"
511           fi
512         ''
513     );
515     babashka-bin = expectSuccessBin (
516       writeBabashkaBin "test-writers-wrapping-babashka-bin"
517         {
518           makeWrapperArgs = [
519             "--set"
520             "ThaigerSprint"
521             "Thailand"
522           ];
523         }
524         ''
525           (when (= (System/getenv "ThaigerSprint") "Thailand")
526             (println "success"))
527         ''
528     );
530     babashka = expectSuccess (
531       writeBabashka "test-writers-wrapping-babashka"
532         {
533           makeWrapperArgs = [
534             "--set"
535             "ThaigerSprint"
536             "Thailand"
537           ];
538         }
539         ''
540           (when (= (System/getenv "ThaigerSprint") "Thailand")
541             (println "success"))
542         ''
543     );
545     nim = expectSuccess (
546       writeNim "test-writers-wrapping-nim"
547         {
548           makeWrapperArgs = [
549             "--set"
550             "ThaigerSprint"
551             "Thailand"
552           ];
553         }
554         ''
555           import os
557           if getEnv("ThaigerSprint") == "Thailand":
558             echo "success"
559         ''
560     );
562     python = expectSuccess (
563       writePython3 "test-writers-wrapping-python"
564         {
565           makeWrapperArgs = [
566             "--set"
567             "ThaigerSprint"
568             "Thailand"
569           ];
570         }
571         ''
572           import os
574           if os.environ.get("ThaigerSprint") == "Thailand":
575               print("success")
576         ''
577     );
579     rust = expectSuccess (
580       writeRust "test-writers-wrapping-rust"
581         {
582           makeWrapperArgs = [
583             "--set"
584             "ThaigerSprint"
585             "Thailand"
586           ];
587         }
588         ''
589           fn main(){
590             if std::env::var("ThaigerSprint").unwrap() == "Thailand" {
591               println!("success")
592             }
593           }
594         ''
595     );
597     no-empty-wrapper =
598       let
599         bin = writeBashBin "bin" { makeWrapperArgs = [ ]; } ''true'';
600       in
601       runCommand "run-test-writers-wrapping-no-empty-wrapper" { } ''
602         ls -A ${bin}/bin
603         if [ $(ls -A ${bin}/bin | wc -l) -eq 1 ]; then
604           touch $out
605         else
606           echo "Error: Empty wrapper was created" >&2
607           exit 1
608         fi
609       '';
610   };