2024-03-04 07:05:23 +01:00
|
|
|
{ glib
|
|
|
|
, haskellPackages
|
2021-03-04 18:37:30 +01:00
|
|
|
, lib
|
|
|
|
, nodePackages
|
|
|
|
, perlPackages
|
2024-03-04 07:05:23 +01:00
|
|
|
, pypy2Packages
|
2021-03-04 18:37:30 +01:00
|
|
|
, python3Packages
|
2024-03-04 07:05:23 +01:00
|
|
|
, pypy3Packages
|
|
|
|
, luaPackages
|
|
|
|
, rubyPackages
|
2021-03-04 18:37:30 +01:00
|
|
|
, runCommand
|
2023-10-24 11:16:34 +02:00
|
|
|
, testers
|
2021-03-04 18:37:30 +01:00
|
|
|
, writers
|
|
|
|
, writeText
|
2019-10-21 23:20:46 +02:00
|
|
|
}:
|
2024-01-15 15:48:50 +01:00
|
|
|
|
|
|
|
# If you are reading this, you can test these writers by running: nix-build . -A tests.writers
|
|
|
|
|
2018-11-27 03:58:17 +01:00
|
|
|
with writers;
|
|
|
|
let
|
2023-07-22 13:38:31 +02:00
|
|
|
expectSuccess = test:
|
|
|
|
runCommand "run-${test.name}" {} ''
|
2023-07-22 11:14:51 +02:00
|
|
|
if [[ "$(${test})" != success ]]; then
|
2023-07-22 13:38:31 +02:00
|
|
|
echo 'test ${test.name} failed'
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-11-27 03:58:17 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
touch $out
|
2018-11-27 03:58:17 +01:00
|
|
|
'';
|
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
expectSuccessBin = test:
|
|
|
|
runCommand "run-${test.name}" {} ''
|
2023-07-22 11:14:51 +02:00
|
|
|
if [[ "$(${lib.getExe test})" != success ]]; then
|
2023-07-22 13:38:31 +02:00
|
|
|
echo 'test ${test.name} failed'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-07-22 11:14:51 +02:00
|
|
|
touch $out
|
|
|
|
'';
|
|
|
|
|
|
|
|
expectDataEqual = { file, expected }:
|
|
|
|
let
|
|
|
|
expectedFile = writeText "${file.name}-expected" expected;
|
|
|
|
in
|
2023-10-24 11:16:34 +02:00
|
|
|
testers.testEqualContents { expected = expectedFile; actual = file; assertion = "${file.name} matches"; };
|
2023-07-22 13:38:31 +02:00
|
|
|
in
|
|
|
|
lib.recurseIntoAttrs {
|
|
|
|
bin = lib.recurseIntoAttrs {
|
|
|
|
bash = expectSuccessBin (writeBashBin "test-writers-bash-bin" ''
|
|
|
|
if [[ "test" == "test" ]]; then echo "success"; fi
|
|
|
|
'');
|
2018-11-27 03:58:17 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
dash = expectSuccessBin (writeDashBin "test-writers-dash-bin" ''
|
|
|
|
test '~' = '~' && echo 'success'
|
|
|
|
'');
|
|
|
|
|
|
|
|
fish = expectSuccessBin (writeFishBin "test-writers-fish-bin" ''
|
2022-10-10 15:58:42 +02:00
|
|
|
if test "test" = "test"
|
|
|
|
echo "success"
|
|
|
|
end
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2022-10-10 15:58:42 +02:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
rust = expectSuccessBin (writeRustBin "test-writers-rust-bin" {} ''
|
2021-01-12 11:03:08 +01:00
|
|
|
fn main(){
|
|
|
|
println!("success")
|
|
|
|
}
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2021-01-12 11:03:08 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
haskell = expectSuccessBin (writeHaskellBin "test-writers-haskell-bin" { libraries = [ haskellPackages.acme-default ]; } ''
|
2018-11-27 03:58:17 +01:00
|
|
|
import Data.Default
|
|
|
|
|
|
|
|
int :: Int
|
|
|
|
int = def
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = case int of
|
|
|
|
18871 -> putStrLn $ id "success"
|
|
|
|
_ -> print "fail"
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2018-11-27 03:58:17 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
js = expectSuccessBin (writeJSBin "test-writers-js-bin" { libraries = [ nodePackages.semver ]; } ''
|
2018-11-27 03:58:17 +01:00
|
|
|
var semver = require('semver');
|
|
|
|
|
|
|
|
if (semver.valid('1.2.3')) {
|
|
|
|
console.log('success')
|
|
|
|
} else {
|
|
|
|
console.log('fail')
|
|
|
|
}
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2018-11-27 03:58:17 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
perl = expectSuccessBin (writePerlBin "test-writers-perl-bin" { libraries = [ perlPackages.boolean ]; } ''
|
2018-11-27 03:58:17 +01:00
|
|
|
use boolean;
|
|
|
|
print "success\n" if true;
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2018-11-27 03:58:17 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
python3 = expectSuccessBin (writePython3Bin "test-writers-python3-bin" { libraries = [ python3Packages.pyyaml ]; } ''
|
2018-11-27 03:58:17 +01:00
|
|
|
import yaml
|
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
y = yaml.safe_load("""
|
2018-11-27 03:58:17 +01:00
|
|
|
- test: success
|
|
|
|
""")
|
|
|
|
print(y[0]['test'])
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2021-12-11 14:02:38 +01:00
|
|
|
|
2024-01-15 15:48:50 +01: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'])
|
|
|
|
#'');
|
2024-01-16 05:51:17 +01:00
|
|
|
|
|
|
|
# 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!"
|
|
|
|
#'');
|
2018-11-27 03:58:17 +01:00
|
|
|
};
|
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
simple = lib.recurseIntoAttrs {
|
|
|
|
bash = expectSuccess (writeBash "test-writers-bash" ''
|
2018-11-27 03:58:17 +01:00
|
|
|
if [[ "test" == "test" ]]; then echo "success"; fi
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2018-11-27 03:58:17 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
dash = expectSuccess (writeDash "test-writers-dash" ''
|
2018-11-27 03:58:17 +01:00
|
|
|
test '~' = '~' && echo 'success'
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2018-11-27 03:58:17 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
fish = expectSuccess (writeFish "test-writers-fish" ''
|
2022-10-10 15:58:42 +02:00
|
|
|
if test "test" = "test"
|
|
|
|
echo "success"
|
|
|
|
end
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2022-10-10 15:58:42 +02:00
|
|
|
|
2024-01-16 05:51:17 +01:00
|
|
|
nu = expectSuccess (writeNu "test-writers-nushell" ''
|
|
|
|
echo "success"
|
|
|
|
'');
|
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
haskell = expectSuccess (writeHaskell "test-writers-haskell" { libraries = [ haskellPackages.acme-default ]; } ''
|
2018-11-27 03:58:17 +01:00
|
|
|
import Data.Default
|
|
|
|
|
|
|
|
int :: Int
|
|
|
|
int = def
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = case int of
|
|
|
|
18871 -> putStrLn $ id "success"
|
|
|
|
_ -> print "fail"
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2018-11-27 03:58:17 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
js = expectSuccess (writeJS "test-writers-js" { libraries = [ nodePackages.semver ]; } ''
|
2018-11-27 03:58:17 +01:00
|
|
|
var semver = require('semver');
|
|
|
|
|
|
|
|
if (semver.valid('1.2.3')) {
|
|
|
|
console.log('success')
|
|
|
|
} else {
|
|
|
|
console.log('fail')
|
|
|
|
}
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2018-11-27 03:58:17 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
perl = expectSuccess (writePerl "test-writers-perl" { libraries = [ perlPackages.boolean ]; } ''
|
2018-11-27 03:58:17 +01:00
|
|
|
use boolean;
|
|
|
|
print "success\n" if true;
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2018-11-27 03:58:17 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
python3 = expectSuccess (writePython3 "test-writers-python3" { libraries = [ python3Packages.pyyaml ]; } ''
|
2018-11-27 03:58:17 +01:00
|
|
|
import yaml
|
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
y = yaml.safe_load("""
|
2018-11-27 03:58:17 +01:00
|
|
|
- test: success
|
|
|
|
""")
|
|
|
|
print(y[0]['test'])
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2020-07-24 12:31:01 +02:00
|
|
|
|
2024-01-15 15:48:50 +01:00
|
|
|
# Commented out because of this issue: https://github.com/NixOS/nixpkgs/issues/39356
|
2021-12-11 14:02:38 +01:00
|
|
|
|
2024-01-15 15:48:50 +01:00
|
|
|
#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-11 14:02:38 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
fsharp = expectSuccess (makeFSharpWriter {
|
2022-02-28 23:40:43 +01:00
|
|
|
libraries = { fetchNuGet }: [
|
|
|
|
(fetchNuGet { pname = "FSharp.SystemTextJson"; version = "0.17.4"; sha256 = "1bplzc9ybdqspii4q28l8gmfvzpkmgq5l1hlsiyg2h46w881lwg2"; })
|
2023-12-11 09:47:06 +01:00
|
|
|
(fetchNuGet { pname = "System.Text.Json"; version = "4.6.0"; sha256 = "0ism236hwi0k6axssfq58s1d8lihplwiz058pdvl8al71hagri39"; })
|
2022-02-28 23:40:43 +01:00
|
|
|
];
|
|
|
|
} "test-writers-fsharp" ''
|
2024-01-15 15:48:50 +01:00
|
|
|
|
2022-02-28 23:40:43 +01: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-07-22 13:38:31 +02:00
|
|
|
'');
|
2022-02-28 23:40:43 +01:00
|
|
|
|
2024-01-15 15:48:50 +01:00
|
|
|
#pypy2NoLibs = expectSuccess (writePyPy2 "test-writers-pypy2-no-libs" {} ''
|
|
|
|
# print("success")
|
|
|
|
#'');
|
2021-12-11 14:02:38 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
python3NoLibs = expectSuccess (writePython3 "test-writers-python3-no-libs" {} ''
|
2020-07-24 12:31:01 +02:00
|
|
|
print("success")
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2021-12-11 14:02:38 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
pypy3NoLibs = expectSuccess (writePyPy3 "test-writers-pypy3-no-libs" {} ''
|
2021-12-11 14:02:38 +01:00
|
|
|
print("success")
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2022-02-28 23:40:43 +01:00
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
fsharpNoNugetDeps = expectSuccess (writeFSharp "test-writers-fsharp-no-nuget-deps" ''
|
2022-02-28 23:40:43 +01:00
|
|
|
printfn "success"
|
2024-01-16 05:51:17 +01:00
|
|
|
'');
|
|
|
|
|
|
|
|
luaNoLibs = expectSuccess (writeLua "test-writers-lua-no-libs" {} ''
|
|
|
|
print("success")
|
|
|
|
'');
|
|
|
|
|
|
|
|
rubyNoLibs = expectSuccess (writeRuby "test-writers-ruby-no-libs" {} ''
|
|
|
|
puts "success"
|
2023-07-22 13:38:31 +02:00
|
|
|
'');
|
2018-11-27 03:58:17 +01:00
|
|
|
};
|
|
|
|
|
2023-07-22 13:38:31 +02:00
|
|
|
path = lib.recurseIntoAttrs {
|
|
|
|
bash = expectSuccess (writeBash "test-writers-bash-path" (writeText "test" ''
|
2018-12-02 00:56:16 +01:00
|
|
|
if [[ "test" == "test" ]]; then echo "success"; fi
|
2023-07-22 13:38:31 +02:00
|
|
|
''));
|
|
|
|
|
|
|
|
haskell = expectSuccess (writeHaskell "test-writers-haskell-path" { libraries = [ haskellPackages.acme-default ]; } (writeText "test" ''
|
2018-12-02 00:56:16 +01:00
|
|
|
import Data.Default
|
|
|
|
|
|
|
|
int :: Int
|
|
|
|
int = def
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = case int of
|
|
|
|
18871 -> putStrLn $ id "success"
|
|
|
|
_ -> print "fail"
|
2023-07-22 13:38:31 +02:00
|
|
|
''));
|
2018-12-02 00:56:16 +01:00
|
|
|
};
|
2023-07-22 11:14:51 +02:00
|
|
|
|
|
|
|
data = {
|
|
|
|
json = expectDataEqual {
|
|
|
|
file = writeJSON "data.json" { hello = "world"; };
|
|
|
|
expected = ''
|
|
|
|
{
|
|
|
|
"hello": "world"
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
toml = expectDataEqual {
|
|
|
|
file = writeTOML "data.toml" { hello = "world"; };
|
2023-10-24 11:01:19 +02:00
|
|
|
expected = ''
|
|
|
|
hello = "world"
|
|
|
|
'';
|
2023-07-22 11:14:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
yaml = expectDataEqual {
|
|
|
|
file = writeYAML "data.yaml" { hello = "world"; };
|
|
|
|
expected = "hello: world\n";
|
|
|
|
};
|
|
|
|
};
|
2023-07-22 13:38:31 +02:00
|
|
|
}
|