diff --git a/src/logic/__test_assets__/example-best-practices-repo/src/practices/npmrc/best-practice/.npmrc b/src/logic/__test_assets__/example-best-practices-repo/src/practices/npmrc/best-practice/.npmrc new file mode 100644 index 0000000..aacf6e9 --- /dev/null +++ b/src/logic/__test_assets__/example-best-practices-repo/src/practices/npmrc/best-practice/.npmrc @@ -0,0 +1,3 @@ +engine-strict=true +save-exact=true +message=%s 🎉 diff --git a/src/logic/__test_assets__/example-best-practices-repo/src/useCases.yml b/src/logic/__test_assets__/example-best-practices-repo/src/useCases.yml index 168ca83..a0e18de 100644 --- a/src/logic/__test_assets__/example-best-practices-repo/src/useCases.yml +++ b/src/logic/__test_assets__/example-best-practices-repo/src/useCases.yml @@ -12,3 +12,4 @@ use-cases: - dates-and-times - serverless - terraform + - npmrc diff --git a/src/logic/__test_assets__/example-project-fails-npmrc-practice/package.json b/src/logic/__test_assets__/example-project-fails-npmrc-practice/package.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/src/logic/__test_assets__/example-project-fails-npmrc-practice/package.json @@ -0,0 +1 @@ +{} diff --git a/src/logic/__test_assets__/example-project-fails-npmrc-practice/readme.md b/src/logic/__test_assets__/example-project-fails-npmrc-practice/readme.md new file mode 100644 index 0000000..3dfa3c4 --- /dev/null +++ b/src/logic/__test_assets__/example-project-fails-npmrc-practice/readme.md @@ -0,0 +1 @@ +this project should fail because the .npmrc file is not defined diff --git a/src/logic/usage/evaluate/__snapshots__/evaluateProjectAgainstPracticeDeclaration.integration.test.ts.snap b/src/logic/usage/evaluate/__snapshots__/evaluateProjectAgainstPracticeDeclaration.integration.test.ts.snap index 5121102..fb2d2fc 100644 --- a/src/logic/usage/evaluate/__snapshots__/evaluateProjectAgainstPracticeDeclaration.integration.test.ts.snap +++ b/src/logic/usage/evaluate/__snapshots__/evaluateProjectAgainstPracticeDeclaration.integration.test.ts.snap @@ -4730,3 +4730,55 @@ exports[`evaluteProjectAgainstPracticeDeclaration should ignore the "node_module }, ] `; + +exports[`evaluteProjectAgainstPracticeDeclaration should include the ".npmrc" file when evaluating glob paths 1`] = ` +[ + FilePracticeEvaluation { + "checks": [ + FileCheckEvaluation { + "context": FileCheckContext { + "declaredFileContents": "engine-strict=true +save-exact=true +message=%s 🎉 +", + "getProjectRootDirectory": [Function], + "projectPractices": [], + "projectVariables": {}, + "relativeFilePath": ".npmrc", + "required": true, + }, + "fix": [Function], + "path": ".npmrc", + "practiceRef": "npmrc.best-practice", + "purpose": "BEST_PRACTICE", + "reason": "- Expected file to exist ++ Received file does not exist", + "required": true, + "result": "FAIL", + "type": "EQUALS", + }, + ], + "path": ".npmrc", + "practice": PracticeDeclaration { + "badPractices": [], + "bestPractice": ProjectCheckDeclaration { + "checks": [ + FileCheckDeclaration { + "check": [Function], + "contents": [Function], + "fix": [Function], + "pathGlob": ".npmrc", + "purpose": "BEST_PRACTICE", + "required": true, + "type": "EQUALS", + }, + ], + "name": "npmrc", + "readme": null, + }, + "name": "npmrc", + }, + "result": "FAIL", + }, +] +`; diff --git a/src/logic/usage/evaluate/evaluateProjectAgainstPracticeDeclaration.integration.test.ts b/src/logic/usage/evaluate/evaluateProjectAgainstPracticeDeclaration.integration.test.ts index 7eb0872..77bec15 100644 --- a/src/logic/usage/evaluate/evaluateProjectAgainstPracticeDeclaration.integration.test.ts +++ b/src/logic/usage/evaluate/evaluateProjectAgainstPracticeDeclaration.integration.test.ts @@ -537,4 +537,62 @@ describe('evaluteProjectAgainstPracticeDeclaration', () => { // now just save an example of the results expect(evaluations).toMatchSnapshot(); }); + + it.only('should include the ".npmrc" file when evaluating glob paths', async () => { + // lookup a practice + const declarations = await readDeclarePracticesConfig({ + configPath: `${testAssetsDirectoryPath}/example-best-practices-repo/declapract.declare.yml`, + }); + const practice = declarations.practices.find( + (thisPractice) => thisPractice.name === 'npmrc', + ); + if (!practice) fail('should have found the practice'); + + // sanity check the practice we'll be using + console.log(practice); + expect(practice.bestPractice).toBeDefined(); // check that our expectations for the test are met + + // now evaluate it + const projectRootDirectory = `${testAssetsDirectoryPath}/example-project-fails-npmrc-practice`; + const project = new ProjectCheckContext({ + getProjectRootDirectory: () => projectRootDirectory, + projectVariables: {}, + projectPractices: [], + }); + const evaluations = await evaluteProjectAgainstPracticeDeclaration({ + practice, + project, + }); + + // check that the evaluation matches what we expect + expect( + evaluations.filter((file) => file.result === FileEvaluationResult.FAIL) + .length, + ).toEqual(1); + expect( + evaluations.filter((file) => file.result === FileEvaluationResult.PASS) + .length, + ).toEqual(0); + + // sanity check a couple of important examples + expect( + evaluations.find((file) => file.path === '.npmrc'), // should have found this path + ).toBeDefined(); // should have found it because `.declapract` directory should be ignored + + expect( + evaluations.find((file) => file.path === '.npmrc'), // should have required it to exist + ).toMatchObject({ + result: FileEvaluationResult.FAIL, + checks: expect.arrayContaining([ + expect.objectContaining({ + result: FileEvaluationResult.FAIL, + purpose: FileCheckPurpose.BEST_PRACTICE, + type: FileCheckType.EQUALS, + }), + ]), + }); + + // now just save an example of the results + expect(evaluations).toMatchSnapshot(); + }); });