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

lint related dependencies conform #45

Merged
merged 5 commits into from
Jan 23, 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
14 changes: 10 additions & 4 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('main', () => {
JSON.stringify({
packageManager: 'yarn',
engines: { node: 'test' },
devDependencies: { eslint: '1.1.0' },
}),
);
await writeFile(
Expand Down Expand Up @@ -106,6 +107,7 @@ repo-1
- Does the package have a well-formed manifest (\`package.json\`)? ✅
- Does the \`packageManager\` field in \`package.json\` conform? ✅
- Does the \`engines.node\` field in \`package.json\` conform? ✅
- Do the lint-related \`devDependencies\` in \`package.json\` conform? ✅
- Is \`README.md\` present? ✅
- Does the README conform by recommending the correct Yarn version to install? ✅
- Does the README conform by recommending node install from nodejs.org? ✅
Expand All @@ -114,7 +116,7 @@ repo-1
- Does the \`src/\` directory exist? ✅
- Is \`.nvmrc\` present, and does it conform? ✅

Results: 11 passed, 0 failed, 11 total
Results: 12 passed, 0 failed, 12 total
Elapsed time: 0 ms


Expand All @@ -125,6 +127,7 @@ repo-2
- Does the package have a well-formed manifest (\`package.json\`)? ✅
- Does the \`packageManager\` field in \`package.json\` conform? ✅
- Does the \`engines.node\` field in \`package.json\` conform? ✅
- Do the lint-related \`devDependencies\` in \`package.json\` conform? ✅
- Is \`README.md\` present? ✅
- Does the README conform by recommending the correct Yarn version to install? ✅
- Does the README conform by recommending node install from nodejs.org? ✅
Expand All @@ -133,7 +136,7 @@ repo-2
- Does the \`src/\` directory exist? ✅
- Is \`.nvmrc\` present, and does it conform? ✅

Results: 11 passed, 0 failed, 11 total
Results: 12 passed, 0 failed, 12 total
Elapsed time: 0 ms

`,
Expand Down Expand Up @@ -337,6 +340,7 @@ Elapsed time: 0 ms
JSON.stringify({
packageManager: 'yarn',
engines: { node: 'test' },
devDependencies: { eslint: '1.1.0' },
}),
);
await writeFile(
Expand Down Expand Up @@ -370,6 +374,7 @@ repo-1
- Does the package have a well-formed manifest (\`package.json\`)? ✅
- Does the \`packageManager\` field in \`package.json\` conform? ✅
- Does the \`engines.node\` field in \`package.json\` conform? ✅
- Do the lint-related \`devDependencies\` in \`package.json\` conform? ✅
- Is \`README.md\` present? ✅
- Does the README conform by recommending the correct Yarn version to install? ✅
- Does the README conform by recommending node install from nodejs.org? ✅
Expand All @@ -378,7 +383,7 @@ repo-1
- Does the \`src/\` directory exist? ✅
- Is \`.nvmrc\` present, and does it conform? ✅

Results: 11 passed, 0 failed, 11 total
Results: 12 passed, 0 failed, 12 total
Elapsed time: 0 ms


Expand All @@ -389,6 +394,7 @@ repo-2
- Does the package have a well-formed manifest (\`package.json\`)? ✅
- Does the \`packageManager\` field in \`package.json\` conform? ✅
- Does the \`engines.node\` field in \`package.json\` conform? ✅
- Do the lint-related \`devDependencies\` in \`package.json\` conform? ✅
- Is \`README.md\` present? ✅
- Does the README conform by recommending the correct Yarn version to install? ✅
- Does the README conform by recommending node install from nodejs.org? ✅
Expand All @@ -397,7 +403,7 @@ repo-2
- Does the \`src/\` directory exist? ✅
- Is \`.nvmrc\` present, and does it conform? ✅

Results: 11 passed, 0 failed, 11 total
Results: 12 passed, 0 failed, 12 total
Elapsed time: 0 ms

`,
Expand Down
2 changes: 2 additions & 0 deletions src/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import allYarnModernFilesConform from './all-yarn-modern-files-conform';
import classicYarnConfigFileAbsent from './classic-yarn-config-file-absent';
import packageEnginesNodeFieldConforms from './package-engines-node-field-conforms';
import packageLintDependenciesConform from './package-lint-dependencies-conform';
import packagePackageManagerFieldConforms from './package-package-manager-field-conforms';
import readmeListsCorrectYarnVersion from './readme-lists-correct-yarn-version';
import readmeListsNodejsWebsite from './readme-recommends-node-install';
Expand All @@ -20,4 +21,5 @@ export const rules = [
requireNvmrc,
packageEnginesNodeFieldConforms,
readmeListsNodejsWebsite,
packageLintDependenciesConform,
] as const;
24 changes: 20 additions & 4 deletions src/rules/package-engines-node-field-conforms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ describe('Rule: package-engines-node-field-conforms', () => {
});
await writeFile(
path.join(template.directoryPath, 'package.json'),
JSON.stringify({ packageManager: 'a', engines: { node: 'test' } }),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: { eslint: '1.0.0' },
}),
);
const project = buildMetaMaskRepository({
shortname: 'project',
directoryPath: path.join(sandbox.directoryPath, 'project'),
});
await writeFile(
path.join(project.directoryPath, 'package.json'),
JSON.stringify({ packageManager: 'a', engines: { node: 'test' } }),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: { eslint: '1.0.0' },
}),
);

