Skip to content

Commit 4b19770

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

File tree

4 files changed

+190
-23
lines changed

4 files changed

+190
-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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
"version": {
26+
"args": [
27+
"--version"
28+
],
29+
"expecting": [
30+
"v@VERSION@",
31+
0
32+
]
33+
}
34+
},
35+
36+
"fakedFs": {
37+
".rc1": "--ignore=important,ids",
38+
"dir": {
39+
"a.css": ".a {color: red!important;}",
40+
"b.css": "#a {color: red;}"
41+
},
42+
}
43+
};

tests/cli/cli-common.js

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

0 commit comments

Comments
 (0)