Skip to content

Commit

Permalink
chore: Add tests and fix merging environment, not replacing it
Browse files Browse the repository at this point in the history
  • Loading branch information
rradczewski committed May 24, 2024
1 parent d091a21 commit 6e6c62b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
38 changes: 38 additions & 0 deletions fixtures/environment/smoke.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 8 additions & 6 deletions spec/environment.yaml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions spec/io/environment.out
Original file line number Diff line number Diff line change
@@ -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
11 changes: 9 additions & 2 deletions src/lib/Test/Smoke/Executable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Test/Smoke/Types/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6e6c62b

Please # to comment.