Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

pass .csslintrc file path through the CLI #502

Merged
merged 5 commits into from

3 participants

Dmitry Lapshukov Joshua Spence Nick Schonning
Dmitry Lapshukov

re #449

Added ability to pass .csslintrc file path as: csslint --config=<path to .csslintrc> <css to lint>, and as well bit of YUI unit tests to check this functionality.

Notes:

  • This also works with CLI as: csslint --config=<path to .csslintrc> --ignore=<ignores> <css to lint>, in this case the ignore from rc file would be overrided by cli, which is current behavior.
  • Rc file could have any valid file name.
  • For unit tests, api stubbing used with vm code execusion for main common.js cli code, also fs was faked as json like file.
  • Look up file traversing to search closest .csslintrc, which was mentioned into #449, is not part of this PR
dmi3y added some commits
Dmitry Lapshukov dmi3y cli config path #449 517ffe4
Dmitry Lapshukov dmi3y wraping up with cli tests+
CSSLint mix helper from utils to mix up cli and rc options
4b19770
Dmitry Lapshukov dmi3y more tests 4994199
Nick Schonning nschonni added the Triage label
Dmitry Lapshukov dmi3y referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
dmi3y added some commits
Dmitry Lapshukov dmi3y wraping up with cli tests+
CSSLint mix helper from utils to mix up cli and rc options
e6c1067
Dmitry Lapshukov dmi3y fixing conflict 0a23182
Joshua Spence

+1 for this... is this likely to be merged?

Nick Schonning nschonni merged commit f844802 into from
Joshua Spence

Awesome... any chance of a version bump for npm?

Nick Schonning nschonni added this to the 0.11.0 milestone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 16, 2014
  1. Dmitry Lapshukov

    cli config path #449

    dmi3y authored
  2. Dmitry Lapshukov

    wraping up with cli tests+

    dmi3y authored
    CSSLint mix helper from utils to mix up cli and rc options
  3. Dmitry Lapshukov

    more tests

    dmi3y authored
Commits on Apr 20, 2014
  1. Dmitry Lapshukov

    wraping up with cli tests+

    dmi3y authored
    CSSLint mix helper from utils to mix up cli and rc options
Commits on Apr 30, 2014
  1. Dmitry Lapshukov

    fixing conflict

    dmi3y authored
