This repository was archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample_test.go
More file actions
203 lines (175 loc) · 5.5 KB
/
Copy pathexample_test.go
File metadata and controls
203 lines (175 loc) · 5.5 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Copyright (C) 2014 Pivotal Software, Inc.
All rights reserved. This program and the accompanying materials
are made available under the terms of the under the Apache License,
Version 2.0 (the "License”); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
)
func helperCommand() *exec.Cmd {
cmd := exec.Command("./levo")
return cmd
}
func TestNotOutputExampleWorkspace(testing *testing.T) {
defer cleanup()
input := "n\n"
p := helperCommand()
p.Args = append(p.Args, "-example")
p.Stdin = strings.NewReader(input)
output, err := p.CombinedOutput()
if err != nil {
testing.Error(err)
}
fmt.Println(string(output))
if strings.Contains(string(output), "Example files not created") == false {
testing.Errorf(string(output))
}
}
func TestOutputExampleWorkspace(testing *testing.T) {
defer cleanup()
input := "y\n"
p := helperCommand()
p.Args = append(p.Args, "-example")
p.Stdin = strings.NewReader(input)
output, err := p.Output()
if err != nil {
testing.Error(err)
}
if strings.Contains(string(output), "Successfully created") == false {
testing.Errorf(string(output))
}
}
func TestCheckForExampleDir(testing *testing.T) {
var err error
//Try removing example directory. Don't care if it doesn't work
os.RemoveAll("example")
err = checkForExampleDir()
if err != nil {
testing.Error("The example directory exists when it shouldn't")
}
err = os.Mkdir("example", 0755)
if err != nil {
testing.Errorf("Could not create example directory (this is not a code failure, it is a failure of the test itself)")
} else {
err = checkForExampleDir()
if err == nil {
testing.Error("The example directory doesn't exist when it should")
}
}
err = os.RemoveAll("example")
if err != nil {
testing.Error("Could not remove the example directory (this is not a code failure, it is a failure of the test itself)")
}
}
func TestCreateExampleDirectory(testing *testing.T) {
var err error
//Try removing example directory. Don't care if it doesn't work
os.RemoveAll("example")
err = createExampleDirectory()
if err != nil {
testing.Error("The example directory was not created when it should have been")
}
err = checkForExampleDir()
if err == nil {
testing.Errorf("The example directory isn't actually there (after a creation) %v", err.Error())
}
err = createExampleDirectory()
if err == nil {
testing.Error("The example directory claimed to be created when it couldn't have been")
}
//theoretically should test a non-writable directory, but that's a pain so we're putting it off
err = os.RemoveAll("example")
if err != nil {
testing.Error("Could not remove the example directory (this is not a code failure, it is a failure of the test itself)")
}
}
func TestPopulateConfigFiles(testing *testing.T) {
var err error
os.RemoveAll("example")
//try creating config files without first setting up a directory
err = populateConfigFiles()
if err == nil {
testing.Errorf("No error when populating a directory that does not exist")
}
//setup a directory
err = createExampleDirectory()
if err != nil {
testing.Error(err.Error())
}
//populate the directory with config files
err = populateConfigFiles()
if err != nil {
testing.Errorf("Error when creating config files: %s", err.Error())
}
//look for specific files
fileInfos, err := ioutil.ReadDir("example")
if err != nil {
testing.Errorf("Error when reading example dir (OS level error, likely not code related): %s", err.Error())
}
//Looking for new files
var configFile os.FileInfo
var schemaFile os.FileInfo
for _, fileInfo := range fileInfos {
fmt.Printf("")
if fileInfo.Name() == "config.json" {
configFile = fileInfo
}
if fileInfo.Name() == "schema.json" {
schemaFile = fileInfo
}
}
//Checking config file
if configFile == nil {
testing.Errorf("Config not created")
} else if configFile.Size() == 0 {
testing.Errorf("Config created, but is empty")
} //should check the actual contents of the file
//Checking schema file
if schemaFile == nil {
testing.Errorf("Schema not created")
} else if schemaFile.Size() == 0 {
testing.Errorf("Schema created, but is empty")
} //should check the actual contents of the file
os.RemoveAll("example")
}
func TestPopulateTemplates(testing *testing.T) {
var err error
os.RemoveAll("templates")
//populate the directory with template files
err = populateTemplates()
if err != nil {
testing.Errorf("Error when creating template files: %s", err.Error())
}
//look for specific files
fileInfos, err := ioutil.ReadDir("templates")
if err != nil {
testing.Errorf("Error when reading tempate dir (OS level error, likely not code related): %s", err.Error())
}
//Looking for new files
var templateFile os.FileInfo
for _, fileInfo := range fileInfos {
fmt.Printf("")
if fileInfo.Name() == "_Name_ListView.lt" {
templateFile = fileInfo
}
}
//Checking config file
if templateFile == nil {
testing.Errorf("template not created")
} else if templateFile.Size() == 0 {
testing.Errorf("template created, but is empty")
} //should check the actual contents of the file
os.RemoveAll("templates")
}