Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

add test coverage #9

Merged
merged 1 commit into from
Dec 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Container struct {
Excludes []Exclude
}

// Config holds the full configation loaded from the toml config file.
// Config holds the full configuration loaded from the toml config file.
type Config struct {
AWS AWS
Storage Storage
Expand All @@ -86,12 +86,9 @@ func (cfg *Config) getContainer(c string) (Container, error) {
return Container{}, errors.New("Container not found")
}

func (cfg *Config) compileRegexes() {
func (cfg *Config) compileRegex() {
for i, c := range cfg.Containers {
cmp, err := regexp.Compile(c.Regex)
if err != nil {
log.Fatalln("Could not compile regex for container:", c.Name)
}
cmp := regexp.MustCompile(c.Regex)
cfg.Containers[i].CompiledRegex = cmp
}
}
Expand Down Expand Up @@ -400,7 +397,7 @@ func run(c *cli.Context) error {
if err != nil {
log.Fatalln(err.Error())
}
cfg.compileRegexes()
cfg.compileRegex()

setupDirectory(cfg)

Expand Down
32 changes: 30 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ func TestReadConfig(t *testing.T) {
if err != nil {
t.Error("could not parse toml file when should have", err.Error())
}
// create bad file
f := "bad.toml"
f1, err := os.Create(f)
f1.Write([]byte("bad toml file"))
f1.Close()
err = readConfig(f, cfg)
if err == nil {
t.Errorf("should have error reading bad toml file")
}
// clean up test file
os.Remove(f)
}

func TestGetContainers(t *testing.T) {
Expand Down Expand Up @@ -54,9 +65,26 @@ func TestCleanupFiles(t *testing.T) {
cleanupFiles(f1Path, f2Path, f3Path)

if _, err := os.Stat(f1Path); !os.IsNotExist(err) {
t.Fatalf("test file (%s) still exists", f1Path)
t.Errorf("test file (%s) still exists", f1Path)
}
if _, err := os.Stat(f2Path); !os.IsNotExist(err) {
t.Fatalf("test file (%s) still exists", f2Path)
t.Errorf("test file (%s) still exists", f2Path)
}
}

func TestCompileRegex(t *testing.T) {
c1 := Container{
Name: "Container 1",
Regex: `([\d.]+) - \[([^\]]*)\] - - \[([^\]]*)\]`,
}
cfg := &Config{
Containers: []Container{c1},
}
if cfg.Containers[0].CompiledRegex != nil {
t.Errorf("compile regex is not nil but compile hasn't been called yet")
}
cfg.compileRegex()
if cfg.Containers[0].CompiledRegex == nil {
t.Errorf("compile regex is nil")
}
}