about summary refs log tree commit diff
path: root/pkgs/development/compilers/dotnet
diff options
context:
space:
mode:
authorDavid McFarland <corngood@gmail.com>2024-03-29 15:22:16 -0300
committerGitHub <noreply@github.com>2024-03-29 15:22:16 -0300
commit1bcfabc7007971a70c997c823af1ba49b633cb7d (patch)
treec47fe3286f2a69d03c1a04bf7c16259fd74d04cc /pkgs/development/compilers/dotnet
parentc6bad4895aa098ff7b97643d42939954e2f156e2 (diff)
parentecad5a115d9f20670c6cac7d40b6e9c81a9ae233 (diff)
Merge pull request #294070 from corngood/dotnet-tests
dotnet: add publish and web tests
Diffstat (limited to 'pkgs/development/compilers/dotnet')
-rw-r--r--pkgs/development/compilers/dotnet/common.nix114
1 files changed, 88 insertions, 26 deletions
diff --git a/pkgs/development/compilers/dotnet/common.nix b/pkgs/development/compilers/dotnet/common.nix
index 0d8890e61da2b..63b3a7de374ef 100644
--- a/pkgs/development/compilers/dotnet/common.nix
+++ b/pkgs/development/compilers/dotnet/common.nix
@@ -4,6 +4,8 @@
 , writeText
 , testers
 , runCommand
+, expect
+, curl
 }: type: args: stdenv.mkDerivation (finalAttrs: args // {
   doInstallCheck = true;
 
@@ -27,37 +29,97 @@
 
 } // lib.optionalAttrs (type == "sdk") {
   passthru = {
-    tests = {
-      version = testers.testVersion {
-        package = finalAttrs.finalPackage;
-      };
+    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);
 
-      console = runCommand "dotnet-test-console" {
-        nativeBuildInputs = [ finalAttrs.finalPackage ];
-      } ''
-        HOME=$(pwd)/fake-home
-        dotnet new nugetconfig
-        dotnet nuget disable source nuget
-        dotnet new console -n test -o .
-        output="$(dotnet run)"
+      checkConsoleOutput = command: ''
+        output="$(${command})"
         # yes, older SDKs omit the comma
         [[ "$output" =~ Hello,?\ World! ]] && touch "$out"
       '';
 
-      single-file = let build = runCommand "dotnet-test-build-single-file" {
-        nativeBuildInputs = [ finalAttrs.finalPackage ];
-      } ''
-        HOME=$(pwd)/fake-home
-        dotnet new nugetconfig
-        dotnet nuget disable source nuget
-        dotnet nuget add source ${finalAttrs.finalPackage.packages}
-        dotnet new console -n test -o .
-        dotnet publish --use-current-runtime -p:PublishSingleFile=true -o $out
-      ''; in runCommand "dotnet-test-run-single-file" {} ''
-        output="$(${build}/test)"
-        # yes, older SDKs omit the comma
-        [[ "$output" =~ Hello,?\ World! ]] && touch "$out"
-      '';
+    in {
+      version = testers.testVersion {
+        package = finalAttrs.finalPackage;
+      };
+
+      console = mkDotnetTest {
+        name = "console";
+        template = "console";
+        build = checkConsoleOutput "dotnet run";
+      };
+
+      publish = mkDotnetTest {
+        name = "publish";
+        template = "console";
+        build = "dotnet publish -o $out";
+        run = checkConsoleOutput "$src/test";
+      };
+
+      single-file = mkDotnetTest {
+        name = "single-file";
+        template = "console";
+        usePackageSource = true;
+        build = "dotnet publish --use-current-runtime -p:PublishSingleFile=true -o $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 or {};
 })