UNPKG

declapract

Version:

A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.

230 lines 14.2 kB
"use strict"; 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 }); /* eslint-disable @typescript-eslint/no-shadow */ const domain_1 = require("../../../../domain"); const doesFileExist_1 = require("../../../../utils/fileio/doesFileExist"); const readFileAsync_1 = require("../../../../utils/fileio/readFileAsync"); const removeFileAsync_1 = require("../../../../utils/fileio/removeFileAsync"); const writeFileAsync_1 = require("../../../../utils/fileio/writeFileAsync"); const dirPath_1 = require("../../../__test_assets__/dirPath"); const readDeclarePracticesConfig_1 = require("../../../declaration/readDeclarePracticesConfig"); const evaluateProjectAgainstPracticeDeclaration_1 = require("../../evaluate/evaluateProjectAgainstPracticeDeclaration"); const fixFile_1 = require("./fixFile"); describe('fixFile', () => { it('should be able to fix by changing contents', () => __awaiter(void 0, void 0, void 0, function* () { var _a, _b; const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-prettier-for-fixing`; const fileToCheckRelativePath = '.prettierignore'; // overwrite the contents of the file we're planning on fixing in this test, to get it back to the failing state yield (0, writeFileAsync_1.writeFileAsync)({ path: `${projectRootDirectory}/${fileToCheckRelativePath}`, content: '.js', }); // write the file contents to get it to failing state // 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((practice) => practice.name === 'prettier'); // lets use the "prettier" practice for this one if (!practice) fail('should have found a practice'); // now evaluate it const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({ practice, project: { getProjectRootDirectory: () => projectRootDirectory, projectVariables: {}, projectPractices: [], }, }); // now find the file's evaluation against this practice const evaluation = (_a = evaluations .find((evaluation) => evaluation.path === fileToCheckRelativePath)) === null || _a === void 0 ? void 0 : _a.checks.find((check) => check.purpose === domain_1.FileCheckPurpose.BEST_PRACTICE); if (!evaluation) throw new Error('should have found the evaluation'); expect(evaluation.result).toEqual(domain_1.FileEvaluationResult.FAIL); // run the fix on that evaluation yield (0, fixFile_1.fixFile)({ evaluation, projectRootDirectory }); // now evaluate it again and see whether it now passes const evaluationsNow = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({ practice, project: { getProjectRootDirectory: () => projectRootDirectory, projectVariables: {}, projectPractices: [], }, }); const evaluationNow = (_b = evaluationsNow .find((evaluation) => evaluation.path === fileToCheckRelativePath)) === null || _b === void 0 ? void 0 : _b.checks.find((check) => check.purpose === domain_1.FileCheckPurpose.BEST_PRACTICE); if (!evaluationNow) throw new Error('should have found the evaluation'); expect(evaluationNow.result).toEqual(domain_1.FileEvaluationResult.PASS); })); it('should be able to fix by creating a new file', () => __awaiter(void 0, void 0, void 0, function* () { var _c, _d; const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-prettier-for-fixing`; const fileToCheckRelativePath = 'prettier.config.js'; // overwrite the contents of the file we're planning on fixing in this test, to get it back to the failing state if (yield (0, doesFileExist_1.doesFileExist)({ filePath: `${projectRootDirectory}/${fileToCheckRelativePath}`, })) yield (0, removeFileAsync_1.removeFileAsync)({ path: `${projectRootDirectory}/${fileToCheckRelativePath}`, }); // 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((practice) => practice.name === 'prettier'); // lets use the "prettier" practice for this one if (!practice) fail('should have found a practice'); // now evaluate it const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({ practice, project: { getProjectRootDirectory: () => projectRootDirectory, projectVariables: {}, projectPractices: [], }, }); // now find the file's evaluation against this practice const evaluation = (_c = evaluations .find((evaluation) => evaluation.path === fileToCheckRelativePath)) === null || _c === void 0 ? void 0 : _c.checks.find((check) => check.purpose === domain_1.FileCheckPurpose.BEST_PRACTICE); if (!evaluation) throw new Error('should have found the evaluation'); expect(evaluation.result).toEqual(domain_1.FileEvaluationResult.FAIL); // run the fix on that evaluation yield (0, fixFile_1.fixFile)({ evaluation, projectRootDirectory }); // now evaluate it again and see whether it now passes const evaluationsNow = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({ practice, project: { getProjectRootDirectory: () => projectRootDirectory, projectVariables: {}, projectPractices: [], }, }); const evaluationNow = (_d = evaluationsNow .find((evaluation) => evaluation.path === fileToCheckRelativePath)) === null || _d === void 0 ? void 0 : _d.checks.find((check) => check.purpose === domain_1.FileCheckPurpose.BEST_PRACTICE); if (!evaluationNow) throw new Error('should have found the evaluation'); expect(evaluationNow.result).toEqual(domain_1.FileEvaluationResult.PASS); })); it('should be able to fix by removing the file', () => __awaiter(void 0, void 0, void 0, function* () { var _e, _f; const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-directory-structure-src-for-fixing`; const fileToCheckRelativePath = 'src/services/someFile.ts'; // overwrite the contents of the file we're planning on fixing in this test, to get it back to the failing state yield (0, writeFileAsync_1.writeFileAsync)({ path: `${projectRootDirectory}/${fileToCheckRelativePath}`, content: 'const shouldExist = false;', // any contents, really }); // 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((practice) => practice.name === 'directory-structure-src'); // lets use the "prettier" practice for this one if (!practice) fail('should have found a practice'); // now evaluate it const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({ practice, project: { getProjectRootDirectory: () => projectRootDirectory, projectVariables: {}, projectPractices: [], }, }); // now find the file's evaluation against this practice const evaluation = (_e = evaluations .find((evaluation) => evaluation.path === fileToCheckRelativePath)) === null || _e === void 0 ? void 0 : _e.checks.find((check) => check.purpose === domain_1.FileCheckPurpose.BAD_PRACTICE); if (!evaluation) throw new Error('should have found the evaluation'); expect(evaluation.result).toEqual(domain_1.FileEvaluationResult.FAIL); // should have failed the bad practice check (i.e., file exists) // run the fix on that evaluation yield (0, fixFile_1.fixFile)({ evaluation, projectRootDirectory }); // now evaluate it again and see whether it now passes const evaluationsNow = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({ practice, project: { getProjectRootDirectory: () => projectRootDirectory, projectVariables: {}, projectPractices: [], }, }); const evaluationNow = (_f = evaluationsNow .find((evaluation) => evaluation.path === fileToCheckRelativePath)) === null || _f === void 0 ? void 0 : _f.checks.find((check) => check.purpose === domain_1.FileCheckPurpose.BAD_PRACTICE); expect(evaluationNow).toEqual(undefined); // should not be defined anymore, since that file should have been deleted })); it('should be able to fix a file by renaming it, from a custom fix function', () => __awaiter(void 0, void 0, void 0, function* () { var _g, _h; const projectRootDirectory = `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-testing-for-fixing`; const fileToCheckRelativePath = 'src/old-syntax.test.integration.ts'; const fileToCreateRelativePath = 'src/old-syntax.integration.test.ts'; const expectedContent = `describe('some integration test', () => { test.todo('something'); }); `; // overwrite the contents of the file we're planning on fixing in this test, to get it back to the failing state yield (0, writeFileAsync_1.writeFileAsync)({ path: `${projectRootDirectory}/${fileToCheckRelativePath}`, content: expectedContent, // any contents, really }); if (yield (0, doesFileExist_1.doesFileExist)({ filePath: `${projectRootDirectory}/${fileToCreateRelativePath}`, })) yield (0, removeFileAsync_1.removeFileAsync)({ path: `${projectRootDirectory}/${fileToCreateRelativePath}`, }); // 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((practice) => practice.name === 'testing'); if (!practice) fail('should have found a practice'); // now evaluate it const evaluations = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({ practice, project: { getProjectRootDirectory: () => projectRootDirectory, projectVariables: {}, projectPractices: [], }, }); // now find the file's evaluation against this practice const evaluation = (_g = evaluations .find((evaluation) => evaluation.path === fileToCheckRelativePath)) === null || _g === void 0 ? void 0 : _g.checks.find((check) => check.purpose === domain_1.FileCheckPurpose.BAD_PRACTICE); if (!evaluation) throw new Error('should have found the evaluation'); expect(evaluation.result).toEqual(domain_1.FileEvaluationResult.FAIL); // should have failed the bad practice check (i.e., file exists) // run the fix on that evaluation yield (0, fixFile_1.fixFile)({ evaluation, projectRootDirectory }); // now evaluate it again and see whether it now passes const evaluationsNow = yield (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({ practice, project: { getProjectRootDirectory: () => projectRootDirectory, projectVariables: {}, projectPractices: [], }, }); const evaluationNow = (_h = evaluationsNow .find((evaluation) => evaluation.path === fileToCheckRelativePath)) === null || _h === void 0 ? void 0 : _h.checks.find((check) => check.purpose === domain_1.FileCheckPurpose.BAD_PRACTICE); expect(evaluationNow).toEqual(undefined); // should not be defined anymore, since that file should have been moved // and check that the contents were unchanged const createdFileContents = yield (0, readFileAsync_1.readFileAsync)({ filePath: `${projectRootDirectory}/${fileToCreateRelativePath}`, }); expect(createdFileContents).toEqual(expectedContent); })); }); //# sourceMappingURL=fixFile.integration.test.js.map