1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00

Properly handle null before/after in input JSON

This commit is contained in:
Timothy Clem 2017-05-23 11:00:20 -07:00
parent ea66ee5bc7
commit 316169bab1
6 changed files with 68 additions and 11 deletions

View File

@ -85,6 +85,8 @@ data Blob = Blob
instance FromJSON BlobPair where
parseJSON = withObject "BlobPair" $ \o ->
case (HM.lookup "before" o, HM.lookup "after" o) of
(Just Null, Just after) -> Join . That <$> parseJSON after
(Just before, Just Null) -> Join . This <$> parseJSON before
(Just before, Just after) -> Join <$> (These <$> parseJSON before <*> parseJSON after)
(Just before, Nothing) -> Join . This <$> parseJSON before
(Nothing, Just after) -> Join . That <$> parseJSON after

View File

@ -6,6 +6,7 @@ import Data.Aeson.Types hiding (parse)
import Data.Functor.Both as Both
import Data.Map
import Data.Maybe
import Data.These
import Data.Record
import Data.String
import Info (DefaultFields, HasDefaultFields)
@ -32,25 +33,37 @@ spec = parallel $ do
nullBlob blob `shouldBe` True
describe "readBlobPairsFromHandle" $ do
let a = sourceBlob "method.rb" (Just Ruby) "def foo; end"
let b = sourceBlob "method.rb" (Just Ruby) "def bar(x); end"
it "returns blobs for valid JSON encoded diff input" $ do
h <- openFile "test/fixtures/input/diff.json" ReadMode
blobs <- runCommand (readBlobPairsFromHandle h)
let a = sourceBlob "method.rb" (Just Ruby) "def foo; end"
let b = sourceBlob "method.rb" (Just Ruby) "def bar(x); end"
blobs <- blobsFromFilePath "test/fixtures/input/diff.json"
blobs `shouldBe` [both a b]
it "returns blobs when there's no before" $ do
blobs <- blobsFromFilePath "test/fixtures/input/diff-no-before.json"
blobs `shouldBe` [both (emptySourceBlob "method.rb") b]
it "returns blobs when there's null before" $ do
blobs <- blobsFromFilePath "test/fixtures/input/diff-null-before.json"
blobs `shouldBe` [both (emptySourceBlob "method.rb") b]
it "returns blobs when there's no after" $ do
blobs <- blobsFromFilePath "test/fixtures/input/diff-no-after.json"
blobs `shouldBe` [both a (emptySourceBlob "method.rb")]
it "returns blobs when there's null after" $ do
blobs <- blobsFromFilePath "test/fixtures/input/diff-null-after.json"
blobs `shouldBe` [both a (emptySourceBlob "method.rb")]
it "returns blobs for unsupported language" $ do
h <- openFile "test/fixtures/input/diff-unsupported-language.json" ReadMode
blobs <- runCommand (readBlobPairsFromHandle h)
let a = emptySourceBlob "test.kt"
let b = sourceBlob "test.kt" Nothing "fun main(args: Array<String>) {\nprintln(\"hi\")\n}\n"
blobs `shouldBe` [both a b]
let b' = sourceBlob "test.kt" Nothing "fun main(args: Array<String>) {\nprintln(\"hi\")\n}\n"
blobs `shouldBe` [both (emptySourceBlob "test.kt") b']
it "detects language based on filepath for empty language" $ do
h <- openFile "test/fixtures/input/diff-empty-language.json" ReadMode
blobs <- runCommand (readBlobPairsFromHandle h)
let a = sourceBlob "method.rb" (Just Ruby) "def foo; end"
let b = sourceBlob "method.rb" (Just Ruby) "def bar(x); end"
blobs <- blobsFromFilePath "test/fixtures/input/diff-empty-language.json"
blobs `shouldBe` [both a b]
it "throws on blank input" $ do
@ -119,6 +132,10 @@ spec = parallel $ do
(both "dfac8fd681b0749af137aebf3203e77a06fbafc2" "2e4144eb8c44f007463ec34cb66353f0041161fe")
[ both (emptySourceBlob "methods.rb") methodsBlob ]
methodsBlob = SourceBlob (Source "def foo\nend\n") "ff7bbbe9495f61d9e1e58c597502d152bab1761e" "methods.rb" (Just defaultPlainBlob) (Just Ruby)
blobsFromFilePath path = do
h <- openFile path ReadMode
blobs <- runCommand (readBlobPairsFromHandle h)
pure blobs
data Fixture = Fixture { shas :: Both String, expectedBlobs :: [Both SourceBlob] }

View File

@ -0,0 +1,9 @@
{
"blobs": [{
"before": {
"path": "method.rb",
"content": "def foo; end",
"language": "Ruby"
}
}]
}

View File

@ -0,0 +1,9 @@
{
"blobs": [{
"after": {
"path": "method.rb",
"content": "def bar(x); end",
"language": "Ruby"
}
}]
}

View File

@ -0,0 +1,10 @@
{
"blobs": [{
"before": {
"path": "method.rb",
"content": "def foo; end",
"language": "Ruby"
},
"after": null
}]
}

View File

@ -0,0 +1,10 @@
{
"blobs": [{
"before": null,
"after": {
"path": "method.rb",
"content": "def bar(x); end",
"language": "Ruby"
}
}]
}