-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlibrary_test.go
181 lines (150 loc) · 6.89 KB
/
library_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
// This file is part of arduino-check.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-check.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package checkfunctions
import (
"os"
"regexp"
"testing"
"github.com/arduino/arduino-check/check/checkdata"
"github.com/arduino/arduino-check/check/checkresult"
"github.com/arduino/arduino-check/project"
"github.com/arduino/arduino-check/project/projecttype"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert"
)
var testDataPath *paths.Path
var schemasPath *paths.Path
func init() {
workingDirectory, _ := os.Getwd()
testDataPath = paths.New(workingDirectory, "testdata", "libraries")
schemasPath = paths.New(workingDirectory, "..", "..", "etc", "schemas")
}
type checkFunctionTestTable struct {
testName string
libraryFolderName string
expectedCheckResult checkresult.Type
expectedOutputQuery string
}
func checkCheckFunction(checkFunction Type, testTables []checkFunctionTestTable, t *testing.T) {
for _, testTable := range testTables {
expectedOutputRegexp := regexp.MustCompile(testTable.expectedOutputQuery)
testProject := project.Type{
Path: testDataPath.Join(testTable.libraryFolderName),
ProjectType: projecttype.Library,
SuperprojectType: projecttype.Library,
}
checkdata.Initialize(testProject, schemasPath)
result, output := checkFunction()
assert.Equal(t, testTable.expectedCheckResult, result, testTable.testName)
assert.True(t, expectedOutputRegexp.MatchString(output), testTable.testName)
}
}
func TestLibraryPropertiesNameFieldMissingOfficialPrefix(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"Not defined", "MissingFields", checkresult.NotRun, ""},
{"Correct prefix", "Arduino_Official", checkresult.Pass, ""},
{"Incorrect prefix", "Recursive", checkresult.Fail, "^Recursive$"},
}
checkCheckFunction(LibraryPropertiesNameFieldMissingOfficialPrefix, testTables, t)
}
func TestLibraryPropertiesNameFieldDuplicate(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"Duplicate", "Indexed", checkresult.Fail, "^Servo$"},
{"Not duplicate", "NotIndexed", checkresult.Pass, ""},
}
checkCheckFunction(LibraryPropertiesNameFieldDuplicate, testTables, t)
}
func TestLibraryPropertiesNameFieldNotInIndex(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"In index", "Indexed", checkresult.Pass, ""},
{"Not in index", "NotIndexed", checkresult.Fail, "^NotIndexed$"},
}
checkCheckFunction(LibraryPropertiesNameFieldNotInIndex, testTables, t)
}
func TestLibraryPropertiesSentenceFieldSpellCheck(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"Not defined", "MissingFields", checkresult.NotRun, ""},
{"Misspelled word", "MisspelledSentenceParagraphValue", checkresult.Fail, "^grill broccoli now$"},
{"Correct spelling", "Recursive", checkresult.Pass, ""},
}
checkCheckFunction(LibraryPropertiesSentenceFieldSpellCheck, testTables, t)
}
func TestLibraryPropertiesParagraphFieldSpellCheck(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"Not defined", "MissingFields", checkresult.NotRun, ""},
{"Misspelled word", "MisspelledSentenceParagraphValue", checkresult.Fail, "^There is a zebra$"},
{"Correct spelling", "Recursive", checkresult.Pass, ""},
}
checkCheckFunction(LibraryPropertiesParagraphFieldSpellCheck, testTables, t)
}
func TestLibraryPropertiesParagraphFieldRepeatsSentence(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"Repeat", "ParagraphRepeatsSentence", checkresult.Fail, ""},
{"No repeat", "Recursive", checkresult.Pass, ""},
}
checkCheckFunction(LibraryPropertiesParagraphFieldRepeatsSentence, testTables, t)
}
func TestLibraryPropertiesUrlFieldDeadLink(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"Not defined", "MissingFields", checkresult.NotRun, ""},
{"Bad URL", "BadURL", checkresult.Fail, "^Get \"http://invalid/\": dial tcp: lookup invalid:"},
{"HTTP error 404", "URL404", checkresult.Fail, "^404 Not Found$"},
{"Good URL", "Recursive", checkresult.Pass, ""},
}
checkCheckFunction(LibraryPropertiesUrlFieldDeadLink, testTables, t)
}
func TestLibraryPropertiesDependsFieldNotInIndex(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"Dependency not in index", "DependsNotIndexed", checkresult.Fail, "^NotIndexed$"},
{"Dependency in index", "DependsIndexed", checkresult.Pass, ""},
{"No depends", "NoDepends", checkresult.NotRun, ""},
}
checkCheckFunction(LibraryPropertiesDependsFieldNotInIndex, testTables, t)
}
func TestLibraryPropertiesDotALinkageFieldTrueWithFlatLayout(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"Not defined", "MissingFields", checkresult.NotRun, ""},
{"Flat layout", "DotALinkageFlat", checkresult.Fail, ""},
{"Recursive layout", "DotALinkage", checkresult.Pass, ""},
}
checkCheckFunction(LibraryPropertiesDotALinkageFieldTrueWithFlatLayout, testTables, t)
}
func TestLibraryPropertiesPrecompiledFieldEnabledWithFlatLayout(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"Not defined", "MissingFields", checkresult.NotRun, ""},
{"Flat layout", "PrecompiledFlat", checkresult.Fail, "^true$"},
{"Recursive layout", "Precompiled", checkresult.Pass, ""},
{"Recursive, not precompiled", "NotPrecompiled", checkresult.NotRun, ""},
{"Flat, not precompiled", "NotPrecompiledFlat", checkresult.NotRun, ""},
}
checkCheckFunction(LibraryPropertiesPrecompiledFieldEnabledWithFlatLayout, testTables, t)
}
func TestLibraryHasSubmodule(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Has submodule", "Submodule", checkresult.Fail, ""},
{"No submodule", "Recursive", checkresult.Pass, ""},
}
checkCheckFunction(LibraryHasSubmodule, testTables, t)
}