Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit acbacbe

Browse files
author
Patrick Thomson
committed
Use golden tests for the CLI.
1 parent 0292506 commit acbacbe

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

test/Semantic/CLI/Spec.hs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,52 @@ import Control.Monad (when)
44
import qualified Data.ByteString as B
55
import Data.ByteString.Builder
66
import Data.Foldable (for_)
7-
import Semantic.Api hiding (File, Blob, BlobPair)
7+
import Semantic.Api hiding (Blob, BlobPair, File)
88
import Semantic.CLI
99
import Semantic.IO
1010
import Semantic.Task
1111
import Serializing.Format
12+
import System.Directory
13+
import System.IO.Unsafe
14+
import System.Process
1215

1316
import SpecHelpers
17+
import Test.Tasty
18+
import Test.Tasty.Golden
1419

20+
spec :: TestTree
21+
spec = testGroup "Semantic.CLI"
22+
[ testGroup "parseDiffBuilder" $ fmap testForDiffFixture diffFixtures
23+
, testGroup "parseTermBuilder" $ fmap testForParseFixture parseFixtures
24+
]
25+
26+
-- If you're investigating these tests and find this output hard to read
27+
-- the `jd` CLI tool (https://github.com/josephburnett/jd) will print
28+
-- a detailed summary of the differences between these JSON files.
29+
renderDiff :: String -> String -> [String]
30+
renderDiff ref new = unsafePerformIO $ do
31+
useJD <- (isExtensionOf ".json" ref &&) <$> fmap isJust (findExecutable "jd")
32+
pure $ if useJD
33+
then ["jd", "-set", ref, new]
34+
else ["git", "diff", ref, new]
35+
{-# NOINLINE renderDiff #-}
36+
37+
38+
-- PT TODO: reduce duplication
39+
40+
testForDiffFixture (diffRenderer, runDiff, files, expected) =
41+
goldenVsStringDiff
42+
("renders to " <> diffRenderer)
43+
renderDiff
44+
expected
45+
(fmap toLazyByteString . runTaskOrDie $ readBlobPairs (Right files) >>= runDiff)
1546

16-
spec :: Spec
17-
spec = parallel $ do
18-
describe "parseDiffBuilder" $
19-
for_ diffFixtures $ \ (diffRenderer, runDiff, files, expected) ->
20-
it ("renders to " <> diffRenderer <> " with files " <> show files) $ do
21-
output <- runTaskOrDie $ readBlobPairs (Right files) >>= runDiff
22-
runBuilder output `shouldBe'` expected
23-
24-
describe "parseTermBuilder" $
25-
for_ parseFixtures $ \ (format, runParse, files, expected) ->
26-
it ("renders to " <> format <> " with files " <> show files) $ do
27-
output <- runTaskOrDie $ readBlobs (FilesFromPaths files) >>= runParse
28-
runBuilder output `shouldBe'` expected
29-
where
30-
shouldBe' actual' expectedFile = do
31-
let actual = verbatim actual'
32-
expected <- verbatim <$> B.readFile expectedFile
33-
actual `shouldBe` expected
47+
testForParseFixture (format, runParse, files, expected) =
48+
goldenVsStringDiff
49+
("renders to " <> format)
50+
renderDiff
51+
expected
52+
(fmap toLazyByteString . runTaskOrDie $ readBlobs (FilesFromPaths files) >>= runParse)
3453

3554
parseFixtures :: [(String, [Blob] -> TaskEff Builder, [File], FilePath)]
3655
parseFixtures =

test/Spec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Test.Tasty.Options as Tasty
4444
tests :: TaskSession -> [TestTree]
4545
tests session =
4646
[ Integration.Spec.spec session
47+
, Semantic.CLI.Spec.spec
4748
]
4849

4950
-- We can't bring this out of the IO monad until we divest
@@ -93,7 +94,6 @@ legacySpecs args = do
9394
describe "Rewriting.Python" Rewriting.Python.Spec.spec
9495
describe "Tags.Spec" Tags.Spec.spec
9596
describe "Semantic" Semantic.Spec.spec
96-
describe "Semantic.CLI" Semantic.CLI.Spec.spec
9797
describe "Semantic.IO" Semantic.IO.Spec.spec
9898
describe "Parsing" Parsing.Spec.spec
9999

0 commit comments

Comments
 (0)