@@ -15,7 +15,6 @@ import Data.Array (findMap)
15import Data.Array as Array
15import Data.Array as Array
16import Data.Array.NonEmpty (NonEmptyArray)
16import Data.Array.NonEmpty (NonEmptyArray)
17import Data.Either (Either(..), either, isRight)
17import Data.Either (Either(..), either, isRight)
18−import Data.Foldable (for_)
19import Data.Foldable as Foldable
18import Data.Foldable as Foldable
20import Data.Map as Map
19import Data.Map as Map
21import Data.Maybe (Maybe(..), fromMaybe, isJust)
20import Data.Maybe (Maybe(..), fromMaybe, isJust)
@@ -26,7 +25,6 @@ import Data.String (Pattern(..))
26import Data.String as String
25import Data.String as String
27import Data.String.CodeUnits as SCU
26import Data.String.CodeUnits as SCU
28import Data.TraversableWithIndex (forWithIndex)
27import Data.TraversableWithIndex (forWithIndex)
29−import Data.Tuple (Tuple(..))
30import Dodo (plainText)
28import Dodo (plainText)
31import Dodo as Dodo
29import Dodo as Dodo
32import Effect (Effect)
30import Effect (Effect)
@@ -41,17 +39,14 @@ import Node.Library.Execa.Which (defaultWhichOptions, which)
41import Node.Path as Path
39import Node.Path as Path
42import Node.Process as Process
40import Node.Process as Process
43import Partial.Unsafe (unsafeCrashWith)
41import Partial.Unsafe (unsafeCrashWith)
42+import PureScript.Backend.Chez.Builder (basicBuildMain)
44import PureScript.Backend.Chez.Constants (moduleForeign, moduleLib, schemeExt)
43import PureScript.Backend.Chez.Constants (moduleForeign, moduleLib, schemeExt)
45import PureScript.Backend.Chez.Convert (codegenModule)
44import PureScript.Backend.Chez.Convert (codegenModule)
46import PureScript.Backend.Chez.Printer as P
45import PureScript.Backend.Chez.Printer as P
47import PureScript.Backend.Chez.Runtime (runtimeModule)
46import PureScript.Backend.Chez.Runtime (runtimeModule)
48−import PureScript.Backend.Optimizer.Builder (buildModules)
49import PureScript.Backend.Optimizer.Convert (BackendModule)
47import PureScript.Backend.Optimizer.Convert (BackendModule)
50import PureScript.Backend.Optimizer.CoreFn (Comment(..), Module(..), ModuleName(..))
48import PureScript.Backend.Optimizer.CoreFn (Comment(..), Module(..), ModuleName(..))
51−import PureScript.Backend.Optimizer.Directives (parseDirectiveFile)
52−import PureScript.Backend.Optimizer.Directives.Defaults (defaultDirectives)
53−import PureScript.Backend.Optimizer.Semantics.Foreign (coreForeignSemantics)
54−import Test.Utils (bufferToUTF8, canRunMain, copyFile, coreFnModulesFromOutput, execWithStdin, loadModuleMain, mkdirp, spawnFromParent)
49+import Test.Utils (bufferToUTF8, canRunMain, copyFile, execWithStdin, loadModuleMain, mkdirp, spawnFromParent)
56type TestArgs =
51type TestArgs =
57 { accept :: Boolean
52 { accept :: Boolean
@@ -103,98 +98,90 @@ runSnapshotTests { accept, filter } = do
103 let runtimeFilePath = Path.concat [ runtimePath, moduleLib <> schemeExt ]
98 let runtimeFilePath = Path.concat [ runtimePath, moduleLib <> schemeExt ]
104 let runtimeContents = Dodo.print plainText Dodo.twoSpaces $ P.printLibrary $ runtimeModule
99 let runtimeContents = Dodo.print plainText Dodo.twoSpaces $ P.printLibrary $ runtimeModule
105 FS.writeTextFile UTF8 runtimeFilePath runtimeContents
100 FS.writeTextFile UTF8 runtimeFilePath runtimeContents
106− coreFnModulesFromOutput "output" filter >>= case _ of
101+ basicBuildMain
107− Left errors -> do
102+ { coreFnDirectory: "output"
108− for_ errors \(Tuple filePath err) -> do
103+ , coreFnGlobs: filter
109− Console.error $ filePath <> " " <> err
110− liftEffect $ Process.exit 1
111− Right coreFnModules -> do
112− let { directives } = parseDirectiveFile defaultDirectives
113− coreFnModules # buildModules
115− , foreignSemantics: coreForeignSemantics -- no chez scheme specific foreign semantics yet
116− , onCodegenModule: \_ (Module { name: ModuleName name, path }) backend _ -> do
104+ , onCodegenModule: \_ (Module { name: ModuleName name, path }) backend _ -> do
119− Dodo.print plainText Dodo.twoSpaces
121− $ codegenModule backend
122− let testFileDir = Path.concat [ testOut, name ]
123− let testFilePath = Path.concat [ testFileDir, moduleLib <> schemeExt ]
125− FS.writeTextFile UTF8 testFilePath formatted
126− unless (Set.isEmpty backend.foreign) do
128− foreignSiblingPath =
129− fromMaybe path (String.stripSuffix (Pattern (Path.extname path)) path) <>
131− let foreignOutputPath = Path.concat [ testFileDir, moduleForeign <> schemeExt ]
132− copyFile foreignSiblingPath foreignOutputPath
133− let snapshotDirFile = Path.concat [ snapshotDir, path ]
134− when (Set.member snapshotDirFile snapshotPaths) do
135− originalFileSourceCode <- FS.readTextFile UTF8 snapshotDirFile
136− hasMain <- either unsafeCrashWith pure $
137− canRunMain originalFileSourceCode
138− void $ liftEffect $ Ref.modify
139− (Map.insert name ({ formatted, failsWith: hasFails backend, hasMain }))
141− , onPrepareModule: \build coreFnMod@(Module { name }) -> do
142− let total = show build.moduleCount
143− let index = show (build.moduleIndex + 1)
144− let padding = power " " (SCU.length total - SCU.length index)
145− Console.log $ "[" <> padding <> index <> " of " <> total <> "] Building " <> unwrap name
147− , traceIdents: Set.empty
149− outputModules <- liftEffect $ Ref.read outputRef
150− results <- forWithIndex outputModules \name ({ formatted, failsWith, hasMain }) -> do
107+ Dodo.print plainText Dodo.twoSpaces
109+ $ codegenModule backend
110+ let testFileDir = Path.concat [ testOut, name ]
111+ let testFilePath = Path.concat [ testFileDir, moduleLib <> schemeExt ]
113+ FS.writeTextFile UTF8 testFilePath formatted
114+ unless (Set.isEmpty backend.foreign) do
116+ foreignSiblingPath =
117+ fromMaybe path (String.stripSuffix (Pattern (Path.extname path)) path) <>
119+ let foreignOutputPath = Path.concat [ testFileDir, moduleForeign <> schemeExt ]
120+ copyFile foreignSiblingPath foreignOutputPath
121+ let snapshotDirFile = Path.concat [ snapshotDir, path ]
122+ when (Set.member snapshotDirFile snapshotPaths) do
123+ originalFileSourceCode <- FS.readTextFile UTF8 snapshotDirFile
124+ hasMain <- either unsafeCrashWith pure $
125+ canRunMain originalFileSourceCode
126+ void $ liftEffect $ Ref.modify
127+ (Map.insert name ({ formatted, failsWith: hasFails backend, hasMain }))
129+ , onPrepareModule: \build coreFnMod@(Module { name }) -> do
130+ let total = show build.moduleCount
131+ let index = show (build.moduleIndex + 1)
132+ let padding = power " " (SCU.length total - SCU.length index)
133+ Console.log $ "[" <> padding <> index <> " of " <> total <> "] Building " <> unwrap name
136+ outputModules <- liftEffect $ Ref.read outputRef
137+ results <- forWithIndex outputModules \name ({ formatted, failsWith, hasMain }) -> do
152− snapshotFilePath = Path.concat [ snapshotsOut, name <> schemeExt ]
139+ snapshotFilePath = Path.concat [ snapshotsOut, name <> schemeExt ]
153− runAcceptedTest = do
140+ runAcceptedTest = do
154− schemeFile <- liftEffect $ Path.resolve [ testOut, name ] $ moduleLib <> schemeExt
141+ schemeFile <- liftEffect $ Path.resolve [ testOut, name ] $ moduleLib <> schemeExt
155− result <- loadModuleMain
142+ result <- loadModuleMain
156− { libdir: vendorDirectory <> ":" <> testOut
143+ { libdir: vendorDirectory <> ":" <> testOut
157− , scheme: schemeBin
144+ , scheme: schemeBin
158− , hasMain
145+ , hasMain
159− , modulePath: schemeFile
146+ , modulePath: schemeFile
160− , moduleName: name
147+ , moduleName: name
162− case result of
149+ case result of
163− Left { message }
150+ Left { message }
164− | matchesFail message failsWith ->
151+ | matchesFail message failsWith ->
167− Console.log $ withGraphics (foreground Red) "✗" <> " " <> name <> " failed."
171− | isJust failsWith -> do
172− Console.log $ withGraphics (foreground Red) "✗" <> " " <> name <>
173− " succeeded when it should have failed."
176− pure true
152+ pure true
177− attempt (FS.readTextFile UTF8 snapshotFilePath) >>= case _ of
179− Console.log $ withGraphics (foreground Yellow) "✓" <> " " <> name <> " saved."
180− FS.writeTextFile UTF8 snapshotFilePath formatted
183− | formatted == prevOutput ->
186− Console.log $ withGraphics (foreground Yellow) "✓" <> " " <> name <> " accepted."
187− FS.writeTextFile UTF8 snapshotFilePath formatted
189 | otherwise -> do
153 | otherwise -> do
190 Console.log $ withGraphics (foreground Red) "✗" <> " " <> name <> " failed."
154 Console.log $ withGraphics (foreground Red) "✗" <> " " <> name <> " failed."
191− diff <- bufferToUTF8 <<< _.stdout =<< execWithStdin
192− ("diff " <> snapshotFilePath <> " -")
194− Console.log diff
155+ Console.log message
195 pure false
156 pure false
158+ | isJust failsWith -> do
159+ Console.log $ withGraphics (foreground Red) "✗" <> " " <> name <>
160+ " succeeded when it should have failed."
164+ attempt (FS.readTextFile UTF8 snapshotFilePath) >>= case _ of
166+ Console.log $ withGraphics (foreground Yellow) "✓" <> " " <> name <> " saved."
167+ FS.writeTextFile UTF8 snapshotFilePath formatted
170+ | formatted == prevOutput ->
173+ Console.log $ withGraphics (foreground Yellow) "✓" <> " " <> name <> " accepted."
174+ FS.writeTextFile UTF8 snapshotFilePath formatted
177+ Console.log $ withGraphics (foreground Red) "✗" <> " " <> name <> " failed."
178+ diff <- bufferToUTF8 <<< _.stdout =<< execWithStdin
179+ ("diff " <> snapshotFilePath <> " -")
196− unless (Foldable.and results) do
183+ unless (Foldable.and results) do
197− liftEffect $ Process.exit 1
184+ liftEffect $ Process.exit 1
199hasFails :: BackendModule -> Maybe String
186hasFails :: BackendModule -> Maybe String
200hasFails = findMap go <<< _.comments
187hasFails = findMap go <<< _.comments