Skip to content

Commit

Permalink
Add linter and linter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed Aug 19, 2021
1 parent 2518f4a commit adf5c76
Show file tree
Hide file tree
Showing 13 changed files with 664 additions and 197 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.cache
build
**/node_modules/**
29 changes: 29 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true,
"node": true,
"jest": true
},
"globals": {},
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
"parserOptions": {
"ecmaVersion": 12
},
"plugins": ["prettier"],
"rules": {
"no-unused-vars": ["error", { "varsIgnorePattern": "^omit.*$" }],
"array-callback-return": "off",
"arrow-parens": ["error", "as-needed"],
"prettier/prettier": [
"error",
{
"quoteProps": "consistent",
"semi": false,
"arrowParens": "avoid",
"singleQuote": true
}
]
}
}
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ name: Tests
on:
pull_request:
push:
# trying and staging branches are for BORS config
branches:
- trying
- staging
- main

jobs:
Expand All @@ -23,3 +26,14 @@ jobs:
run: yarn --dev
- name: Run tests
run: yarn test
linter_check:
runs-on: ubuntu-latest
name: linter test
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v1
- name: Install dependencies
run: yarn --dev
- name: Run style check
run: yarn lint
106 changes: 56 additions & 50 deletions __tests__/combine.tests.js
Original file line number Diff line number Diff line change
@@ -1,118 +1,124 @@
const combineJson = require('../src/combine-json');
const fs = require('fs');
const rimraf = require('rimraf');
const combineJson = require('../src/combine-json')
const fs = require('fs')
const rimraf = require('rimraf')

const OUTPUT_DIR = 'test-output';
const OUTPUT_DIR = 'test-output'

beforeAll(() => {
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR);
fs.mkdirSync(OUTPUT_DIR)
}
});
})

test('Tests on 1 empty file', async () => {
const res = await combineJson({
inputDir: 'misc/one_empty',
outputFile: 'test-output/combine_empty.json',
});
})

const data = JSON.parse(
fs.readFileSync(`${process.cwd()}/test-output/combine_empty.json`, 'utf-8')
);
const expected = [];
expect(res).toBe(1);
expect(data).toEqual(expected);
});
)
const expected = []
expect(res).toBe(1)
expect(data).toEqual(expected)
})

test('Tests on multiple empty files', async () => {
const res = await combineJson({
inputDir: 'misc/multiple_empty',
outputFile: 'test-output/combine_multiple_empty.json',
});
})
const data = JSON.parse(
fs.readFileSync(
`${process.cwd()}/test-output/combine_multiple_empty.json`,
'utf-8'
)
);
const expected = [];
expect(res).toBe(1);
expect(data).toEqual(expected);
});
)
const expected = []
expect(res).toBe(1)
expect(data).toEqual(expected)
})

test('Tests on some invalid files and some valid', async () => {
const res = await combineJson({
inputDir: 'misc/multiple_empty',
outputFile: 'test-output/combine_multiple_empty.json',
});
})
const data = JSON.parse(
fs.readFileSync(
`${process.cwd()}/test-output/combine_multiple_empty.json`,
'utf-8'
)
);
const expected = [];
expect(res).toBe(1);
expect(data).toEqual(expected);
});
)
const expected = []
expect(res).toBe(1)
expect(data).toEqual(expected)
})

test('Tests if on 1 file containing one primitive', async () => {
const res = await combineJson({
inputDir: 'misc/one_primitive',
outputFile: 'test-output/combine_a_single_primitive.json',
});
})
const data = JSON.parse(
fs.readFileSync(
`${process.cwd()}/test-output/combine_a_single_primitive.json`,
'utf-8'
)
);
const expected = [1];
expect(res).toBe(1);
expect(data).toEqual(expected);
});
)
const expected = [1]
expect(res).toBe(1)
expect(data).toEqual(expected)
})

test('Tests if on 1 files', async () => {
const res = await combineJson({
inputDir: 'misc/one_file',
outputFile: 'test-output/combine_one.json',
});
})
const data = JSON.parse(
fs.readFileSync(`${process.cwd()}/test-output/combine_one.json`, 'utf-8')
);
const expected = [{ name: 'Hello' }, { name: 'Hello' }, { name: 'Hello' }];
expect(res).toBe(1);
expect(data).toEqual(expected);
});
)
const expected = [{ name: 'Hello' }, { name: 'Hello' }, { name: 'Hello' }]
expect(res).toBe(1)
expect(data).toEqual(expected)
})

test('Tests on 3 array', async () => {
const res = await combineJson({
inputDir: 'misc/array_test',
outputFile: 'test-output/combine_array.json',
});
})
const data = JSON.parse(
fs.readFileSync(`${process.cwd()}/test-output/combine_array.json`, 'utf-8')
);
const expected = [{ name: 'far away' }, { name: 'far away and behind' }, 1, 2, 3];
expect(res).toBe(1);
expect(data).toEqual(expected);
});
)
const expected = [
{ name: 'far away' },
{ name: 'far away and behind' },
1,
2,
3,
]
expect(res).toBe(1)
expect(data).toEqual(expected)
})

test('Tests if on all files', async () => {
const res = await combineJson({
inputDir: 'misc',
outputFile: 'test-output/combine_all.json',
});
})
const data = JSON.parse(
fs.readFileSync(`${process.cwd()}/test-output/combine_all.json`, 'utf-8')
);
const expected = require('./assets/combine_all.json');
expect(res).toBe(1);
expect(data).toEqual(expected);
});
)
const expected = require('./assets/combine_all.json')
expect(res).toBe(1)
expect(data).toEqual(expected)
})

afterAll(() => {
if (fs.existsSync(OUTPUT_DIR)) {
// rimraf.sync(OUTPUT_DIR);
rimraf.sync(OUTPUT_DIR)
}
});
})
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
verbose: true,
};
}

module.exports = config;
module.exports = config

// Or async function
module.exports = async () => {
Expand All @@ -14,5 +14,5 @@ module.exports = async () => {
watchPathIgnorePatterns: ['<rootDir>/test-output'],
rootDir: '.',
testEnvironment: 'node',
};
};
}
}
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"scripts": {
"start": "src/cli.js",
"test": "jest",
"test:watch": "npx jest --watch"
"test:watch": "npx jest --watch",
"lint": "eslint --ext .js .",
"lint:fix": "eslint --ext .js . --fix"
},
"preferGlobal": true,
"bugs": {
Expand Down Expand Up @@ -38,7 +40,10 @@
},
"devDependencies": {
"jest": "^26.6.3",
"prettier": "2.3.0",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"eslint": "^7.29.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"prettier": "^2.3.2"
}
}
29 changes: 11 additions & 18 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#!/usr/bin/env node
const commander = require('commander');
const pkg = require('../package.json');
const combineJson = require('./combine-json');
const commander = require('commander')
const pkg = require('../package.json')
const combineJson = require('./combine-json')

const program = new commander.Command()


program
.argument(
'<input-directory>',
'Directory from witch to fetch the json files'
)
.argument('<input-directory>', 'Directory from witch to fetch the json files')
.version(pkg.version)
.option(
'-b, --buffer-size <integer>',
Expand All @@ -22,19 +18,16 @@ program
'combine.json'
)
.action(async (directory, options) => {
await combineJson({ options, inputDir: directory});
});



(async () => {
await combineJson({ options, inputDir: directory })
})
;(async () => {
try {
if (process.argv.length < 3) {
console.log( program.helpInformation() );
console.log(program.helpInformation())
}
await program.parse()
} catch (e) {
console.error(e);
throw e;
console.error(e)
throw e
}
})();
})()
Loading

0 comments on commit adf5c76

Please sign in to comment.