From 2f8e84bf0ac7f047d03f13e733bf6ce8da976ca3 Mon Sep 17 00:00:00 2001 From: Alex David Date: Sun, 30 Nov 2025 11:11:05 -0800 Subject: [PATCH] Backfill tests for `fwd-env` and `try-fwd-env` --- tests/spec/CombinatorsSpec.hs | 48 +++++++++++++++++++++++++++++++++++ tests/spec/TestPrelude.hs | 5 ++++ 2 files changed, 53 insertions(+) diff --git a/tests/spec/CombinatorsSpec.hs b/tests/spec/CombinatorsSpec.hs index 466dba1..9eb47e2 100644 --- a/tests/spec/CombinatorsSpec.hs +++ b/tests/spec/CombinatorsSpec.hs @@ -10,6 +10,7 @@ import System.Posix (getProcessStatus, sigKILL, signalProcess) import System.Process qualified as Process import Test.Hspec import TestPrelude +import GHC.IO.Exception (ExitCode(..)) spec :: Spec spec = parallel $ inTestM $ do @@ -68,6 +69,26 @@ spec = parallel $ inTestM $ do |] `shouldOutput` "secret\n" + it "correctly escapes variables" $ do + withEnv "MY_ENV_VAR" "$(echo foo)" $ do + [i| + jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') (c: [ + (c.fwd-env "MY_ENV_VAR") + ]) + |] + `shouldOutput` "$(echo foo)\n" + + it "exits non-zero if the variable isn't set at runtime" $ do + (exitCode, _stdout, stderr) <- + runNixDrvWithExitCode + [i| + jail "test" (sh ''echo ok'') (c: [ + (c.fwd-env "MY_ENV_VAR") + ]) + |] + liftIO $ exitCode `shouldBe` ExitFailure 1 + liftIO $ stderr `shouldContain` "MY_ENV_VAR: unbound variable" + describe "jail-to-host-channel" $ do it "calls a script on the outside of the jail with an argument and returns its stdout" $ do tmpDir <- getTestDir @@ -379,6 +400,33 @@ spec = parallel $ inTestM $ do |] `shouldOutput` "UTC\n" + describe "try-fwd-env" $ do + it "forwards the environment variable if set" $ do + withEnv "MY_ENV_VAR" "secret" $ do + [i| + jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') (c: [ + (c.try-fwd-env "MY_ENV_VAR") + ]) + |] + `shouldOutput` "secret\n" + + it "correctly escapes variables" $ do + withEnv "MY_ENV_VAR" "$(echo foo)" $ do + [i| + jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') (c: [ + (c.try-fwd-env "MY_ENV_VAR") + ]) + |] + `shouldOutput` "$(echo foo)\n" + + it "does not set the environment variable if it is not set" $ do + [i| + jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') (c: [ + (c.try-fwd-env "MY_ENV_VAR") + ]) + |] + `shouldOutput` "unset\n" + describe "try-readonly" $ do it "binds paths as readonly in the jail" $ do tmpDir <- getTestDir diff --git a/tests/spec/TestPrelude.hs b/tests/spec/TestPrelude.hs index f3ec87f..a12c225 100644 --- a/tests/spec/TestPrelude.hs +++ b/tests/spec/TestPrelude.hs @@ -10,6 +10,7 @@ module TestPrelude inTestM, liftIO, runNixDrv, + runNixDrvWithExitCode, spawnNixDrv, toNixString, void, @@ -34,6 +35,7 @@ import Data.Map qualified as Map import Data.Maybe (fromJust) import Data.String.Conversions import Data.String.Interpolate (i) +import GHC.IO.Exception (ExitCode) import GHC.IO.Handle (Handle) import System.Environment (lookupEnv) import System.FilePath (()) @@ -122,6 +124,9 @@ mkNixDrvProccess nixDrvExpr = do runNixDrv :: String -> TestM String runNixDrv = liftIO . flip Process.readCreateProcess "" <=< mkNixDrvProccess +runNixDrvWithExitCode :: String -> TestM (ExitCode, String, String) +runNixDrvWithExitCode = liftIO . flip Process.readCreateProcessWithExitCode "" <=< mkNixDrvProccess + spawnNixDrv :: String -> TestM (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) spawnNixDrv = liftIO . createProcess <=< mkNixDrvProccess