scan.TestScanDir   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 44
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 40
nop 1
dl 0
loc 44
rs 7.52
c 0
b 0
f 0
1
package scan
2
3
import (
4
	"github.com/vvval/go-metadata-scanner/util"
5
	"github.com/vvval/go-metadata-scanner/vars"
6
	"path/filepath"
7
	"testing"
8
)
9
10
func TestCandidates(t *testing.T) {
11
	type check struct {
12
		file  string
13
		files vars.Chunk
14
		ext   []string
15
		exp   string
16
	}
17
18
	set := []check{
19
		// Adding extension
20
		{"1", vars.Chunk{"folder/1.png", "folder/2.png"}, []string{"png"}, "folder/1.png"},
21
		// Adding extension even if file has it
22
		{"1.png", vars.Chunk{"folder/1.png.ext", "folder/2.png"}, []string{"png", "ext"}, "folder/1.png.ext"},
23
		// Adding prefixes ([a-z]?_0?)
24
		{"1.png", vars.Chunk{"folder/01.png"}, []string{"png"}, "folder/01.png"},
25
		{"1.png", vars.Chunk{"folder/img_01.png"}, []string{"png"}, "folder/img_01.png"},
26
		// If prefix has letters, it should end with "underscore" char
27
		{"1.png", vars.Chunk{"folder/img01.png"}, []string{"png"}, ""},
28
		// Prefix can't have prefix made of non-zero digits
29
		{"1.png", vars.Chunk{"folder/21.png"}, []string{"png"}, ""},
30
		// If more than 1 candidate - ignore file
31
		{"1.png", vars.Chunk{"folder/01.png", "folder/001.png"}, []string{"png", "ext"}, ""},
32
		// But if has full match - it's fine
33
		{"folder2/1", vars.Chunk{"folder2/1.png", "folder/01.png", "folder/001.png"}, []string{"png", "ext"}, "folder2/1.png"},
34
		// This is not a full match, folder prefix is missing
35
		{"1", vars.Chunk{"folder2/1.png", "folder/01.png", "folder/001.png"}, []string{"png", "ext"}, ""},
36
	}
37
38
	for i, v := range set {
39
		c, _ := Candidates(v.file, &v.files, v.ext)
40
		if c != v.exp {
41
			t.Errorf("candidates incorrect for file `%s` (line `%d`) and ext `%+v`:\ngot `%+v`\nexpected `%+v`", v.file, i, v.ext, c, v.exp)
42
		}
43
	}
44
}
45
46
func TestScanDir(t *testing.T) {
47
	type check struct {
48
		dir    string
49
		ext    []string
50
		exp    vars.Chunk
51
		should bool
52
	}
53
54
	set := []check{
55
		{"./fixtures", []string{"ext", "ext3"}, vars.Chunk{
56
			filepath.Join("fixtures", "subFolder1", "file1.ext"),
57
			filepath.Join("fixtures", "SubFolder2", "file3.ext3"),
58
			filepath.Join("fixtures", "file2.ext"),
59
			filepath.Join("fixtures", "file4.ext3"),
60
		}, true},
61
		{"./fixtures/SubFolder2", []string{"ext2"}, vars.Chunk{}, true},
62
		{"./fixtures/SubFolder2", []string{"ext3"}, vars.Chunk{
63
			filepath.Join("fixtures", "SubFolder2", "file3.ext3"),
64
		}, true},
65
		{"./fixtures/SubFolder2", []string{"ext3"}, vars.Chunk{
66
			filepath.Join("fixtures", "SubFolder2", "file3.ext3"),
67
		}, true},
68
		{"./fixtures/SubFolder2", []string{}, vars.Chunk{
69
			filepath.Join("fixtures", "SubFolder2", "file3.ext3"),
70
		}, true},
71
		{"./fixtures", []string{"ext2"}, vars.Chunk{
72
			filepath.Join("fixtures", "subFolder1", "subfolder3", "file5.ext2"),
73
		}, true},
74
		{"./fixtures/SubFolder2", []string{}, vars.Chunk{
75
			filepath.Join("fixtures", "subFolder2", "file3.ext3"),
76
		}, false},
77
		{"./fixtures", []string{"ext2"}, vars.Chunk{
78
			filepath.Join("fixtures", "subfolder1", "subfolder3", "file5.ext2"),
79
		}, false},
80
	}
81
82
	for i, v := range set {
83
		res := MustDir(v.dir, v.ext)
84
		exp := v.exp
85
		var eq = util.Equal(res, exp)
86
		if eq && !v.should {
87
			t.Errorf("scan dir failed for dir `%s` (line `%d`) and ext `%+v` (wrong inequality):\ngot `%+v` (`%t`)\nexp `%+v` (`%t`)", v.dir, i, v.ext, res, eq, exp, v.should)
88
		} else if !eq && v.should {
89
			t.Errorf("scan dir failed for dir `%s` (line `%d`) and ext `%+v` (wrong equality):\ngot `%+v` (`%t`)\nexp `%+v` (`%t`)", v.dir, i, v.ext, res, eq, exp, v.should)
90
		}
91
	}
92
}
93