Răsfoiți Sursa

abstract dependencies for full test coverage

Casey DeLorme 8 ani în urmă
părinte
comite
b29a919e08
2 a modificat fișierele cu 119 adăugiri și 10 ștergeri
  1. 30 10
      staticmd.go
  2. 89 0
      staticmd_test.go

+ 30 - 10
staticmd.go

@@ -9,16 +9,42 @@ import (
 	"time"
 )
 
+func init() {
+	runnable = cmd{}
+}
+
+// logger component for printing status messages to stderr
+type logger interface {
+	Debug(string, ...interface{})
+	Error(string, ...interface{})
+	Info(string, ...interface{})
+}
+
+var stat = os.Stat
+var isNotExist = os.IsNotExist
+
+type runner interface {
+	Run(string, ...string) ([]byte, error)
+}
+
+var runnable runner
+
+type cmd struct{}
+
+func (self cmd) Run(command string, args ...string) ([]byte, error) {
+	return exec.Command(command, args...).Output()
+}
+
 // check that a path exists
 // does not care if it is a directory
 // will not say whether user has rw access, but
 // will throw an error if the user cannot read the parent directory
 func exists(path string) (bool, error) {
-	_, err := os.Stat(path)
+	_, err := stat(path)
 	if err == nil {
 		return true, nil
 	}
-	if os.IsNotExist(err) {
+	if isNotExist(err) {
 		return false, nil
 	}
 	return false, err
@@ -26,9 +52,9 @@ func exists(path string) (bool, error) {
 
 // if within a git repo, gets git version as a short-hash
 // otherwise falls back to a unix timestamp
-func version() string {
+func version(dir string) string {
 	version := strconv.FormatInt(time.Now().Unix(), 10)
-	out, err := exec.Command("sh", "-c", "git rev-parse --short HEAD").Output()
+	out, err := runnable.Run("sh", "-c", "git", "-C", dir, "rev-parse", "--short", "HEAD")
 	if err == nil {
 		version = strings.Trim(string(out), "\n")
 	}
@@ -39,9 +65,3 @@ func version() string {
 func basename(name string) string {
 	return filepath.Base(strings.TrimSuffix(name, filepath.Ext(name)))
 }
-
-type logger interface {
-	Debug(string, ...interface{})
-	Error(string, ...interface{})
-	Info(string, ...interface{})
-}

+ 89 - 0
staticmd_test.go

@@ -0,0 +1,89 @@
+package staticmd
+
+import (
+	"errors"
+	"os"
+	"testing"
+)
+
+func init() {
+	runnable = mockCmd{}
+	stat = func(_ string) (os.FileInfo, error) { return nil, statError }
+	isNotExist = func(_ error) bool { return notExist }
+}
+
+var mockError = errors.New("mock error")
+
+var mockCmdByteArray []byte
+var mockCmdError error
+var statError error
+var notExist bool
+
+type mockCmd struct{}
+
+func (self mockCmd) Run(command string, args ...string) ([]byte, error) {
+	return mockCmdByteArray, mockCmdError
+}
+
+func TestPlacebo(_ *testing.T) {}
+
+func TestCmd(t *testing.T) {
+	t.Parallel()
+	c := cmd{}
+	if _, e := c.Run(""); e == nil {
+		t.FailNow()
+	}
+}
+
+func TestExists(t *testing.T) {
+	t.Parallel()
+
+	// test stat success exists fail
+	if _, e := exists(""); e != nil {
+		t.FailNow()
+	}
+
+	// test stat success exists success
+	notExist = true
+	if _, e := exists(""); e != nil {
+		t.FailNow()
+	}
+
+	// test stat fail
+	statError = mockError
+	if _, e := exists(""); e != nil {
+		t.FailNow()
+	}
+
+	// test stat fail exists success
+	notExist = false
+	if _, e := exists(""); e == nil {
+		t.FailNow()
+	}
+}
+
+func TestVersion(t *testing.T) {
+	t.Parallel()
+
+	// test with byte array
+	compare := "newp"
+	mockCmdByteArray = []byte(compare)
+	if v := version(""); v != compare {
+		t.FailNow()
+	}
+
+	// test with error
+	mockCmdError = mockError
+	if v := version(""); v == compare || len(v) == 0 {
+		t.FailNow()
+	}
+}
+
+func TestBasename(t *testing.T) {
+	t.Parallel()
+
+	f := "/some/long/path"
+	if o := basename(f); len(o) == 0 {
+		t.FailNow()
+	}
+}