1
1
mirror of https://github.com/github/semantic.git synced 2024-12-01 09:15:01 +03:00
semantic/test/Integration/Spec.hs

100 lines
3.8 KiB
Haskell
Raw Normal View History

2019-06-12 18:04:42 +03:00
{-# LANGUAGE ImplicitParams, LambdaCase, NamedFieldPuns #-}
2019-06-20 00:56:37 +03:00
module Integration.Spec (testTree) where
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
import System.FilePath.Glob
2018-04-18 23:33:17 +03:00
import System.FilePath.Posix
2019-06-12 18:04:42 +03:00
import System.IO.Unsafe
2018-03-13 21:04:58 +03:00
import SpecHelpers
2019-06-12 18:04:42 +03:00
import Test.Tasty
import Test.Tasty.Golden
2018-04-18 23:33:17 +03:00
languages :: [FilePath]
2019-06-07 01:44:15 +03:00
languages = ["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
2019-06-12 18:04:42 +03:00
testsForLanguage :: (?session :: TaskSession) => FilePath -> TestTree
testsForLanguage language = do
let dir = "test/fixtures" </> language </> "corpus"
let items = unsafePerformIO (examples dir)
localOption (mkTimeout 3000000) $ testGroup language $ fmap testForExample items
{-# NOINLINE testsForLanguage #-}
data Example = DiffExample { fileA :: FilePath, fileB :: FilePath, diffOutput :: FilePath }
| ParseExample { file :: FilePath, parseOutput :: FilePath }
2017-02-22 03:30:39 +03:00
deriving (Eq, Show)
2019-06-12 18:04:42 +03:00
testForExample :: (?session :: TaskSession) => Example -> TestTree
testForExample = \case
DiffExample{fileA, fileB, diffOutput} ->
goldenVsStringDiff
("diffs " <> diffOutput)
(\ref new -> ["git", "diff", ref, new])
diffOutput
(BL.fromStrict <$> diffFilePaths ?session (Both fileA fileB))
ParseExample{file, parseOutput} ->
goldenVsStringDiff
("parses " <> parseOutput)
(\ref new -> ["git", "diff", ref, new])
parseOutput
(parseFilePath ?session file >>= either throw (pure . BL.fromStrict))
2019-06-12 18:04:42 +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-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
2017-02-22 03:30:39 +03:00
examples :: FilePath -> IO [Example]
examples directory = do
as <- globFor "*.A.*"
bs <- globFor "*.B.*"
sExpAs <- globFor "*.parseA.txt"
sExpBs <- globFor "*.parseB.txt"
sExpDiffsAB <- globFor "*.diffA-B.txt"
sExpDiffsBA <- globFor "*.diffB-A.txt"
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
let keys = (normalizeName <$> as) `union` (normalizeName <$> bs)
pure $ merge [ getExamples (exampleParse as) sExpAs keys
, getExamples (exampleParse bs) sExpBs keys
, getExamples (exampleDiff as bs) sExpDiffsAB keys
, getExamples (exampleDiff bs as) sExpDiffsBA keys ]
where
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
2017-02-22 03:05:08 +03:00
lookupNormalized :: FilePath -> [FilePath] -> FilePath
lookupNormalized name xs = fromMaybe
2017-07-28 21:37:02 +03:00
(error ("cannot find " <> name <> " make sure .A, .B and exist."))
2017-02-23 01:07:47 +03:00
(lookupNormalized' name xs)
lookupNormalized' :: FilePath -> [FilePath] -> Maybe FilePath
lookupNormalized' name = find ((== name) . normalizeName)
globFor :: FilePath -> IO [FilePath]
globFor p = globDir1 (compile p) directory
2017-02-22 03:05:08 +03:00
-- | Given a test name like "foo.A.js", return "foo".
normalizeName :: FilePath -> FilePath
normalizeName path = dropExtension $ dropExtension path