const result = await packageEnginesNodeFieldConforms.execute({
Expand All @@ -46,15 +54,23 @@ describe('Rule: package-engines-node-field-conforms', () => {
});
await writeFile(
path.join(template.directoryPath, 'package.json'),
JSON.stringify({ packageManager: 'a', engines: { node: 'test1' } }),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test1' },
devDependencies: { eslint: '1.0.0' },
}),
);
const project = buildMetaMaskRepository({
shortname: 'project',
directoryPath: path.join(sandbox.directoryPath, 'project'),
});
await writeFile(
path.join(project.directoryPath, 'package.json'),
JSON.stringify({ packageManager: 'a', engines: { node: 'test2' } }),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test2' },
devDependencies: { eslint: '1.0.0' },
}),
);

const result = await packageEnginesNodeFieldConforms.execute({
Expand Down
206 changes: 206 additions & 0 deletions src/rules/package-lint-dependencies-conform.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import { writeFile } from '@metamask/utils/node';
import path from 'path';

import packageLintDependenciesConform from './package-lint-dependencies-conform';
import { buildMetaMaskRepository, withinSandbox } from '../../tests/helpers';
import { fail, pass } from '../rule-helpers';

describe('Rule: package-lint-dependencies-conform', () => {
it("passes if the lint related dependencies in the project's package.json matches the one in the template's package.json", async () => {
await withinSandbox(async (sandbox) => {
const template = buildMetaMaskRepository({
shortname: 'template',
directoryPath: path.join(sandbox.directoryPath, 'template'),
});
await writeFile(
path.join(template.directoryPath, 'package.json'),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: {
'@metamask/eslint-config-foo': '1.0.0',
'@typescript-eslint/foo': '1.0.0',
eslint: '1.0.0',
'eslint-plugin-foo': '1.0.0',
'eslint-config-foo': '1.0.0',
prettier: '1.0.0',
'prettier-plugin-foo': '1.0.0',
'prettier-config-foo': '1.0.0',
},
}),
);
const project = buildMetaMaskRepository({
shortname: 'project',
directoryPath: path.join(sandbox.directoryPath, 'project'),
});
await writeFile(
path.join(project.directoryPath, 'package.json'),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: {
'@metamask/eslint-config-foo': '1.0.0',
'@typescript-eslint/foo': '1.0.0',
eslint: '1.0.0',
'eslint-plugin-foo': '1.0.0',
'eslint-config-foo': '1.0.0',
prettier: '1.0.0',
'prettier-plugin-foo': '1.0.0',
'prettier-config-foo': '1.0.0',
},
}),
);

const result = await packageLintDependenciesConform.execute({
template,
project,
pass,
fail,
});

expect(result).toStrictEqual({
passed: true,
});
});
});

it("fails if the version of lint related dependencies in the project's package.json does not match the one in the template's package.json", async () => {
await withinSandbox(async (sandbox) => {
const template = buildMetaMaskRepository({
shortname: 'template',
directoryPath: path.join(sandbox.directoryPath, 'template'),
});
await writeFile(
path.join(template.directoryPath, 'package.json'),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: { eslint: '1.1.0' },
}),
);
const project = buildMetaMaskRepository({
shortname: 'project',
directoryPath: path.join(sandbox.directoryPath, 'project'),
});
await writeFile(
path.join(project.directoryPath, 'package.json'),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: { eslint: '1.0.0' },
}),
);

const result = await packageLintDependenciesConform.execute({
template,
project,
pass,
fail,
});

expect(result).toStrictEqual({
passed: false,
failures: [
{ message: '`eslint` is "1.0.0", when it should be "1.1.0".' },
],
});
});
});

it("fails if the lint related dependency exist in the template's package.json, but not in the project's package.json", async () => {
await withinSandbox(async (sandbox) => {
const template = buildMetaMaskRepository({
shortname: 'template',
directoryPath: path.join(sandbox.directoryPath, 'template'),
});
await writeFile(
path.join(template.directoryPath, 'package.json'),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: { eslint: '1.1.0' },
}),
);
const project = buildMetaMaskRepository({
shortname: 'project',
directoryPath: path.join(sandbox.directoryPath, 'project'),
});
await writeFile(
path.join(project.directoryPath, 'package.json'),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: { testlint: '1.0.0' },
}),
);

const result = await packageLintDependenciesConform.execute({
template,
project,
pass,
fail,
});

expect(result).toStrictEqual({
passed: false,
failures: [
{
message:
'`package.json` should list `"eslint": "1.1.0"` in `devDependencies`, but does not.',
},
],
});
});
});

it("passes if the there're no lint related dependencies in the template's package.json", async () => {
await withinSandbox(async (sandbox) => {
const template = buildMetaMaskRepository({
shortname: 'template',
directoryPath: path.join(sandbox.directoryPath, 'template'),
});
await writeFile(
path.join(template.directoryPath, 'package.json'),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: {
'@metamask/test-config-foo': '1.0.0',
},
}),
);
const project = buildMetaMaskRepository({
shortname: 'project',
directoryPath: path.join(sandbox.directoryPath, 'project'),
});
await writeFile(
path.join(project.directoryPath, 'package.json'),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: {
'@metamask/eslint-config-foo': '1.0.0',
'@typescript-eslint/foo': '1.0.0',
eslint: '1.0.0',
'eslint-plugin-foo': '1.0.0',
'eslint-config-foo': '1.0.0',
prettier: '1.0.0',
'prettier-plugin-foo': '1.0.0',
'prettier-config-foo': '1.0.0',
},
}),
);

const result = await packageLintDependenciesConform.execute({
template,
project,
pass,
fail,
});

expect(result).toStrictEqual({
passed: true,
});
});
});
});
Loading