Skip to content

Commit

Permalink
Add support for environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
rradczewski committed May 23, 2024
1 parent 3a3c12d commit 64b56be
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 7 deletions.
8 changes: 8 additions & 0 deletions spec/environment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests:
- name: shell
environment:
TEST_ENV: "test-value"
command: |
echo $TEST_ENV
stdout: |
test-value
2 changes: 1 addition & 1 deletion src/lib/Test/Smoke/Assert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ assertResult location testPlan@TestPlan {planTest = test} (ExecutionSucceeded ac
either (TestErrored test . AssertionError) (TestFinished testPlan) <$> runExceptT (processOutputs location testPlan actualOutputs)

processOutputs :: Path Resolved Dir -> TestPlan -> ActualOutputs -> Asserting FinishedTest
processOutputs location (TestPlan _ _ fallbackShell _ _ _ expectedStatus expectedStdOuts expectedStdErrs expectedFiles _) (ActualOutputs actualStatus actualStdOut actualStdErr actualFiles) = do
processOutputs location (TestPlan _ _ fallbackShell _ _ _ _ expectedStatus expectedStdOuts expectedStdErrs expectedFiles _) (ActualOutputs actualStatus actualStdOut actualStdErr actualFiles) = do
let statusResult = assertEqual expectedStatus actualStatus
stdOutResult <- assertAll (defaultIfEmpty expectedStdOuts) actualStdOut
stdErrResult <- assertAll (defaultIfEmpty expectedStdErrs) actualStdErr
Expand Down
10 changes: 7 additions & 3 deletions src/lib/Test/Smoke/Executable.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Test.Smoke.Executable where

import Control.Monad.Trans.Except (ExceptT)
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
Expand All @@ -17,19 +18,21 @@ runExecutable ::
Executable ->
Args ->
StdIn ->
Maybe EnvVars ->
Maybe WorkingDirectory ->
IO (ExitCode, Text, Text)
runExecutable (ExecutableProgram executablePath executableArgs) args (StdIn stdIn) workingDirectory =
runExecutable (ExecutableProgram executablePath executableArgs) args (StdIn stdIn) env workingDirectory =
readCreateProcessWithExitCode
( ( proc
(toFilePath executablePath)
(Vector.toList (unArgs (executableArgs <> args)))
)
{ cwd = toFilePath . unWorkingDirectory <$> workingDirectory
{ cwd = toFilePath . unWorkingDirectory <$> workingDirectory,
env = fmap Map.toList (unEnvVars <$> env)
}
)
stdIn
runExecutable (ExecutableScript (Shell shellPath shellArgs) (Script script)) args stdIn workingDirectory =
runExecutable (ExecutableScript (Shell shellPath shellArgs) (Script script)) args stdIn env workingDirectory =
withSystemTempFile defaultShellScriptName $ \scriptPath scriptHandle -> do
Text.IO.hPutStr scriptHandle script
hClose scriptHandle
Expand All @@ -38,6 +41,7 @@ runExecutable (ExecutableScript (Shell shellPath shellArgs) (Script script)) arg
(ExecutableProgram shellPath executableArgs)
args
stdIn
env
workingDirectory

convertCommandToExecutable ::
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Test/Smoke/Execution.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ runTest location testPlan =
else either ExecutionFailed ExecutionSucceeded <$> runExceptT (executeTest location testPlan)

executeTest :: Path Resolved Dir -> TestPlan -> Execution ActualOutputs
executeTest location (TestPlan _ workingDirectory _ executable args processStdIn _ _ _ files revert) = do
executeTest location (TestPlan _ workingDirectory _ executable args env processStdIn _ _ _ files revert) = do
let workingDirectoryFilePath =
toFilePath $ unWorkingDirectory workingDirectory
workingDirectoryExists <- liftIO $ doesDirectoryExist workingDirectoryFilePath
Expand All @@ -41,7 +41,7 @@ executeTest location (TestPlan _ workingDirectory _ executable args processStdIn
revertingDirectories revert $ do
(exitCode, processStdOut, processStdErr) <-
tryIO (CouldNotExecuteCommand executable) $
runExecutable executable args processStdIn (Just workingDirectory)
runExecutable executable args processStdIn env (Just workingDirectory)
actualFiles <- Map.fromList <$> mapM (liftIO . readTestFile) (Map.keys files)
pure $ ActualOutputs (convertExitCode exitCode) (StdOut processStdOut) (StdErr processStdErr) actualFiles
where
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Test/Smoke/Filters.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ applyFilters fallbackShell (Filter command) value = do
withExceptT (CouldNotExecuteFilter executable) $
ExceptT $
tryIOError $
runExecutable executable mempty (StdIn (serializeFixture value)) Nothing
runExecutable executable mempty (StdIn (serializeFixture value)) Nothing Nothing
case exitCode of
ExitSuccess -> pure $ deserializeFixture processStdOut
ExitFailure code ->
Expand Down
1 change: 1 addition & 0 deletions src/lib/Test/Smoke/Plan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ readTest location fallbackWorkingDirectory fallbackShell fallbackCommand test =
planShell = fallbackShell,
planExecutable = executable,
planArgs = args,
planEnvironment = testEnvironment test,
planStdIn = stdIn,
planStatus = status,
planStdOut = stdOut,
Expand Down
6 changes: 6 additions & 0 deletions src/lib/Test/Smoke/Types/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Control.Monad (when)
import Data.Aeson
import Data.Aeson.Types (Parser, typeMismatch)
import Data.Default
import Data.Map.Strict (Map)
import Data.String (IsString)
import Data.Text (Text)
import Data.Text qualified as Text
Expand Down Expand Up @@ -55,6 +56,11 @@ instance FromFixture Args where
fixtureName = "args"
serializeFixture = Text.unlines . Vector.toList . Vector.map Text.pack . unArgs

newtype EnvVars = EnvVars
{ unEnvVars :: Map String String
}
deriving (FromJSON)

newtype Script = Script
{ unScript :: Text
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/Test/Smoke/Types/Plans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ data TestPlan = TestPlan
planShell :: Maybe Shell,
planExecutable :: Executable,
planArgs :: Args,
planEnvironment :: Maybe EnvVars,
planStdIn :: StdIn,
planStatus :: Status,
planStdOut :: Vector (Assert StdOut),
Expand Down
2 changes: 2 additions & 0 deletions src/lib/Test/Smoke/Types/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ data Test = Test
testWorkingDirectory :: Maybe (Path Relative Dir),
testCommand :: Maybe Command,
testArgs :: Maybe Args,
testEnvironment :: Maybe EnvVars,
testStdIn :: Maybe (TestInput StdIn),
testStatus :: Status,
testStdOut :: Vector (TestOutput StdOut),
Expand All @@ -62,6 +63,7 @@ instance FromJSON Test where
<*> (v .:? "working-directory")
<*> (v .:? "command")
<*> (v .:? "args")
<*> (v .:? "environment")
<*> (v .:? "stdin")
<*> (v .:? "exit-status" .!= def)
<*> (manyMaybe <$> (v .:? "stdout"))
Expand Down

0 comments on commit 64b56be

Please # to comment.