2017-03-09 00:56:08 +03:00
|
|
|
{-# LANGUAGE DataKinds, GeneralizedNewtypeDeriving, OverloadedStrings #-}
|
2017-02-22 01:02:31 +03:00
|
|
|
module IntegrationSpec where
|
|
|
|
|
2017-07-28 21:37:02 +03:00
|
|
|
import qualified Data.ByteString as B
|
|
|
|
import Data.Foldable (find, traverse_)
|
2017-02-22 01:02:31 +03:00
|
|
|
import Data.Functor.Both
|
2017-04-03 23:34:25 +03:00
|
|
|
import Data.List (union, concat, transpose)
|
2017-07-28 21:37:02 +03:00
|
|
|
import Data.Maybe (fromMaybe)
|
|
|
|
import Data.Semigroup ((<>))
|
2017-02-22 01:02:31 +03:00
|
|
|
import qualified Data.Text as T
|
2017-03-09 00:56:08 +03:00
|
|
|
import Data.Text.Encoding (decodeUtf8)
|
2017-02-22 01:02:31 +03:00
|
|
|
import System.FilePath
|
|
|
|
import System.FilePath.Glob
|
2017-04-19 19:12:19 +03:00
|
|
|
import SpecHelpers
|
2017-09-26 19:23:16 +03:00
|
|
|
import Test.Hspec (Spec, describe, it, SpecWith, runIO, parallel, pendingWith)
|
2017-02-22 01:02:31 +03:00
|
|
|
import Test.Hspec.Expectations.Pretty
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = parallel $ do
|
|
|
|
it "lists example fixtures" $ do
|
2017-02-23 21:06:49 +03:00
|
|
|
examples "test/fixtures/go/" `shouldNotReturn` []
|
2017-02-23 20:55:30 +03:00
|
|
|
examples "test/fixtures/javascript/" `shouldNotReturn` []
|
2017-08-15 17:05:49 +03:00
|
|
|
examples "test/fixtures/python/" `shouldNotReturn` []
|
2017-02-24 19:44:50 +03:00
|
|
|
examples "test/fixtures/ruby/" `shouldNotReturn` []
|
2017-03-24 23:56:59 +03:00
|
|
|
examples "test/fixtures/typescript/" `shouldNotReturn` []
|
2017-02-22 01:02:31 +03:00
|
|
|
|
2017-09-26 19:23:16 +03:00
|
|
|
describe "go" $ runTestsIn "test/fixtures/go/" []
|
|
|
|
describe "javascript" $ runTestsIn "test/fixtures/javascript/" []
|
2017-09-26 19:27:04 +03:00
|
|
|
describe "python" $ runTestsIn "test/fixtures/python/" [ ("test/fixtures/python/while-statement.diffB-A.txt", "https://github.com/github/semantic-diff/issues/1359") ]
|
2017-09-26 19:23:16 +03:00
|
|
|
describe "ruby" $ runTestsIn "test/fixtures/ruby/" []
|
|
|
|
describe "typescript" $ runTestsIn "test/fixtures/typescript/" []
|
2017-02-22 01:02:31 +03:00
|
|
|
|
|
|
|
where
|
2017-09-26 19:26:13 +03:00
|
|
|
runTestsIn :: FilePath -> [(FilePath, String)] -> SpecWith ()
|
2017-09-26 19:23:16 +03:00
|
|
|
runTestsIn directory pending = do
|
2017-02-22 03:05:08 +03:00
|
|
|
examples <- runIO $ examples directory
|
2017-09-26 19:23:16 +03:00
|
|
|
traverse_ (runTest pending) examples
|
2017-09-26 19:26:13 +03:00
|
|
|
runTest pending ParseExample{..} = it ("parses " <> file) $ maybe (testParse file parseOutput) pendingWith (lookup parseOutput pending)
|
|
|
|
runTest pending DiffExample{..} = it ("diffs " <> diffOutput) $ maybe (testDiff (both fileA fileB) diffOutput) pendingWith (lookup diffOutput pending)
|
2017-02-22 01:02:31 +03:00
|
|
|
|
2017-02-22 22:31:56 +03:00
|
|
|
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)
|
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
|
2017-02-22 03:30:39 +03:00
|
|
|
examples :: FilePath -> 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
|
|
|
|
|
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)
|
|
|
|
|
2017-02-22 01:02:31 +03:00
|
|
|
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".
|
2017-02-22 01:02:31 +03:00
|
|
|
normalizeName :: FilePath -> FilePath
|
|
|
|
normalizeName path = dropExtension $ dropExtension path
|
|
|
|
|
2017-02-22 19:32:19 +03:00
|
|
|
testParse :: FilePath -> FilePath -> Expectation
|
|
|
|
testParse path expectedOutput = do
|
2017-04-20 02:33:27 +03:00
|
|
|
actual <- verbatim <$> parseFilePath path
|
2017-04-19 23:25:46 +03:00
|
|
|
expected <- verbatim <$> B.readFile expectedOutput
|
2017-02-22 19:32:19 +03:00
|
|
|
actual `shouldBe` expected
|
|
|
|
|
2017-04-19 23:26:47 +03:00
|
|
|
testDiff :: Both FilePath -> FilePath -> Expectation
|
|
|
|
testDiff paths expectedOutput = do
|
2017-04-20 02:33:27 +03:00
|
|
|
actual <- verbatim <$> diffFilePaths paths
|
2017-04-19 23:25:46 +03:00
|
|
|
expected <- verbatim <$> B.readFile expectedOutput
|
2017-02-22 03:05:08 +03:00
|
|
|
actual `shouldBe` expected
|
2017-02-22 01:02:31 +03:00
|
|
|
|
2017-07-28 21:37:02 +03:00
|
|
|
stripWhitespace :: B.ByteString -> B.ByteString
|
2017-03-09 00:56:08 +03:00
|
|
|
stripWhitespace = B.foldl' go B.empty
|
|
|
|
where go acc x | x `B.elem` " \t\n" = acc
|
|
|
|
| otherwise = B.snoc acc x
|
2017-02-22 01:02:31 +03:00
|
|
|
|
2017-07-28 21:37:02 +03:00
|
|
|
-- | A wrapper around 'B.ByteString' with a more readable 'Show' instance.
|
|
|
|
newtype Verbatim = Verbatim B.ByteString
|
2017-09-08 19:35:55 +03:00
|
|
|
deriving (Eq)
|
2017-02-22 01:02:31 +03:00
|
|
|
|
|
|
|
instance Show Verbatim where
|
2017-03-09 00:56:08 +03:00
|
|
|
showsPrec _ (Verbatim byteString) = ('\n':) . (T.unpack (decodeUtf8 byteString) ++)
|
2017-04-19 23:25:46 +03:00
|
|
|
|
2017-07-28 21:37:02 +03:00
|
|
|
verbatim :: B.ByteString -> Verbatim
|
2017-04-19 23:25:46 +03:00
|
|
|
verbatim = Verbatim . stripWhitespace
|