2019-10-30 21:12:53 +03:00
|
|
|
{-# LANGUAGE ImplicitParams, LambdaCase #-}
|
2019-06-20 00:56:37 +03:00
|
|
|
module Integration.Spec (testTree) where
|
2017-02-22 01:02:31 +03:00
|
|
|
|
2019-06-05 21:28:28 +03:00
|
|
|
import Control.Exception (throw)
|
2019-06-12 20:16:34 +03:00
|
|
|
import Data.Foldable (find)
|
2017-04-03 23:34:25 +03:00
|
|
|
import Data.List (union, concat, transpose)
|
2019-06-12 18:04:42 +03:00
|
|
|
import qualified Data.ByteString.Lazy as BL
|
2017-02-22 01:02:31 +03:00
|
|
|
import System.FilePath.Glob
|
2019-06-12 18:04:42 +03:00
|
|
|
import System.IO.Unsafe
|
2018-03-13 21:04:58 +03:00
|
|
|
|
2019-09-20 19:26:49 +03:00
|
|
|
import SpecHelpers
|
2019-09-20 18:45:48 +03:00
|
|
|
import qualified System.Path as Path
|
|
|
|
import System.Path ((</>))
|
2017-02-22 01:02:31 +03:00
|
|
|
|
2019-06-12 18:04:42 +03:00
|
|
|
import Test.Tasty
|
|
|
|
import Test.Tasty.Golden
|
|
|
|
|
2019-09-20 18:45:48 +03:00
|
|
|
languages :: [Path.RelDir]
|
|
|
|
languages = fmap Path.relDir ["go", "javascript", "json", "python", "ruby", "typescript", "tsx"]
|
2018-03-13 21:10:50 +03:00
|
|
|
|
2019-06-20 00:56:37 +03:00
|
|
|
testTree :: (?session :: TaskSession) => TestTree
|
|
|
|
testTree = testGroup "Integration (golden tests)" $ fmap testsForLanguage languages
|
2017-02-22 01:02:31 +03:00
|
|
|
|
2019-09-20 18:45:48 +03:00
|
|
|
testsForLanguage :: (?session :: TaskSession) => Path.RelDir -> TestTree
|
2019-06-12 18:04:42 +03:00
|
|
|
testsForLanguage language = do
|
2019-09-20 18:45:48 +03:00
|
|
|
let dir = Path.relDir "test/fixtures" </> language </> Path.relDir "corpus"
|
2019-06-12 18:04:42 +03:00
|
|
|
let items = unsafePerformIO (examples dir)
|
2019-09-20 18:45:48 +03:00
|
|
|
localOption (mkTimeout 3000000) $ testGroup (Path.toString language) $ fmap testForExample items
|
2019-06-12 18:30:20 +03:00
|
|
|
{-# NOINLINE testsForLanguage #-}
|
2017-02-22 01:02:31 +03:00
|
|
|
|
2019-10-30 21:11:39 +03:00
|
|
|
data Example = DiffExample Path.RelFile Path.RelFile Path.RelFile
|
|
|
|
| ParseExample Path.RelFile Path.RelFile
|
2017-02-22 03:30:39 +03:00
|
|
|
deriving (Eq, Show)
|
2017-02-22 01:02:31 +03:00
|
|
|
|
2019-06-12 18:04:42 +03:00
|
|
|
testForExample :: (?session :: TaskSession) => Example -> TestTree
|
|
|
|
testForExample = \case
|
2019-10-30 21:11:39 +03:00
|
|
|
DiffExample fileA fileB diffOutput ->
|
2019-06-12 18:04:42 +03:00
|
|
|
goldenVsStringDiff
|
2019-09-20 18:45:48 +03:00
|
|
|
("diffs " <> Path.toString diffOutput)
|
2019-06-12 18:04:42 +03:00
|
|
|
(\ref new -> ["git", "diff", ref, new])
|
2019-09-20 18:45:48 +03:00
|
|
|
(Path.toString diffOutput)
|
2019-10-18 17:45:59 +03:00
|
|
|
(BL.fromStrict <$> diffFilePaths ?session fileA fileB)
|
2019-10-30 21:11:39 +03:00
|
|
|
ParseExample file parseOutput ->
|
2019-06-12 18:30:20 +03:00
|
|
|
goldenVsStringDiff
|
2019-09-20 18:45:48 +03:00
|
|
|
("parses " <> Path.toString parseOutput)
|
2019-06-12 18:30:20 +03:00
|
|
|
(\ref new -> ["git", "diff", ref, new])
|
2019-09-20 18:45:48 +03:00
|
|
|
(Path.toString parseOutput)
|
2019-09-20 18:52:39 +03:00
|
|
|
(parseFilePath ?session file >>= either throw (pure . BL.fromStrict))
|
2019-06-12 18:04:42 +03:00
|
|
|
|
|
|
|
|
2017-02-22 01:02:31 +03:00
|
|
|
-- | Return all the examples from the given directory. Examples are expected to
|
2017-02-22 19:32:19 +03:00
|
|
|
-- | have the form:
|
2017-02-22 01:02:31 +03:00
|
|
|
-- |
|
2017-02-23 01:07:47 +03:00
|
|
|
-- | example-name.A.rb - The left hand side of the diff.
|
|
|
|
-- | example-name.B.rb - The right hand side of the diff.
|
2017-02-22 03:05:08 +03:00
|
|
|
-- |
|
2017-02-23 01:07:47 +03:00
|
|
|
-- | example-name.diffA-B.txt - The expected sexpression diff output for A -> B.
|
|
|
|
-- | example-name.diffB-A.txt - The expected sexpression diff output for B -> A.
|
2017-02-22 03:05:08 +03:00
|
|
|
-- |
|
2017-02-23 01:07:47 +03:00
|
|
|
-- | example-name.parseA.txt - The expected sexpression parse tree for example-name.A.rb
|
|
|
|
-- | example-name.parseB.txt - The expected sexpression parse tree for example-name.B.rb
|
2019-09-20 18:45:48 +03:00
|
|
|
examples :: Path.RelDir -> IO [Example]
|
2017-02-22 01:02:31 +03:00
|
|
|
examples directory = do
|
|
|
|
as <- globFor "*.A.*"
|
|
|
|
bs <- globFor "*.B.*"
|
2017-02-22 22:31:56 +03:00
|
|
|
sExpAs <- globFor "*.parseA.txt"
|
|
|
|
sExpBs <- globFor "*.parseB.txt"
|
|
|
|
sExpDiffsAB <- globFor "*.diffA-B.txt"
|
|
|
|
sExpDiffsBA <- globFor "*.diffB-A.txt"
|
2017-02-22 01:02:31 +03:00
|
|
|
|
2017-02-27 22:24:52 +03:00
|
|
|
let exampleDiff lefts rights out name = DiffExample (lookupNormalized name lefts) (lookupNormalized name rights) out
|
2017-02-23 01:07:47 +03:00
|
|
|
let exampleParse files out name = ParseExample (lookupNormalized name files) out
|
2017-02-22 01:02:31 +03:00
|
|
|
|
|
|
|
let keys = (normalizeName <$> as) `union` (normalizeName <$> bs)
|
2017-02-27 22:24:52 +03:00
|
|
|
pure $ merge [ getExamples (exampleParse as) sExpAs keys
|
|
|
|
, getExamples (exampleParse bs) sExpBs keys
|
|
|
|
, getExamples (exampleDiff as bs) sExpDiffsAB keys
|
|
|
|
, getExamples (exampleDiff bs as) sExpDiffsBA keys ]
|
2017-02-22 01:02:31 +03:00
|
|
|
where
|
2017-02-27 22:24:52 +03:00
|
|
|
merge = concat . transpose
|
2017-02-23 01:07:47 +03:00
|
|
|
-- Only returns examples if they exist
|
|
|
|
getExamples f list = foldr (go f list) []
|
|
|
|
where go f list name acc = case lookupNormalized' name list of
|
|
|
|
Just out -> f out name : acc
|
|
|
|
Nothing -> acc
|
|
|
|
|
2019-09-20 18:45:48 +03:00
|
|
|
lookupNormalized :: Path.RelFile -> [Path.RelFile] -> Path.RelFile
|
2017-02-22 03:05:08 +03:00
|
|
|
lookupNormalized name xs = fromMaybe
|
2019-09-20 18:45:48 +03:00
|
|
|
(error ("cannot find " <> Path.toString name <> " make sure .A, .B and exist."))
|
2017-02-23 01:07:47 +03:00
|
|
|
(lookupNormalized' name xs)
|
|
|
|
|
2019-09-20 18:45:48 +03:00
|
|
|
lookupNormalized' :: Path.RelFile -> [Path.RelFile] -> Maybe Path.RelFile
|
2017-02-23 01:07:47 +03:00
|
|
|
lookupNormalized' name = find ((== name) . normalizeName)
|
|
|
|
|
2019-09-20 18:45:48 +03:00
|
|
|
globFor :: String -> IO [Path.RelFile]
|
|
|
|
globFor p = fmap Path.relFile <$> globDir1 (compile p) (Path.toString directory)
|
2017-02-22 01:02:31 +03:00
|
|
|
|
2017-02-22 03:05:08 +03:00
|
|
|
-- | Given a test name like "foo.A.js", return "foo".
|
2019-09-20 18:45:48 +03:00
|
|
|
normalizeName :: Path.RelFile -> Path.RelFile
|
|
|
|
normalizeName = Path.dropExtension . Path.dropExtension
|