forked from less/less.js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.gradle
347 lines (294 loc) · 10.2 KB
/
build.gradle
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import groovy.io.FileType
import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.api.tasks.Exec
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.eriwen:gradle-js-plugin:1.8.0'
classpath 'com.moowork.gradle:gradle-grunt-plugin:0.2'
}
}
apply plugin: 'js'
apply plugin: 'grunt'
repositories {
mavenCentral()
}
configurations {
rhino
}
dependencies {
rhino 'org.mozilla:rhino:1.7R4'
}
project.ext {
packageProps = new groovy.json.JsonSlurper().parseText(new File("package.json").toURL().text)
failures = 0;
rhinoTestSrc = "out/rhino-test-${packageProps.version}.js"
testSrc = 'test/less'
testOut = 'out/test'
}
task runGruntRhino(type: GruntTask) {
gruntArgs = "rhino"
}
combineJs {
dependsOn runGruntRhino
source = ["dist/less-rhino-${packageProps.version}.js", "test/rhino/test-header.js","dist/lessc-rhino-${packageProps.version}.js"]
dest = file(rhinoTestSrc)
}
task testRhino(type: AllRhinoTests) {
// dependsOn 'testRhinoBase'
dependsOn 'testRhinoBase', 'testRhinoErrors', 'testRhinoLegacy', 'testRhinoStaticUrls', 'testRhinoCompression', 'testRhinoDebugAll', 'testRhinoDebugComments', 'testRhinoDebugMediaquery', 'testRhinoNoJsError', 'testRhinoSourceMap'
}
task testRhinoBase(type: RhinoTest) {
options = [ '--strict-math=true', '--relative-urls' ]
}
task testRhinoDebugAll(type: DebugRhinoTest) {
options = [ '--strict-math=true', '--line-numbers=all' ]
testDir = 'debug' + fs
suffix = "-all"
}
task testRhinoDebugComments(type: DebugRhinoTest) {
options = [ '--strict-math=true', '--line-numbers=comments' ]
testDir = 'debug' + fs
suffix = "-comments"
}
task testRhinoDebugMediaquery(type: DebugRhinoTest) {
options = [ '--strict-math=true', '--line-numbers=mediaquery' ]
testDir = 'debug' + fs
suffix = "-mediaquery"
}
task testRhinoErrors(type: RhinoTest) {
options = [ '--strict-math=true', '--strict-units=true' ]
testDir = 'errors/'
expectErrors = true
}
task testRhinoChyby(type: RhinoTest) {
options = [ '--strict-math=true', '--strict-units=true' ]
testDir = 'chyby/'
// expectErrors = true
}
task testRhinoNoJsError(type: RhinoTest) {
options = [ '--strict-math=true', '--strict-units=true', '--no-js' ]
testDir = 'no-js-errors/'
expectErrors = true
}
task testRhinoLegacy(type: RhinoTest) {
testDir = 'legacy/'
}
task testRhinoStaticUrls(type: RhinoTest) {
options = [ '--strict-math=true', '--rootpath=folder (1)/' ]
testDir = 'static-urls/'
}
task testRhinoCompression(type: RhinoTest) {
options = [ '--compress=true' ]
testDir = 'compression/'
}
task testRhinoSourceMap(type: SourceMapRhinoTest) {
options = [ '--strict-math=true', '--strict-units=true']
testDir = 'sourcemaps/'
}
task setupTest {
dependsOn combineJs
doLast {
file(testOut).deleteDir()
}
}
task clean << {
file(rhinoTestSrc).delete()
file(testOut).deleteDir()
}
class SourceMapRhinoTest extends RhinoTest {
// helper to get the output map file
def getOutputMap(lessFile) {
def outFile = project.file(lessFile.path.replace('test/less', project.testOut).replace('.less', '.css'))
return project.file(outFile.path + ".map");
}
// callback to add SourceMap options to the options list
def postProcessOptions(options, lessFile) {
def outFile = getOutputMap(lessFile)
project.file(outFile.parent).mkdirs()
options << "--source-map=${testDir}${lessFile.name.replace('.less','.css')}"
options << "--source-map-basepath=${lessRootDir}"
options << "--source-map-rootpath=testweb/"
options << "--source-map-output-map-file=${outFile}"
options
}
// Callback to validate output
def handleResult(exec, out, lessFile) {
def actualFile = getOutputMap(lessFile)
def expectedFile = project.file(projectDir + fs + "test" + fs + testDir + fs + lessFile.name.replace(".less", ".json"))
assert actualFile.text == expectedFile.text
}
}
class DebugRhinoTest extends RhinoTest {
def escapeIt(it) {
return it.replaceAll("\\\\", "\\\\\\\\").replaceAll("/", "\\\\/").replaceAll(":", "\\\\:").replaceAll("\\.", "\\\\.");
}
def globalReplacements(input, directory) {
def pDirectory = toPlatformFs(directory)
def p = lessRootDir + fs + pDirectory
def pathimport = p + toPlatformFs("import/")
def pathesc = escapeIt(p)
def pathimportesc = escapeIt(pathimport)
def result = input.replace("{path}", p).replace("{pathesc}", pathesc).replace("{pathimport}", pathimport)
return result.replace("{pathimportesc}", pathimportesc).replace("\r\n", "\n")
}
}
class RhinoTest extends DefaultTask {
RhinoTest() {
dependsOn 'setupTest'
}
def suffix = ""
def testDir = ''
def options = []
def expectErrors = false
def fs = File.separator;
def projectDir = toUpperCaseDriveLetter(System.getProperty("user.dir"));
def lessRootDir = projectDir + fs + "test" + fs + "less"
def toUpperCaseDriveLetter(path) {
if (path.charAt(1)==':' && path.charAt(2)=='\\') {
return path.substring(0,1).toUpperCase() + path.substring(1);
}
return path;
}
def toPlatformFs(path) {
return path.replace('\\', fs).replace('/', fs);
}
def expectedCssPath(lessFilePath) {
lessFilePath.replace('.less', "${suffix}.css").replace("${fs}less${fs}", "${fs}css${fs}");
}
def globalReplacements(input, directory) {
return input;
}
def stylize(str, style) {
def styles = [
reset : [0, 0],
bold : [1, 22],
inverse : [7, 27],
underline : [4, 24],
yellow : [33, 39],
green : [32, 39],
red : [31, 39],
grey : [90, 39]
];
return '\033[' + styles[style][0] + 'm' + str +
'\033[' + styles[style][1] + 'm';
}
// Callback for subclasses to make any changes to the options
def postProcessOptions(options, lessFile) {
options
}
// Callback to validate output
def handleResult(exec, out, lessFile) {
def actual = out.toString().trim()
def actualResult = project.file(lessFile.path.replace('test/less', project.testOut).replace('.less', '.css'))
project.file(actualResult.parent).mkdirs()
actualResult << actual
def expected
if (expectErrors) {
assert exec.exitValue != 0
expected = project.file(lessFile.path.replace('.less', '.txt')).text.trim().
replace('{path}', lessFile.parent + '/').
replace('{pathhref}', '').
replace('{404status}', '')
} else {
assert exec.exitValue == 0
def expectedFile = expectedCssPath(lessFile.path)
expected = project.file(expectedFile).text.trim()
expected = globalReplacements(expected, testDir)
}
actual=actual.trim()
actual = actual.replace('\r\n', '\n')
expected = expected.replace('\r\n', '\n')
actual = actual.replace("/","\\")
expected = expected.replace("/","\\")
// println "* actual *"
// println actual
// new File("actual.txt").write(actual)
// println "* expected *"
// println expected
// new File("expected.txt").write(expected)
assert actual == expected
actualResult.delete()
}
@TaskAction
def runTest() {
int testSuccesses = 0, testFailures = 0, testErrors = 0
project.file('test/less/' + testDir).eachFileMatch(FileType.FILES, ~/.*\.less/) { lessFile ->
println "lessfile: $lessFile"
if (!project.hasProperty('test') || lessFile.name.startsWith(project.test)) {
def out = new java.io.ByteArrayOutputStream()
def processedOptions = postProcessOptions([project.rhinoTestSrc, lessFile] + options, lessFile)
def execOptions = {
main = 'org.mozilla.javascript.tools.shell.Main'
// main = 'org.mozilla.javascript.tools.debugger.Main'
classpath = project.configurations.rhino
args = processedOptions
standardOutput = out
ignoreExitValue = true
}
println "rhinoTestSrc: ${project.rhinoTestSrc}"
try {
def exec = project.javaexec(execOptions)
handleResult(exec, out, lessFile)
testSuccesses++
println stylize(' ok', 'green')
}
catch (ex) {
println ex
println()
testErrors++;
}
catch (AssertionError ae) {
println stylize(' failed', 'red')
println ae
testFailures++
}
} else {
println stylize(' skipped', 'yellow')
}
}
println stylize(testSuccesses + ' ok', 'green')
println stylize(testFailures + ' assertion failed', testFailures == 0 ? 'green' : 'red')
println stylize(testErrors + ' errors', testErrors == 0 ? 'green' : 'red')
if (testFailures != 0 || testErrors != 0) {
project.failures++;
}
}
}
class AllRhinoTests extends DefaultTask {
AllRhinoTests() {
}
@TaskAction
def runTest() {
println stylize(project.failures + ' test suites failed', project.failures == 0 ? 'green' : 'red')
}
def stylize(str, style) {
def styles = [
reset : [0, 0],
bold : [1, 22],
inverse : [7, 27],
underline : [4, 24],
yellow : [33, 39],
green : [32, 39],
red : [31, 39],
grey : [90, 39]
];
return '\033[' + styles[style][0] + 'm' + str +
'\033[' + styles[style][1] + 'm';
}
}
class GruntTask extends Exec {
private String gruntExecutable = Os.isFamily(Os.FAMILY_WINDOWS) ? "grunt.cmd" : "grunt"
private String switches = "--no-color"
String gruntArgs = ""
public GruntTask() {
super()
this.setExecutable(gruntExecutable)
}
public void setGruntArgs(String gruntArgs) {
this.args = "$switches $gruntArgs".trim().split(" ") as List
}
}