2024-04-21 15:54:59 +00:00
|
|
|
{ haskellPackages
|
2021-03-09 03:18:52 +00:00
|
|
|
, lib
|
|
|
|
, nodePackages
|
|
|
|
, perlPackages
|
|
|
|
, python3Packages
|
|
|
|
, runCommand
|
2023-11-16 04:20:00 +00:00
|
|
|
, testers
|
2021-03-09 03:18:52 +00:00
|
|
|
, writers
|
|
|
|
, writeText
|
2020-04-24 23:36:52 +00:00
|
|
|
}:
|
2024-02-07 01:22:34 +00:00
|
|
|
|
|
|
|
# If you are reading this, you can test these writers by running: nix-build . -A tests.writers
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
let
|
2024-04-21 15:54:59 +00:00
|
|
|
inherit (lib) getExe recurseIntoAttrs;
|
|
|
|
|
|
|
|
inherit (writers)
|
|
|
|
makeFSharpWriter
|
|
|
|
writeBash
|
|
|
|
writeBashBin
|
|
|
|
writeDash
|
|
|
|
writeDashBin
|
|
|
|
writeFish
|
|
|
|
writeFishBin
|
|
|
|
writeFSharp
|
|
|
|
writeHaskell
|
|
|
|
writeHaskellBin
|
|
|
|
writeJS
|
|
|
|
writeJSBin
|
|
|
|
writeJSON
|
|
|
|
writeLua
|
|
|
|
writeNu
|
|
|
|
writePerl
|
|
|
|
writePerlBin
|
|
|
|
writePyPy3
|
|
|
|
writePython3
|
|
|
|
writePython3Bin
|
|
|
|
writeRuby
|
|
|
|
writeRust
|
|
|
|
writeRustBin
|
|
|
|
writeText
|
|
|
|
writeTOML
|
|
|
|
writeYAML
|
|
|
|
;
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
expectSuccess = test:
|
|
|
|
runCommand "run-${test.name}" {} ''
|
|
|
|
if [[ "$(${test})" != success ]]; then
|
|
|
|
echo 'test ${test.name} failed'
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
touch $out
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
expectSuccessBin = test:
|
|
|
|
runCommand "run-${test.name}" {} ''
|
2024-04-21 15:54:59 +00:00
|
|
|
if [[ "$(${getExe test})" != success ]]; then
|
2023-08-04 22:07:22 +00:00
|
|
|
echo 'test ${test.name} failed'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
touch $out
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
expectDataEqual = { file, expected }:
|
|
|
|
let
|
|
|
|
expectedFile = writeText "${file.name}-expected" expected;
|
|
|
|
in
|
2023-11-16 04:20:00 +00:00
|
|
|
testers.testEqualContents { expected = expectedFile; actual = file; assertion = "${file.name} matches"; };
|
2023-08-04 22:07:22 +00:00
|
|
|
in
|
2024-04-21 15:54:59 +00:00
|
|
|
recurseIntoAttrs {
|
|
|
|
bin = recurseIntoAttrs {
|
2023-08-04 22:07:22 +00:00
|
|
|
bash = expectSuccessBin (writeBashBin "test-writers-bash-bin" ''
|
|
|
|
if [[ "test" == "test" ]]; then echo "success"; fi
|
|
|
|
'');
|
|
|
|
|
|
|
|
dash = expectSuccessBin (writeDashBin "test-writers-dash-bin" ''
|
|
|
|
test '~' = '~' && echo 'success'
|
|
|
|
'');
|
|
|
|
|
|
|
|
fish = expectSuccessBin (writeFishBin "test-writers-fish-bin" ''
|
2022-10-21 18:38:19 +00:00
|
|
|
if test "test" = "test"
|
|
|
|
echo "success"
|
|
|
|
end
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2022-10-21 18:38:19 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
rust = expectSuccessBin (writeRustBin "test-writers-rust-bin" {} ''
|
2021-01-15 22:18:51 +00:00
|
|
|
fn main(){
|
|
|
|
println!("success")
|
|
|
|
}
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2021-01-15 22:18:51 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
haskell = expectSuccessBin (writeHaskellBin "test-writers-haskell-bin" { libraries = [ haskellPackages.acme-default ]; } ''
|
2020-04-24 23:36:52 +00:00
|
|
|
import Data.Default
|
|
|
|
|
|
|
|
int :: Int
|
|
|
|
int = def
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = case int of
|
|
|
|
18871 -> putStrLn $ id "success"
|
|
|
|
_ -> print "fail"
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
js = expectSuccessBin (writeJSBin "test-writers-js-bin" { libraries = [ nodePackages.semver ]; } ''
|
2020-04-24 23:36:52 +00:00
|
|
|
var semver = require('semver');
|
|
|
|
|
|
|
|
if (semver.valid('1.2.3')) {
|
|
|
|
console.log('success')
|
|
|
|
} else {
|
|
|
|
console.log('fail')
|
|
|
|
}
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
perl = expectSuccessBin (writePerlBin "test-writers-perl-bin" { libraries = [ perlPackages.boolean ]; } ''
|
2020-04-24 23:36:52 +00:00
|
|
|
use boolean;
|
|
|
|
print "success\n" if true;
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
python3 = expectSuccessBin (writePython3Bin "test-writers-python3-bin" { libraries = [ python3Packages.pyyaml ]; } ''
|
2020-04-24 23:36:52 +00:00
|
|
|
import yaml
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
y = yaml.safe_load("""
|
2020-04-24 23:36:52 +00:00
|
|
|
- test: success
|
|
|
|
""")
|
|
|
|
print(y[0]['test'])
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2021-12-19 01:06:50 +00:00
|
|
|
|
2024-02-07 01:22:34 +00:00
|
|
|
# Commented out because of this issue: https://github.com/NixOS/nixpkgs/issues/39356
|
|
|
|
|
|
|
|
#pypy2 = expectSuccessBin (writePyPy2Bin "test-writers-pypy2-bin" { libraries = [ pypy2Packages.enum ]; } ''
|
|
|
|
# from enum import Enum
|
|
|
|
#
|
|
|
|
# class Test(Enum):
|
|
|
|
# a = "success"
|
|
|
|
#
|
|
|
|
# print Test.a
|
|
|
|
#'');
|
|
|
|
|
|
|
|
#pypy3 = expectSuccessBin (writePyPy3Bin "test-writers-pypy3-bin" { libraries = [ pypy3Packages.pyyaml ]; } ''
|
|
|
|
# import yaml
|
|
|
|
#
|
|
|
|
# y = yaml.safe_load("""
|
|
|
|
# - test: success
|
|
|
|
# """)
|
|
|
|
# print(y[0]['test'])
|
|
|
|
#'');
|
|
|
|
|
|
|
|
# Could not test this because of external package issues :(
|
|
|
|
#lua = writeLuaBin "test-writers-lua-bin" { libraries = [ pkgs.luaPackages.say ]; } ''
|
|
|
|
# s = require("say")
|
|
|
|
# s:set_namespace("en")
|
|
|
|
|
|
|
|
# s:set('money', 'I have %s dollars')
|
|
|
|
# s:set('wow', 'So much money!')
|
|
|
|
|
|
|
|
# print(s('money', {1000})) -- I have 1000 dollars
|
|
|
|
|
|
|
|
# s:set_namespace("fr") -- switch to french!
|
|
|
|
# s:set('wow', "Tant d'argent!")
|
|
|
|
|
|
|
|
# print(s('wow')) -- Tant d'argent!
|
|
|
|
# s:set_namespace("en") -- switch back to english!
|
|
|
|
# print(s('wow')) -- So much money!
|
|
|
|
#'';
|
|
|
|
|
|
|
|
#ruby = expectSuccessBin (writeRubyBin "test-writers-ruby-bin" { libraries = [ rubyPackages.rubocop ]; } ''
|
|
|
|
#puts "This should work!"
|
|
|
|
#'');
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
simple = recurseIntoAttrs {
|
2023-08-04 22:07:22 +00:00
|
|
|
bash = expectSuccess (writeBash "test-writers-bash" ''
|
2020-04-24 23:36:52 +00:00
|
|
|
if [[ "test" == "test" ]]; then echo "success"; fi
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
dash = expectSuccess (writeDash "test-writers-dash" ''
|
2020-04-24 23:36:52 +00:00
|
|
|
test '~' = '~' && echo 'success'
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
fish = expectSuccess (writeFish "test-writers-fish" ''
|
2022-10-21 18:38:19 +00:00
|
|
|
if test "test" = "test"
|
|
|
|
echo "success"
|
|
|
|
end
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2022-10-21 18:38:19 +00:00
|
|
|
|
2024-02-07 01:22:34 +00:00
|
|
|
nu = expectSuccess (writeNu "test-writers-nushell" ''
|
|
|
|
echo "success"
|
|
|
|
'');
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
haskell = expectSuccess (writeHaskell "test-writers-haskell" { libraries = [ haskellPackages.acme-default ]; } ''
|
2020-04-24 23:36:52 +00:00
|
|
|
import Data.Default
|
|
|
|
|
|
|
|
int :: Int
|
|
|
|
int = def
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = case int of
|
|
|
|
18871 -> putStrLn $ id "success"
|
|
|
|
_ -> print "fail"
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
js = expectSuccess (writeJS "test-writers-js" { libraries = [ nodePackages.semver ]; } ''
|
2020-04-24 23:36:52 +00:00
|
|
|
var semver = require('semver');
|
|
|
|
|
|
|
|
if (semver.valid('1.2.3')) {
|
|
|
|
console.log('success')
|
|
|
|
} else {
|
|
|
|
console.log('fail')
|
|
|
|
}
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
perl = expectSuccess (writePerl "test-writers-perl" { libraries = [ perlPackages.boolean ]; } ''
|
2020-04-24 23:36:52 +00:00
|
|
|
use boolean;
|
|
|
|
print "success\n" if true;
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
python3 = expectSuccess (writePython3 "test-writers-python3" { libraries = [ python3Packages.pyyaml ]; } ''
|
2020-04-24 23:36:52 +00:00
|
|
|
import yaml
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
y = yaml.safe_load("""
|
2020-04-24 23:36:52 +00:00
|
|
|
- test: success
|
|
|
|
""")
|
|
|
|
print(y[0]['test'])
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2024-02-07 01:22:34 +00:00
|
|
|
# Commented out because of this issue: https://github.com/NixOS/nixpkgs/issues/39356
|
|
|
|
|
|
|
|
#pypy2 = expectSuccessBin (writePyPy2Bin "test-writers-pypy2-bin" { libraries = [ pypy2Packages.enum ]; } ''
|
|
|
|
# from enum import Enum
|
|
|
|
#
|
|
|
|
# class Test(Enum):
|
|
|
|
# a = "success"
|
|
|
|
#
|
|
|
|
# print Test.a
|
|
|
|
#'');
|
|
|
|
|
|
|
|
#pypy3 = expectSuccessBin (writePyPy3Bin "test-writers-pypy3-bin" { libraries = [ pypy3Packages.pyyaml ]; } ''
|
|
|
|
# import yaml
|
|
|
|
#
|
|
|
|
# y = yaml.safe_load("""
|
|
|
|
# - test: success
|
|
|
|
# """)
|
|
|
|
# print(y[0]['test'])
|
|
|
|
#'');
|
2021-12-19 01:06:50 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
fsharp = expectSuccess (makeFSharpWriter {
|
2022-03-05 16:20:37 +00:00
|
|
|
libraries = { fetchNuGet }: [
|
|
|
|
(fetchNuGet { pname = "FSharp.SystemTextJson"; version = "0.17.4"; sha256 = "1bplzc9ybdqspii4q28l8gmfvzpkmgq5l1hlsiyg2h46w881lwg2"; })
|
2024-01-25 14:12:00 +00:00
|
|
|
(fetchNuGet { pname = "System.Text.Json"; version = "4.6.0"; sha256 = "0ism236hwi0k6axssfq58s1d8lihplwiz058pdvl8al71hagri39"; })
|
2022-03-05 16:20:37 +00:00
|
|
|
];
|
|
|
|
} "test-writers-fsharp" ''
|
2024-02-07 01:22:34 +00:00
|
|
|
|
2022-03-05 16:20:37 +00:00
|
|
|
#r "nuget: FSharp.SystemTextJson, 0.17.4"
|
|
|
|
|
|
|
|
module Json =
|
|
|
|
open System.Text.Json
|
|
|
|
open System.Text.Json.Serialization
|
|
|
|
let options = JsonSerializerOptions()
|
|
|
|
options.Converters.Add(JsonFSharpConverter())
|
|
|
|
let serialize<'a> (o: 'a) = JsonSerializer.Serialize<'a>(o, options)
|
|
|
|
let deserialize<'a> (str: string) = JsonSerializer.Deserialize<'a>(str, options)
|
|
|
|
|
|
|
|
type Letter = A | B
|
|
|
|
let a = {| Hello = Some "World"; Letter = A |}
|
|
|
|
if a |> Json.serialize |> Json.deserialize |> (=) a
|
|
|
|
then "success"
|
|
|
|
else "failed"
|
|
|
|
|> printfn "%s"
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2022-03-05 16:20:37 +00:00
|
|
|
|
2024-02-07 01:22:34 +00:00
|
|
|
#pypy2NoLibs = expectSuccess (writePyPy2 "test-writers-pypy2-no-libs" {} ''
|
|
|
|
# print("success")
|
|
|
|
#'');
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
python3NoLibs = expectSuccess (writePython3 "test-writers-python3-no-libs" {} ''
|
2020-08-20 17:08:02 +00:00
|
|
|
print("success")
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2021-12-19 01:06:50 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
pypy3NoLibs = expectSuccess (writePyPy3 "test-writers-pypy3-no-libs" {} ''
|
2021-12-19 01:06:50 +00:00
|
|
|
print("success")
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2022-03-05 16:20:37 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
fsharpNoNugetDeps = expectSuccess (writeFSharp "test-writers-fsharp-no-nuget-deps" ''
|
2022-03-05 16:20:37 +00:00
|
|
|
printfn "success"
|
2024-02-07 01:22:34 +00:00
|
|
|
'');
|
|
|
|
|
|
|
|
luaNoLibs = expectSuccess (writeLua "test-writers-lua-no-libs" {} ''
|
|
|
|
print("success")
|
|
|
|
'');
|
|
|
|
|
|
|
|
rubyNoLibs = expectSuccess (writeRuby "test-writers-ruby-no-libs" {} ''
|
|
|
|
puts "success"
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
path = recurseIntoAttrs {
|
2023-08-04 22:07:22 +00:00
|
|
|
bash = expectSuccess (writeBash "test-writers-bash-path" (writeText "test" ''
|
2020-04-24 23:36:52 +00:00
|
|
|
if [[ "test" == "test" ]]; then echo "success"; fi
|
2023-08-04 22:07:22 +00:00
|
|
|
''));
|
|
|
|
|
|
|
|
haskell = expectSuccess (writeHaskell "test-writers-haskell-path" { libraries = [ haskellPackages.acme-default ]; } (writeText "test" ''
|
2020-04-24 23:36:52 +00:00
|
|
|
import Data.Default
|
|
|
|
|
|
|
|
int :: Int
|
|
|
|
int = def
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = case int of
|
|
|
|
18871 -> putStrLn $ id "success"
|
|
|
|
_ -> print "fail"
|
2023-08-04 22:07:22 +00:00
|
|
|
''));
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
data = {
|
|
|
|
json = expectDataEqual {
|
|
|
|
file = writeJSON "data.json" { hello = "world"; };
|
|
|
|
expected = ''
|
|
|
|
{
|
|
|
|
"hello": "world"
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
toml = expectDataEqual {
|
|
|
|
file = writeTOML "data.toml" { hello = "world"; };
|
2023-11-16 04:20:00 +00:00
|
|
|
expected = ''
|
|
|
|
hello = "world"
|
|
|
|
'';
|
2023-08-04 22:07:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
yaml = expectDataEqual {
|
|
|
|
file = writeYAML "data.yaml" { hello = "world"; };
|
|
|
|
expected = "hello: world\n";
|
|
|
|
};
|
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
wrapping = recurseIntoAttrs {
|
|
|
|
bash-bin = expectSuccessBin (
|
|
|
|
writeBashBin "test-writers-wrapping-bash-bin"
|
|
|
|
{
|
|
|
|
makeWrapperArgs = [
|
|
|
|
"--set"
|
|
|
|
"ThaigerSprint"
|
|
|
|
"Thailand"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
''
|
|
|
|
if [[ "$ThaigerSprint" == "Thailand" ]]; then
|
|
|
|
echo "success"
|
|
|
|
fi
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
bash = expectSuccess (
|
|
|
|
writeBash "test-writers-wrapping-bash"
|
|
|
|
{
|
|
|
|
makeWrapperArgs = [
|
|
|
|
"--set"
|
|
|
|
"ThaigerSprint"
|
|
|
|
"Thailand"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
''
|
|
|
|
if [[ "$ThaigerSprint" == "Thailand" ]]; then
|
|
|
|
echo "success"
|
|
|
|
fi
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
python = expectSuccess (
|
|
|
|
writePython3 "test-writers-wrapping-python"
|
|
|
|
{
|
|
|
|
makeWrapperArgs = [
|
|
|
|
"--set"
|
|
|
|
"ThaigerSprint"
|
|
|
|
"Thailand"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
''
|
|
|
|
import os
|
|
|
|
|
|
|
|
if os.environ.get("ThaigerSprint") == "Thailand":
|
|
|
|
print("success")
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
rust = expectSuccess (
|
|
|
|
writeRust "test-writers-wrapping-rust"
|
|
|
|
{
|
|
|
|
makeWrapperArgs = [
|
|
|
|
"--set"
|
|
|
|
"ThaigerSprint"
|
|
|
|
"Thailand"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
''
|
|
|
|
fn main(){
|
|
|
|
if std::env::var("ThaigerSprint").unwrap() == "Thailand" {
|
|
|
|
println!("success")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
no-empty-wrapper = let
|
|
|
|
bin = writeBashBin "bin" { makeWrapperArgs = []; } ''true'';
|
|
|
|
in runCommand "run-test-writers-wrapping-no-empty-wrapper" {} ''
|
|
|
|
ls -A ${bin}/bin
|
|
|
|
if [ $(ls -A ${bin}/bin | wc -l) -eq 1 ]; then
|
|
|
|
touch $out
|
|
|
|
else
|
|
|
|
echo "Error: Empty wrapper was created" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
2023-08-04 22:07:22 +00:00
|
|
|
}
|