import groovy.io.FileType buildscript { repositories { mavenCentral() } dependencies { classpath 'com.eriwen:gradle-js-plugin:1.8.0' } } apply plugin: 'js' repositories { mavenCentral() } configurations { rhino } dependencies { rhino 'org.mozilla:rhino:1.7R4' } project.ext { failures = 0; rhinoTestSrc = 'out/rhino-test.js' testSrc = 'test/less' testOut = 'out/test' } javascript.source { test { js { srcDir '.' include 'test/rhino/test-header.js' include 'dist/less-rhino-1.5.1.js' } } } combineJs { source = javascript.source.test.js.files dest = file(rhinoTestSrc) } task testRhino(type: AllRhinoTests) { dependsOn 'testRhinoBase', 'testRhinoDebugAll', 'testRhinoDebugComments', 'testRhinoDebugMediaquery' // dependsOn 'testRhinoBase', 'testRhinoErrors', 'testRhinoLegacy', 'testRhinoStaticUrls', 'testRhinoCompression', 'testRhinoDebugAll', 'testRhinoDebugComments', 'testRhinoDebugMediaquery', 'testRhinoNoJsError' } 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 setupTest { dependsOn combineJs doLast { file(testOut).deleteDir() } } task clean << { file(rhinoTestSrc).delete() file(testOut).deleteDir() } class DebugRhinoTest extends RhinoTest { def suffix = "" def expectedCssPath(lessFilePath) { return lessFilePath.replace('.less', suffix+'.css').replace('\\less\\', '\\css\\'); //FIXME: now it works only on windows } def escapeIt(it) { return it.replaceAll("\\\\", "\\\\\\\\").replaceAll("/", "\\\\/").replaceAll(":", "\\\\:").replaceAll("\\.", "\\\\."); } def globalReplacements(input, directory) { def pDirectory = toPlatformFs(directory) println("projectDir: " + projectDir) 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 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(2); } return path; } def toPlatformFs(path) { return path.replace('\\', fs).replace('/', fs); } def expectedCssPath(lessFilePath) { return lessFilePath.replace('.less', '.css').replace('\\less\\', '\\css\\'); } 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'; } @TaskAction def runTest() { int testSuccesses = 0, testFailures = 0, testErrors = 0 project.file('test/less/' + testDir).eachFileMatch(FileType.FILES, ~/.*\.less/) { lessFile -> print lessFile if (!project.hasProperty('test') || lessFile.name.startsWith(project.test)) { def out = new java.io.ByteArrayOutputStream() def execOptions = { main = 'org.mozilla.javascript.tools.shell.Main' // main = 'org.mozilla.javascript.tools.debugger.Main' classpath = project.configurations.rhino args = [project.rhinoTestSrc, lessFile] + options standardOutput = out ignoreExitValue = true } println project.rhinoTestSrc println lessFile try { def exec = project.javaexec(execOptions) 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 testSuccesses++ println stylize(' ok', 'green') actualResult.delete() } 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 sutes 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'; } }