declapract
Version:
A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.
455 lines • 26.6 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const domain_1 = require("../../../domain");
const ProjectCheckContext_1 = require("../../../domain/objects/ProjectCheckContext");
const dirPath_1 = require("../../__test_assets__/dirPath");
const readDeclarePracticesConfig_1 = require("../../declaration/readDeclarePracticesConfig");
const evaluateProjectAgainstPracticeDeclaration_1 = require("./evaluateProjectAgainstPracticeDeclaration");
describe('evaluteProjectAgainstPracticeDeclaration', () => {
it('should be able to evaluate a practice with only a best practice', () => __awaiter(void 0, void 0, void 0, function* () {
// lookup a practice
const declarations = yield (0, readDeclarePracticesConfig_1.readDeclarePracticesConfig)({
configPath: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/declapract.declare.yml`,
});
const practice = declarations.practices.find((thisPractice) => thisPractice.name === 'prettier'); // lets use the "prettier" practice for this one, since its a "best-practice" only one
if (!practice)
fail('should have found a practice');
// sanity check the practice we'll be using
expect(practice.badPractices.length).toEqual(0); // check that our expectations for the test are met; should not have any bad practices
// now evaluate it
const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-prettier`;
const project = new ProjectCheckContext_1.ProjectCheckContext({
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
});
const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project,
});
// check that the evaluation matches what we expect
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.FAIL)
.length).toEqual(2); // 2 out of 3 fail
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.PASS)
.length).toEqual(1); // 1 out of 3 pass
expect(evaluations).toMatchSnapshot();
}));
it('should be able to evaluate a practice with both a best practice and bad practices', () => __awaiter(void 0, void 0, void 0, function* () {
// lookup a practice
const declarations = yield (0, readDeclarePracticesConfig_1.readDeclarePracticesConfig)({
configPath: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/declapract.declare.yml`,
});
const practice = declarations.practices.find((thisPractice) => thisPractice.name === 'dates-and-times');
if (!practice)
fail('should have found a practice');
// sanity check the practice we'll be using
expect(practice.badPractices.length).toBeGreaterThan(0); // check that our expectations for the test are met
// now evaluate it
const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-dates-and-times`;
const project = new ProjectCheckContext_1.ProjectCheckContext({
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
});
const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project,
});
// check that the evaluation matches what we expect
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.FAIL)
.length).toEqual(1);
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.PASS)
.length).toEqual(0);
expect(evaluations[0].checks.filter((check) => check.purpose === domain_1.FileCheckPurpose.BEST_PRACTICE)[0].result).toEqual(domain_1.FileEvaluationResult.PASS);
expect(evaluations[0].checks.filter((check) => check.purpose === domain_1.FileCheckPurpose.BAD_PRACTICE)[0].result).toEqual(domain_1.FileEvaluationResult.FAIL);
expect(evaluations).toMatchSnapshot();
}));
it('should be able to evaluate a practice with wildcard glob pattern path file checks - fails best and bad practices', () => __awaiter(void 0, void 0, void 0, function* () {
// lookup a practice
const declarations = yield (0, readDeclarePracticesConfig_1.readDeclarePracticesConfig)({
configPath: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/declapract.declare.yml`,
});
const practice = declarations.practices.find((thisPractice) => thisPractice.name === 'directory-structure-src');
if (!practice)
fail('should have found a practice');
// sanity check the practice we'll be using
expect(practice.badPractices.length).toBeGreaterThan(0); // check that our expectations for the test are met
// now evaluate it
const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-directory-structure-src`;
const project = new ProjectCheckContext_1.ProjectCheckContext({
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
});
const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project,
});
// check that the evaluation matches what we expect
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.FAIL)
.length).toEqual(7);
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.PASS)
.length).toEqual(2);
// sanity check a couple of important texamples
expect(evaluations.find((file) => file.path === 'src/logic/**/*.ts')).toMatchObject({
// should have found this file by wildcard _and_ failed it due to the contains check
result: domain_1.FileEvaluationResult.FAIL,
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.FAIL,
purpose: domain_1.FileCheckPurpose.BEST_PRACTICE,
type: domain_1.FileCheckType.EXISTS,
}),
]),
});
expect(evaluations.find((file) => file.path === 'src/data/clients/coolServiceClient.ts')).toMatchObject({
// should have found this file by wildcard _and_ failed it due to the contains check not being satisfied correctly
result: domain_1.FileEvaluationResult.FAIL,
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.FAIL,
purpose: domain_1.FileCheckPurpose.BEST_PRACTICE,
type: domain_1.FileCheckType.CONTAINS,
}),
]),
});
expect(evaluations.find((file) => file.path === 'src/services/someFile.ts')).toMatchObject({
result: domain_1.FileEvaluationResult.FAIL, // it failed,
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.FAIL,
purpose: domain_1.FileCheckPurpose.BAD_PRACTICE,
type: domain_1.FileCheckType.EXISTS,
}),
]),
});
// now just save an example of the results
expect(evaluations).toMatchSnapshot();
}));
it('should be able to evaluate a practice with wildcard glob pattern path file checks - passes without optionals', () => __awaiter(void 0, void 0, void 0, function* () {
// lookup a practice
const declarations = yield (0, readDeclarePracticesConfig_1.readDeclarePracticesConfig)({
configPath: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/declapract.declare.yml`,
});
const practice = declarations.practices.find((thisPractice) => thisPractice.name === 'directory-structure-src');
if (!practice)
fail('should have found a practice');
// sanity check the practice we'll be using
expect(practice.badPractices.length).toBeGreaterThan(0); // check that our expectations for the test are met
// now evaluate it
const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-passes-directory-structure-src-without-optionals`;
const project = new ProjectCheckContext_1.ProjectCheckContext({
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
});
const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project,
});
// console.log(JSON.stringify(evaluations, null, 2));
// check that the evaluation matches what we expect
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.FAIL)
.length).toEqual(0);
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.PASS)
.length).toEqual(9);
// sanity check a couple of important texamples
expect(evaluations.find((file) => file.path === 'src/contract/handlers/doSomething.ts')).toMatchObject({
result: domain_1.FileEvaluationResult.PASS, // passed, due to existance
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.PASS,
purpose: domain_1.FileCheckPurpose.BEST_PRACTICE,
type: domain_1.FileCheckType.EXISTS,
}),
]),
});
expect(evaluations.find((file) => file.path === 'src/data/clients/**/*.ts')).toMatchObject({
result: domain_1.FileEvaluationResult.PASS,
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.PASS,
purpose: domain_1.FileCheckPurpose.BEST_PRACTICE,
type: domain_1.FileCheckType.CONTAINS,
required: false, // optional file -> that's why it passed
}),
]),
});
expect(evaluations.find((file) => file.path === 'src/model/**/*.ts')).toMatchObject({
result: domain_1.FileEvaluationResult.PASS, // it passed, because the bad practice check failed
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.PASS,
type: domain_1.FileCheckType.EXISTS,
purpose: domain_1.FileCheckPurpose.BAD_PRACTICE,
}),
]),
});
// now just save an example of the results
expect(evaluations).toMatchSnapshot();
}));
it('should be able to evaluate a practice with wildcard glob pattern path file checks - passes with optionals', () => __awaiter(void 0, void 0, void 0, function* () {
// lookup a practice
const declarations = yield (0, readDeclarePracticesConfig_1.readDeclarePracticesConfig)({
configPath: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/declapract.declare.yml`,
});
const practice = declarations.practices.find((thisPractice) => thisPractice.name === 'directory-structure-src');
if (!practice)
fail('should have found a practice');
// sanity check the practice we'll be using
expect(practice.badPractices.length).toBeGreaterThan(0); // check that our expectations for the test are met
// now evaluate it
const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-passes-directory-structure-src-with-optionals`;
const project = new ProjectCheckContext_1.ProjectCheckContext({
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
});
const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project,
});
// console.log(JSON.stringify(evaluation, null, 2));
// check that the evaluation matches what we expect
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.FAIL)
.length).toEqual(0);
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.PASS)
.length).toEqual(9);
// sanity check a couple of important examples
expect(evaluations.find((file) => file.path === 'src/data/clients/svcAwesomeStuff.ts')).toMatchObject({
result: domain_1.FileEvaluationResult.PASS,
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.PASS,
purpose: domain_1.FileCheckPurpose.BEST_PRACTICE,
type: domain_1.FileCheckType.CONTAINS,
}),
]),
});
expect(evaluations.find((file) => file.path === 'src/data/dao/superCoolThingDao/index.ts')).toMatchObject({
result: domain_1.FileEvaluationResult.PASS, // passed, due to existance
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.PASS,
purpose: domain_1.FileCheckPurpose.BEST_PRACTICE,
type: domain_1.FileCheckType.EXISTS,
}),
]),
});
expect(evaluations.find((file) => file.path === 'src/model/**/*.ts')).toMatchObject({
result: domain_1.FileEvaluationResult.PASS,
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.PASS,
purpose: domain_1.FileCheckPurpose.BAD_PRACTICE,
type: domain_1.FileCheckType.EXISTS,
}),
]),
});
// now just save an example of the results
expect(evaluations).toMatchSnapshot();
}));
it('should be able to evaluate a practice which reference project variables in the declared contents and custom checks', () => __awaiter(void 0, void 0, void 0, function* () {
// lookup a practice
const declarations = yield (0, readDeclarePracticesConfig_1.readDeclarePracticesConfig)({
configPath: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/declapract.declare.yml`,
});
const practice = declarations.practices.find((thisPractice) => thisPractice.name === 'serverless');
if (!practice)
fail('should have found a practice');
// sanity check the practice we'll be using
expect(practice.bestPractice).toBeDefined(); // check that our expectations for the test are met
// now evaluate it
const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-passes-serverless`;
const project = new ProjectCheckContext_1.ProjectCheckContext({
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {
organizationName: 'awesome-org',
serviceName: 'svc-awesome-thing',
infrastructureNamespaceId: 'abcde12345',
slackReleaseWebHook: 'https://...',
},
projectPractices: [],
});
const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project,
});
// console.log(JSON.stringify(evaluations, null, 2));
evaluations
.filter(domain_1.hasFailed)
.forEach((evaluation) => evaluation.checks
.filter(domain_1.hasFailed)
.forEach((failedCheck) => console.log(failedCheck.reason)));
// check that the evaluation matches what we expect
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.FAIL)
.length).toEqual(0);
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.PASS)
.length).toEqual(2);
// sanity check a couple of important examples
expect(evaluations.find((file) => file.path === 'serverless.yml')).toMatchObject({
result: domain_1.FileEvaluationResult.PASS,
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.PASS, // passed, due to having correct contents
purpose: domain_1.FileCheckPurpose.BEST_PRACTICE,
type: domain_1.FileCheckType.CONTAINS,
}),
]),
});
expect(evaluations.find((file) => file.path === 'package.json')).toMatchObject({
result: domain_1.FileEvaluationResult.PASS, // passed, due to having correct contents
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.PASS,
purpose: domain_1.FileCheckPurpose.BEST_PRACTICE,
type: domain_1.FileCheckType.CUSTOM,
}),
]),
});
// now just save an example of the results
expect(evaluations).toMatchSnapshot();
}));
it('should be able to evaluate a practice which references project practices in the declared contents function for a contains check', () => __awaiter(void 0, void 0, void 0, function* () {
// lookup a practice
const declarations = yield (0, readDeclarePracticesConfig_1.readDeclarePracticesConfig)({
configPath: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/declapract.declare.yml`,
});
const practice = declarations.practices.find((thisPractice) => thisPractice.name === 'format');
if (!practice)
fail('should have found a practice');
// sanity check the practice we'll be using
expect(practice.bestPractice).toBeDefined(); // check that our expectations for the test are met
// now evaluate it
const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-passes-serverless`;
const project = new ProjectCheckContext_1.ProjectCheckContext({
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: ['terraform'],
});
const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project,
});
// console.log(JSON.stringify(evaluations, null, 2));
evaluations
.filter(domain_1.hasFailed)
.forEach((evaluation) => evaluation.checks
.filter(domain_1.hasFailed)
.forEach((failedCheck) => console.log(failedCheck.reason)));
// check that the evaluation matches what we expect
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.FAIL)
.length).toEqual(1);
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.PASS)
.length).toEqual(0);
// sanity check a couple of important examples
expect(evaluations.find((file) => file.path === 'package.json')).toMatchObject({
result: domain_1.FileEvaluationResult.FAIL,
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.FAIL, // passed, due to having missing contents
purpose: domain_1.FileCheckPurpose.BEST_PRACTICE,
type: domain_1.FileCheckType.CONTAINS,
}),
]),
});
// now just save an example of the results
expect(evaluations).toMatchSnapshot();
}));
it('should ignore the "node_modules" and ".declapract" directory when evaluating glob paths', () => __awaiter(void 0, void 0, void 0, function* () {
// lookup a practice
const declarations = yield (0, readDeclarePracticesConfig_1.readDeclarePracticesConfig)({
configPath: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/declapract.declare.yml`,
});
const practice = declarations.practices.find((thisPractice) => thisPractice.name === 'testing');
if (!practice)
fail('should have found a practice');
// sanity check the practice we'll be using
expect(practice.badPractices.length).toBeGreaterThan(0); // check that our expectations for the test are met
// now evaluate it
const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-passes-testing-with-bad-practices-in-ignored-dirs`;
const project = new ProjectCheckContext_1.ProjectCheckContext({
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
});
const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project,
});
// console.log(JSON.stringify(evaluations, null, 2));
// check that the evaluation matches what we expect
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.FAIL)
.length).toEqual(0);
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.PASS)
.length).toEqual(1);
// sanity check a couple of important examples
expect(evaluations.find((file) => file.path === 'node_modules/old-syntax.test.integration.ts')).not.toBeDefined(); // should have found it because `.declapract` directory should be ignored
expect(evaluations.find((file) => file.path ===
'node_modules/another-dir/old-syntax.test.integration.ts')).not.toBeDefined(); // should have found it because `.declapract` directory should be ignored
expect(evaluations.find((file) => file.path === '**/*.test.integration.ts')).toMatchObject({
result: domain_1.FileEvaluationResult.PASS,
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.PASS,
purpose: domain_1.FileCheckPurpose.BAD_PRACTICE,
type: domain_1.FileCheckType.EXISTS,
}),
]),
});
// now just save an example of the results
expect(evaluations).toMatchSnapshot();
}));
it.only('should include the ".npmrc" file when evaluating glob paths', () => __awaiter(void 0, void 0, void 0, function* () {
// lookup a practice
const declarations = yield (0, readDeclarePracticesConfig_1.readDeclarePracticesConfig)({
configPath: `${dirPath_1.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 = `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-npmrc-practice`;
const project = new ProjectCheckContext_1.ProjectCheckContext({
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
});
const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project,
});
// check that the evaluation matches what we expect
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.FAIL)
.length).toEqual(1);
expect(evaluations.filter((file) => file.result === domain_1.FileEvaluationResult.PASS)
.length).toEqual(0);
// sanity check a couple of important examples
expect(evaluations.find((file) => file.path === '.npmrc')).toBeDefined(); // should have found it because `.declapract` directory should be ignored
expect(evaluations.find((file) => file.path === '.npmrc')).toMatchObject({
result: domain_1.FileEvaluationResult.FAIL,
checks: expect.arrayContaining([
expect.objectContaining({
result: domain_1.FileEvaluationResult.FAIL,
purpose: domain_1.FileCheckPurpose.BEST_PRACTICE,
type: domain_1.FileCheckType.EQUALS,
}),
]),
});
// now just save an example of the results
expect(evaluations).toMatchSnapshot();
}));
});
//# sourceMappingURL=evaluateProjectAgainstPracticeDeclaration.integration.test.js.map