diff --git a/src/Spago/Build.hs b/src/Spago/Build.hs index 6cf5c3e08..0d0cebd4a 100644 --- a/src/Spago/Build.hs +++ b/src/Spago/Build.hs @@ -85,9 +85,9 @@ build BuildOptions{..} maybePostBuild = do ExitFailure n -> die [ "Backend " <> displayShow backend <> " exited with error:" <> repr n ] buildAction globs = do + let action = buildBackend globs >> fromMaybe (pure ()) maybePostBuild runCommands "Before" beforeCommands - onException ( buildBackend globs ) $ runCommands "Else" elseCommands - fromMaybe (pure ()) maybePostBuild + action `onException` (runCommands "Else" elseCommands) runCommands "Then" thenCommands case shouldWatch of diff --git a/test/SpagoSpec.hs b/test/SpagoSpec.hs index 43504d587..0ea23dc26 100644 --- a/test/SpagoSpec.hs +++ b/test/SpagoSpec.hs @@ -448,7 +448,7 @@ spec = around_ setup $ do test <- readTextFile dumpFile test `shouldBe` "before\nthen\n" - it "Spago should run an else command if there is an error" $ do + it "Spago should run an else command if there is an error in the build" $ do spago ["init"] >>= shouldBeSuccess @@ -463,6 +463,22 @@ spec = around_ setup $ do test <- readTextFile dumpFile test `shouldBe` "else\n" + + it "Spago should run an else command if there is an error in the run file" $ do + + spago ["init"] >>= shouldBeSuccess + spago ["install", "exceptions"] >>= shouldBeSuccess + + dir <- pwd + let dumpFile = dir "testOutput" + rm "src/Main.purs" + writeTextFile "src/Main.purs" "module Main where\nimport Effect.Exception\nmain = throw \"error\"" + spago [ "run" + , "--else", "echo else>> " <> ( Text.pack $ encodeString dumpFile ) + ] >>= shouldBeFailure + test <- readTextFile dumpFile + test `shouldBe` "else\n" + it "Spago should run multiple commands in order" $ do spago ["init"] >>= shouldBeSuccess