hooky-1.0.9: test/Hooky/E2ESpec.hs
{-# LANGUAGE MultilineStrings #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
module Hooky.E2ESpec (spec) where
import Control.Monad (forM_, (<=<))
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text.IO qualified as Text
import Hooky.Internal.Output (allOutputFormats, renderOutputFormat)
import Hooky.TestUtils.Git (TestGitClient, withGitRepo)
import Hooky.TestUtils.Hooky (HookyExe (..))
import Skeletest
import Skeletest.Predicate qualified as P
import System.Directory (
createDirectory,
createDirectoryLink,
createFileLink,
removeFile,
renameFile,
)
import System.Exit (ExitCode (..))
import System.FilePath ((</>))
import System.IO qualified as IO
import System.Process qualified as Process
import System.Timeout (timeout)
spec :: Spec
spec = do
describe "git commit" $ do
it "runs hooky" . withGitRepo $ \git -> do
writeFile ".hooky.kdl" hookyConfigEofFixer
writeFile "good.txt" "good\n"
writeFile "bad.txt" "bad"
runHooky ["install"] `shouldSatisfy` P.returns (P.eq ExitSuccess)
git.exec ["add", ".hooky.kdl", "good.txt", "bad.txt"]
(code0, _, stderr0) <- git.run ["commit", "-m", "initial commit"]
code0 `shouldBe` ExitFailure 1
stderr0 `shouldSatisfy` P.hasInfix "1 hook failed"
stderr0 `shouldNotSatisfy` P.hasInfix "[warn]"
writeFile "bad.txt" "bad\n"
git.exec ["add", "bad.txt"]
(code1, _, stderr1) <- git.run ["commit", "-m", "commit"]
code1 `shouldBe` ExitSuccess
stderr1 `shouldSatisfy` P.hasInfix "1 hook passed"
stderr1 `shouldNotSatisfy` P.hasInfix "[warn]"
describe "hooky run" $ do
it "defaults to --stash --staged" $ do
withGitRepo $ \git -> do
commitHookyEofFixer git
writeFile "good.txt" "good\n"
writeFile "bad-in-stage.txt" "bad"
writeFile "bad-untracked.txt" "bad"
git.exec ["add", "good.txt", "bad-in-stage.txt"]
writeFile "bad-in-stage.txt" "good\n"
runHooky ["run"] `shouldSatisfy` P.returns (P.eq (ExitFailure 1))
withGitRepo $ \git -> do
commitHookyEofFixer git
writeFile "good.txt" "good\n"
writeFile "bad-modified.txt" "good\n"
writeFile "bad-untracked.txt" "bad"
git.exec ["add", "good.txt", "bad-modified.txt"]
writeFile "bad-modified.txt" "bad"
runHooky ["run"] `shouldSatisfy` P.returns (P.eq ExitSuccess)
it "errors if multiple file selection flags are passed" $ do
(code, _, stderr) <- readHooky ["run", "--all", "--modified"]
code `shouldBe` ExitFailure 1
stderr `shouldBe` "Expected exactly one of: FILES, --modified, --staged, --all, --prev\n"
it "errors if file argument and flags are both passed" $ do
(code, _, stderr) <- readHooky ["run", "--all", "file.txt"]
code `shouldBe` ExitFailure 1
stderr `shouldBe` "Expected exactly one of: FILES, --modified, --staged, --all, --prev\n"
it "filters out deleted files" $ do
withGitRepo $ \git -> do
commitHookyEofFixer git
writeFile "bad.txt" "bad"
git.exec ["add", "bad.txt"] >> git.exec ["commit", "-m", "test"]
removeFile "bad.txt"
runHooky ["run", "--all"] `shouldSatisfy` P.returns (P.eq ExitSuccess)
it "works on files with whitespace in name" $ do
withGitRepo $ \git -> do
commitHookyEofFixer git
writeFile "foo bar.txt" "bad"
git.exec ["add", "foo bar.txt"]
runHooky ["run", "-a"] `shouldSatisfy` P.returns (P.eq (ExitFailure 1))
writeFile "foo bar.txt" "good\n"
git.exec ["add", "foo bar.txt"]
runHooky ["run", "-a"] `shouldSatisfy` P.returns (P.eq ExitSuccess)
it "works after `git mv`" $ do
withGitRepo $ \git -> do
commitHookyEofFixer git
writeFile "good.txt" ""
git.exec ["add", "good.txt"] >> git.exec ["commit", "-m", "test"]
git.exec ["mv", "good.txt", "good-moved.txt"]
runHooky ["run"] `shouldSatisfy` P.returns (P.eq ExitSuccess)
it "stashes intent-to-add files" $ do
withGitRepo $ \git -> do
commitHookyEofFixer git
writeFile "bad.txt" "bad"
git.exec ["add", "-N", "bad.txt"]
let checkIntentToAdd = do
(_, stdout, _) <- git.run ["diff-files", "--name-only", "--diff-filter=A"]
stdout `shouldBe` "bad.txt"
readFile "bad.txt" `shouldSatisfy` P.returns (P.eq "bad")
checkIntentToAdd
runHooky ["run", "--stash", "--all"] `shouldSatisfy` P.returns (P.eq ExitSuccess)
checkIntentToAdd
it "stashes untracked files" $ do
withGitRepo $ \git -> do
commitHookyEofFixer git
writeFile "bad.txt" "bad"
let checkUntracked = do
(_, stdout, _) <- git.run ["ls-files", "--others", "--exclude-standard"]
stdout `shouldBe` "bad.txt"
readFile "bad.txt" `shouldSatisfy` P.returns (P.eq "bad")
checkUntracked
runHooky ["run", "--stash", "--all"] `shouldSatisfy` P.returns (P.eq ExitSuccess)
checkUntracked
it "stashes unstaged renamed files" $ do
withGitRepo $ \git -> do
commitHookyEofFixer git
writeFile "test1.txt" "test\n"
writeFile "test2.txt" "test\n"
git.exec ["add", "test1.txt", "test2.txt"]
git.exec ["commit", "-m", "test"]
-- Rename test{1,2}.txt => test{1,2}-new.txt;
-- Intent-to-add `test1-new.txt`, leave `test2-new.txt` untracked
renameFile "test1.txt" "test1-new.txt"
renameFile "test2.txt" "test2-new.txt"
git.exec ["add", "-N", "test2-new.txt"]
(_, before, _) <- git.run ["status"]
runHooky ["run"] `shouldSatisfy` P.returns (P.eq ExitSuccess)
(_, after, _) <- git.run ["status"]
after `shouldBe` before
it "error if .hooky.kdl is to be stashed" $ do
withGitRepo $ \git -> do
commitHookyEofFixer git
writeFile ".hooky.kdl" $ hookyConfigEofFixer <> "\n\n\n"
(code, _, stderr) <- readHooky ["run", "--stash", "--staged"]
code `shouldBe` ExitFailure 1
stderr `shouldBe` ".hooky.kdl has changes, stage it first\n"
forM_ allOutputFormats $ \format -> do
let flag = Text.unpack $ "--format=" <> renderOutputFormat format
scrubDuration s =
case Text.breakOn "duration: " s of
(_, "") -> s
(pre, post) -> pre <> "duration: XX.XXXs" <> scrubDuration (Text.dropWhile (/= '\n') post)
-- https://github.com/brandonchinn178/hooky/issues/9
skip "until we can set --max-parallel-hooks 1" . it ("outputs " <> flag) $ do
(code, stdout, _) <-
withGitRepo $ \git -> do
writeFile ".hooky.kdl" $
"""
hook hooky_pass {
command hooky lint
files *.pass
}
hook hooky_fail {
command hooky lint
files *.fail
}
hook hooky_skip {
command hooky lint
files *.skip
}
lint_rules {
- end_of_file_fixer
}
"""
writeFile "file.pass" "good\n"
writeFile "file.fail" "bad"
git.exec ["add", "."]
readHooky ["run", "--all", flag]
code `shouldBe` ExitFailure 1
scrubDuration stdout `shouldSatisfy` P.matchesSnapshot
it "runs on all files in directory" $ do
withGitRepo $ \git -> do
commitHookyEofFixer git
createDirectory "foo"
writeFile "foo/bar.txt" "asdf"
(code, stdout, _) <- readHooky ["run", "foo"]
code `shouldBe` ExitFailure 1
stdout `shouldSatisfy` P.hasInfix "foo/bar.txt"
it "treats symlinked files as a symlink" $ do
withGitRepo $ \git -> do
commitHookyEofFixer git
writeFile "foo.txt" "bad"
createFileLink "foo" "foo-link"
(code, _, _) <- readHooky ["run", "foo-link"]
code `shouldBe` ExitSuccess
it "treats symlinked directories as a symlink" $ do
withGitRepo $ \git -> do
commitHookyEofFixer git
createDirectory "foo"
createDirectoryLink "foo" "foo-link"
writeFile "foo/bar.txt" "bad"
(code, _, _) <- readHooky ["run", "foo-link"]
code `shouldBe` ExitSuccess
it "runs on all files in implicit symlinked directory" $ do
withGitRepo $ \git -> do
writeFile ".hooky.kdl" $
"""
hook hooky {
command hooky lint {
// Bug appears only when `pass_files` is `file`
pass_files file
}
files *
}
lint_rules {
- end_of_file_fixer
}
"""
git.exec ["add", ".hooky.kdl"]
git.exec ["commit", "-m", "test"]
createDirectory "foo"
createDirectoryLink "foo" "foo-link"
writeFile "foo/bar.txt" ""
git.exec ["add", "foo", "foo-link"]
(code, _, _) <- readHooky ["run"]
code `shouldBe` ExitSuccess
describe "skipping hooks with env var" $ do
let runHookyWithEnv env args = do
HookyExe hooky <- getFixture
Process.withCreateProcess (Process.proc "env" $ [env, hooky] <> args) $ \_ _ _ h -> do
Process.waitForProcess h
it "skips entire hook" $ do
withGitRepo $ \git -> do
writeFile ".hooky.kdl" $
"""
hook hooky1 {
command hooky lint
files *.good
}
hook hooky2 {
command hooky lint
files *.bad
}
lint_rules {
- end_of_file_fixer
}
"""
writeFile "test.good" "test\n"
writeFile "test.bad" "test"
git.exec ["add", "."]
runHookyWithEnv "SKIP=hooky2" ["run", "--all"] `shouldSatisfy` P.returns (P.eq ExitSuccess)
it "skips hooky lint rules" $ do
withGitRepo $ \git -> do
writeFile ".hooky.kdl" $
"""
hook hooky {
command hooky lint
files *
}
lint_rules {
- end_of_file_fixer
}
"""
writeFile "test.txt" "test"
git.exec ["add", "."]
runHookyWithEnv "SKIP=end_of_file_fixer" ["run", "--all"] `shouldSatisfy` P.returns (P.eq ExitSuccess)
describe "hooky lint" $ do
it "works on large repos using pass_files=file" $ do
withGitRepo $ \git -> do
writeFile ".hooky.kdl" $
"""
hook hooky {
command hooky lint {
pass_files file
}
files *
}
lint_rules {
- end_of_file_fixer
}
"""
-- 10,000 files, each with 200 character paths
-- should trigger "posix_spawnp: resource exhausted (Argument list too long)"
forM_ [1 .. 100 :: Int] $ \x -> do
let dir = Text.unpack $ Text.replicate 100 "a" <> Text.show x
createDirectory dir
forM_ [1 .. 100 :: Int] $ \y -> do
let file = Text.unpack $ Text.replicate 100 "b" <> Text.show y
writeFile (dir </> file) ""
git.exec ["add", "."]
git.exec ["commit", "-m", "test", "-q"]
maybe (failTest "Timed out") pure <=< timeout 2_000_000 $ do
code <- runHooky ["run", "-a", "--format=verbose"]
code `shouldBe` ExitSuccess
runHooky :: [String] -> IO ExitCode
runHooky args = do
HookyExe hooky <- getFixture
Process.withCreateProcess (Process.proc hooky args) $ \_ _ _ h -> do
Process.waitForProcess h
readHooky :: [String] -> IO (ExitCode, Text, Text)
readHooky args = do
HookyExe hooky <- getFixture
(code, stdoutS, stderrS) <- Process.readProcessWithExitCode hooky args ""
let (stdout, stderr) = (Text.pack stdoutS, Text.pack stderrS)
Text.hPutStrLn IO.stdout stdout
Text.hPutStrLn IO.stderr stderr
pure (code, stripControlChars stdout, stripControlChars stderr)
where
stripControlChars s =
case Text.breakOn "\x1b" s of
(_, "") -> s
(pre, post) -> pre <> stripControlChars (Text.drop 1 . Text.dropWhile (/= 'm') $ post)
commitHookyEofFixer :: TestGitClient -> IO ()
commitHookyEofFixer git = do
writeFile ".hooky.kdl" hookyConfigEofFixer
git.exec ["add", ".hooky.kdl"]
git.exec ["commit", "-m", "test"]
hookyConfigEofFixer :: String
hookyConfigEofFixer =
"""
hook hooky {
command hooky lint
files *
}
lint_rules {
- end_of_file_fixer
}
"""