dotnet: add publish and web tests

This adds a helper for creating dotnet tests, and adds tests for:

- normal publish and run via wrapper
- aspnetcore web app
This commit is contained in:
David McFarland 2024-03-06 18:46:27 -04:00
parent 445b240555
commit ecad5a115d

View File

@ -4,6 +4,8 @@
, writeText , writeText
, testers , testers
, runCommand , runCommand
, expect
, curl
}: type: args: stdenv.mkDerivation (finalAttrs: args // { }: type: args: stdenv.mkDerivation (finalAttrs: args // {
doInstallCheck = true; doInstallCheck = true;
@ -27,37 +29,97 @@
} // lib.optionalAttrs (type == "sdk") { } // lib.optionalAttrs (type == "sdk") {
passthru = { passthru = {
tests = { tests = let
mkDotnetTest =
{
name,
template,
usePackageSource ? false,
build,
# TODO: use correct runtimes instead of sdk
runtime ? finalAttrs.finalPackage,
runInputs ? [],
run ? null,
}:
let
built = runCommand "dotnet-test-${name}" { buildInputs = [ finalAttrs.finalPackage ]; } (''
HOME=$PWD/.home
dotnet new nugetconfig
dotnet nuget disable source nuget
'' + lib.optionalString usePackageSource ''
dotnet nuget add source ${finalAttrs.finalPackage.packages}
'' + ''
dotnet new ${template} -n test -o .
'' + build);
in
if run == null
then build
else
runCommand "${built.name}-run" { src = built; nativeBuildInputs = runInputs; } (
lib.optionalString (runtime != null) ''
# TODO: use runtime here
export DOTNET_ROOT=${runtime}
'' + run);
checkConsoleOutput = command: ''
output="$(${command})"
# yes, older SDKs omit the comma
[[ "$output" =~ Hello,?\ World! ]] && touch "$out"
'';
in {
version = testers.testVersion { version = testers.testVersion {
package = finalAttrs.finalPackage; package = finalAttrs.finalPackage;
}; };
console = runCommand "dotnet-test-console" { console = mkDotnetTest {
nativeBuildInputs = [ finalAttrs.finalPackage ]; name = "console";
} '' template = "console";
HOME=$(pwd)/fake-home build = checkConsoleOutput "dotnet run";
dotnet new nugetconfig };
dotnet nuget disable source nuget
dotnet new console -n test -o .
output="$(dotnet run)"
# yes, older SDKs omit the comma
[[ "$output" =~ Hello,?\ World! ]] && touch "$out"
'';
single-file = let build = runCommand "dotnet-test-build-single-file" { publish = mkDotnetTest {
nativeBuildInputs = [ finalAttrs.finalPackage ]; name = "publish";
} '' template = "console";
HOME=$(pwd)/fake-home build = "dotnet publish -o $out";
dotnet new nugetconfig run = checkConsoleOutput "$src/test";
dotnet nuget disable source nuget };
dotnet nuget add source ${finalAttrs.finalPackage.packages}
dotnet new console -n test -o . single-file = mkDotnetTest {
dotnet publish --use-current-runtime -p:PublishSingleFile=true -o $out name = "single-file";
''; in runCommand "dotnet-test-run-single-file" {} '' template = "console";
output="$(${build}/test)" usePackageSource = true;
# yes, older SDKs omit the comma build = "dotnet publish --use-current-runtime -p:PublishSingleFile=true -o $out";
[[ "$output" =~ Hello,?\ World! ]] && touch "$out" runtime = null;
''; run = checkConsoleOutput "$src/test";
};
web = mkDotnetTest {
name = "publish";
template = "web";
build = "dotnet publish -o $out";
runInputs = [ expect curl ];
run = ''
expect <<"EOF"
set status 1
spawn $env(src)/test
expect_before default abort
expect -re {Now listening on: ([^\r]+)\r} {
set url $expect_out(1,string)
}
expect "Application started. Press Ctrl+C to shut down."
set output [exec curl -sSf $url]
if {$output != "Hello World!"} {
send_error "Unexpected output: $output\n"
exit 1
}
send \x03
catch wait result
exit [lindex $result 3]
EOF
touch $out
'';
};
} // args.passthru.tests or {}; } // args.passthru.tests or {};
} // args.passthru or {}; } // args.passthru or {};
}) })