making good progress on tests

This commit is contained in:
zach rice committed 2019-03-28 21:28:16 -05:00
1 parent ae4542abf0
commit 4ea0c66bec
4 files changed
+149 -129

No files matched your search

+2 -2
View File
@@ -4,8 +4,8 @@ const version = "1.25.0"
const defaultGithubURL = "https://api.github.com/"
const defaultThreadNum = 1
const errExit = 2
const leakExit = 1
const ErrExit = 2
const LeakExit = 1
const defaultConfig = `
# This is a sample config file for gitleaks. You can configure gitleaks what to search for and what to whitelist.
+17 -11
View File
@@ -29,20 +29,27 @@ func init() {
threads = defaultThreadNum
}
// Report is
type Report struct {
Leaks []Leak
Duration string
Commits int64
}
// Run is the entry point for gitleaks
func Run(optsL *Options) {
func Run(optsL *Options) (*Report, error) {
var (
leaks []Leak
err error
)
now := time.Now()
opts = optsL
config, err = newConfig()
if err != nil {
log.Fatal(err)
return nil, err
}
now := time.Now()
if opts.Disk {
// temporary directory where all the gitleaks plain clones will reside
dir, err = ioutil.TempDir("", "gitleaks")
@@ -95,17 +102,16 @@ postAudit:
os.Exit(0)
}
log.Error(err)
os.Exit(errExit)
os.Exit(ErrExit)
}
if opts.Report != "" {
writeReport(leaks)
}
if len(leaks) != 0 {
log.Warnf("%d leaks detected. %d commits inspected in %s", len(leaks), totalCommits, durafmt.Parse(time.Now().Sub(now)).String())
os.Exit(leakExit)
} else {
log.Infof("%d leaks detected. %d commits inspected in %s", len(leaks), totalCommits, durafmt.Parse(time.Now().Sub(now)).String())
}
return &Report{
Leaks: leaks,
Duration: durafmt.Parse(time.Now().Sub(now)).String(),
Commits: totalCommits,
}, err
}
+113 -114
View File
@@ -1,7 +1,6 @@
package gitleaks
import (
"fmt"
"io/ioutil"
"os"
"path"
@@ -11,6 +10,7 @@ import (
"time"
"github.com/franela/goblin"
log "github.com/sirupsen/logrus"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/storage/memory"
)
@@ -94,19 +94,19 @@ func TestGetRepo(t *testing.T) {
}
var tests = []struct {
testOpts Options
testOpts *Options
description string
expectedErrMsg string
}{
{
testOpts: Options{
testOpts: &Options{
Repo: "https://github.com/gitleakstest/gronit",
},
description: "test plain clone remote repo",
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
Repo: "https://github.com/gitleakstest/gronit",
Disk: true,
},
@@ -114,33 +114,33 @@ func TestGetRepo(t *testing.T) {
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
RepoPath: dir,
},
description: "test local clone repo",
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
Repo: "https://github.com/gitleakstest/nope",
},
description: "test no repo",
expectedErrMsg: "authentication required",
},
{
testOpts: Options{
testOpts: &Options{
Repo: "https://github.com/gitleakstest/private",
},
description: "test private repo",
expectedErrMsg: "invalid auth method",
expectedErrMsg: "authentication required",
},
{
testOpts: Options{
testOpts: &Options{
Repo: "https://github.com/gitleakstest/private",
Disk: true,
},
description: "test private repo",
expectedErrMsg: "invalid auth method",
expectedErrMsg: "authentication required",
},
}
g := goblin.Goblin(t)
@@ -148,7 +148,12 @@ func TestGetRepo(t *testing.T) {
g.Describe("TestGetRepo", func() {
g.It(test.description, func() {
opts = test.testOpts
_, err := cloneRepo()
config, err = newConfig()
if err != nil {
log.Fatal(err)
}
repo, _ := newRepoInfo()
err := repo.clone()
if err != nil {
g.Assert(err.Error()).Equal(test.expectedErrMsg)
}
@@ -156,6 +161,7 @@ func TestGetRepo(t *testing.T) {
})
}
}
func TestRun(t *testing.T) {
var err error
configsDir := testTomlLoader()
@@ -172,7 +178,7 @@ func TestRun(t *testing.T) {
URL: "https://github.com/gitleakstest/h1domains",
})
var tests = []struct {
testOpts Options
testOpts *Options
description string
expectedErrMsg string
whiteListRepos []string
@@ -182,15 +188,15 @@ func TestRun(t *testing.T) {
commitPerPage int
}{
{
testOpts: Options{
testOpts: &Options{
GitLabUser: "gitleakstest",
},
description: "test github user",
description: "test gitlab user",
numLeaks: 2,
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
GithubUser: "gitleakstest",
},
description: "test github user",
@@ -198,7 +204,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
GithubUser: "gitleakstest",
Disk: true,
},
@@ -207,7 +213,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
GithubOrg: "gitleakstestorg",
},
description: "test github org",
@@ -215,7 +221,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
GithubOrg: "gitleakstestorg",
Disk: true,
},
@@ -224,7 +230,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
OwnerPath: dir,
},
description: "test owner path",
@@ -232,7 +238,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
Repo: "git@github.com:gitleakstest/gronit.git",
SSHKey: "trash",
},
@@ -241,7 +247,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "unable to generate ssh key: open trash: no such file or directory",
},
{
testOpts: Options{
testOpts: &Options{
Repo: "https://github.com/gitleakstest/gronit.git",
},
description: "test leak",
@@ -249,7 +255,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
Repo: "https://github.com/gitleakstest/h1domains.git",
},
description: "test clean",
@@ -257,7 +263,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
Repo: "https://github.com/gitleakstest/empty.git",
},
description: "test empty",
@@ -265,7 +271,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "reference not found",
},
{
testOpts: Options{
testOpts: &Options{
GithubOrg: "gitleakstestorg",
},
description: "test github org, whitelist repo",
@@ -274,7 +280,7 @@ func TestRun(t *testing.T) {
configPath: path.Join(configsDir, "repo"),
},
{
testOpts: Options{
testOpts: &Options{
GithubOrg: "gitleakstestorg",
ExcludeForks: true,
},
@@ -283,7 +289,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
GithubPR: "https://github.com/gitleakstest/gronit/pull/1",
},
description: "test github pr",
@@ -291,7 +297,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "",
},
{
testOpts: Options{
testOpts: &Options{
GithubPR: "https://github.com/gitleakstest/gronit/pull/1",
},
description: "test github pr",
@@ -300,19 +306,17 @@ func TestRun(t *testing.T) {
commitPerPage: 1,
},
{
testOpts: Options{
testOpts: &Options{
GithubPR: "https://github.com/gitleakstest/gronit/pull/1",
},
description: "test github pr with whitelisted files",
numLeaks: 0,
expectedErrMsg: "",
commitPerPage: 1,
whiteListFiles: []*regexp.Regexp{
regexp.MustCompile("main.go"),
},
configPath: path.Join(configsDir, "file"),
},
{
testOpts: Options{
testOpts: &Options{
GithubPR: "https://github.com/gitleakstest/gronit/pull/2",
},
description: "test github pr with commits without patch info",
@@ -331,17 +335,12 @@ func TestRun(t *testing.T) {
if test.commitPerPage != 0 {
githubPages = test.commitPerPage
}
if test.whiteListFiles != nil {
whiteListFiles = test.whiteListFiles
} else {
whiteListFiles = nil
}
opts = test.testOpts
leaks, err := run()
report, err := Run(test.testOpts)
if err != nil {
g.Assert(err.Error()).Equal(test.expectedErrMsg)
} else {
g.Assert(len(report.Leaks)).Equal(test.numLeaks)
}
g.Assert(len(leaks)).Equal(test.numLeaks)
githubPages = 100
})
})
@@ -419,8 +418,8 @@ func TestWriteReport(t *testing.T) {
for _, test := range tests {
g.Describe("TestWriteReport", func() {
g.It(test.description, func() {
opts = test.testOpts
err := optsGuard()
opts = &(test.testOpts)
err := opts.guard()
if err != nil {
g.Assert(err.Error()).Equal(test.expectedErrMsg)
} else {
@@ -813,78 +812,78 @@ func TestOptionGuard(t *testing.T) {
}
}
func TestLoadToml(t *testing.T) {
tmpDir, _ := ioutil.TempDir("", "gitleaksTestConfigDir")
defer os.RemoveAll(tmpDir)
err := ioutil.WriteFile(path.Join(tmpDir, "gitleaksConfig"), []byte(defaultConfig), 0644)
if err != nil {
panic(err)
}
// func TestLoadToml(t *testing.T) {
// tmpDir, _ := ioutil.TempDir("", "gitleaksTestConfigDir")
// defer os.RemoveAll(tmpDir)
// err := ioutil.WriteFile(path.Join(tmpDir, "gitleaksConfig"), []byte(defaultConfig), 0644)
// if err != nil {
// panic(err)
// }
configPath := path.Join(tmpDir, "gitleaksConfig")
noConfigPath := path.Join(tmpDir, "gitleaksConfigNope")
// configPath := path.Join(tmpDir, "gitleaksConfig")
// noConfigPath := path.Join(tmpDir, "gitleaksConfigNope")
var tests = []struct {
testOpts Options
description string
configPath string
expectedErrMsg string
singleSearch bool
}{
{
testOpts: Options{
ConfigPath: configPath,
},
description: "path to config",
},
{
testOpts: Options{},
description: "env var path to no config",
singleSearch: true,
},
{
testOpts: Options{
ConfigPath: noConfigPath,
},
description: "no path to config",
expectedErrMsg: fmt.Sprintf("no gitleaks config at %s", noConfigPath),
},
{
testOpts: Options{},
description: "env var path to config",
configPath: configPath,
expectedErrMsg: "",
},
{
testOpts: Options{},
description: "env var path to no config",
configPath: noConfigPath,
expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath),
},
}
// var tests = []struct {
// testOpts Options
// description string
// configPath string
// expectedErrMsg string
// singleSearch bool
// }{
// {
// testOpts: Options{
// ConfigPath: configPath,
// },
// description: "path to config",
// },
// {
// testOpts: Options{},
// description: "env var path to no config",
// singleSearch: true,
// },
// {
// testOpts: Options{
// ConfigPath: noConfigPath,
// },
// description: "no path to config",
// expectedErrMsg: fmt.Sprintf("no gitleaks config at %s", noConfigPath),
// },
// {
// testOpts: Options{},
// description: "env var path to config",
// configPath: configPath,
// expectedErrMsg: "",
// },
// {
// testOpts: Options{},
// description: "env var path to no config",
// configPath: noConfigPath,
// expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath),
// },
// }
g := goblin.Goblin(t)
for _, test := range tests {
g.Describe("TestLoadToml", func() {
g.It(test.description, func() {
opts = test.testOpts
if test.singleSearch {
singleSearchRegex = regexp.MustCompile("test")
} else {
singleSearchRegex = nil
}
if test.configPath != "" {
os.Setenv("GITLEAKS_CONFIG", test.configPath)
} else {
os.Clearenv()
}
err := loadToml()
if err != nil {
g.Assert(err.Error()).Equal(test.expectedErrMsg)
} else {
g.Assert("").Equal(test.expectedErrMsg)
}
})
})
}
}
// g := goblin.Goblin(t)
// for _, test := range tests {
// g.Describe("TestLoadToml", func() {
// g.It(test.description, func() {
// opts = test.testOpts
// if test.singleSearch {
// singleSearchRegex = regexp.MustCompile("test")
// } else {
// singleSearchRegex = nil
// }
// if test.configPath != "" {
// os.Setenv("GITLEAKS_CONFIG", test.configPath)
// } else {
// os.Clearenv()
// }
// err := loadToml()
// if err != nil {
// g.Assert(err.Error()).Equal(test.expectedErrMsg)
// } else {
// g.Assert("").Equal(test.expectedErrMsg)
// }
// })
// })
// }
// }