module GitCommitSpec (spec) where import Converge (gitCommitAll) import qualified Data.Text as T import System.Exit (ExitCode (..)) import System.FilePath (()) import Test.Hspec import TestHelper spec :: Spec spec = do describe "gitCommitAll" $ do it "creates a commit after a file is changed" $ do logger <- testLogger withTempGitRepo $ \repo -> do countBefore <- commitCount repo countBefore `shouldBe` 1 -- initial commit from helper writeFile (repo "hello.txt") "hello world" (code, _, _) <- gitCommitAll logger repo code `shouldBe` ExitSuccess countAfter <- commitCount repo countAfter `shouldBe` (countBefore + 1) it "includes the changed file in the commit" $ do logger <- testLogger withTempGitRepo $ \repo -> do writeFile (repo "data.txt") "some data" (code, _, _) <- gitCommitAll logger repo code `shouldBe` ExitSuccess (_, logOut, _) <- runGitIn repo ["log", "-1", "--name-only", "--oneline"] logOut `shouldSatisfy` ("data.txt" `T.isInfixOf`) it "does not create a new commit when there are no changes" $ do logger <- testLogger withTempGitRepo $ \repo -> do countBefore <- commitCount repo (code, _, _) <- gitCommitAll logger repo -- git commit exits non-zero when there is nothing to commit code `shouldSatisfy` (/= ExitSuccess) countAfter <- commitCount repo countAfter `shouldBe` countBefore it "creates only one commit when called multiple times with the same staged changes" $ do logger <- testLogger withTempGitRepo $ \repo -> do writeFile (repo "single.txt") "single change" (code1, _, _) <- gitCommitAll logger repo code1 `shouldBe` ExitSuccess -- Second call: nothing left to commit countBefore2 <- commitCount repo (code2, _, _) <- gitCommitAll logger repo code2 `shouldSatisfy` (/= ExitSuccess) countAfter2 <- commitCount repo countAfter2 `shouldBe` countBefore2