소스 검색

Add testutil/tempfile

Improve error messages raised by assert.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
Daniel Nephin 9 년 전
부모
커밋
7c556849aa
2개의 변경된 파일60개의 추가작업 그리고 9개의 파일을 삭제
  1. 24 9
      pkg/testutil/assert/assert.go
  2. 36 0
      pkg/testutil/tempfile/tempfile.go

+ 24 - 9
pkg/testutil/assert/assert.go

@@ -18,7 +18,7 @@ type TestingT interface {
 // they are not equal.
 func Equal(t TestingT, actual, expected interface{}) {
 	if expected != actual {
-		fatal(t, fmt.Sprintf("Expected '%v' (%T) got '%v' (%T)", expected, expected, actual, actual))
+		fatal(t, "Expected '%v' (%T) got '%v' (%T)", expected, expected, actual, actual)
 	}
 }
 
@@ -26,12 +26,12 @@ func Equal(t TestingT, actual, expected interface{}) {
 // the same items.
 func EqualStringSlice(t TestingT, actual, expected []string) {
 	if len(actual) != len(expected) {
-		t.Fatalf("Expected (length %d): %q\nActual (length %d): %q",
+		fatal(t, "Expected (length %d): %q\nActual (length %d): %q",
 			len(expected), expected, len(actual), actual)
 	}
 	for i, item := range actual {
 		if item != expected[i] {
-			t.Fatalf("Slices differ at element %d, expected %q got %q",
+			fatal(t, "Slices differ at element %d, expected %q got %q",
 				i, expected[i], item)
 		}
 	}
@@ -40,7 +40,7 @@ func EqualStringSlice(t TestingT, actual, expected []string) {
 // NilError asserts that the error is nil, otherwise it fails the test.
 func NilError(t TestingT, err error) {
 	if err != nil {
-		fatal(t, fmt.Sprintf("Expected no error, got: %s", err.Error()))
+		fatal(t, "Expected no error, got: %s", err.Error())
 	}
 }
 
@@ -52,7 +52,7 @@ func Error(t TestingT, err error, contains string) {
 	}
 
 	if !strings.Contains(err.Error(), contains) {
-		fatal(t, fmt.Sprintf("Expected error to contain '%s', got '%s'", contains, err.Error()))
+		fatal(t, "Expected error to contain '%s', got '%s'", contains, err.Error())
 	}
 }
 
@@ -60,11 +60,26 @@ func Error(t TestingT, err error, contains string) {
 // test.
 func Contains(t TestingT, actual, contains string) {
 	if !strings.Contains(actual, contains) {
-		fatal(t, fmt.Sprintf("Expected '%s' to contain '%s'", actual, contains))
+		fatal(t, "Expected '%s' to contain '%s'", actual, contains)
 	}
 }
 
-func fatal(t TestingT, msg string) {
-	_, file, line, _ := runtime.Caller(2)
-	t.Fatalf("%s:%d: %s", filepath.Base(file), line, msg)
+// NotNil fails the test if the object is nil
+func NotNil(t TestingT, obj interface{}) {
+	if obj == nil {
+		fatal(t, "Expected non-nil value.")
+	}
+}
+
+func fatal(t TestingT, format string, args ...interface{}) {
+	t.Fatalf(errorSource()+format, args...)
+}
+
+// See testing.decorate()
+func errorSource() string {
+	_, filename, line, ok := runtime.Caller(3)
+	if !ok {
+		return ""
+	}
+	return fmt.Sprintf("%s:%d: ", filepath.Base(filename), line)
 }

+ 36 - 0
pkg/testutil/tempfile/tempfile.go

@@ -0,0 +1,36 @@
+package tempfile
+
+import (
+	"io/ioutil"
+	"os"
+
+	"github.com/docker/docker/pkg/testutil/assert"
+)
+
+// TempFile is a temporary file that can be used with unit tests. TempFile
+// reduces the boilerplate setup required in each test case by handling
+// setup errors.
+type TempFile struct {
+	File *os.File
+}
+
+// NewTempFile returns a new temp file with contents
+func NewTempFile(t assert.TestingT, prefix string, content string) *TempFile {
+	file, err := ioutil.TempFile("", prefix+"-")
+	assert.NilError(t, err)
+
+	_, err = file.Write([]byte(content))
+	assert.NilError(t, err)
+	file.Close()
+	return &TempFile{File: file}
+}
+
+// Name returns the filename
+func (f *TempFile) Name() string {
+	return f.File.Name()
+}
+
+// Remove removes the file
+func (f *TempFile) Remove() {
+	os.Remove(f.Name())
+}