Skip to content

Commit

Permalink
fix(tests): add test to verify that .npmrc files are covered
Browse files Browse the repository at this point in the history
  • Loading branch information
uladkasach committed May 26, 2024
1 parent a343f0c commit 05a9f52
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
engine-strict=true
save-exact=true
message=%s 🎉
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ use-cases:
- dates-and-times
- serverless
- terraform
- npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this project should fail because the .npmrc file is not defined
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 05a9f52

Please sign in to comment.