Standardize environment passed to Git (#159)

Summary:
When we shell out to `git`, we don’t want the end user’s environment
variables and Git configuration to influence the results. This commit
standardizes those inputs. Standardizing the environment has the side
benefit that the `GIT_DIR` environment variable is not set, which means
that the test suite will work properly when run from the `exec` step of
a Git rebase.

Test Plan:
Tests pass and snapshots are unchanged. Note that
```shell
$ git rebase HEAD --exec 'CI=1 yarn test'
```
works after this commit but not before it.

wchargin-branch: standardize-git-environment
This commit is contained in:
William Chargin 2018-04-27 11:50:32 -07:00 committed by GitHub
parent c7235f6e49
commit f4de3e2067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 10 deletions

View File

@ -12,13 +12,32 @@ export interface Utils {
deterministicCommit(message: string): void; deterministicCommit(message: string): void;
} }
export type GitDriver = (args: string[], options?: ExecOptions) => string; export type GitDriver = (args: string[], env?: {[string]: string}) => string;
// `ExecOptions` is the type of the second argument to `execFileSync`.
// See here for details: https://nodejs.org/api/child_process.html
type ExecOptions = Object;
export function localGit(repositoryPath: string): GitDriver { export function localGit(repositoryPath: string): GitDriver {
return function git(args: string[], options?: ExecOptions): string { return function git(args: string[], env?: {[string]: string}): string {
// We standardize the environment variables shown to Git, using
// Git's test suite [1] as inspiration. It is particularly important
// that `GIT_DIR` be unset from the parent process environment.
// Otherwise, these tests have the wrong behavior when running in an
// `exec` step of a Git rebase.
//
// [1]: https://github.com/git/git/blob/1f1cddd558b54bb0ce19c8ace353fd07b758510d/t/test-lib.sh#L90
const fullEnv = {
// Standardize output.
LANG: "C",
LC_ALL: "C",
PAGER: "cat",
TZ: "UTC",
// Short-circuit editing.
EDITOR: "true", // (this is `true` the command-line program)
GIT_MERGE_AUTOEDIT: "no",
// Ignore global Git settings, for test isolation.
GIT_CONFIG_NOSYSTEM: "1",
GIT_ATTR_NOSYSTEM: "1",
...(env || {}),
};
const options = {env: fullEnv};
return execFileSync( return execFileSync(
"git", "git",
["-C", repositoryPath, ...args], ["-C", repositoryPath, ...args],
@ -56,11 +75,8 @@ export function makeUtils(repositoryPath: string): Utils {
message, message,
], ],
{ {
env: { GIT_AUTHOR_DATE: "2001-02-03T04:05:06",
TZ: "UTC", GIT_COMMITTER_DATE: "2002-03-04T05:06:07",
GIT_AUTHOR_DATE: "2001-02-03T04:05:06",
GIT_COMMITTER_DATE: "2002-03-04T05:06:07",
},
} }
); );
}, },