This page is out of date. Refresh to see the latest.
54 src/cli/common.js
View
@@ -16,6 +16,7 @@ function cli(api){
"warnings" : { "format" : "<rule[,rule]+>", "description" : "Indicate which rules to include as warnings."},
"ignore" : { "format" : "<rule[,rule]+>", "description" : "Indicate which rules to ignore completely."},
"exclude-list": { "format" : "<file|dir[,file|dir]+>", "description" : "Indicate which files/directories to exclude from being linted."},
+ "config" : { "format" : "<file>", "description" : "Reads csslint options from specified file."},
"version" : { "format" : "", "description" : "Outputs the current version number."}
};
@@ -250,8 +251,9 @@ function cli(api){
}
- function processArguments(args, options) {
+ function processArguments(args, extend) {
var arg = args.shift(),
+ options = extend || {},
argName,
parts,
files = [];
@@ -293,9 +295,16 @@ function cli(api){
}
}
- function readConfigFile(options) {
- var data = api.readFile(api.getFullPath(".csslintrc")),
- json;
+ function readConfigFile(config) {
+ var csslintrc = config || ".csslintrc",
+ data = api.readFile(api.getFullPath(csslintrc));
+ return data;
+ }
+
+ function readConfigData(config) {
+ var data = readConfigFile(config),
+ json,
+ options = {};
if (data) {
if (data.charAt(0) === "{") {
try {
@@ -308,45 +317,52 @@ function cli(api){
}
} catch(e) {}
}
- options = processArguments(data.split(/[\s\n\r]+/m), options);
+ options = processArguments(data.split(/[\s\n\r]+/m));
}
return options;
}
-
-
//-----------------------------------------------------------------------------
// Process command line
//-----------------------------------------------------------------------------
var args = api.args,
argCount = args.length,
- options = {};
+ options,
+ rcOptions,
+ cliOptions;
- // first look for config file .csslintrc
- options = readConfigFile(options);
+ // Preprocess command line arguments
+ cliOptions = processArguments(args);
- // Command line arguments override config file
- options = processArguments(args, options);
-
- if (options.help || argCount === 0){
+ if (cliOptions.help || argCount === 0){
outputHelp();
api.quit(0);
}
- // Validate options
- validateOptions(options);
-
- if (options.version){
+ if (cliOptions.version){
api.print("v" + CSSLint.version);
api.quit(0);
}
- if (options["list-rules"]){
+ if (cliOptions["list-rules"]){
printRules();
api.quit(0);
}
+ // Look for config file
+ rcOptions = readConfigData(cliOptions.config);
+
+ // Command line arguments override config file
+ options = CSSLint.Util.mix(rcOptions, cliOptions);
+
+ // hot fix for CSSLint.Util.mix current behavior
+ // https://github.com/CSSLint/csslint/issues/501
+ options = rcOptions;
+
+ // Validate options
+ validateOptions(options);
+
api.quit(processFiles(options.files,options));
}
68 tests/cli/assets/apiStub.js
View
@@ -0,0 +1,68 @@
+/*jshint node:true*/
+"use strict";
+var
+ stub = {
+ logbook: function (log) {
+ this.logs.push(log);
+ },
+ readLogs: function () {
+ return this.logs.slice();
+ },
+
+ getFullPath: function (path) {
+ return path;
+ },
+ getFiles: function (dir) {
+ var
+ filesobj = this.fakedFs[dir],
+ fileix,
+ out = [];
+ for (fileix in filesobj) {
+ if ( filesobj.hasOwnProperty(fileix) && /\.css$/.test(fileix) ) {
+ out.push(dir + "/" + fileix);
+ }
+ }
+ return out;
+ },
+ readFile: function (path) {
+ var
+ spath = path.split("/"),
+ spathLen = spath.length,
+ i,
+ out = this.fakedFs;
+
+ for (i = 0; i < spathLen; i += 1) {
+ out = out[spath[i]];
+ }
+
+ return out;
+ },
+ isDirectory: function (checkit) {
+ var
+ result = this.fakedFs[checkit];
+ return typeof result === "object";
+ },
+ print: function (msg) {
+ this.logbook(msg);
+ },
+ quit: function (signal) {
+ this.logbook(signal);
+ }
+ };
+
+module.exports = function (setup) {
+ var
+ api,
+ setix;
+
+ api = Object.create(stub);
+
+ for (setix in setup) {
+ if (setup.hasOwnProperty(setix)) {
+ api[setix] = setup[setix];
+ }
+ }
+
+ api.logs = [];
+ return api;
+};
67 tests/cli/assets/data.js
View
@@ -0,0 +1,67 @@
+/*jshint node:true*/
+module.exports = {
+ "suites": {
+ "config csslintrc override": {
+ "args": [
+ "--config=.rc1",
+ "dir"
+ ],
+ "expecting": [
+ "csslint: No errors in dir/a.css.",
+ "csslint: No errors in dir/b.css.",
+ 0
+ ]
+ },
+ "straight linting": {
+ "args": [
+ "dir"
+ ],
+ "expecting": [
+ "csslint: There is 1 problem in dir/a.css.",
+ "csslint: There is 1 problem in dir/b.css.",
+ 0
+ ]
+ },
+ "mix of cli options": {
+ "args": [
+ "--config=.rc1",
+ "--ignore=important",
+ "dir"
+ ],
+ "expecting": [
+ "csslint: No errors in dir/a.css.",
+ "csslint: There is 1 problem in dir/b.css.",
+ 0
+ ]
+ },
+ "more mixes of cli options": {
+ "args": [
+ "--config=.rc1",
+ "--errors=important",
+ "dir"
+ ],
+ "expecting": [
+ "csslint: There is 1 problem in dir/a.css.",
+ "csslint: No errors in dir/b.css.",
+ 1
+ ]
+ },
+ "version": {
+ "args": [
+ "--version"
+ ],
+ "expecting": [
+ "v@VERSION@",
+ 0
+ ]
+ }
+ },
+
+ "fakedFs": {
+ ".rc1": "--ignore=important,ids",
+ "dir": {
+ "a.css": ".a {color: red!important;}",
+ "b.css": "#a {color: red;}"
+ },
+ }
+};
73 tests/cli/cli-common.js
View
@@ -0,0 +1,73 @@
+/*jshint loopfunc:true, node:true */
+"use strict";
+function include(path, sandbox) {
+ var
+ vm = require("vm"),
+ fs = require("fs"),
+ file;
+
+ file = fs.readFileSync(path);
+ vm.runInNewContext(file, sandbox);
+}
+
+
+
+(function(){
+
+ var Assert = YUITest.Assert,
+ suite = new YUITest.TestSuite("General Tests for CLI"),
+ apiStub = require("./tests/cli/assets/apiStub.js"),
+ data = require("./tests/cli/assets/data.js"),
+ suites = data.suites,
+ suiteix,
+ sandbox = {
+ CSSLint: CSSLint
+ };
+
+ include(__dirname + "/src/cli/common.js", sandbox); /* expose sandbox.cli */
+
+ for (suiteix in suites) {
+ if (suites.hasOwnProperty(suiteix)) {
+ (function (suiteix) {
+
+ suite.add(new YUITest.TestCase({
+
+ name: "Test " + suiteix,
+
+ "Outcome logs should match expected": function (){
+ var
+ it = suites[suiteix],
+ expecting = it.expecting,
+ expectingLen = expecting.length,
+ outcome,
+ api,
+ exp,
+ out,
+ i = 0;
+
+ data.args = it.args.slice();
+ api = apiStub(data);
+ sandbox.cli(api);
+ outcome = api.readLogs();
+
+ for (i; i < expectingLen; i += 1) {
+ exp = expecting[i];
+ out = outcome[i];
+
+ if ( typeof out === "string") {
+ out = /^.*/.exec(out.trim())[0];
+ }
+ if ( exp !== out ) {
+ Assert.fail("Expecting: " + exp + " Got: " + out);
+ }
+ }
+ Assert.pass();
+
+ }
+ }));
+ })(suiteix);
+ }
+ }
+
+ YUITest.TestRunner.add(suite);
+})();
Something went wrong with that request. Please try again.