瀏覽代碼

Add missing tests and docs for pkg/fileutils

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
Vincent Demeester 10 年之前
父節點
當前提交
09adf87f23
共有 2 個文件被更改,包括 60 次插入8 次删除
  1. 13 6
      pkg/fileutils/fileutils.go
  2. 47 2
      pkg/fileutils/fileutils_test.go

+ 13 - 6
pkg/fileutils/fileutils.go

@@ -12,11 +12,13 @@ import (
 	"github.com/Sirupsen/logrus"
 )
 
-func Exclusion(pattern string) bool {
+// exclusion return true if the specified pattern is an exclusion
+func exclusion(pattern string) bool {
 	return pattern[0] == '!'
 }
 
-func Empty(pattern string) bool {
+// empty return true if the specified pattern is empty
+func empty(pattern string) bool {
 	return pattern == ""
 }
 
@@ -35,10 +37,10 @@ func CleanPatterns(patterns []string) ([]string, [][]string, bool, error) {
 	for _, pattern := range patterns {
 		// Eliminate leading and trailing whitespace.
 		pattern = strings.TrimSpace(pattern)
-		if Empty(pattern) {
+		if empty(pattern) {
 			continue
 		}
-		if Exclusion(pattern) {
+		if exclusion(pattern) {
 			if len(pattern) == 1 {
 				return nil, nil, false, errors.New("Illegal exclusion pattern: !")
 			}
@@ -46,7 +48,7 @@ func CleanPatterns(patterns []string) ([]string, [][]string, bool, error) {
 		}
 		pattern = filepath.Clean(pattern)
 		cleanedPatterns = append(cleanedPatterns, pattern)
-		if Exclusion(pattern) {
+		if exclusion(pattern) {
 			pattern = pattern[1:]
 		}
 		patternDirs = append(patternDirs, strings.Split(pattern, "/"))
@@ -86,7 +88,7 @@ func OptimizedMatches(file string, patterns []string, patDirs [][]string) (bool,
 	for i, pattern := range patterns {
 		negative := false
 
-		if Exclusion(pattern) {
+		if exclusion(pattern) {
 			negative = true
 			pattern = pattern[1:]
 		}
@@ -116,6 +118,9 @@ func OptimizedMatches(file string, patterns []string, patDirs [][]string) (bool,
 	return matched, nil
 }
 
+// CopyFile copies from src to dst until either EOF is reached
+// on src or an error occurs. It verifies src exists and remove
+// the dst if it exists.
 func CopyFile(src, dst string) (int64, error) {
 	cleanSrc := filepath.Clean(src)
 	cleanDst := filepath.Clean(dst)
@@ -138,6 +143,8 @@ func CopyFile(src, dst string) (int64, error) {
 	return io.Copy(df, sf)
 }
 
+// GetTotalUsedFds Returns the number of used File Descriptors by
+// reading it via /proc filesystem.
 func GetTotalUsedFds() int {
 	if fds, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil {
 		logrus.Errorf("Error opening /proc/%d/fd: %s", os.Getpid(), err)

+ 47 - 2
pkg/fileutils/fileutils_test.go

@@ -4,6 +4,7 @@ import (
 	"io/ioutil"
 	"os"
 	"path"
+	"path/filepath"
 	"testing"
 )
 
@@ -268,7 +269,7 @@ func TestSingleExclamationError(t *testing.T) {
 
 // A string preceded with a ! should return true from Exclusion.
 func TestExclusion(t *testing.T) {
-	exclusion := Exclusion("!")
+	exclusion := exclusion("!")
 	if !exclusion {
 		t.Errorf("failed to get true for a single !, got %v", exclusion)
 	}
@@ -298,7 +299,7 @@ func TestMatchesWithMalformedPatterns(t *testing.T) {
 
 // An empty string should return true from Empty.
 func TestEmpty(t *testing.T) {
-	empty := Empty("")
+	empty := empty("")
 	if !empty {
 		t.Errorf("failed to get true for an empty string, got %v", empty)
 	}
@@ -355,3 +356,47 @@ func TestCleanPatternsFolderSplit(t *testing.T) {
 		t.Errorf("expected first element in dirs slice to be config, got %v", dirs[0][1])
 	}
 }
+
+func TestCreateIfNotExistsDir(t *testing.T) {
+	tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer os.RemoveAll(tempFolder)
+
+	folderToCreate := filepath.Join(tempFolder, "tocreate")
+
+	if err := CreateIfNotExists(folderToCreate, true); err != nil {
+		t.Fatal(err)
+	}
+	fileinfo, err := os.Stat(folderToCreate)
+	if err != nil {
+		t.Fatalf("Should have create a folder, got %v", err)
+	}
+
+	if !fileinfo.IsDir() {
+		t.Fatalf("Should have been a dir, seems it's not")
+	}
+}
+
+func TestCreateIfNotExistsFile(t *testing.T) {
+	tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer os.RemoveAll(tempFolder)
+
+	fileToCreate := filepath.Join(tempFolder, "file/to/create")
+
+	if err := CreateIfNotExists(fileToCreate, false); err != nil {
+		t.Fatal(err)
+	}
+	fileinfo, err := os.Stat(fileToCreate)
+	if err != nil {
+		t.Fatalf("Should have create a file, got %v", err)
+	}
+
+	if fileinfo.IsDir() {
+		t.Fatalf("Should have been a file, seems it's not")
+	}
+}