From 6e6c62b1cd6474479bf032b811740f7dee4b7be2 Mon Sep 17 00:00:00 2001 From: Raimo Radczewski Date: Fri, 24 May 2024 12:53:26 +0200 Subject: [PATCH] chore: Add tests and fix merging environment, not replacing it --- fixtures/environment/smoke.yaml | 38 ++++++++++++++++++++++++++++++++ spec/environment.yaml | 14 +++++++----- spec/io/environment.out | 12 ++++++++++ src/lib/Test/Smoke/Executable.hs | 11 +++++++-- src/lib/Test/Smoke/Types/Base.hs | 2 +- 5 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 fixtures/environment/smoke.yaml create mode 100644 spec/io/environment.out diff --git a/fixtures/environment/smoke.yaml b/fixtures/environment/smoke.yaml new file mode 100644 index 0000000..5864c4f --- /dev/null +++ b/fixtures/environment/smoke.yaml @@ -0,0 +1,38 @@ +tests: + - name: environment + environment: + TEST_ENV: "test-value" + command: | + echo $TEST_ENV + stdout: | + test-value + + - name: inherits + command: | + echo $ONLY_DEFINED_IN_SPEC + stdout: | + defined_in_spec + + - name: overwrites + environment: + OVERWRITE_IN_FIXTURE: "overwritten" + command: | + echo $OVERWRITE_IN_FIXTURE + stdout: | + overwritten + + - name: not-defined + command: | + echo $NOT_DEFINED + stdout: "\n" + + - name: spec-is-merged-into-fixture-not-replaced + environment: + OVERWRITE_IN_FIXTURE: "overwritten" + command: | + echo $OVERWRITE_IN_FIXTURE + echo $ONLY_DEFINED_IN_SPEC + stdout: | + overwritten + defined_in_spec + diff --git a/spec/environment.yaml b/spec/environment.yaml index b60f58f..afbc8a6 100644 --- a/spec/environment.yaml +++ b/spec/environment.yaml @@ -1,8 +1,10 @@ tests: - - name: shell + - name: environment + args: + - fixtures/environment/smoke.yaml + exit-code: 0 environment: - TEST_ENV: "test-value" - command: | - echo $TEST_ENV - stdout: | - test-value + OVERWRITE_IN_FIXTURE: "not_overwritten" + ONLY_DEFINED_IN_SPEC: "defined_in_spec" + stdout: + - file: io/environment.out diff --git a/spec/io/environment.out b/spec/io/environment.out new file mode 100644 index 0000000..89667db --- /dev/null +++ b/spec/io/environment.out @@ -0,0 +1,12 @@ +environment + succeeded +inherits + succeeded +overwrites + succeeded +not-defined + succeeded +spec-is-merged-into-fixture-not-replaced + succeeded + +5 tests, 0 failures diff --git a/src/lib/Test/Smoke/Executable.hs b/src/lib/Test/Smoke/Executable.hs index fbcf218..898f769 100644 --- a/src/lib/Test/Smoke/Executable.hs +++ b/src/lib/Test/Smoke/Executable.hs @@ -5,6 +5,7 @@ import Data.Map.Strict qualified as Map import Data.Text (Text) import Data.Text.IO qualified as Text.IO import Data.Vector qualified as Vector +import System.Environment (getEnvironment) import System.Exit (ExitCode) import System.IO (hClose) import System.IO.Temp (withSystemTempFile) @@ -21,17 +22,23 @@ runExecutable :: Maybe EnvVars -> Maybe WorkingDirectory -> IO (ExitCode, Text, Text) -runExecutable (ExecutableProgram executablePath executableArgs) args (StdIn stdIn) env workingDirectory = +runExecutable (ExecutableProgram executablePath executableArgs) args (StdIn stdIn) env workingDirectory = do + mergedEnv <- traverse addOriginalEnv env readCreateProcessWithExitCode ( ( proc (toFilePath executablePath) (Vector.toList (unArgs (executableArgs <> args))) ) { cwd = toFilePath . unWorkingDirectory <$> workingDirectory, - env = Map.toList . unEnvVars <$> env + env = Map.toList . unEnvVars <$> mergedEnv } ) stdIn + where + addOriginalEnv :: EnvVars -> IO EnvVars + addOriginalEnv overriddenEnv = do + originalEnv <- EnvVars . Map.fromList <$> getEnvironment + pure $ overriddenEnv <> originalEnv runExecutable (ExecutableScript (Shell shellPath shellArgs) (Script script)) args stdIn env workingDirectory = withSystemTempFile defaultShellScriptName $ \scriptPath scriptHandle -> do Text.IO.hPutStr scriptHandle script diff --git a/src/lib/Test/Smoke/Types/Base.hs b/src/lib/Test/Smoke/Types/Base.hs index 8eece25..b48d4d3 100644 --- a/src/lib/Test/Smoke/Types/Base.hs +++ b/src/lib/Test/Smoke/Types/Base.hs @@ -59,7 +59,7 @@ instance FromFixture Args where newtype EnvVars = EnvVars { unEnvVars :: Map String String } - deriving (FromJSON) + deriving (Semigroup, FromJSON) newtype Script = Script { unScript :: Text