Skip to content

Commit 00bc142

Browse files
Narrow public interface of GitTesting
Authored-by: Owen Nelson <owen.nelson@thoughtworks.com>
1 parent 7a3072e commit 00bc142

File tree

6 files changed

+58
-58
lines changed

6 files changed

+58
-58
lines changed

cmd/acceptance_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func runTalismanInPrePushMode(git *git_testing.GitTesting) int {
402402

403403
func runTalisman(git *git_testing.GitTesting) int {
404404
wd, _ := os.Getwd()
405-
os.Chdir(git.GetRoot())
405+
os.Chdir(git.Root())
406406
defer func() { os.Chdir(wd) }()
407407
prompter := prompt.NewPrompt()
408408
promptContext := prompt.NewPromptContext(false, prompter)

cmd/checksum_cmd_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestChecksumCalculatorShouldExitSuccess(t *testing.T) {
1414
git.CreateFileWithContents("private.pem", "secret")
1515
git.CreateFileWithContents("another/private.pem", "secret")
1616
git.CreateFileWithContents("sample.txt", "password")
17-
os.Chdir(git.GetRoot())
17+
os.Chdir(git.Root())
1818

1919
checksumCmd := NewChecksumCmd([]string{"*.txt"})
2020
assert.Equal(t, 0, checksumCmd.Run(), "Expected run() to return 0 as given patterns are found and .talsimanrc is suggested")
@@ -28,7 +28,7 @@ func TestChecksumCalculatorShouldExitFailure(t *testing.T) {
2828
git.CreateFileWithContents("private.pem", "secret")
2929
git.CreateFileWithContents("another/private.pem", "secret")
3030
git.CreateFileWithContents("sample.txt", "password")
31-
os.Chdir(git.GetRoot())
31+
os.Chdir(git.Root())
3232

3333
checksumCmd := NewChecksumCmd([]string{"*.java"})
3434
assert.Equal(t, 1, checksumCmd.Run(), "Expected run() to return 1 as given patterns are found and .talsimanrc is suggested")
@@ -38,7 +38,7 @@ func TestChecksumCalculatorShouldExitFailure(t *testing.T) {
3838

3939
func TestChecksumCalculatorShouldExitFailureWhenHasherIsEmpty(t *testing.T) {
4040
git_testing.DoInTempGitRepo(func(git *git_testing.GitTesting) {
41-
checksumCmd := ChecksumCmd{[]string{"*.java"}, nil, git.GetRoot()}
41+
checksumCmd := ChecksumCmd{[]string{"*.java"}, nil, git.Root()}
4242
assert.Equal(t, 1, checksumCmd.Run(), "Expected run() to return 1 because hasher failed to start")
4343
})
4444
}

cmd/scanner_cmd_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ func TestScannerCmdRunsSuccessfully(t *testing.T) {
1414
git.SetupBaselineFiles("simple-file")
1515
git.CreateFileWithContents("some-dir/should-be-included.txt", "safeContents")
1616
git.AddAndcommit("*", "Start of Scan")
17-
os.Chdir(git.GetRoot())
17+
os.Chdir(git.Root())
1818

19-
scannerCmd := NewScannerCmd(true, &talismanrc.TalismanRC{}, git.GetRoot())
19+
scannerCmd := NewScannerCmd(true, &talismanrc.TalismanRC{}, git.Root())
2020
scannerCmd.Run()
2121
assert.Equal(t, 0, scannerCmd.exitStatus(), "Expected ScannerCmd.exitStatus() to return 0 since no secret is found")
2222
})
@@ -31,9 +31,9 @@ func TestScannerCmdDetectsSecretAndFails(t *testing.T) {
3131
git.AddAndcommit("*", "Removed secret")
3232
git.CreateFileWithContents("some-dir/safe-file.txt", "safeContents")
3333
git.AddAndcommit("*", "Start of Scan")
34-
os.Chdir(git.GetRoot())
34+
os.Chdir(git.Root())
3535

36-
scannerCmd := NewScannerCmd(false, &talismanrc.TalismanRC{}, git.GetRoot())
36+
scannerCmd := NewScannerCmd(false, &talismanrc.TalismanRC{}, git.Root())
3737
scannerCmd.Run()
3838
assert.Equal(t, 1, scannerCmd.exitStatus(), "Expected ScannerCmd.exitStatus() to return 1 since secret present in history")
3939
})
@@ -45,10 +45,10 @@ func TestScannerCmdAddingSecretKeyShouldExitZeroIfFileIsWithinConfiguredScope(t
4545
git.CreateFileWithContents("go.sum", awsAccessKeyIDExample)
4646
git.CreateFileWithContents("go.mod", awsAccessKeyIDExample)
4747
git.AddAndcommit("*", "go sum file")
48-
os.Chdir(git.GetRoot())
48+
os.Chdir(git.Root())
4949

5050
tRC := &talismanrc.TalismanRC{ScopeConfig: []talismanrc.ScopeConfig{{ScopeName: "go"}}}
51-
scannerCmd := NewScannerCmd(false, tRC, git.GetRoot())
51+
scannerCmd := NewScannerCmd(false, tRC, git.Root())
5252
scannerCmd.Run()
5353
assert.Equal(t, 0, scannerCmd.exitStatus(), "Expected ScannerCmd.exitStatus() to return 0 since no secret is found")
5454
})
@@ -60,14 +60,14 @@ func TestScannerCmdDetectsSecretAndIgnoresWhileRunningInIgnoreHistoryModeWithVal
6060
git.CreateFileWithContents("go.sum", awsAccessKeyIDExample)
6161
git.CreateFileWithContents("go.mod", awsAccessKeyIDExample)
6262
git.AddAndcommit("*", "go sum file")
63-
os.Chdir(git.GetRoot())
63+
os.Chdir(git.Root())
6464

6565
tRC := &talismanrc.TalismanRC{
6666
FileIgnoreConfig: []talismanrc.FileIgnoreConfig{
6767
{FileName: "go.sum", Checksum: "582093519ae682d5170aecc9b935af7e90ed528c577ecd2c9dd1fad8f4924ab9"},
6868
{FileName: "go.mod", Checksum: "8a03b9b61c505ace06d590d2b9b4f4b6fa70136e14c26875ced149180e00d1af"},
6969
}}
70-
scannerCmd := NewScannerCmd(true, tRC, git.GetRoot())
70+
scannerCmd := NewScannerCmd(true, tRC, git.Root())
7171
scannerCmd.Run()
7272
assert.Equal(t, 0, scannerCmd.exitStatus(), "Expected ScannerCmd.exitStatus() to return 0 since secrets file ignore is enabled")
7373
})
@@ -79,14 +79,14 @@ func TestScannerCmdDetectsSecretWhileRunningNormalScanMode(t *testing.T) {
7979
git.CreateFileWithContents("go.sum", awsAccessKeyIDExample)
8080
git.CreateFileWithContents("go.mod", awsAccessKeyIDExample)
8181
git.AddAndcommit("*", "go sum file")
82-
os.Chdir(git.GetRoot())
82+
os.Chdir(git.Root())
8383

8484
tRC := &talismanrc.TalismanRC{
8585
FileIgnoreConfig: []talismanrc.FileIgnoreConfig{
8686
{FileName: "go.sum", Checksum: "582093519ae682d5170aecc9b935af7e90ed528c577ecd2c9dd1fad8f4924ab9"},
8787
{FileName: "go.mod", Checksum: "8a03b9b61c505ace06d590d2b9b4f4b6fa70136e14c26875ced149180e00d1af"},
8888
}}
89-
scannerCmd := NewScannerCmd(false, tRC, git.GetRoot())
89+
scannerCmd := NewScannerCmd(false, tRC, git.Root())
9090
scannerCmd.Run()
9191
assert.Equal(t, 1, scannerCmd.exitStatus(), "Expected ScannerCmd.exitStatus() to return 1 because file ignore is disabled when scanning history")
9292
})

