2018-03-09 20:07:34 +03:00
|
|
|
module Semantic.IO.Spec (spec) where
|
2017-03-01 01:55:57 +03:00
|
|
|
|
2017-07-28 21:37:02 +03:00
|
|
|
import Prelude hiding (readFile)
|
2018-05-23 00:01:17 +03:00
|
|
|
|
2018-05-22 23:53:03 +03:00
|
|
|
import Control.Concurrent.Async
|
2018-05-23 00:01:17 +03:00
|
|
|
import Foreign
|
|
|
|
import Foreign.C.Types (CBool (..))
|
|
|
|
import Semantic.IO
|
|
|
|
import System.Exit (ExitCode (..))
|
|
|
|
import System.IO (IOMode (..))
|
|
|
|
import Parsing.TreeSitter
|
|
|
|
import System.Timeout
|
2018-05-22 23:53:03 +03:00
|
|
|
|
|
|
|
import qualified TreeSitter.Language as TS
|
|
|
|
import qualified TreeSitter.Node as TS
|
|
|
|
import qualified TreeSitter.Parser as TS
|
|
|
|
import qualified TreeSitter.Tree as TS
|
2018-03-13 21:10:50 +03:00
|
|
|
|
2018-06-13 19:34:35 +03:00
|
|
|
import SpecHelpers
|
2018-03-13 21:10:50 +03:00
|
|
|
|
2017-03-01 01:55:57 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = parallel $ do
|
2017-04-21 01:13:28 +03:00
|
|
|
describe "readFile" $ do
|
|
|
|
it "returns a blob for extant files" $ do
|
2018-06-05 01:26:47 +03:00
|
|
|
Just blob <- readFile (File "semantic.cabal" Unknown)
|
2018-02-26 21:16:56 +03:00
|
|
|
blobPath blob `shouldBe` "semantic.cabal"
|
2017-04-21 01:13:28 +03:00
|
|
|
|
2017-12-11 21:51:52 +03:00
|
|
|
it "throws for absent files" $ do
|
2018-06-05 01:26:47 +03:00
|
|
|
readFile (File "this file should not exist" Unknown) `shouldThrow` anyIOException
|
2017-04-21 01:13:28 +03:00
|
|
|
|
2017-05-17 22:53:05 +03:00
|
|
|
describe "readBlobPairsFromHandle" $ do
|
2018-06-05 01:26:47 +03:00
|
|
|
let a = sourceBlob "method.rb" Ruby "def foo; end"
|
|
|
|
let b = sourceBlob "method.rb" Ruby "def bar(x); end"
|
2017-05-17 22:47:45 +03:00
|
|
|
it "returns blobs for valid JSON encoded diff input" $ do
|
2018-04-18 23:55:21 +03:00
|
|
|
blobs <- blobsFromFilePath "test/fixtures/cli/diff.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairDiffing a b]
|
2017-05-17 22:47:45 +03:00
|
|
|
|
2017-05-23 21:00:20 +03:00
|
|
|
it "returns blobs when there's no before" $ do
|
2018-04-18 23:55:21 +03:00
|
|
|
blobs <- blobsFromFilePath "test/fixtures/cli/diff-no-before.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairInserting b]
|
2017-05-23 21:00:20 +03:00
|
|
|
|
|
|
|
it "returns blobs when there's null before" $ do
|
2018-04-18 23:55:21 +03:00
|
|
|
blobs <- blobsFromFilePath "test/fixtures/cli/diff-null-before.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairInserting b]
|
2017-05-23 21:00:20 +03:00
|
|
|
|
|
|
|
it "returns blobs when there's no after" $ do
|
2018-04-18 23:55:21 +03:00
|
|
|
blobs <- blobsFromFilePath "test/fixtures/cli/diff-no-after.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairDeleting a]
|
2017-05-23 21:00:20 +03:00
|
|
|
|
|
|
|
it "returns blobs when there's null after" $ do
|
2018-04-18 23:55:21 +03:00
|
|
|
blobs <- blobsFromFilePath "test/fixtures/cli/diff-null-after.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairDeleting a]
|
2017-05-23 21:00:20 +03:00
|
|
|
|
|
|
|
|
2017-05-19 00:04:44 +03:00
|
|
|
it "returns blobs for unsupported language" $ do
|
2018-05-14 17:23:29 +03:00
|
|
|
h <- openFileForReading "test/fixtures/cli/diff-unsupported-language.json"
|
2017-06-15 17:17:41 +03:00
|
|
|
blobs <- readBlobPairsFromHandle h
|
2018-06-05 01:26:47 +03:00
|
|
|
let b' = sourceBlob "test.kt" Unknown "fun main(args: Array<String>) {\nprintln(\"hi\")\n}\n"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairInserting b']
|
2017-05-19 00:04:44 +03:00
|
|
|
|
|
|
|
it "detects language based on filepath for empty language" $ do
|
2018-04-18 23:55:21 +03:00
|
|
|
blobs <- blobsFromFilePath "test/fixtures/cli/diff-empty-language.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairDiffing a b]
|
2017-05-19 00:04:44 +03:00
|
|
|
|
|
|
|
it "throws on blank input" $ do
|
2018-05-14 17:23:29 +03:00
|
|
|
h <- openFileForReading "test/fixtures/cli/blank.json"
|
2017-06-15 17:17:41 +03:00
|
|
|
readBlobPairsFromHandle h `shouldThrow` (== ExitFailure 1)
|
2017-05-17 22:47:45 +03:00
|
|
|
|
2017-05-19 00:04:44 +03:00
|
|
|
it "throws if language field not given" $ do
|
2018-05-14 17:23:29 +03:00
|
|
|
h <- openFileForReading "test/fixtures/cli/diff-no-language.json"
|
2017-06-15 17:17:41 +03:00
|
|
|
readBlobsFromHandle h `shouldThrow` (== ExitFailure 1)
|
2017-05-19 00:04:44 +03:00
|
|
|
|
2017-12-11 23:59:14 +03:00
|
|
|
it "throws if null on before and after" $ do
|
2018-05-14 17:23:29 +03:00
|
|
|
h <- openFileForReading "test/fixtures/cli/diff-null-both-sides.json"
|
2017-12-11 23:59:14 +03:00
|
|
|
readBlobPairsFromHandle h `shouldThrow` (== ExitFailure 1)
|
|
|
|
|
2018-05-22 23:53:03 +03:00
|
|
|
describe "cancelable parsing" $
|
|
|
|
it "should be cancelable asynchronously" $ do
|
|
|
|
p <- TS.ts_parser_new
|
|
|
|
|
2018-05-23 00:25:17 +03:00
|
|
|
churn <- async $ do
|
2018-05-23 00:01:17 +03:00
|
|
|
TS.ts_parser_loop_until_cancelled p nullPtr nullPtr 0
|
|
|
|
pure True
|
2018-05-22 23:53:03 +03:00
|
|
|
|
2018-06-05 20:13:08 +03:00
|
|
|
res <- timeout 1500 (wait churn)
|
2018-05-22 23:53:03 +03:00
|
|
|
res `shouldBe` Nothing
|
|
|
|
|
2018-05-23 00:01:17 +03:00
|
|
|
TS.ts_parser_set_enabled p (CBool 0)
|
2018-06-05 20:13:08 +03:00
|
|
|
done <- timeout 1500 (wait churn)
|
2018-05-22 23:53:03 +03:00
|
|
|
|
2018-05-23 00:01:17 +03:00
|
|
|
done `shouldBe` (Just True)
|
2018-05-22 23:53:03 +03:00
|
|
|
|
2018-05-23 00:01:17 +03:00
|
|
|
TS.ts_parser_delete p
|
2018-05-22 23:53:03 +03:00
|
|
|
|
2017-05-17 23:34:09 +03:00
|
|
|
describe "readBlobsFromHandle" $ do
|
|
|
|
it "returns blobs for valid JSON encoded parse input" $ do
|
2018-05-14 17:23:29 +03:00
|
|
|
h <- openFileForReading "test/fixtures/cli/parse.json"
|
2017-06-15 17:17:41 +03:00
|
|
|
blobs <- readBlobsFromHandle h
|
2018-06-05 01:26:47 +03:00
|
|
|
let a = sourceBlob "method.rb" Ruby "def foo; end"
|
2017-05-17 23:34:09 +03:00
|
|
|
blobs `shouldBe` [a]
|
|
|
|
|
2017-05-19 00:04:44 +03:00
|
|
|
it "throws on blank input" $ do
|
2018-05-14 17:23:29 +03:00
|
|
|
h <- openFileForReading "test/fixtures/cli/blank.json"
|
2017-06-15 17:17:41 +03:00
|
|
|
readBlobsFromHandle h `shouldThrow` (== ExitFailure 1)
|
2017-05-17 23:34:09 +03:00
|
|
|
|
2017-06-15 06:20:12 +03:00
|
|
|
where blobsFromFilePath path = do
|
2018-05-14 17:23:29 +03:00
|
|
|
h <- openFileForReading path
|
2017-06-15 17:17:41 +03:00
|
|
|
blobs <- readBlobPairsFromHandle h
|
2017-05-23 21:00:20 +03:00
|
|
|
pure blobs
|