Skip to content

Commit

Permalink
VCS tests: Make Git deterministic
Browse files Browse the repository at this point in the history
Don't read configuration files, set a consistent date for commits, and
pass a tempdir as the home directory.

This will prevent the test suite from attempting to GPG-sign commits in
the tests!
  • Loading branch information
9999years authored and Mikolaj committed Nov 27, 2024
1 parent 074ae04 commit 7fc108d
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions cabal-install/tests/UnitTests/Distribution/Client/VCS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -850,23 +850,22 @@ vcsTestDriverGit
{ vcsVCS = vcs'
, vcsRepoRoot = repoRoot
, vcsIgnoreFiles = Set.empty
, vcsInit =
, vcsInit = do
createDirectoryIfMissing True home
gitconfigExists <- doesFileExist gitconfigPath
unless gitconfigExists $ do
writeFile gitconfigPath gitconfig
git $ ["init"] ++ verboseArg
, vcsAddFile = \_ filename ->
git ["add", filename]
, vcsCommitChanges = \_state -> do
git $
[ "-c"
, "user.name=A"
, "-c"
, "[email protected]"
, "commit"
[ "commit"
, "--all"
, "--message=a patch"
, "--author=A <[email protected]>"
]
++ verboseArg
commit <- git' ["log", "--format=%H", "-1"]
commit <- git' ["rev-parse", "HEAD"]
let commit' = takeWhile (not . isSpace) commit
return (Just commit')
, vcsTagState = \_ tagname ->
Expand Down Expand Up @@ -906,21 +905,35 @@ vcsTestDriverGit
updateSubmodulesAndCleanup
}
where
home = tmpDir </> "home"
gitconfigPath = home </> ".gitconfig"
-- Git 2.38.1 and newer fails to clone from local paths with `fatal: transport 'file'
-- not allowed` unless `protocol.file.allow=always` is set.
--
-- This is not safe in general, but it's fine in the test suite.
--
-- See: https://github.blog/open-source/git/git-security-vulnerabilities-announced/#fn-67904-1
-- See: https://git-scm.com/docs/git-config#Documentation/git-config.txt-protocolallow
gitconfig =
"[protocol.file]\n\
\allow = always\n\
\[user]\n\
\name = Puppy Doggy\n\
\email = [email protected]\n"

vcs' =
vcs
{ vcsProgram =
(vcsProgram vcs)
{ programDefaultArgs =
programDefaultArgs (vcsProgram vcs)
++ [ "-c"
, "protocol.file.allow=always"
{ programOverrideEnv =
programOverrideEnv (vcsProgram vcs)
++ [ -- > Whether to skip reading settings from the system-wide $(prefix)/etc/gitconfig file.
("GIT_CONFIG_NOSYSTEM", Just "1")
, ("GIT_CONFIG_GLOBAL", Just gitconfigPath)
, -- Setting the author and committer dates makes commit hashes deterministic between test runs.
("GIT_AUTHOR_DATE", Just "1998-04-30T18:25:03-0400")
, ("GIT_COMMITTER_DATE", Just "1998-04-30T18:25:00-0400")
, ("HOME", Just home)
]
}
}
Expand Down

0 comments on commit 7fc108d

Please sign in to comment.