on master help sign in
Expected JS test will write current output if env var exists.
5efc238Laszlo Pandy
tests/Test/Compiler.hs+40−9
@@ -5,9 +5,12 @@ module Test.Compiler (compilerTests) where
5import Control.Exception (try, IOException)
5import Control.Exception (try, IOException)
6
6
7import qualified Data.Map as Map
7import qualified Data.Map as Map
8+import Data.Maybe (isJust)
8import Data.Traversable (traverse)
9import Data.Traversable (traverse)
9
10
11+import System.Directory (createDirectoryIfMissing)
12+import System.Environment (lookupEnv)
10−import System.FilePath ((</>), joinPath, splitDirectories)
13+import System.FilePath ((</>), (<.>), dropFileName, joinPath, splitDirectories)
11import System.FilePath.Find (find, (==?), extension)
14import System.FilePath.Find (find, (==?), extension)
12
15
13import Test.Framework
16import Test.Framework
@@ -17,6 +20,11 @@ import Test.HUnit (Assertion, assertFailure, assertBool, assertEqual)
17import qualified Elm.Compiler as Compiler
20import qualified Elm.Compiler as Compiler
18import qualified Elm.Package as Package
21import qualified Elm.Package as Package
19
22
23+writeExpectedJsEnvVarName :: String
24+writeExpectedJsEnvVarName =
25+ "ELM_WRITE_NEW_EXPECTED"
26+
27+
20compilerTests :: Test
28compilerTests :: Test
21compilerTests =
29compilerTests =
22 buildTest $ do
30 buildTest $ do
@@ -75,9 +83,21 @@ testIf handleResult filePaths =
75
83
76
84
77testMatchesExpected :: String -> String -> IO [Test]
85testMatchesExpected :: String -> String -> IO [Test]
78−testMatchesExpected elmDir expectedJsDir =
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
79− traverse doTest =<< getElms elmDir
93+ traverse doTest elmFiles
80 where
94 where
95+ getExpectedFilePath filePath =
96+ testsDir
97+ </> expectedJsDir
98+ </> joinPath (drop 3 (splitDirectories filePath))
99+ <.> ".js"
100+
81 readFileOrErrorStr filePath = do
101 readFileOrErrorStr filePath = do
82 strOrExc <- try $ readFile filePath
102 strOrExc <- try $ readFile filePath
83 case strOrExc of
103 case strOrExc of
@@ -86,16 +106,27 @@ testMatchesExpected elmDir expectedJsDir =
86
106
87 doTest filePath = do
107 doTest filePath = do
88 source <- readFile filePath
108 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)
93
110
94 let formattedResult = compileString filePath source
111 let formattedResult = compileString filePath source
95 assertion = matchesExpected expectedJs formattedResult
112 assertion = matchesExpected expectedJs formattedResult
96
113
97 return $ testCase filePath assertion
114 return $ testCase filePath assertion
98
115
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+
99
130
100-- CHECK RESULTS
131-- CHECK RESULTS
101
132
@@ -126,11 +157,11 @@ matchesExpected expectedJs result =
126 assertEqual matchFailureMessage expectedJs js
157 assertEqual matchFailureMessage expectedJs js
127
158
128 _ ->
159 _ ->
129− assertFailure "compile failed; could not match expected output"
160+ assertFailure "Compile failed. There is nothing to match expected output."
130
161
131
162
132matchFailureMessage :: String
163matchFailureMessage :: String
133matchFailureMessage =
164matchFailureMessage =
134 "Compiled JS did not match expected JS." ++
165 "Compiled JS did not match expected JS." ++
135 "\n If the change is intentional, rerun " ++
166 "\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"