diff --git a/src/Command/Files.hs b/src/Command/Files.hs index f143ec5cc..744e29f3c 100644 --- a/src/Command/Files.hs +++ b/src/Command/Files.hs @@ -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 diff --git a/test/CommandSpec.hs b/test/CommandSpec.hs index 58dbbc67e..01f8a0a1e 100644 --- a/test/CommandSpec.hs +++ b/test/CommandSpec.hs @@ -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) {\nprintln(\"hi\")\n}\n" - blobs `shouldBe` [both a b] + let b' = sourceBlob "test.kt" Nothing "fun main(args: Array) {\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] } diff --git a/test/fixtures/input/diff-no-after.json b/test/fixtures/input/diff-no-after.json new file mode 100644 index 000000000..fffbb3458 --- /dev/null +++ b/test/fixtures/input/diff-no-after.json @@ -0,0 +1,9 @@ +{ + "blobs": [{ + "before": { + "path": "method.rb", + "content": "def foo; end", + "language": "Ruby" + } + }] +} diff --git a/test/fixtures/input/diff-no-before.json b/test/fixtures/input/diff-no-before.json new file mode 100644 index 000000000..4212240a6 --- /dev/null +++ b/test/fixtures/input/diff-no-before.json @@ -0,0 +1,9 @@ +{ + "blobs": [{ + "after": { + "path": "method.rb", + "content": "def bar(x); end", + "language": "Ruby" + } + }] +} diff --git a/test/fixtures/input/diff-null-after.json b/test/fixtures/input/diff-null-after.json new file mode 100644 index 000000000..ecca51a8b --- /dev/null +++ b/test/fixtures/input/diff-null-after.json @@ -0,0 +1,10 @@ +{ + "blobs": [{ + "before": { + "path": "method.rb", + "content": "def foo; end", + "language": "Ruby" + }, + "after": null + }] +} diff --git a/test/fixtures/input/diff-null-before.json b/test/fixtures/input/diff-null-before.json new file mode 100644 index 000000000..320e0acb8 --- /dev/null +++ b/test/fixtures/input/diff-null-before.json @@ -0,0 +1,10 @@ +{ + "blobs": [{ + "before": null, + "after": { + "path": "method.rb", + "content": "def bar(x); end", + "language": "Ruby" + } + }] +}