Browse code

Add missing tests and docs for pkg/fileutils

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2015/07/13 05:43:42
Showing 2 changed files
... ...
@@ -12,11 +12,13 @@ import (
12 12
 	"github.com/Sirupsen/logrus"
13 13
 )
14 14
 
15
-func Exclusion(pattern string) bool {
15
+// exclusion return true if the specified pattern is an exclusion
16
+func exclusion(pattern string) bool {
16 17
 	return pattern[0] == '!'
17 18
 }
18 19
 
19
-func Empty(pattern string) bool {
20
+// empty return true if the specified pattern is empty
21
+func empty(pattern string) bool {
20 22
 	return pattern == ""
21 23
 }
22 24
 
... ...
@@ -35,10 +37,10 @@ func CleanPatterns(patterns []string) ([]string, [][]string, bool, error) {
35 35
 	for _, pattern := range patterns {
36 36
 		// Eliminate leading and trailing whitespace.
37 37
 		pattern = strings.TrimSpace(pattern)
38
-		if Empty(pattern) {
38
+		if empty(pattern) {
39 39
 			continue
40 40
 		}
41
-		if Exclusion(pattern) {
41
+		if exclusion(pattern) {
42 42
 			if len(pattern) == 1 {
43 43
 				return nil, nil, false, errors.New("Illegal exclusion pattern: !")
44 44
 			}
... ...
@@ -46,7 +48,7 @@ func CleanPatterns(patterns []string) ([]string, [][]string, bool, error) {
46 46
 		}
47 47
 		pattern = filepath.Clean(pattern)
48 48
 		cleanedPatterns = append(cleanedPatterns, pattern)
49
-		if Exclusion(pattern) {
49
+		if exclusion(pattern) {
50 50
 			pattern = pattern[1:]
51 51
 		}
52 52
 		patternDirs = append(patternDirs, strings.Split(pattern, "/"))
... ...
@@ -86,7 +88,7 @@ func OptimizedMatches(file string, patterns []string, patDirs [][]string) (bool,
86 86
 	for i, pattern := range patterns {
87 87
 		negative := false
88 88
 
89
-		if Exclusion(pattern) {
89
+		if exclusion(pattern) {
90 90
 			negative = true
91 91
 			pattern = pattern[1:]
92 92
 		}
... ...
@@ -116,6 +118,9 @@ func OptimizedMatches(file string, patterns []string, patDirs [][]string) (bool,
116 116
 	return matched, nil
117 117
 }
118 118
 
119
+// CopyFile copies from src to dst until either EOF is reached
120
+// on src or an error occurs. It verifies src exists and remove
121
+// the dst if it exists.
119 122
 func CopyFile(src, dst string) (int64, error) {
120 123
 	cleanSrc := filepath.Clean(src)
121 124
 	cleanDst := filepath.Clean(dst)
... ...
@@ -138,6 +143,8 @@ func CopyFile(src, dst string) (int64, error) {
138 138
 	return io.Copy(df, sf)
139 139
 }
140 140
 
141
+// GetTotalUsedFds Returns the number of used File Descriptors by
142
+// reading it via /proc filesystem.
141 143
 func GetTotalUsedFds() int {
142 144
 	if fds, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil {
143 145
 		logrus.Errorf("Error opening /proc/%d/fd: %s", os.Getpid(), err)
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"io/ioutil"
5 5
 	"os"
6 6
 	"path"
7
+	"path/filepath"
7 8
 	"testing"
8 9
 )
9 10
 
... ...
@@ -268,7 +269,7 @@ func TestSingleExclamationError(t *testing.T) {
268 268
 
269 269
 // A string preceded with a ! should return true from Exclusion.
270 270
 func TestExclusion(t *testing.T) {
271
-	exclusion := Exclusion("!")
271
+	exclusion := exclusion("!")
272 272
 	if !exclusion {
273 273
 		t.Errorf("failed to get true for a single !, got %v", exclusion)
274 274
 	}
... ...
@@ -298,7 +299,7 @@ func TestMatchesWithMalformedPatterns(t *testing.T) {
298 298
 
299 299
 // An empty string should return true from Empty.
300 300
 func TestEmpty(t *testing.T) {
301
-	empty := Empty("")
301
+	empty := empty("")
302 302
 	if !empty {
303 303
 		t.Errorf("failed to get true for an empty string, got %v", empty)
304 304
 	}
... ...
@@ -355,3 +356,47 @@ func TestCleanPatternsFolderSplit(t *testing.T) {
355 355
 		t.Errorf("expected first element in dirs slice to be config, got %v", dirs[0][1])
356 356
 	}
357 357
 }
358
+
359
+func TestCreateIfNotExistsDir(t *testing.T) {
360
+	tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
361
+	if err != nil {
362
+		t.Fatal(err)
363
+	}
364
+	defer os.RemoveAll(tempFolder)
365
+
366
+	folderToCreate := filepath.Join(tempFolder, "tocreate")
367
+
368
+	if err := CreateIfNotExists(folderToCreate, true); err != nil {
369
+		t.Fatal(err)
370
+	}
371
+	fileinfo, err := os.Stat(folderToCreate)
372
+	if err != nil {
373
+		t.Fatalf("Should have create a folder, got %v", err)
374
+	}
375
+
376
+	if !fileinfo.IsDir() {
377
+		t.Fatalf("Should have been a dir, seems it's not")
378
+	}
379
+}
380
+
381
+func TestCreateIfNotExistsFile(t *testing.T) {
382
+	tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
383
+	if err != nil {
384
+		t.Fatal(err)
385
+	}
386
+	defer os.RemoveAll(tempFolder)
387
+
388
+	fileToCreate := filepath.Join(tempFolder, "file/to/create")
389
+
390
+	if err := CreateIfNotExists(fileToCreate, false); err != nil {
391
+		t.Fatal(err)
392
+	}
393
+	fileinfo, err := os.Stat(fileToCreate)
394
+	if err != nil {
395
+		t.Fatalf("Should have create a file, got %v", err)
396
+	}
397
+
398
+	if fileinfo.IsDir() {
399
+		t.Fatalf("Should have been a file, seems it's not")
400
+	}
401
+}