declapract
Version:
A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.
225 lines • 13 kB
JavaScript
;
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', async () => {
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
await (0, writeFileAsync_1.writeFileAsync)({
path: `${projectRootDirectory}/${fileToCheckRelativePath}`,
content: '.js',
}); // write the file contents to get it to failing state
// lookup a practice
const declarations = await (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 = await (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project: {
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
},
});
// now find the file's evaluation against this practice
const evaluation = evaluations
.find((evaluation) => evaluation.path === fileToCheckRelativePath)
?.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
await (0, fixFile_1.fixFile)({ evaluation, projectRootDirectory });
// now evaluate it again and see whether it now passes
const evaluationsNow = await (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project: {
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
},
});
const evaluationNow = evaluationsNow
.find((evaluation) => evaluation.path === fileToCheckRelativePath)
?.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', async () => {
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 (await (0, doesFileExist_1.doesFileExist)({
filePath: `${projectRootDirectory}/${fileToCheckRelativePath}`,
}))
await (0, removeFileAsync_1.removeFileAsync)({
path: `${projectRootDirectory}/${fileToCheckRelativePath}`,
});
// lookup a practice
const declarations = await (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 = await (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project: {
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
},
});
// now find the file's evaluation against this practice
const evaluation = evaluations
.find((evaluation) => evaluation.path === fileToCheckRelativePath)
?.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
await (0, fixFile_1.fixFile)({ evaluation, projectRootDirectory });
// now evaluate it again and see whether it now passes
const evaluationsNow = await (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project: {
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
},
});
const evaluationNow = evaluationsNow
.find((evaluation) => evaluation.path === fileToCheckRelativePath)
?.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', async () => {
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
await (0, writeFileAsync_1.writeFileAsync)({
path: `${projectRootDirectory}/${fileToCheckRelativePath}`,
content: 'const shouldExist = false;', // any contents, really
});
// lookup a practice
const declarations = await (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 = await (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project: {
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
},
});
// now find the file's evaluation against this practice
const evaluation = evaluations
.find((evaluation) => evaluation.path === fileToCheckRelativePath)
?.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
await (0, fixFile_1.fixFile)({ evaluation, projectRootDirectory });
// now evaluate it again and see whether it now passes
const evaluationsNow = await (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project: {
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
},
});
const evaluationNow = evaluationsNow
.find((evaluation) => evaluation.path === fileToCheckRelativePath)
?.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', async () => {
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
await (0, writeFileAsync_1.writeFileAsync)({
path: `${projectRootDirectory}/${fileToCheckRelativePath}`,
content: expectedContent, // any contents, really
});
if (await (0, doesFileExist_1.doesFileExist)({
filePath: `${projectRootDirectory}/${fileToCreateRelativePath}`,
}))
await (0, removeFileAsync_1.removeFileAsync)({
path: `${projectRootDirectory}/${fileToCreateRelativePath}`,
});
// lookup a practice
const declarations = await (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 = await (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project: {
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
},
});
// now find the file's evaluation against this practice
const evaluation = evaluations
.find((evaluation) => evaluation.path === fileToCheckRelativePath)
?.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
await (0, fixFile_1.fixFile)({ evaluation, projectRootDirectory });
// now evaluate it again and see whether it now passes
const evaluationsNow = await (0, evaluateProjectAgainstPracticeDeclaration_1.evaluteProjectAgainstPracticeDeclaration)({
practice,
project: {
getProjectRootDirectory: () => projectRootDirectory,
projectVariables: {},
projectPractices: [],
},
});
const evaluationNow = evaluationsNow
.find((evaluation) => evaluation.path === fileToCheckRelativePath)
?.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 = await (0, readFileAsync_1.readFileAsync)({
filePath: `${projectRootDirectory}/${fileToCreateRelativePath}`,
});
expect(createdFileContents).toEqual(expectedContent);
});
});
//# sourceMappingURL=fixFile.integration.test.js.map