forked from thoughtworks/talisman
/
acceptance_test.go
75 lines (62 loc) · 2.39 KB
/
acceptance_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
git "github.com/thoughtworks/talisman/git_testing"
)
func TestNotHavingAnyOutgoingChangesShouldNotFail(t *testing.T) {
assert.Equal(t, 0, run(strings.NewReader("")), "Expected run() to return 0 if no input is available on stdin. This happens when there are no outgoing changes")
}
func TestAddingSimpleFileShouldExitZero(t *testing.T) {
withNewTmpGitRepo(func(gitPath string) {
git.SetupBaselineFiles(gitPath, "simple-file")
exitStatus := runTalisman(gitPath, git.EarliestCommit(gitPath), git.LatestCommit(gitPath))
assert.Equal(t, 0, exitStatus, "Expected run() to return 0 and pass as no suspicious files are in the repo")
})
}
func TestAddingSecretKeyShouldExitOne(t *testing.T) {
withNewTmpGitRepo(func(gitPath string) {
git.SetupBaselineFiles(gitPath, "simple-file")
git.CreateFileWithContents(gitPath, "private.pem", "secret")
git.AddAndcommit(gitPath, "*", "add private key")
exitStatus := runTalisman(gitPath, git.EarliestCommit(gitPath), git.LatestCommit(gitPath))
assert.Equal(t, 1, exitStatus, "Expected run() to return 1 and fail as pem file was present in the repo")
})
}
func TestAddingSecretKeyShouldExitZeroIfPEMFilesAreIgnored(t *testing.T) {
withNewTmpGitRepo(func(gitPath string) {
git.SetupBaselineFiles(gitPath, "simple-file")
git.CreateFileWithContents(gitPath, "private.pem", "secret")
git.CreateFileWithContents(gitPath, ".talismanignore", "*.pem")
git.AddAndcommit(gitPath, "*", "add private key")
exitStatus := runTalisman(gitPath, git.EarliestCommit(gitPath), git.LatestCommit(gitPath))
assert.Equal(t, 0, exitStatus, "Expected run() to return 0 and pass as pem file was ignored")
})
}
func runTalisman(gitPath, oldCommit, newCommit string) int {
os.Chdir(gitPath)
return run(mockStdIn(git.EarliestCommit(gitPath), git.LatestCommit(gitPath)))
}
func withNewTmpGitRepo(gitOp func(gitPath string)) {
WithNewTmpDirNamed("talisman-acceptance-test", func(gitPath string) {
git.Init(gitPath)
gitOp(gitPath)
})
}
type DirOp func(dirName string)
func WithNewTmpDirNamed(dirName string, dop DirOp) {
path, err := ioutil.TempDir(os.TempDir(), dirName)
if err != nil {
panic(err)
}
defer os.RemoveAll(path)
dop(path)
}
func mockStdIn(oldSha string, newSha string) io.Reader {
return strings.NewReader(fmt.Sprintf("master %s master %s\n", newSha, oldSha))
}