Skip to content

Commit 0003ee5

Browse files
committed
possibility to generate tests
1 parent 23ea59b commit 0003ee5

File tree

7 files changed

+343
-78
lines changed

7 files changed

+343
-78
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dist
22
coverage
33
node_modules
4+
test/common-test-cases.js

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
.git*
44
.npmignore
5+
generate-tests.js
56
test/
67
src/

generate-tests.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { existsSync, readdirSync, writeFile } from 'fs';
2+
import { join, resolve, sep } from 'path';
3+
import { toArray } from 'lodash';
4+
5+
const destination = resolve('test/common-test-cases.js');
6+
const cases = ['test-cases', 'cssi'];
7+
8+
function resolveTo() {
9+
const args = toArray(arguments);
10+
return resolve(join.apply(null, ['test'].concat(args)));
11+
}
12+
13+
let content =
14+
`import { equal } from 'assert';\n`+
15+
`import { readFileSync } from 'fs';\n`+
16+
`import { resolve } from 'path';\n`+
17+
`import { extend } from 'lodash';\n`+
18+
`import FileSystemLoader from 'css-modules-loader-core/lib/file-system-loader';\n`+
19+
`import hook from '../src';\n`+
20+
`\n`+
21+
`const normalize = str => str.replace(/\\r\\n?/g, '\\n');\n`+
22+
`const pipelines = {\n`+
23+
` 'test-cases': undefined,\n`+
24+
` 'cssi': [],\n`+
25+
`};\n`+
26+
`\n`+
27+
`let expectedCSS;\n`+
28+
`let expectedTokens;\n`+
29+
`\n`+
30+
`describe('common-test-cases', () => {\n`;
31+
32+
cases.forEach(dirname => {
33+
content +=
34+
`\n`+
35+
` describe('${dirname}', () => {\n`;
36+
37+
readdirSync(resolveTo(dirname)).forEach(testCase => {
38+
if (existsSync(resolveTo(dirname, testCase, 'source.css'))) {
39+
40+
content +=
41+
`\n`+
42+
` describe('${testCase.replace(/-/g, ' ')}', () => {\n`+
43+
` before(() => {\n`+
44+
` expectedCSS = normalize(readFileSync(resolve('test${sep + dirname + sep + testCase + sep}expected.css'), 'utf8'));\n`+
45+
` expectedTokens = JSON.parse(readFileSync(resolve('test${sep + dirname + sep + testCase + sep}expected.json'), 'utf8'));\n`+
46+
` hook({rootDir: resolve('test${sep + dirname}'), use: pipelines['${dirname}']});\n`+
47+
` });\n`+
48+
`\n`+
49+
` it('loader-core', done => {\n`+
50+
` const loader = new FileSystemLoader(resolve('test${sep + dirname}'), pipelines['${dirname}']);\n`+
51+
`\n`+
52+
` loader.fetch('${testCase + sep}source.css', '/')\n`+
53+
` .then(tokens => {\n`+
54+
` equal(loader.finalSource, expectedCSS);\n`+
55+
` equal(JSON.stringify(tokens), JSON.stringify(expectedTokens));\n`+
56+
` })\n`+
57+
` .then(done, done);\n`+
58+
` });\n`+
59+
`\n`+
60+
` it('require-hook', () => {\n`+
61+
` const tokens = require(resolve('test${sep + dirname + sep + testCase + sep}source.css'));\n`+
62+
` equal(JSON.stringify(tokens), JSON.stringify(expectedTokens));\n`+
63+
` });\n`+
64+
` });\n`;
65+
66+
} else {
67+
68+
content +=
69+
`\n`+
70+
` describe('${testCase.replace(/-/g, ' ')}', () => {\n`+
71+
` before(() => {\n`+
72+
` expectedCSS = normalize(readFileSync(resolve('test${sep + dirname + sep + testCase + sep}expected.css'), 'utf8'));\n`+
73+
` expectedTokens = JSON.parse(readFileSync(resolve('test${sep + dirname + sep + testCase + sep}expected.json'), 'utf8'));\n`+
74+
` hook({rootDir: resolve('test${sep + dirname}'), use: pipelines['${dirname}']});\n`+
75+
` });\n`+
76+
`\n`+
77+
` it('loader-core', done => {\n`+
78+
` const loader = new FileSystemLoader(resolve('test${sep + dirname}'), pipelines['${dirname}']);\n`+
79+
`\n`+
80+
` loader.fetch('${testCase + sep}source1.css', '/').then(tokens1 => {\n`+
81+
` loader.fetch('${testCase + sep}source2.css', '/').then(tokens2 => {\n`+
82+
` equal(loader.finalSource, expectedCSS);\n`+
83+
` const tokens = extend({}, tokens1, tokens2);\n`+
84+
` equal(JSON.stringify(tokens), JSON.stringify(expectedTokens));\n`+
85+
` }).then(done, done);\n`+
86+
` }).catch(done);\n`+
87+
` });\n`+
88+
`\n`+
89+
` it('require-hook', () => {\n`+
90+
` const tokens = extend({},\n`+
91+
` require(resolve('test${sep + dirname + sep + testCase + sep}source1.css')),\n`+
92+
` require(resolve('test${sep + dirname + sep + testCase + sep}source2.css'))\n`+
93+
` );\n`+
94+
`\n`+
95+
` equal(JSON.stringify(tokens), JSON.stringify(expectedTokens));\n`+
96+
` });\n`+
97+
` });\n`;
98+
99+
}
100+
});
101+
102+
content +=
103+
`\n`+
104+
` });\n`;
105+
});
106+
107+
content +=
108+
`\n`+
109+
`});\n`;
110+
111+
writeFile(destination, content, 'utf8', err => {
112+
if (err) {
113+
throw err;
114+
}
115+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"pretest": "npm run -s lint",
3939
"test": "mocha --compilers js:babel/register",
4040
"test:cov": "`npm bin`/babel-node `npm bin`/isparta cover --report text --report html `npm bin`/_mocha",
41+
"test:gen": "babel-node generate-tests",
4142
"build": "babel src --out-dir dist",
4243
"prepublish": "in-publish && npm run -s build || in-install"
4344
},

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let rootDir = process.cwd();
1313

1414
export default function (opts = {}) {
1515
plugins = opts.use ? opts.use : [ExtractImports, LocalByDefault, Scope];
16-
rootDir = opts.root ? opts.root : process.cwd();
16+
rootDir = opts.rootDir ? opts.rootDir : process.cwd();
1717
}
1818

1919
function pathFetcher(_newPath, sourcePath, trace) {

test/common-cases.js

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)