git_testing/git_testing.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var Logger *logrus.Entry
1616

1717
// GitTesting provides an API for manipulating a git repository during tests
1818
type GitTesting struct {
19-
gitRoot string
19+
root string
2020
}
2121

2222
type GitOperation func(*GitTesting)
@@ -32,36 +32,41 @@ func DoInTempGitRepo(gitOperation GitOperation) {
3232
func Init() *GitTesting {
3333
fs := afero.NewMemMapFs()
3434
path, _ := afero.TempDir(fs, afero.GetTempDir(fs, "talisman-test"), "")
35-
return InitAt(path)
35+
return initAt(path)
3636
}
3737

38-
// InitAt creates a GitTesting based at the specified path
39-
func InitAt(gitRoot string) *GitTesting {
38+
// initAt creates a GitTesting based at the specified path
39+
func initAt(gitRoot string) *GitTesting {
4040
os.MkdirAll(gitRoot, 0777)
4141
testingRepo := &GitTesting{gitRoot}
42-
output := testingRepo.ExecCommand("git", "init", ".")
42+
output := testingRepo.execCommand("git", "init", ".")
4343
logrus.Debugf("Git init result %v", string(output))
44-
testingRepo.ExecCommand("git", "config", "user.email", "talisman-test-user@example.com")
45-
testingRepo.ExecCommand("git", "config", "user.name", "Talisman Test User")
46-
testingRepo.ExecCommand("git", "config", "commit.gpgsign", "false")
44+
testingRepo.execCommand("git", "config", "user.email", "talisman-test-user@example.com")
45+
testingRepo.execCommand("git", "config", "user.name", "Talisman Test User")
46+
testingRepo.execCommand("git", "config", "commit.gpgsign", "false")
4747
testingRepo.removeHooks()
4848
return testingRepo
4949
}
5050

51+
// Clean removes the directory containing the git repository represented by a GitTesting
52+
func (git *GitTesting) Clean() {
53+
os.RemoveAll(git.root)
54+
}
55+
5156
func (git *GitTesting) SetupBaselineFiles(filenames ...string) {
52-
Logger.Debugf("Creating %v in %s\n", filenames, git.gitRoot)
57+
Logger.Debugf("Creating %v in %s\n", filenames, git.root)
5358
for _, filename := range filenames {
5459
git.CreateFileWithContents(filename, lorem.Sentence(8, 10), lorem.Sentence(8, 10))
5560
}
5661
git.AddAndcommit("*", "initial commit")
5762
}
5863

5964
func (git *GitTesting) EarliestCommit() string {
60-
return git.ExecCommand("git", "rev-list", "--max-parents=0", "HEAD")
65+
return git.execCommand("git", "rev-list", "--max-parents=0", "HEAD")
6166
}
6267

6368
func (git *GitTesting) LatestCommit() string {
64-
return git.ExecCommand("git", "rev-parse", "HEAD")
69+
return git.execCommand("git", "rev-parse", "HEAD")
6570
}
6671

6772
func (git *GitTesting) CreateFileWithContents(filePath string, contents ...string) string {
@@ -125,11 +130,11 @@ func (git *GitTesting) AddAndcommit(fileName string, message string) {
125130
}
126131

127132
func (git *GitTesting) Add(fileName string) {
128-
git.ExecCommand("git", "add", fileName)
133+
git.execCommand("git", "add", fileName)
129134
}
130135

131136
func (git *GitTesting) Commit(fileName string, message string) {
132-
git.ExecCommand("git", "commit", "-m", message)
137+
git.execCommand("git", "commit", "-m", message)
133138
}
134139

135140
// GetBlobDetails returns git blob details for a path
@@ -152,14 +157,14 @@ func (git *GitTesting) GetBlobDetails(fileName string) string {
152157
return objectHashAndFilename
153158
}
154159

155-
// ExecCommand executes a command with given arguments in the git repo directory
156-
func (git *GitTesting) ExecCommand(commandName string, args ...string) string {
160+
// execCommand executes a command with given arguments in the git repo directory
161+
func (git *GitTesting) execCommand(commandName string, args ...string) string {
157162
var output []byte
158163
git.doInGitRoot(func() {
159164
result := exec.Command(commandName, args...)
160165
var err error
161166
output, err = result.Output()
162-
summaryMessage := fmt.Sprintf("Command: %s %v\nWorkingDirectory: %s\nOutput %s\nError: %v", commandName, args, git.gitRoot, string(output), err)
167+
summaryMessage := fmt.Sprintf("Command: %s %v\nWorkingDirectory: %s\nOutput %s\nError: %v", commandName, args, git.root, string(output), err)
163168
git.die(summaryMessage, err)
164169
Logger.Debug(summaryMessage)
165170
})
@@ -178,23 +183,18 @@ func (git *GitTesting) die(msg string, err error) {
178183

179184
func (git *GitTesting) doInGitRoot(operation func()) {
180185
wd, _ := os.Getwd()
181-
os.Chdir(git.gitRoot)
186+
os.Chdir(git.root)
182187
defer func() { os.Chdir(wd) }()
183188
operation()
184189
}
185190

186-
// GetRoot returns the root directory of the git-testing repo
187-
func (git *GitTesting) GetRoot() string {
188-
return git.gitRoot
191+
// Root returns the root directory of the git-testing repo
192+
func (git *GitTesting) Root() string {
193+
return git.root
189194
}
190195

191196
// removeHooks removes all file-system hooks from git-test repo.
192197
// We do this to prevent any user-installed hooks from interfering with tests.
193198
func (git *GitTesting) removeHooks() {
194-
git.ExecCommand("rm", "-rf", ".git/hooks/")
195-
}
196-
197-
// Clean removes the directory containing the git repository represented by a GitTesting
198-
func (git *GitTesting) Clean() {
199-
os.RemoveAll(git.gitRoot)
199+
git.execCommand("rm", "-rf", ".git/hooks/")
200200
}

git_testing/git_testing_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ func init() {
2424

2525
func TestInitializingANewRepoSetsUpFolderAndGitStructures(t *testing.T) {
2626
DoInTempGitRepo(func(repo *GitTesting) {
27-
assert.True(t, exists(repo.gitRoot), "GitTesting initialization should create the directory structure required")
28-
assert.True(t, isGitRepo(repo.gitRoot), "Repo root does not contain the .git folder")
27+
assert.True(t, exists(repo.root), "GitTesting initialization should create the directory structure required")
28+
assert.True(t, isGitRepo(repo.root), "Repo root does not contain the .git folder")
2929
})
3030
}
3131

3232
func TestSettingUpBaselineFilesSetsUpACommitInRepo(t *testing.T) {
3333
DoInTempGitRepo(func(repo *GitTesting) {
3434
repo.SetupBaselineFiles("a.txt", filepath.Join("alice", "bob", "b.txt"))
35-
verifyPresenceOfGitRepoWithCommits(t, 1, repo.gitRoot)
35+
verifyPresenceOfGitRepoWithCommits(t, 1, repo.root)
3636
})
3737
}
3838

@@ -43,7 +43,7 @@ func TestEditingFilesInARepoWorks(t *testing.T) {
4343
content := repo.FileContents("a.txt")
4444
assert.True(t, strings.HasSuffix(string(content), "monkey see.\nmonkey do."))
4545
repo.AddAndcommit("a.txt", "modified content")
46-
verifyPresenceOfGitRepoWithCommits(t, 2, repo.gitRoot)
46+
verifyPresenceOfGitRepoWithCommits(t, 2, repo.root)
4747
})
4848
}
4949

@@ -53,7 +53,7 @@ func TestRemovingFilesInARepoWorks(t *testing.T) {
5353
repo.RemoveFile("a.txt")
5454
assert.False(t, exists(filepath.Join("data", "testLocation1", "a.txt")), "Unexpected. Deleted file a.txt still exists inside the repo")
5555
repo.AddAndcommit("a.txt", "removed it")
56-
verifyPresenceOfGitRepoWithCommits(t, 2, repo.gitRoot)
56+
verifyPresenceOfGitRepoWithCommits(t, 2, repo.root)
5757
})
5858
}
5959

gitrepo/gitrepo_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestNewRepoGetsCreatedWithAbsolutePath(t *testing.T) {
3131

3232
func TestNoAdditionsBetweenSameRef(t *testing.T) {
3333
doInRepoWithCommit(func(git *git_testing.GitTesting) {
34-
assert.Len(t, RepoLocatedAt(git.GetRoot()).AdditionsWithinRange("HEAD", "HEAD"), 0,
34+
assert.Len(t, RepoLocatedAt(git.Root()).AdditionsWithinRange("HEAD", "HEAD"), 0,
3535
"There should be no additions between a ref and itself.")
3636
})
3737
}
@@ -42,7 +42,7 @@ func TestGetDiffForStagedFiles(t *testing.T) {
4242
git.CreateFileWithContents("new.txt", "created contents")
4343
git.Add("a.txt")
4444
git.Add("new.txt")
45-
repo := RepoLocatedAt(git.GetRoot())
45+
repo := RepoLocatedAt(git.Root())
4646
additions := repo.GetDiffForStagedFiles()
4747

4848
if assert.Len(t, additions, 2) {
@@ -80,7 +80,7 @@ func TestGetDiffForStagedFilesWithSpacesInPath(t *testing.T) {
8080
doInRepoWithCommit(func(git *git_testing.GitTesting) {
8181
git.AppendFileContent("folder b/c.txt", "New content.\n", "Spanning multiple lines, even.")
8282
git.Add("folder b/c.txt")
83-
repo := RepoLocatedAt(git.GetRoot())
83+
repo := RepoLocatedAt(git.Root())
8484
additions := repo.GetDiffForStagedFiles()
8585

8686
if assert.Len(t, additions, 1) {
@@ -109,7 +109,7 @@ func TestAdditionsReturnsEditsAndAdds(t *testing.T) {
109109
git.CreateFileWithContents("new.txt", "created contents")
110110
git.AddAndcommit("*", "added to lorem-ipsum content with my own stuff!")
111111

112-
additions := RepoLocatedAt(git.GetRoot()).additionsInLastCommit()
112+
additions := RepoLocatedAt(git.Root()).additionsInLastCommit()
113113
assert.Len(t, additions, 2)
114114
assert.True(t, strings.HasSuffix(string(additions[0].Data), "New content.\nSpanning multiple lines, even."))
115115
})
@@ -120,15 +120,15 @@ func TestNewlyAddedFilesAreCountedAsChanges(t *testing.T) {
120120
git.CreateFileWithContents("h", "Hello")
121121
git.CreateFileWithContents("foo/bar/w", ", World!")
122122
git.AddAndcommit("*", "added hello world")
123-
assert.Len(t, RepoLocatedAt(git.GetRoot()).additionsInLastCommit(), 2)
123+
assert.Len(t, RepoLocatedAt(git.Root()).additionsInLastCommit(), 2)
124124
})
125125
}
126126

127127
func TestOutgoingContentOfNewlyAddedFilesIsAvailableInChanges(t *testing.T) {
128128
doInRepoWithCommit(func(git *git_testing.GitTesting) {
129129
git.CreateFileWithContents("foo/bar/w", "new contents")
130130
git.AddAndcommit("*", "added new files")
131-
repo := RepoLocatedAt(git.GetRoot())
131+
repo := RepoLocatedAt(git.Root())
132132
assert.Len(t, repo.additionsInLastCommit(), 1)
133133
assert.True(t, strings.HasSuffix(string(repo.AdditionsWithinRange("HEAD~1", "HEAD")[0].Data), "new contents"))
134134
})
@@ -138,7 +138,7 @@ func TestOutgoingContentOfModifiedFilesIsAvailableInChanges(t *testing.T) {
138138
doInRepoWithCommit(func(git *git_testing.GitTesting) {
139139
git.AppendFileContent("a.txt", "New content.\n", "Spanning multiple lines, even.")
140140
git.AddAndcommit("a.txt", "added to lorem-ipsum content with my own stuff!")
141-
repo := RepoLocatedAt(git.GetRoot())
141+
repo := RepoLocatedAt(git.Root())
142142
assert.Len(t, repo.additionsInLastCommit(), 1)
143143
assert.True(t, strings.HasSuffix(string(repo.additionsInLastCommit()[0].Data), "New content.\nSpanning multiple lines, even."))
144144
})
@@ -152,7 +152,7 @@ func TestMultipleOutgoingChangesToTheSameFileAreAvailableInAdditions(t *testing.
152152
git.AppendFileContent("a.txt", "More new content.\n")
153153
git.AddAndcommit("a.txt", "added some more new content")
154154

155-
repo := RepoLocatedAt(git.GetRoot())
155+
repo := RepoLocatedAt(git.Root())
156156
assert.Len(t, repo.additionsInLastCommit(), 1)
157157
assert.True(t, strings.HasSuffix(string(repo.AdditionsWithinRange("HEAD~2", "HEAD")[0].Data), "New content.\nMore new content.\n"))
158158
})
@@ -162,14 +162,14 @@ func TestContentOfDeletedFilesIsNotAvailableInChanges(t *testing.T) {
162162
doInRepoWithCommit(func(git *git_testing.GitTesting) {
163163
git.RemoveFile("a.txt")
164164
git.AddAndcommit("a.txt", "Deleted this file. After all, it only had lorem-ipsum content.")
165-
assert.Equal(t, 0, len(RepoLocatedAt(git.GetRoot()).additionsInLastCommit()),
165+
assert.Equal(t, 0, len(RepoLocatedAt(git.Root()).additionsInLastCommit()),
166166
"There should be no additions because there is only an outgoing deletion")
167167
})
168168
}
169169

170170
func TestDiffContainingBinaryFileChangesDoesNotBlowUp(t *testing.T) {
171171
doInRepoWithCommit(func(git *git_testing.GitTesting) {
172-
repo := RepoLocatedAt(git.GetRoot())
172+
repo := RepoLocatedAt(git.Root())
173173
exec.Command("cp", "./pixel.jpg", repo.root).Run()
174174
git.AddAndcommit("pixel.jpg", "Testing binary diff.")
175175
assert.Len(t, repo.additionsInLastCommit(), 1)
@@ -185,7 +185,7 @@ func TestStagedAdditionsIncludeStagedFiles(t *testing.T) {
185185
git.AppendFileContent("a.txt", "More new content\n")
186186
git.AppendFileContent("alice/bob/b.txt", "New content to b\n")
187187

188-
stagedAdditions := RepoLocatedAt(git.GetRoot()).StagedAdditions()
188+
stagedAdditions := RepoLocatedAt(git.Root()).StagedAdditions()
189189
assert.Len(t, stagedAdditions, 1)
190190
assert.Equal(t, "a.txt", string(stagedAdditions[0].Name))
191191
assert.Equal(t, "New content.\n", string(stagedAdditions[0].Data))
@@ -197,7 +197,7 @@ func TestStagedAdditionsIncludeStagedNewFiles(t *testing.T) {
197197
git.CreateFileWithContents("new.txt", "New content.\n")
198198
git.Add("new.txt")
199199

200-
stagedAdditions := RepoLocatedAt(git.GetRoot()).StagedAdditions()
200+
stagedAdditions := RepoLocatedAt(git.Root()).StagedAdditions()
201201
assert.Len(t, stagedAdditions, 1)
202202
assert.Equal(t, "new.txt", string(stagedAdditions[0].Name))
203203
assert.Equal(t, "New content.\n", string(stagedAdditions[0].Data))
@@ -209,7 +209,7 @@ func TestStagedAdditionsShouldNotIncludeDeletedFiles(t *testing.T) {
209209
git.RemoveFile("a.txt")
210210
git.Add(".")
211211

212-
stagedAdditions := RepoLocatedAt(git.GetRoot()).StagedAdditions()
212+
stagedAdditions := RepoLocatedAt(git.Root()).StagedAdditions()
213213
assert.Len(t, stagedAdditions, 0)
214214
})
215215
}

0 commit comments

Comments
 (0)