Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Refactor tests #27

Merged
merged 12 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 11 additions & 40 deletions .github/workflows/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,58 +77,29 @@ jobs:
- name: Build
run: npm run build

# Run tests
#
# - Run from $RUNNER_TEMP for auto-cleanup.
# - `./dist/main.js` is executing local `css-typed` as if installed (same as `bin`).
# But it is `$GITHUB_WORKSPACE/dist/main.js` b/c we `cd $RUNNER_TEMP`.
# - Use `diff` to compare the files.
# Use `-I '//.*'` to ignore the first line (comment) which has generated path and timestamp.

- name: "Test 1: Default case"
run: |
cp src/fixtures/casing/casing.css $RUNNER_TEMP/casing.css
cp src/fixtures/casing/dashesOnly.d.css.ts $RUNNER_TEMP/expected.d.css.ts

cd $RUNNER_TEMP
$GITHUB_WORKSPACE/dist/main.js '*.css'
diff --strip-trailing-cr -uI '//.*' expected.d.css.ts casing.d.css.ts
scripts/test.sh foo

- name: "Test 2: localsConvention, second position"
- name: "Test 2: localsConvention, first position"
if: success() || failure()
run: |
cp src/fixtures/casing/casing.css $RUNNER_TEMP/casing.css
cp src/fixtures/casing/camelCaseOnly.d.css.ts $RUNNER_TEMP/expected.d.css.ts

cd $RUNNER_TEMP
$GITHUB_WORKSPACE/dist/main.js '*.css' --localsConvention camelCaseOnly
diff --strip-trailing-cr -uI '//.*' expected.d.css.ts casing.d.css.ts
scripts/test.sh casing/casing "--localsConvention camelCaseOnly" casing/camelCaseOnly

- name: "Test 3: localsConvention, first position"
- name: "Test 3: localsConvention, second position"
if: success() || failure()
run: |
cp src/fixtures/casing/casing.css $RUNNER_TEMP/casing.css
cp src/fixtures/casing/camelCaseOnly.d.css.ts $RUNNER_TEMP/expected.d.css.ts

cd $RUNNER_TEMP
$GITHUB_WORKSPACE/dist/main.js --localsConvention camelCaseOnly '*.css'
diff --strip-trailing-cr -uI '//.*' expected.d.css.ts casing.d.css.ts
scripts/test.sh casing/casing "" casing/camelCaseOnly "--localsConvention camelCaseOnly"

- name: "Test 4: relative outdir"
if: success() || failure()
run: |
cp src/fixtures/casing/casing.css $RUNNER_TEMP/casing.css
cp src/fixtures/casing/dashesOnly.d.css.ts $RUNNER_TEMP/expected.d.css.ts

cd $RUNNER_TEMP
$GITHUB_WORKSPACE/dist/main.js '*.css' --outdir generated
diff --strip-trailing-cr -uI '//.*' expected.d.css.ts generated/casing.d.css.ts
scripts/test.sh foo "--outdir generated" "" "" generated/

- name: "Test 5: absolute outdir"
if: success() || failure()
run: |
cp src/fixtures/casing/casing.css $RUNNER_TEMP/casing.css
cp src/fixtures/casing/dashesOnly.d.css.ts $RUNNER_TEMP/expected.d.css.ts

cd $RUNNER_TEMP
$GITHUB_WORKSPACE/dist/main.js '*.css' -o $GITHUB_WORKSPACE/generated
diff --strip-trailing-cr -uI '//.*' expected.d.css.ts $GITHUB_WORKSPACE/generated/casing.d.css.ts
scripts/test.sh foo "-o $GITHUB_WORKSPACE/generated" "" "" "$GITHUB_WORKSPACE"/generated/

Publish:
if: ${{ github.ref == 'refs/heads/main' }}
Expand Down
13 changes: 8 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in BCP 14 [[RFC2119]] [[RFC8174]] when, and only when, they appear in all capitals, as shown here.
The keywords "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in BCP 14 [[RFC2119]] [[RFC8174]] when, and only when, they appear in all capitals, as shown here.

## Getting started

Expand All @@ -20,9 +20,12 @@ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "S

The [src](./src) directory contains the main and test sources.

- [main.js](./src/main.js) represents the entry point (the CLI tool).
- [generate-declaration.js](./src/generate-declaration.js) represents the unit-tested JS logic.
- [fixtures](./src/fixtures) directory contains files for data-file-driven unit tests.
- [main.ts](./src/main.ts) represents the entry point (the CLI tool).
- [logic.ts](./src/logic.ts) represents the unit-tested logic.

The [fixtures](fixtures) directory contains files for data-file-driven unit tests.

The [scripts](./scripts) directory contains the esbuild build script and pipeline test helper script.

## Expectations

Expand All @@ -31,7 +34,7 @@ All contributions MUST adhere to the following expectations.
1. Every change MUST have unit tests.
2. Every change MUST have a GitHub issue linked.
3. Any configuration option change SHOULD be discussed in a GitHub issue first.
4. The PR build (see [pipeline.yaml](./.github/workflows/pipeline.yaml)) MUST succeed.
4. The PR build and test (see [pipeline.yaml](./.github/workflows/pipeline.yaml)) MUST succeed.
5. I will squash-merge the changeset into `main` upon approval.

