Skip to content

Commit

Permalink
chore: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smlx committed Feb 8, 2021
1 parent 7428b3c commit e7d9d06
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
8 changes: 8 additions & 0 deletions helper_test.go
@@ -0,0 +1,8 @@
package main

import "go.uber.org/zap"

// NextVersion is a test fixture to expose the private nextVersion() function.
func NextVersion(log *zap.Logger, path string) (string, error) {
return nextVersion(log, path)
}
75 changes: 75 additions & 0 deletions main_test.go
@@ -0,0 +1,75 @@
package main_test

import (
"fmt"
"io/ioutil"
"os/exec"
"testing"

ccv "github.com/smlx/ccv"
"go.uber.org/zap"
)

func TestNextVersion(t *testing.T) {
var testCases = map[string]struct {
gitCmds [][]string
expect string
}{
"none": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat: initial commit"},
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "chore: not much"},
}, expect: "v0.1.0"},
"patch": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat: initial commit"},
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "fix: minor bug"},
}, expect: "v0.1.1"},
"minor": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat: initial commit"},
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "feat: cool new feature"},
}, expect: "v0.2.0"},
"major": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat: initial commit"},
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "feat: major refactor\nBREAKING CHANGE: new stuff"},
}, expect: "v1.0.0"},
}
log, err := zap.NewDevelopment()
if err != nil {
t.Fatalf("couldn't get logger: %v", err)
}
for name, tc := range testCases {
t.Run(name, func(tt *testing.T) {
// create test dir
dir, err := ioutil.TempDir("", "example")
if err != nil {
tt.Fatalf("couldn't get a tempdir: %v", err)
}
// init git repo
initCmd := exec.Command("git", "init")
initCmd.Dir = dir
initCmd.Env = []string{fmt.Sprintf("HOME=%s", dir)}
if err = initCmd.Run(); err != nil {
tt.Fatalf("couldn't git init: %v", err)
}
for _, c := range tc.gitCmds {
cmd := exec.Command("git", c...)
cmd.Dir = dir
cmd.Env = []string{fmt.Sprintf("HOME=%s", dir)}
if err = cmd.Run(); err != nil {
tt.Fatalf("couldn't run git command `%s`: %v", c, err)
}
}
tt.Log(dir)
next, err := ccv.NextVersion(log, dir)
if err != nil {
tt.Fatalf("error from main.nextVersion(): %v", err)
}
if next != tc.expect {
tt.Fatalf("expected: %v, got: %v", tc.expect, next)
}
})
}
}

0 comments on commit e7d9d06

Please sign in to comment.