Skip to content

Commit e6c1067

Browse files
committed
wraping up with cli tests+
CSSLint mix helper from utils to mix up cli and rc options
1 parent 517ffe4 commit e6c1067

File tree

4 files changed

+213
-23
lines changed

4 files changed

+213
-23
lines changed

src/cli/common.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -323,28 +323,6 @@ function cli(api){
323323
return options;
324324
}
325325

326-
function mergeOptions(/*arguments*/) {
327-
var allOptions = Array.apply(null, arguments).sort(),
328-
allOptionsCount = allOptions.length,
329-
options = allOptions[0],
330-
overrideOptions,
331-
overrideOptionix,
332-
overrideOption;
333-
334-
for (var i = 1; i < allOptionsCount; i += 1) {
335-
overrideOptions = allOptions[i];
336-
337-
for (overrideOptionix in overrideOptions) {
338-
if (overrideOptions.hasOwnProperty(overrideOptionix)) {
339-
overrideOption = overrideOptions[overrideOptionix];
340-
options[overrideOptionix] = overrideOption;
341-
}
342-
}
343-
}
344-
345-
return options;
346-
}
347-
348326
//-----------------------------------------------------------------------------
349327
// Process command line
350328
//-----------------------------------------------------------------------------
@@ -377,7 +355,11 @@ function cli(api){
377355
rcOptions = readConfigData(cliOptions.config);
378356

379357
// Command line arguments override config file
380-
options = mergeOptions(rcOptions, cliOptions);
358+
options = CSSLint.Util.mix(rcOptions, cliOptions);
359+
360+
// hot fix for CSSLint.Util.mix current behavior
361+
// https://github.com/CSSLint/csslint/issues/501
362+
options = rcOptions;
381363

382364
// Validate options
383365
validateOptions(options);

tests/cli/assets/apiStub.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*jshint node:true*/
2+
"use strict";
3+
var
4+
stub = {
5+
logbook: function (log) {
6+
this.logs.push(log);
7+
},
8+
readLogs: function () {
9+
return this.logs.slice();
10+
},
11+
12+
getFullPath: function (path) {
13+
return path;
14+
},
15+
getFiles: function (dir) {
16+
var
17+
filesobj = this.fakedFs[dir],
18+
fileix,
19+
out = [];
20+
for (fileix in filesobj) {
21+
if ( filesobj.hasOwnProperty(fileix) && /\.css$/.test(fileix) ) {
22+
out.push(dir + "/" + fileix);
23+
}
24+
}
25+
return out;
26+
},
27+
readFile: function (path) {
28+
var
29+
spath = path.split("/"),
30+
spathLen = spath.length,
31+
i,
32+
out = this.fakedFs;
33+
34+
for (i = 0; i < spathLen; i += 1) {
35+
out = out[spath[i]];
36+
}
37+
38+
return out;
39+
},
40+
isDirectory: function (checkit) {
41+
var
42+
result = this.fakedFs[checkit];
43+
return typeof result === "object";
44+
},
45+
print: function (msg) {
46+
this.logbook(msg);
47+
},
48+
quit: function (signal) {
49+
this.logbook(signal);
50+
}
51+
};
52+
53+
module.exports = function (setup) {
54+
var
55+
api,
56+
setix;
57+
58+
api = Object.create(stub);
59+
60+
for (setix in setup) {
61+
if (setup.hasOwnProperty(setix)) {
62+
api[setix] = setup[setix];
63+
}
64+
}
65+
66+
api.logs = [];
67+
return api;
68+
};

tests/cli/assets/data.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*jshint node:true*/
2+
module.exports = {
3+
"suites": {
4+
"config csslintrc override": {
5+
"args": [
6+
"--config=.rc1",
7+
"dir"
8+
],
9+
"expecting": [
10+
"csslint: No errors in dir/a.css.",
11+
"csslint: No errors in dir/b.css.",
12+
0
13+
]
14+
},
15+
"straight linting": {
16+
"args": [
17+
"dir"
18+
],
19+
"expecting": [
20+
"csslint: There is 1 problem in dir/a.css.",
21+
"csslint: There is 1 problem in dir/b.css.",
22+
0
23+
]
24+
},
25+
"mix of cli options": {
26+
"args": [
27+
"--config=.rc1",
28+
"--ignore=important",
29+
"dir"
30+
],
31+
"expecting": [
32+
"csslint: No errors in dir/a.css.",
33+
"csslint: There is 1 problem in dir/b.css.",
34+
0
35+
]
36+
},
37+
"more mixes of cli options": {
38+
"args": [
39+
"--config=.rc1",
40+
"--errors=important",
41+
"dir"
42+
],
43+
"expecting": [
44+
"csslint: There is 1 problem in dir/a.css.",
45+
"csslint: No errors in dir/b.css.",
46+
1
47+
]
48+
},
49+
"version": {
50+
"args": [
51+
"--version"
52+
],
53+
"expecting": [
54+
"v@VERSION@",
55+
0
56+
]
57+
}
58+
},
59+
60+
"fakedFs": {
61+
".rc1": "--ignore=important,ids",
62+
"dir": {
63+
"a.css": ".a {color: red!important;}",
64+
"b.css": "#a {color: red;}"
65+
},
66+
}
67+
};

tests/cli/cli-common.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*jshint loopfunc:true, node:true */
2+
"use strict";
3+
function include(path, sandbox) {
4+
var
5+
vm = require("vm"),
6+
fs = require("fs"),
7+
file;
8+
9+
file = fs.readFileSync(path);
10+
vm.runInNewContext(file, sandbox);
11+
}
12+
13+
14+
15+
(function(){
16+
17+
var Assert = YUITest.Assert,
18+
suite = new YUITest.TestSuite("General Tests for CLI"),
19+
apiStub = require("./tests/cli/assets/apiStub.js"),
20+
data = require("./tests/cli/assets/data.js"),
21+
suites = data.suites,
22+
suiteix,
23+
sandbox = {
24+
CSSLint: CSSLint
25+
};
26+
27+
include(__dirname + "/src/cli/common.js", sandbox); /* expose sandbox.cli */
28+
29+
for (suiteix in suites) {
30+
if (suites.hasOwnProperty(suiteix)) {
31+
(function (suiteix) {
32+
33+
suite.add(new YUITest.TestCase({
34+
35+
name: "Test " + suiteix,
36+
37+
"Outcome logs should match expected": function (){
38+
var
39+
it = suites[suiteix],
40+
expecting = it.expecting,
41+
expectingLen = expecting.length,
42+
outcome,
43+
api,
44+
exp,
45+
out,
46+
i = 0;
47+
48+
data.args = it.args.slice();
49+
api = apiStub(data);
50+
sandbox.cli(api);
51+
outcome = api.readLogs();
52+
53+
for (i; i < expectingLen; i += 1) {
54+
exp = expecting[i];
55+
out = outcome[i];
56+
57+
if ( typeof out === "string") {
58+
out = /^.*/.exec(out.trim())[0];
59+
}
60+
if ( exp !== out ) {
61+
Assert.fail("Expecting: " + exp + " Got: " + out);
62+
}
63+
}
64+
Assert.pass();
65+
66+
}
67+
}));
68+
})(suiteix);
69+
}
70+
}
71+
72+
YUITest.TestRunner.add(suite);
73+
})();

0 commit comments

Comments
 (0)