on master help sign in
Expected JS test will write current output if env var exists.
commit5efc2383c91adc9f2a24330b53ffe54a0a4fb237 authorLaszlo Pandy <laszlo.pandy@prezi.com> dateMon 31 Aug 2015, 15:16 (11 years ago) parentbb7eb96
1 file changed+40−9
tests/Test/Compiler.hs+40−9
tests/Test/Compiler.hs+40−9
@@ -5,9 +5,12 @@ module Test.Compiler (compilerTests) where
55import Control.Exception (try, IOException)
66
77import qualified Data.Map as Map
8+import Data.Maybe (isJust)
89import Data.Traversable (traverse)
910
10−import System.FilePath ((</>), joinPath, splitDirectories)
11+import System.Directory (createDirectoryIfMissing)
12+import System.Environment (lookupEnv)
13+import System.FilePath ((</>), (<.>), dropFileName, joinPath, splitDirectories)
1114import System.FilePath.Find (find, (==?), extension)
1215
1316import Test.Framework
@@ -17,6 +20,11 @@ import Test.HUnit (Assertion, assertFailure, assertBool, assertEqual)
1720import qualified Elm.Compiler as Compiler
1821import qualified Elm.Package as Package
1922
23+writeExpectedJsEnvVarName :: String
24+writeExpectedJsEnvVarName =
25+ "ELM_WRITE_NEW_EXPECTED"
26+
27+
2028compilerTests :: Test
2129compilerTests =
2230 buildTest $ do
@@ -75,9 +83,21 @@ testIf handleResult filePaths =
7583
7684
7785testMatchesExpected :: String -> String -> IO [Test]
78−testMatchesExpected elmDir expectedJsDir =
79− traverse doTest =<< getElms elmDir
86+testMatchesExpected elmDir expectedJsDir = do
87+ elmFiles <- getElms elmDir
88+ envVar <- lookupEnv writeExpectedJsEnvVarName
89+ if isJust envVar
90+ then
91+ traverse writeNewExpected elmFiles
92+ else
93+ traverse doTest elmFiles
8094 where
95+ getExpectedFilePath filePath =
96+ testsDir
97+ </> expectedJsDir
98+ </> joinPath (drop 3 (splitDirectories filePath))
99+ <.> ".js"
100+
81101 readFileOrErrorStr filePath = do
82102 strOrExc <- try $ readFile filePath
83103 case strOrExc of
@@ -86,16 +106,27 @@ testMatchesExpected elmDir expectedJsDir =
86106
87107 doTest filePath = do
88108 source <- readFile filePath
89−
90− let expectedFilePath =
91− testsDir </> expectedJsDir </> joinPath (drop 3 (splitDirectories filePath)) ++ ".js"
92− expectedJs <- readFileOrErrorStr expectedFilePath
109+ expectedJs <- readFileOrErrorStr (getExpectedFilePath filePath)
93110
94111 let formattedResult = compileString filePath source
95112 assertion = matchesExpected expectedJs formattedResult
96113
97114 return $ testCase filePath assertion
98115
116+ writeNewExpected filePath = do
117+ source <- readFile filePath
118+ let formattedResult = compileString filePath source
119+ expectedFilePath = getExpectedFilePath filePath
120+ assertion =
121+ case formattedResult of
122+ Right (Compiler.Result _ _ js) -> do
123+ createDirectoryIfMissing True (dropFileName expectedFilePath)
124+ writeFile expectedFilePath js
125+ assertFailure ("Wrote new expected js: " ++ expectedFilePath)
126+ _ ->
127+ assertFailure "Compile failed. Could not write new expected js."
128+ return $ testCase filePath assertion
129+
99130
100131-- CHECK RESULTS
101132
@@ -126,11 +157,11 @@ matchesExpected expectedJs result =
126157 assertEqual matchFailureMessage expectedJs js
127158
128159 _ ->
129− assertFailure "compile failed; could not match expected output"
160+ assertFailure "Compile failed. There is nothing to match expected output."
130161
131162
132163matchFailureMessage :: String
133164matchFailureMessage =
134165 "Compiled JS did not match expected JS." ++
135166 "\n If the change is intentional, rerun " ++
136− "the tests with environment variable: ELM_WRITE_NEW_EXPECTED=1"
167+ "the tests with environment variable: " ++ writeExpectedJsEnvVarName ++ "=1"