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

add initial implementation #2

Merged
merged 3 commits into from
Mar 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions .editorconfig
@@ -0,0 +1,18 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
insert_final_newline = true
indent_style = space
indent_size = 2

[*.hbs]
insert_final_newline = false

[*.{diff,md}]
trim_trailing_whitespace = false
19 changes: 19 additions & 0 deletions .eslintrc.cjs
@@ -0,0 +1,19 @@
module.exports = {
"env": {
"es2021": true,
"node": true
},
"extends": "eslint:recommended",
"overrides": [{
files: 'test/**/*.js',
env: {
mocha: true,
}
}],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
}
}
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,26 @@
name: Node.js CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
fail-fast: false

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm i -g npm@latest
- run: npm ci
- run: npm test
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/node_modules/
67 changes: 67 additions & 0 deletions README.md
@@ -0,0 +1,67 @@
# compare-fixture

This is a super simple tool to compare two folders. It is intended to be used to test build systems where you have a known expected output (a fixture) that you want to compare a build process against.

## Installation

You can use this tool directly with npx and not install it as follows:

```
npx compare-fixture
```

or you can install it locally to make sure that you don't need to install it before use:

```
npm i --save-dev compare-fixture
```

## Usage

```
npx compare-fixture <fixture folder> <comaprison folder>
```

This will test each file in `<fixture folder>` against each corresponding file in the `<comparison folder>`

**Note:** if any extra files exist in `<comparison folder>` that is **not considered an error**. This allows you to easily test a sub-section of your files with a sparse fixture. On the other hand, if a file exists in the fixture but is missing in the comparison folder then that is considered an error.

## Example output

```
thing.js is different in the fixture 🚨

+ expected - actual

function aVeryNiceTestFunction() {
- console.log('I really should impolement something here');
- console.log('I really should impolement something here');
- console.log('I really should impolement something here');
- console.log('I really should impolement something here');
- console.log('I really should impolement something here');
+ console.log('I really should implement something here');
+ console.log('I really should implement something here');
}

function badlyIndented() {
let items = [
'one',
- 'two',
+ 'two',
'three',
- 'four',
- 'five'
+ 'four',
+ 'five',
];
}
-
-function possiblyMissing() {
- console.log('a very important function');
-}
```

And if your terminal supports colours it will output the diff with colours:

<img width="715" alt="Screenshot 2023-03-19 at 16 25 18" src="https://user-images.githubusercontent.com/594890/226189889-3151b7fb-dbf6-4889-8b22-46b58dea26d6.png">

30 changes: 30 additions & 0 deletions index.js
@@ -0,0 +1,30 @@
import walkSync from 'walk-sync';
import { readFileSync } from 'fs';
import { join } from 'path';

import { generateDiff } from 'mocha-diff';

const fixtureDir = process.argv[2];
const comparisonDir = process.argv[3];

if (!fixtureDir) {
console.error('You need to provide the path to a fixture and to the comparison dir');
process.exit(1);
}

if (!comparisonDir) {
console.error('You need to provide the path to the comparison dir');
process.exit(1);
}

const fixtureFiles = walkSync(fixtureDir, { directories: false });

fixtureFiles.forEach((file) => {
const compareFile = readFileSync(join(comparisonDir, file), 'utf8');
const fixtureFile = readFileSync(join(fixtureDir, file), 'utf8');

if (compareFile != fixtureFile) {
console.log(`${file} is different in the fixture 🚨`);
console.log(generateDiff(compareFile, fixtureFile));
}
})