|
| 1 | +{-# LANGUAGE ImplicitParams, LambdaCase, NamedFieldPuns #-} |
1 | 2 | module Integration.Spec (spec) where |
2 | 3 |
|
3 | 4 | import Control.Exception (throw) |
4 | 5 | import Data.Foldable (find, traverse_, for_) |
5 | 6 | import Data.List (union, concat, transpose) |
6 | 7 | import qualified Data.ByteString as B |
| 8 | +import qualified Data.ByteString.Lazy as BL |
7 | 9 | import System.FilePath.Glob |
8 | 10 | import System.FilePath.Posix |
| 11 | +import System.IO.Unsafe |
9 | 12 |
|
10 | 13 | import SpecHelpers |
11 | 14 |
|
| 15 | +import Test.Tasty |
| 16 | +import Test.Tasty.Golden |
| 17 | +import Test.Tasty.HUnit |
| 18 | + |
12 | 19 | languages :: [FilePath] |
13 | 20 | languages = ["go", "javascript", "json", "python", "ruby", "typescript", "tsx"] |
14 | 21 |
|
15 | | -spec :: TaskSession -> Spec |
16 | | -spec config = parallel $ do |
17 | | - for_ languages $ \language -> do |
18 | | - let dir = "test/fixtures" </> language </> "corpus" |
19 | | - it (language <> " corpus exists") $ examples dir `shouldNotReturn` [] |
20 | | - describe (language <> " corpus") $ runTestsIn dir [] |
| 22 | +spec :: TaskSession -> TestTree |
| 23 | +spec config = let ?session = config in testGroup "Integration (golden tests)" $ fmap testsForLanguage languages |
21 | 24 |
|
22 | | - where |
23 | | - runTestsIn :: FilePath -> [(FilePath, String)] -> SpecWith () |
24 | | - runTestsIn directory pending = do |
25 | | - examples <- runIO $ examples directory |
26 | | - traverse_ (runTest pending) examples |
27 | | - runTest pending ParseExample{..} = it ("parses " <> file) $ maybe (testParse config file parseOutput) pendingWith (lookup parseOutput pending) |
28 | | - runTest pending DiffExample{..} = it ("diffs " <> diffOutput) $ maybe (testDiff config (Both fileA fileB) diffOutput) pendingWith (lookup diffOutput pending) |
| 25 | +testsForLanguage :: (?session :: TaskSession) => FilePath -> TestTree |
| 26 | +testsForLanguage language = do |
| 27 | + let dir = "test/fixtures" </> language </> "corpus" |
| 28 | + let items = unsafePerformIO (examples dir) |
| 29 | + testGroup language (fmap testForExample items) |
29 | 30 |
|
30 | 31 | data Example = DiffExample { fileA :: FilePath, fileB :: FilePath, diffOutput :: FilePath } |
31 | 32 | | ParseExample { file :: FilePath, parseOutput :: FilePath } |
32 | 33 | deriving (Eq, Show) |
33 | 34 |
|
| 35 | +testForExample :: (?session :: TaskSession) => Example -> TestTree |
| 36 | +testForExample = \case |
| 37 | + DiffExample{fileA, fileB, diffOutput} -> |
| 38 | + goldenVsStringDiff |
| 39 | + ("diffs " <> diffOutput) |
| 40 | + (\ref new -> ["git", "diff", ref, new]) |
| 41 | + diffOutput |
| 42 | + (BL.fromStrict <$> diffFilePaths ?session (Both fileA fileB)) |
| 43 | + ParseExample{file, parseOutput} -> testCase ("parses " <> file) (pure ()) |
| 44 | + |
| 45 | + |
34 | 46 | -- | Return all the examples from the given directory. Examples are expected to |
35 | 47 | -- | have the form: |
36 | 48 | -- | |
@@ -81,18 +93,3 @@ examples directory = do |
81 | 93 | -- | Given a test name like "foo.A.js", return "foo". |
82 | 94 | normalizeName :: FilePath -> FilePath |
83 | 95 | normalizeName path = dropExtension $ dropExtension path |
84 | | - |
85 | | -testParse :: TaskSession -> FilePath -> FilePath -> Expectation |
86 | | -testParse session path expectedOutput = do |
87 | | - actual <- fmap verbatim <$> parseFilePath session path |
88 | | - case actual of |
89 | | - Left err -> throw err |
90 | | - Right actual -> do |
91 | | - expected <- verbatim <$> B.readFile expectedOutput |
92 | | - actual `shouldBe` expected |
93 | | - |
94 | | -testDiff :: TaskSession -> Both FilePath -> FilePath -> Expectation |
95 | | -testDiff config paths expectedOutput = do |
96 | | - actual <- verbatim <$> diffFilePaths config paths |
97 | | - expected <- verbatim <$> B.readFile expectedOutput |
98 | | - actual `shouldBe` expected |
0 commit comments