Skip to content

Commit 8bdef13

Browse files
committed
test: add cli tests
1 parent 5769f5f commit 8bdef13

File tree

11 files changed

+144
-14
lines changed

11 files changed

+144
-14
lines changed

.github/workflows/main.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ jobs:
2525
- name: Lint
2626
run: yarn lint
2727

28-
- name: Test
29-
run: yarn test --ci --coverage --maxWorkers=2
30-
3128
- name: Build
3229
run: yarn build
3330

31+
- name: Test
32+
run: yarn test --ci --coverage --maxWorkers=2
33+
3434
publish:
3535
needs: build
3636
runs-on: ubuntu-latest

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"**/tsdx/ts-jest": "27.1.3"
4242
},
4343
"devDependencies": {
44+
"@types/fs-extra": "^9.0.13",
4445
"@types/glob": "^7.2.0",
4546
"@types/node": "^17.0.13",
4647
"dripip": "^0.10.0",
@@ -55,6 +56,7 @@
5556
"bundle-require": "^3.0.2",
5657
"cac": "^6.7.12",
5758
"esbuild": "^0.14.14",
59+
"fs-extra": "^10.0.0",
5860
"glob": "^7.2.0",
5961
"postcss": "^8.4.5"
6062
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.a {
2+
color: green;
3+
}
4+
5+
.b {
6+
margin-top: 123px;
7+
}
8+
9+
.c {
10+
background-color: orange;
11+
color: #fff;
12+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.a {
2+
color: red;
3+
}
4+
5+
.b {
6+
margin-top: 123px;
7+
}
8+
9+
.c {
10+
background-color: orange;
11+
color: red;
12+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.a {
2+
color: green;
3+
}
4+
5+
.b {
6+
margin-top: 123px;
7+
}
8+
9+
.c {
10+
background-color: orange;
11+
color: #fff;
12+
}
13+
14+
.camelCase {
15+
border: 1px solid red;
16+
}
17+
18+
.kebab-case {
19+
border: 1px solid blue;
20+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.A {
2+
color: green;
3+
}
4+
5+
.B {
6+
margin-top: 123px;
7+
}
8+
9+
.C {
10+
background-color: orange;
11+
color: #fff;
12+
}
13+
14+
.CAMELCASE {
15+
border: 1px solid red;
16+
}
17+
18+
.KEBAB-CASE {
19+
border: 1px solid blue;
20+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Transform } from '../../src';
2+
3+
export const transform: Transform = (fileInfo, api) => {
4+
const root = api.parse(fileInfo.source);
5+
6+
root.walk(node => {
7+
if (node.type === 'rule') {
8+
node.selector = node.selector.toUpperCase();
9+
}
10+
});
11+
12+
return root.toString();
13+
};

test/cli.test.ts

+46-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
1+
import { execSync } from 'child_process';
2+
import glob from 'glob';
3+
import path from 'path';
4+
import fs from 'fs-extra';
5+
6+
const bin = './dist/cli.js';
7+
const cache = path.join(__dirname, '.cache');
8+
9+
const run = (recipe: string) => {
10+
// Setup the test files
11+
const originalInput = `./recipes/${recipe}/input`;
12+
const inputDest = path.resolve(cache, recipe);
13+
fs.copySync(originalInput, inputDest);
14+
15+
// Run the command
16+
const transform = `./recipes/${recipe}/transform.ts`;
17+
const inputGlob = `${inputDest}/**/*.css`;
18+
const command = `${bin} -t ${transform} '${inputGlob}'`;
19+
execSync(command);
20+
21+
// Compare results
22+
const expectedOutput = `./recipes/${recipe}/output`;
23+
const expectedOutputGlob = `${expectedOutput}/**/*.css`;
24+
const expectedFiles = glob.sync(expectedOutputGlob);
25+
26+
expectedFiles.forEach(expectedFile => {
27+
const uniquePath = path.relative(expectedOutput, expectedFile);
28+
const actualFilePath = path.join(inputDest, uniquePath);
29+
const expectedContent = fs.readFileSync(expectedFile).toString();
30+
const actualContent = fs.readFileSync(actualFilePath).toString();
31+
expect(actualContent).toEqual(expectedContent);
32+
});
33+
};
34+
35+
const recipes = fs.readdirSync(path.resolve(__dirname, '../recipes'));
36+
137
describe('cli', () => {
2-
it('should perform file transformations', () => {
3-
// cli();
38+
beforeAll(() => {
39+
fs.removeSync(cache);
40+
});
41+
42+
afterAll(() => {
43+
fs.removeSync(cache);
44+
});
45+
46+
it.each(recipes)('should perform %s transform correctly', recipe => {
47+
run(recipe);
448
});
549
});

test/perform.test.ts

-9
This file was deleted.

yarn.lock

+16
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,13 @@
14991499
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
15001500
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==
15011501

1502+
"@types/fs-extra@^9.0.13":
1503+
version "9.0.13"
1504+
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.13.tgz#7594fbae04fe7f1918ce8b3d213f74ff44ac1f45"
1505+
integrity sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==
1506+
dependencies:
1507+
"@types/node" "*"
1508+
15021509
"@types/glob@^7.2.0":
15031510
version "7.2.0"
15041511
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.2.0.tgz#bc1b5bf3aa92f25bd5dd39f35c57361bdce5b2eb"
@@ -3282,6 +3289,15 @@ fs-extra@8.1.0, fs-extra@^8.1:
32823289
jsonfile "^4.0.0"
32833290
universalify "^0.1.0"
32843291

3292+
fs-extra@^10.0.0:
3293+
version "10.0.0"
3294+
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-10.0.0.tgz#9ff61b655dde53fb34a82df84bb214ce802e17c1"
3295+
integrity sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==
3296+
dependencies:
3297+
graceful-fs "^4.2.0"
3298+
jsonfile "^6.0.1"
3299+
universalify "^2.0.0"
3300+
32853301
fs-extra@^9.0.0:
32863302
version "9.1.0"
32873303
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.1.0.tgz#5954460c764a8da2094ba3554bf839e6b9a7c86d"

0 commit comments

Comments
 (0)