All tests passing for @plugin

- Inline JavaScript disabled by default
- Deprecated "preprocessor" option removed (preprocessor plugins still valid)
This commit is contained in:
Matthew Dean
2016-07-12 22:26:39 -07:00
parent 270fd097c9
commit ad57737ce0
27 changed files with 141 additions and 120 deletions

View File

@@ -31,7 +31,7 @@ module.exports = function (grunt) {
shell: {
options: {stdout: true, failOnError: true},
test: {
command: 'node test'
command: 'node test/index.js'
},
benchmark: {
command: 'node benchmark/index.js'
@@ -241,14 +241,6 @@ module.exports = function (grunt) {
outfile: 'tmp/browser/test-runner-global-vars.html'
}
},
postProcessor: {
src: ['test/browser/less/postProcessor/*.less'],
options: {
helpers: 'test/browser/runner-postProcessor-options.js',
specs: 'test/browser/runner-postProcessor.js',
outfile: 'tmp/browser/test-runner-post-processor.html'
}
},
postProcessorPlugin: {
src: ['test/less/postProcessorPlugin/*.less'],
options: {

View File

@@ -43,4 +43,6 @@ module.exports = function(window, options) {
options.onReady = true;
}
options.javascriptEnabled = (options.javascriptEnabled || options.inlineJavaScript) ? true : false;
};

View File

@@ -8,8 +8,7 @@ var addDataAttr = require("./utils").addDataAttr,
module.exports = function(window, options) {
var document = window.document;
var less = require('../less')();
options.javascriptEnabled = !!(options.javascriptEnabled || options.inlineJavaScript);
less.options = options;
var environment = less.environment,
FileManager = require("./file-manager")(options, less.logger),

View File

@@ -1,5 +1,4 @@
var path = require("path"),
AbstractPluginLoader = require("../less/environment/abstract-plugin-loader.js");
var AbstractPluginLoader = require("../less/environment/abstract-plugin-loader.js");
/**
* Browser Plugin Loader
@@ -11,7 +10,7 @@ var PluginLoader = function(less) {
PluginLoader.prototype = new AbstractPluginLoader();
PluginLoader.prototype.tryLoadPlugin = function(name, argument, basePath, callback) {
PluginLoader.prototype.tryLoadPlugin = function(name, basePath, callback) {
var self = this;
var prefix = name.slice(0, 1);
var explicit = prefix === "." || prefix === "/" || name.slice(-3).toLowerCase() === ".js";
@@ -34,14 +33,13 @@ PluginLoader.prototype.tryLoadPlugin = function(name, argument, basePath, callba
};
PluginLoader.prototype.tryLoadFromEnvironment = function(filename, basePath, explicit, callback) {
var fileManager;
var fileManager = new this.less.FileManager();
if (basePath) {
filename = path.join(basePath, filename);
filename = (fileManager.extractUrlParts(filename, basePath)).url;
}
if (filename) {
fileManager = new this.less.FileManager();
filename = fileManager.tryAppendExtension(filename, '.js');

View File

@@ -13,7 +13,6 @@ less.fs = require("./fs");
less.FileManager = FileManager;
less.UrlFileManager = UrlFileManager;
less.options = less.options || {};
less.options.javascriptEnabled = false;
less.formatError = function(ctx, options) {
options = options || {};

View File

@@ -36,9 +36,7 @@ PluginLoader.prototype.tryLoadPlugin = function(name, basePath, callback) {
callback(null, data);
}
else {
self.tryLoadFromEnvironment(name, basePath, explicit, function(err2, data2) {
callback(err2, data2);
});
self.tryLoadFromEnvironment(name, basePath, explicit, callback);
}
});
}
@@ -46,11 +44,10 @@ PluginLoader.prototype.tryLoadPlugin = function(name, basePath, callback) {
};
PluginLoader.prototype.tryLoadFromEnvironment = function(name, basePath, explicit, callback) {
var filename;
var filename = name;
var self = this;
function getFile(filename) {
var fileManager = new self.less.FileManager();
filename = fileManager.tryAppendExtension(filename, '.js');
@@ -60,7 +57,7 @@ PluginLoader.prototype.tryLoadFromEnvironment = function(name, basePath, explici
self.require = self.requireRelative(filename);
}
catch(e) {
console.log(e.stack.toString());
callback(e);
}
callback(null, data);
},
@@ -103,7 +100,7 @@ PluginLoader.prototype.tryLoadFromEnvironment = function(name, basePath, explici
catch(e) {
}
}
if (basePath && !filename) {
if (basePath) {
filename = path.join(basePath, name);
}
if (filename) {

View File

@@ -48,7 +48,7 @@ var evalCopyProperties = [
'sourceMap', // whether to output a source map
'importMultiple', // whether we are currently importing multiple copies
'urlArgs', // whether to add args into url tokens
'javascriptEnabled',// option - whether JavaScript is enabled. if undefined, defaults to true
'javascriptEnabled',// option - whether Inline JavaScript is enabled. if undefined, defaults to false
'pluginManager', // Used as the plugin manager for the session
'importantScope' // used to bubble up !important statements
];

View File

@@ -35,13 +35,14 @@ abstractFileManager.prototype.alwaysMakePathsAbsolute = function() {
abstractFileManager.prototype.isPathAbsolute = function(filename) {
return (/^(?:[a-z-]+:|\/|\\|#)/i).test(filename);
};
// TODO: pull out - this is part of Node & Browserify
abstractFileManager.prototype.join = function(basePath, laterPath) {
if (!basePath) {
return laterPath;
}
return basePath + laterPath;
};
abstractFileManager.prototype.pathDiff = function pathDiff(url, baseUrl) {
// diff between two paths to create a relative path

View File

@@ -36,6 +36,7 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, pluginOp
pluginObj = pluginManager.get(filename);
if (pluginObj) {
this.trySetOptions(pluginObj, filename, pluginOptions);
if (pluginObj.use) {
pluginObj.use(this.less);
}
@@ -58,24 +59,27 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, pluginOp
pluginObj = localModule.exports;
}
pluginObj = this.validatePlugin(pluginObj, filename, pluginOptions);
pluginObj = this.validatePlugin(pluginObj, filename);
if (pluginObj) {
// Run on first load
pluginManager.addPlugin(pluginObj, fileInfo.filename);
pluginObj.functions = registry.getLocalFunctions();
this.trySetOptions(pluginObj, filename, pluginOptions);
// Run every @plugin call
if (pluginObj.use) {
pluginObj.use(this.less);
}
}
else {
throw new Error();
throw new SyntaxError("Not a valid plugin");
}
} catch(e) {
// TODO pass the error
console.log(e.stack.toString());
console.log(e);
return new this.less.LessError({
message: "Plugin evaluation error: '" + e.name + ': ' + e.message.replace(/["]/g, "'") + "'" ,
filename: filename,
@@ -88,7 +92,24 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, pluginOp
};
AbstractPluginLoader.prototype.validatePlugin = function(plugin, filename, options) {
AbstractPluginLoader.prototype.trySetOptions = function(plugin, filename, options) {
var name = require('path').basename(filename);
if (options) {
if (!plugin.setOptions) {
error("Options have been provided but the plugin " + name + " does not support any options.");
return null;
}
try {
plugin.setOptions(options);
}
catch(e) {
error("Error setting options on plugin " + name + '\n' + e.message);
return null;
}
}
};
AbstractPluginLoader.prototype.validatePlugin = function(plugin, filename) {
if (plugin) {
// support plugins being a function
// so that the plugin can be more usable programmatically
@@ -98,20 +119,7 @@ AbstractPluginLoader.prototype.validatePlugin = function(plugin, filename, optio
var name = require('path').basename(filename);
if (plugin.minVersion) {
if (this.compareVersion(plugin.minVersion, this.less.version) < 0) {
error("plugin " + name + " requires version " + this.versionToString(plugin.minVersion));
return null;
}
}
if (options) {
if (!plugin.setOptions) {
error("options have been provided but the plugin " + name + "does not support any options");
return null;
}
try {
plugin.setOptions(options);
}
catch(e) {
error("Error setting options on plugin " + name + '\n' + e.message);
error("Plugin " + name + " requires version " + this.versionToString(plugin.minVersion));
return null;
}
}

View File

@@ -107,8 +107,7 @@ module.exports = function(environment) {
}
if (importOptions.isPlugin) {
plugin = pluginLoader.evalPlugin(contents, newEnv, importOptions, newFileInfo);
plugin = pluginLoader.evalPlugin(contents, newEnv, importOptions.pluginArgs, newFileInfo);
if (plugin instanceof LessError) {
fileParsedFunc(plugin, null, resolvedFilename);
}
@@ -132,9 +131,8 @@ module.exports = function(environment) {
}
};
if (importOptions.isPlugin) {
// TODO: implement options for plugins
try {
pluginLoader.tryLoadPlugin(path, null, currentFileInfo.currentDirectory, done);
pluginLoader.tryLoadPlugin(path, currentFileInfo.currentDirectory, done);
}
catch(e) {
callback(e);

View File

@@ -1384,22 +1384,27 @@ var Parser = function Parser(context, imports, fileInfo) {
},
//
// A @plugin directive, used to import scoped extensions dynamically.
// A @plugin directive, used to import plugins dynamically.
//
// @plugin "lib";
//
// Depending on our environment, importing is done differently:
// In the browser, it's an XHR request, in Node, it would be a
// file-system operation. The function used for importing is
// stored in `import`, which we pass to the Import constructor.
// @plugin (args) "lib";
//
plugin: function () {
var path,
var path, args, options,
index = parserInput.i,
dir = parserInput.$re(/^@plugin?\s+/);
if (dir) {
var options = { isPlugin : true };
args = this.pluginArgs();
if (args) {
options = {
pluginArgs: args,
isPlugin: true
};
}
else {
options = { isPlugin: true };
}
if ((path = this.entities.quoted() || this.entities.url())) {
@@ -1407,7 +1412,6 @@ var Parser = function Parser(context, imports, fileInfo) {
parserInput.i = index;
error("missing semi-colon on @plugin");
}
return new(tree.Import)(path, null, options, index, fileInfo);
}
else {
@@ -1417,6 +1421,24 @@ var Parser = function Parser(context, imports, fileInfo) {
}
},
pluginArgs: function() {
// list of options, surrounded by parens
parserInput.save();
if (! parserInput.$char('(')) {
parserInput.restore();
return null;
}
var args = parserInput.$re(/^\s*([^\);]+)\)\s*/);
if (args[1]) {
parserInput.forget();
return args[1].trim();
}
else {
parserInput.restore();
return null;
}
},
//
// A CSS Directive
//

View File

@@ -63,7 +63,9 @@ module.exports = function(root, options) {
if (options.pluginManager) {
visitorIterator.first();
while ((v = visitorIterator.get())) {
v.run(root);
if (!v.isPreEvalVisitor) {
v.run(evaldRoot);
}
}
}

View File

@@ -127,8 +127,8 @@ Import.prototype.doEval = function (context) {
features = this.features && this.features.eval(context);
if (this.options.isPlugin) {
if (this.root && this.root.setContext) {
this.root.setContext(context);
if (this.root && this.root.eval) {
this.root.eval(context);
}
registry = context.frames[0] && context.frames[0].functionRegistry;
if ( registry && this.root && this.root.functions ) {

View File

@@ -10,7 +10,7 @@ JsEvalNode.prototype.evaluateJavaScript = function (expression, context) {
that = this,
evalContext = {};
if (context.javascriptEnabled !== undefined && !context.javascriptEnabled) {
if (!context.javascriptEnabled) {
throw { message: "Inline JavaScript is not enabled. Is it set in your options?",
filename: this.currentFileInfo.filename,
index: this.index };

View File

@@ -1,6 +1,7 @@
/* Add js reporter for sauce */
jasmine.getEnv().addReporter(new jasmine.JSReporter2());
jasmine.getEnv().defaultTimeoutInterval = 3000;
/* record log messages for testing */

View File

@@ -1,4 +1,4 @@
var less = {logLevel: 4, errorReporting: "console"};
var less = {logLevel: 4, errorReporting: "console", javascriptEnabled: true};
// There originally run inside describe method. However, since they have not
// been inside it, they run at jasmine compile time (not runtime). It all

View File

@@ -1,4 +1,6 @@
var less = {
strictUnits: true,
strictMath: true,
logLevel: 4 };
logLevel: 4,
javascriptEnabled: true
};

View File

@@ -1,5 +0,0 @@
var less = {logLevel: 4,
errorReporting: "console"};
less.postProcessor = function(styles) {
return 'hr {height:50px;}\n' + styles;
};

View File

@@ -1,3 +0,0 @@
describe("less.js postProcessor (deprecated)", function() {
testLessEqualsInDocument();
});

View File

@@ -3,7 +3,6 @@
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner</title>
<!-- generate script tags for tests -->
<% var generateScriptTags = function(allScripts) { allScripts.forEach(function(script){ %>
<script src="<%= script %>"></script>

View File

@@ -16,10 +16,11 @@ function getErrorPathReplacementFunction(dir) {
}
console.log("\n" + stylize("Less", 'underline') + "\n");
lessTester.prepBomTest();
lessTester.runTestSet({strictMath: true, relativeUrls: true, silent: true});
lessTester.runTestSet({strictMath: true, strictUnits: true}, "errors/",
lessTester.testErrors, null, getErrorPathReplacementFunction("errors"));
lessTester.runTestSet({strictMath: true, relativeUrls: true, silent: true, javascriptEnabled: true});
lessTester.runTestSet({strictMath: true, strictUnits: true, javascriptEnabled: true}, "errors/",
lessTester.testErrors, null, getErrorPathReplacementFunction("errors"));
lessTester.runTestSet({strictMath: true, strictUnits: true, javascriptEnabled: false}, "no-js-errors/",
lessTester.testErrors, null, getErrorPathReplacementFunction("no-js-errors"));
lessTester.runTestSet({strictMath: true, dumpLineNumbers: 'comments'}, "debug/", null,

View File

@@ -58,7 +58,8 @@ module.exports = function() {
var totalTests = 0,
failedTests = 0,
passedTests = 0;
passedTests = 0,
finishTimer = setInterval(endTest, 500);
less.functions.functionRegistry.addMultiple({
add: function (a, b) {
@@ -221,48 +222,49 @@ module.exports = function() {
var doubleCallCheck = false;
queue(function() {
toCSS(options, path.join(baseFolder, foldername + file), function (err, result) {
if (doubleCallCheck) {
totalTests++;
fail("less is calling back twice");
process.stdout.write(doubleCallCheck + "\n");
process.stdout.write((new Error()).stack + "\n");
return;
}
doubleCallCheck = (new Error()).stack;
if (doubleCallCheck) {
totalTests++;
fail("less is calling back twice");
process.stdout.write(doubleCallCheck + "\n");
process.stdout.write((new Error()).stack + "\n");
return;
}
doubleCallCheck = (new Error()).stack;
if (verifyFunction) {
var verificationResult = verifyFunction(name, err, result && result.css, doReplacements, result && result.map, baseFolder);
release();
return verificationResult;
}
if (err) {
fail("ERROR: " + (err && err.message));
if (isVerbose) {
process.stdout.write("\n");
if (err.stack) {
process.stdout.write(err.stack + "\n");
} else {
//this sometimes happen - show the whole error object
console.log(err);
if (verifyFunction) {
var verificationResult = verifyFunction(name, err, result && result.css, doReplacements, result && result.map, baseFolder);
release();
return verificationResult;
}
if (err) {
fail("ERROR: " + (err && err.message));
if (isVerbose) {
process.stdout.write("\n");
if (err.stack) {
process.stdout.write(err.stack + "\n");
} else {
//this sometimes happen - show the whole error object
console.log(err);
}
}
release();
return;
}
release();
return;
}
var css_name = name;
if (nameModifier) { css_name = nameModifier(name); }
fs.readFile(path.join('test/css', css_name) + '.css', 'utf8', function (e, css) {
process.stdout.write("- " + path.join(baseFolder, css_name) + ": ");
var css_name = name;
if (nameModifier) { css_name = nameModifier(name); }
fs.readFile(path.join('test/css', css_name) + '.css', 'utf8', function (e, css) {
process.stdout.write("- " + path.join(baseFolder, css_name) + ": ");
css = css && doReplacements(css, path.join(baseFolder, foldername));
if (result.css === css) { ok('OK'); }
else {
difference("FAIL", css, result.css);
}
release();
css = css && doReplacements(css, path.join(baseFolder, foldername));
if (result.css === css) { ok('OK'); }
else {
difference("FAIL", css, result.css);
}
release();
});
});
});
});
});
}
@@ -305,8 +307,8 @@ module.exports = function() {
function endTest() {
if (isFinished && ((failedTests + passedTests) >= totalTests)) {
clearInterval(finishTimer);
var leaked = checkGlobalLeaks();
process.stdout.write("\n");
if (failedTests > 0) {
process.stdout.write(failedTests + stylize(" Failed", "red") + ", " + passedTests + " passed\n");

View File

@@ -150,7 +150,7 @@
.generic(abc, "abc");
.generic('abc', "abd");
.generic(6, e("6"));
.generic(`9`, 8);
.generic(9, 8);
.generic(a, b);
.generic(1 2, 3);
}

View File

@@ -1,4 +1,4 @@
SyntaxError: You are using JavaScript, which has been disabled. in {path}no-js-errors.less on line 2, column 6:
SyntaxError: Inline JavaScript is not enabled. Is it set in your options? in {path}no-js-errors.less on line 2, column 6:
1 .a {
2 a: `1 + 1`;
3 }

View File

@@ -40,7 +40,7 @@
ruleset-shadow : test-shadow();
};
#ns {
@plugin "./plugin/plugin-local";
@plugin (test=test) "./plugin/plugin-local";
.mixin() {
ns-mixin-global : test-global();
ns-mixin-local : test-local();

View File

@@ -6,3 +6,9 @@ functions.addMultiple({
return new tree.Anonymous( "local" );
}
});
return {
setOptions: function(raw) {
// do nothing
}
}

View File

@@ -1,4 +1,4 @@
@plugin "extension-transitive";
@plugin "plugin-transitive";
.other {
trans : test-transitive();