-
Notifications
You must be signed in to change notification settings - Fork 10
/
project_test.go
135 lines (114 loc) · 2.5 KB
/
project_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// Copyright (c) 2018 Dean Jackson <[email protected]>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2018-01-27
//
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
var (
testProjJS = `{
"folders":
[
{
"path": "/usr/local/bin"
},
{
"path": "/etc"
},
{
"path": "."
}
]
}`
testProjPaths = []string{"/usr/local/bin", "/etc"}
)
func withTestFile(data []byte, fn func(path string)) error {
f, err := ioutil.TempFile("", "alfred-sublime-")
if err != nil {
return err
}
defer os.Remove(f.Name())
if _, err := f.Write(data); err != nil {
return err
}
fn(f.Name())
return nil
}
func TestParseProject(t *testing.T) {
err := withTestFile([]byte(testProjJS), func(path string) {
dir := filepath.Dir(path)
paths := make([]string, len(testProjPaths))
copy(paths, testProjPaths)
paths = append(paths, dir)
proj, err := NewProject(path)
if err != nil {
t.Fatalf("couldn't create new project: %v", err)
}
if proj.Path != path {
t.Errorf("Bad Path. Expected=%v, Got=%v", path, proj.Path)
}
if len(proj.Folders) != len(paths) {
t.Fatalf("Bad Folders length. Expected=%v, Got=%v", len(paths), len(proj.Folders))
}
for i, s := range proj.Folders {
if s != paths[i] {
t.Errorf("Bad Folder. Expected=%v, Got=%v", paths[i], s)
}
}
if s := proj.Folder(); s != paths[0] {
t.Errorf("Bad Folder. Expected=%v, Got=%v", paths[0], s)
}
})
if err != nil {
t.Fatalf("couldn't create tempfile: %v", err)
}
}
func TestResolvePath(t *testing.T) {
data := []struct {
base, rel, out string
}{
{"/", "home/bob", "/home/bob"},
{"/home/bob", ".", "/home/bob"},
{".", "/home/bob", "/home/bob"},
{".", "bob", "bob"},
{".", "bob/public", "bob/public"},
{"./bob", "public", "bob/public"},
{"home", "bob", "home/bob"},
{"", "", ""},
{"home", "", ""},
{"", "bob", ""},
}
for _, td := range data {
s := resolvePath(td.base, td.rel)
if s != td.out {
t.Errorf("Bad ResolvePath. Expected=%v, Got=%v", td.out, s)
}
}
}
func TestProjectNames(t *testing.T) {
paths := []struct {
in, out string
}{
{"", ""},
{".", "."},
{"path/.", "."},
{"/", "/"},
{"~/Documents", "Documents"},
{"/Applications/Safari.app", "Safari"},
{"./Alfred Sublime.sublime-project", "Alfred Sublime"},
{"./path/to/something.txt", "something"},
}
for _, td := range paths {
proj := Project{Path: td.in}
if proj.Name() != td.out {
t.Errorf("Bad Name. Expected=%v, Got=%v", td.out, proj.Name())
}
}
}