[RFC2119]: https://www.rfc-editor.org/rfc/rfc2119
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export default [
...connorjsConfig,
{
// Ignore declaration files used for tests. These represent generated files.
ignores: [`src/fixtures/**/*.d.css.ts`],
ignores: [`fixtures/**/*.d.css.ts`],
},
];
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
// Generated from `fixtures/casing/casing.css` by css-typed at $TIME

const lowercase: string;
const UPPERCASE: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
// Generated from `fixtures/casing/casing.css` by css-typed at $TIME

export const lowercase: string;
export const uppercase: string;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
// Generated from `fixtures/casing/casing.css` by css-typed at $TIME

const lowercase: string;
const UPPERCASE: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
// Generated from `fixtures/casing/casing.css` by css-typed at $TIME

export const lowercase: string;
export const UPPERCASE: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
// Generated from `fixtures/casing/casing.css` by css-typed at $TIME

const lowercase: string;
const UPPERCASE: string;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/fixtures/foo.d.css.ts → fixtures/foo.d.css.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from `src/fixtures/foo.css` by css-typed at $TIME
// Generated from `fixtures/foo.css` by css-typed at $TIME

export const foo: string;
export const bar: string;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from `src/fixtures/foo.module.css` by css-typed at $TIME
// Generated from `fixtures/foo.module.css` by css-typed at $TIME

export const foo: string;
export const bar: string;
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
"engines": {
"node": ">=18"
},
"engineStrict": true,
"bin": {
"css-typed": "dist/main.js"
},
"files": [
"dist"
],
"main": "dist/main.js",
"scripts": {
"build": "node scripts/build.js",
"ci-build": "npm-run-all -l -p eslint prettier test tsc -s build",
Expand All @@ -43,7 +45,6 @@
"test": "vitest run",
"tsc": "tsc"
},
"engineStrict": true,
"dependencies": {
"@commander-js/extra-typings": "^12.1.0",
"commander": "^12.1.0",
Expand All @@ -57,14 +58,13 @@
"@types/lodash.camelcase": "^4.3.9",
"@types/node": "^20.14.14",
"esbuild": "~0.23.0",
"eslint-config-connorjs": "^1.0.0",
"eslint-config-connorjs": "^1.1.0",
"husky": "^9.1.4",
"is-ci": "^3.0.1",
"lint-staged": "^15.2.8",
"npm-run-all": "^4.1.5",
"prettier": "^3.3.3",
"typescript": "^5.5.4",
"vitest": "^2.0.5"
},
"main": "dist/main.js"
}
}
31 changes: 31 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

# $1 is the input name, relative to `fixtures`. Required.
input=$1

# $2 is the standard (before) options. Defaults to "".
IFS=" " read -r -a beforeOpts <<< "${2:-}"

# $3 is the output name, relative to `fixtures`. Defaults to $1.
output=${3:-$1}

# $4 is the after options. Use an array. Defaults to "".
IFS=" " read -r -a afterOpts <<< "${4:-}"

# $5 is the path prefix for output. Defaults to "".
prefix=${5:-}

# Run from $RUNNER_TEMP for auto-cleanup.
cp fixtures/${input}.css $RUNNER_TEMP/test.css
cp fixtures/${output}.d.css.ts $RUNNER_TEMP/expected.d.css.ts
pushd $RUNNER_TEMP > /dev/null || exit

# `./dist/main.js` is executing local `css-typed` as if installed (same as `bin`).
# But it is `$GITHUB_WORKSPACE/dist/main.js` b/c we `cd $RUNNER_TEMP`.
echo "css-typed ${beforeOpts[*]} \"*.css\" ${afterOpts[*]}"
# shellcheck disable=SC2068
$GITHUB_WORKSPACE/dist/main.js ${beforeOpts[@]} "*.css" ${afterOpts[@]}

# Use `diff` to compare the files.
# Use `-I '//.*'` to ignore the first line (comment) which has generated path and timestamp.
diff --color=auto --strip-trailing-cr -uI "//.*" expected.d.css.ts ${prefix}test.d.css.ts
7 changes: 2 additions & 5 deletions src/logic.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { readFileSync } from "node:fs";
import path from "node:path";
import * as process from "node:process";

Expand Down Expand Up @@ -31,10 +30,8 @@ describe(`css-typed`, () => {
const inputPath = fixtureFile(inputFilename);
const outputPath = fixtureFile(outputFilename);

const expected = readFileSync(outputPath, { encoding: `utf8` });

const generated = await generateDeclaration(inputPath, `$TIME`, options);
expect(generated).toStrictEqual(expected);
await expect(generated).toMatchFileSnapshot(outputPath);
});
});

Expand All @@ -57,5 +54,5 @@ describe(`css-typed`, () => {
});

function fixtureFile(filename: string) {
return path.join(import.meta.dirname, `fixtures`, filename);
return path.join(import.meta.dirname, `..`, `fixtures`, filename);
}