Skip to content

Commit

Permalink
Switch files being passed in to transformer (#599)
Browse files Browse the repository at this point in the history
Fixes #554
  • Loading branch information
tclindner committed Mar 13, 2022
1 parent 251d677 commit 7dee0d4
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Config {
if (this.configFile) {
debug(`Config file specified, loading it.`);
config = cosmiconfigSync('npmpackagejsonlint', {
transform: transform(this.cwd, this.configBaseDirectory, this.configFile),
transform: transform(this.cwd, this.configBaseDirectory, filePath),
}).load(this.configFile);
} else {
debug(`Config file wasn't specified, searching for config.`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "npm-package-json-lint-config-default",
"rules": {
"require-keywords": "off",
"require-private": "error",
"valid-values-private": ["error", [true]]
},
"overrides": [
{
"patterns": ["packages/*/package.json"],
"rules": {
"require-private": "off",
"valid-values-private": "off",
"require-repository-directory": "error"
}
}
]
}
17 changes: 17 additions & 0 deletions test/fixtures/config-file-with-overrides/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "npm-package-json-lint-errors",
"version": "0.1.0",
"description": "CLI app for linting package.json files.",
"keywords": [
"lint"
],
"homepage": "https://github.com/tclindner/npm-package-json-lint",
"author": "Thomas Lindner",
"repository": {
"type": "git",
"url": "https://github.com/tclindner/npm-package-json-lint"
},
"devDependencies": {
"mocha": "^2.4.5"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "npm-package-json-lint-errors",
"version": "0.1.0",
"description": "CLI app for linting package.json files.",
"keywords": [
"lint"
],
"homepage": "https://github.com/tclindner/npm-package-json-lint",
"author": "Thomas Lindner",
"repository": {
"type": "git",
"url": "https://github.com/tclindner/npm-package-json-lint"
},
"devDependencies": {
"mocha": "^2.4.5"
}
}
40 changes: 40 additions & 0 deletions test/integration/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,46 @@ Totals
});
});

describe('when the cli is run using a config file with overrides and nested packages', () => {
test('run for main pkg. results and totals will be output', () => {
const cli = spawnSync('../../../dist/cli.js', ['-c', '.npmpackagejsonlintrc.json', './package.json'], {
env,
cwd: './test/fixtures/config-file-with-overrides',
});
const expected = `
./package.json
${figures.cross} require-private - node: private - private is required
1 error
0 warnings
`;

expect(cli.stdout.toString()).toStrictEqual(expected);
expect(cli.stderr.toString()).toStrictEqual('');
expect(cli.status).toStrictEqual(twoLintErrorsDetected);
});

test('run for sub pkg. results and totals will be output', () => {
const cli = spawnSync(
'../../../dist/cli.js',
['-c', '.npmpackagejsonlintrc.json', './packages/my-package/package.json'],
{
env,
cwd: './test/fixtures/config-file-with-overrides',
}
);
const expected = `
./packages/my-package/package.json
${figures.cross} require-repository-directory - node: repository - repository object missing directory property
1 error
0 warnings
`;

expect(cli.stdout.toString()).toStrictEqual(expected);
expect(cli.stderr.toString()).toStrictEqual('');
expect(cli.status).toStrictEqual(twoLintErrorsDetected);
});
});

describe('when the cli is run against a monorepo with overrides', () => {
test('each file results and totals will be output', () => {
const cli = spawnSync('../../../dist/cli.js', [`**/package.json`], {
Expand Down
31 changes: 16 additions & 15 deletions test/unit/utils/getFileList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ describe('getFileList Unit Tests', () => {

const results = getFileList(patterns, cwd);

expect(results[0]).toContain('/test/fixtures/configJavaScriptFile/package.json');
expect(results[1]).toContain('/test/fixtures/errors/package.json');
expect(results[2]).toContain('/test/fixtures/errorsAndWarnings/package.json');
expect(results[3]).toContain('/test/fixtures/hierarchyWithoutRoot/package.json');
expect(results[4]).toContain('/test/fixtures/ignorePath/package.json');
expect(results[5]).toContain('/test/fixtures/invalidConfig/package.json');
expect(results[6]).toContain('/test/fixtures/monorepo/package.json');
expect(results[7]).toContain('/test/fixtures/npmPackageJsonLintIgnore/package.json');
expect(results[8]).toContain('/test/fixtures/overrides/package.json');
expect(results[9]).toContain('/test/fixtures/packageJsonProperty/package.json');
expect(results[10]).toContain('/test/fixtures/valid/package.json');
expect(results[11]).toContain('/test/fixtures/warnings/package.json');
expect(results[12]).toContain('/test/fixtures/hierarchyWithoutRoot/subdirectory/package.json');
expect(results[13]).toContain('/test/fixtures/ignorePath/ignoredDirectory/package.json');
expect(results[14]).toContain('/test/fixtures/npmPackageJsonLintIgnore/ignoredDirectory/package.json');
expect(results[0]).toContain('/test/fixtures/config-file-with-overrides/package.json');
expect(results[1]).toContain('/test/fixtures/configJavaScriptFile/package.json');
expect(results[2]).toContain('/test/fixtures/errors/package.json');
expect(results[3]).toContain('/test/fixtures/errorsAndWarnings/package.json');
expect(results[4]).toContain('/test/fixtures/hierarchyWithoutRoot/package.json');
expect(results[5]).toContain('/test/fixtures/ignorePath/package.json');
expect(results[6]).toContain('/test/fixtures/invalidConfig/package.json');
expect(results[7]).toContain('/test/fixtures/monorepo/package.json');
expect(results[8]).toContain('/test/fixtures/npmPackageJsonLintIgnore/package.json');
expect(results[9]).toContain('/test/fixtures/overrides/package.json');
expect(results[10]).toContain('/test/fixtures/packageJsonProperty/package.json');
expect(results[11]).toContain('/test/fixtures/valid/package.json');
expect(results[12]).toContain('/test/fixtures/warnings/package.json');
expect(results[13]).toContain('/test/fixtures/hierarchyWithoutRoot/subdirectory/package.json');
expect(results[14]).toContain('/test/fixtures/ignorePath/ignoredDirectory/package.json');
expect(results[15]).toContain('/test/fixtures/npmPackageJsonLintIgnore/ignoredDirectory/package.json');
});
});

0 comments on commit 7dee0d4

Please sign in to comment.