-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
220 lines (198 loc) · 5.3 KB
/
main_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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
//This file holds test performed on binaries
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"testing"
)
// Create the tests
// basic tests
var tests = []struct {
path string
outfile string
folderFlag string
exp string
}{
{"test.go", "", "", "test.go false"},
{"todo", "", "./", "todo true"},
}
var outputfiletests = []struct {
text []byte
path string
showlines bool
outputFile string
exp string
}{
{[]byte("//@todo some comment"), "tests/files/test.go", true, "output.txt", "tests/files/test.go\n 1)//@todo some comment\n"},
{[]byte("//@todo some comment"), "tests/files/test.go", true, "./output.txt", "tests/files/test.go\n 1)//@todo some comment\n"},
}
// //Testing starts
// Performs pre-test actions like creating the binaries and a tmp folder for test files
func TestPre(t *testing.T) {
//Create binaries folders
err := os.MkdirAll("bin/flowcat-darwin-arm64", 0775)
if err != nil {
t.Fatal("[PRE-TEST][BIN FOLDER] creating the folder bin/flowcat-darwin-arm64 failed", err.Error())
}
err = os.MkdirAll("bin/flowcat-linux-386", 0775)
if err != nil {
t.Fatal("[PRE-TEST][BIN FOLDER] creating the folder bin/flowcat-linux-386 failed", err.Error())
}
err = os.MkdirAll("bin/flowcat-linux-amd64", 0775)
if err != nil {
t.Fatal("[PRE-TEST][BIN FOLDER] creating the folder bin/flowcat-linux-amd64 failed", err.Error())
}
//Create tmp file folder
err = os.MkdirAll("tests/files", 0775)
if err != nil {
t.Fatal("Could not perform pre-test actions, creating the tmp file folder failed", err.Error())
}
}
// func TestCfg(t *testing.T) {
// var c Config
// c.Linenum := false
// c.Match := ""
// }
func TestCheckExclude(t *testing.T) {
//Run the tests
Cfg.IgnoredItems = append(Cfg.IgnoredItems, "todo")
for _, e := range tests {
file, exc := CheckExclude(e.path, e.outfile, e.folderFlag)
res := file + " " + strconv.FormatBool(exc)
if res != e.exp {
t.Errorf("Got: %s Expected: %s", res, e.exp)
}
}
}
func TestOutputFile(t *testing.T) {
for _, e := range outputfiletests {
lexer = newLexer("@todo") //sets the matching string
err := Scan(e.text, e.path, e.showlines, e.outputFile)
if err != nil {
t.Errorf("Scan failed %s", err.Error())
}
dir, _ := filepath.Split(e.outputFile)
if dir == "" {
folder, _ := filepath.Split(e.path)
b, err := os.ReadFile(folder + e.outputFile) // pass path at -f plus output filename
if err != nil {
t.Errorf("Could not read output file %s %s", e.outputFile, err.Error())
}
if e.exp != string(b) {
t.Errorf("Got: %s Expected: %s", string(b), e.exp)
}
err = os.Remove(folder + e.outputFile)
if err != nil {
t.Errorf("Could not remove old file %s %s", folder+e.outputFile, err.Error())
}
} else {
b, err := os.ReadFile(e.outputFile) // just pass the output file name
if err != nil {
t.Errorf("Could not read output file %s", e.outputFile)
}
if e.exp != string(b) {
t.Errorf("Got: %s Expected: %s", string(b), e.exp)
}
err = os.Remove(e.outputFile)
if err != nil {
t.Errorf("Could not remove old file %s %s", e.outputFile, err.Error())
}
}
}
}
func TestReadme(t *testing.T) {
var buffer string
set := map[string]struct{}{}
var stdout bytes.Buffer
var stderr bytes.Buffer
//copy the readme.md file to readme_copy.md
source, err := os.Open("README.md") //open the source file
if err != nil {
panic(err)
}
defer source.Close()
destination, err := os.Create("README_COPY.md") //create the destination file
if err != nil {
panic(err)
}
_, err = io.Copy(destination, source)
if err != nil {
t.Errorf(err.Error())
}
//Update the extensions in the readme
file, err := os.Open("README.md")
if err != nil {
t.Errorf(err.Error())
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
buffer += scanner.Text() + "\n"
if strings.Contains(buffer, "#### Supported Filetypes") {
break
}
}
buffer += "\n```text\n"
if err := scanner.Err(); err != nil {
t.Errorf(err.Error())
}
//creates a set so we dont have duplicates
for l := range Extensions {
curext := Extensions[l]
for _, ext := range curext.Ext {
if ext != "" {
set[ext] = struct{}{}
}
}
}
keys := make([]string, 0, len(set))
for k := range set {
keys = append(keys, k)
}
//sort the keys so they are always list in the same order
sort.Strings(keys)
for _, k := range keys {
buffer += "." + k + "\n"
}
buffer += "```"
file.Close()
file, err = os.OpenFile("README.md", os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
t.Errorf(err.Error())
}
err = file.Truncate(0)
if err != nil {
t.Errorf(err.Error())
}
_, err = file.Seek(0, 0)
if err != nil {
t.Errorf(err.Error())
}
_, err = fmt.Fprintf(file, "%s", buffer)
if err != nil {
t.Errorf(err.Error())
}
//Run diff on the two files
cmd := exec.Command("/usr/bin/bash", "-c", "diff README.md README_COPY.md")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
t.Errorf("[ERROR]Readme was autoupdated commit the changes and run the test again %s", err.Error())
}
//Remove COPY
err = os.Remove("README_COPY.md")
if err != nil {
t.Errorf(err.Error())
}
}
//@todo init settings works
//@todo init settings fails if ran a second time and the file is already there
//@todo can get config settings from file