Skip to content

Commit

Permalink
Todo from apps/generator/test/integration.test.js line 153 is completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Light13008 committed Dec 19, 2024
1 parent 8bc3a78 commit 9162ffd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
27 changes: 24 additions & 3 deletions apps/generator/lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ class Generator {
* @param {String} [options.registry.token] Optional parameter to pass npm registry auth token that you can grab from .npmrc file
*/

/**
* Checks if a given file should be skipped based on the noOverwriteGlobs option.
*
* @private
* @param {string} filePath Path to the file to check against a list of glob patterns.
* @return {boolean}
*/
skipOverwrite(filePath) {
if (!Array.isArray(this.noOverwriteGlobs)) return false;
const shouldSkip = this.noOverwriteGlobs.some(globExp => minimatch(filePath, globExp));
if (shouldSkip) {
console.debug(`Skipping overwrite for: ${filePath}`);
}
return shouldSkip;
}


constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwriteGlobs, disabledHooks, output = 'fs', forceWrite = false, install = false, debug = false, mapBaseUrlToFolder = {}, registry = {}, compile = true } = {}) {
const options = arguments[arguments.length - 1];
this.verifyoptions(options);
Expand Down Expand Up @@ -871,10 +888,14 @@ class Generator {
const relativeTargetFile = path.relative(this.targetDir, targetFile);

if (shouldIgnoreFile(relativeSourceFile)) return;

if (this.skipOverwrite(relativeTargetFile)) {
return;
}
const shouldOverwriteFile = await this.shouldOverwriteFile(relativeTargetFile);
if (!shouldOverwriteFile) return;

if (!shouldOverwriteFile) {
log.debug(logMessage.skipOverwrite(sourceFile));
return;
}
if (this.templateConfig.conditionalFiles?.[relativeSourceFile]) {
const server = this.templateParams.server && asyncapiDocument.servers().get(this.templateParams.server);
const source = jmespath.search({
Expand Down
4 changes: 2 additions & 2 deletions apps/generator/lib/logMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function relativeSourceFileNotGenerated(relativeSourceFile , subject) {
return `${relativeSourceFile} was not generated because ${subject} specified in template configuration in conditionalFiles was not found in provided AsyncAPI specification file.`;
}

function skipOverwrite(testFilePath) {
function write(testFilePath) {
return `Skipping overwrite for: ${testFilePath}`;
}

Expand All @@ -64,5 +64,5 @@ module.exports = {
relativeSourceFileNotGenerated,
conditionalFilesMatched,
compileEnabled,
skipOverwrite
write
};
14 changes: 13 additions & 1 deletion apps/generator/lib/renderer/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ reactExport.renderReact = async (asyncapiDocument, filePath, extraTemplateData,
);
};

const skipOverwrite = (filePath, noOverwriteGlobs) => {
if (!Array.isArray(noOverwriteGlobs)) return false;
const shouldSkip = noOverwriteGlobs.some(globExp => minimatch(filePath, globExp));
if (shouldSkip) {
console.debug(`Skipping overwrite for: ${filePath}`);
}
return shouldSkip;
};



/**
* Save the single rendered react content based on the meta data available.
*
Expand Down Expand Up @@ -86,9 +97,10 @@ const saveContentToFile = async (renderedContent, outputPath, noOverwriteGlobs =
// get the final file name of the file
const finalFileName = path.basename(filePath);
// check whether the filename should be ignored based on user's inputs
const shouldOverwrite = !noOverwriteGlobs.some(globExp => minimatch(finalFileName, globExp));
const shouldOverwrite = !skipOverwrite(filePath, noOverwriteGlobs);

// Write the file only if it should not be skipped

if (shouldOverwrite) {
await writeFile(filePath, content, {
mode: permissions
Expand Down
11 changes: 11 additions & 0 deletions apps/generator/test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,20 @@ describe('Integration testing generateFromFile() to make sure the result of the
const testFilePath = path.normalize(path.resolve(outputDir, testOutputFile));
await writeFile(testFilePath, testContent);

// Mock the console.debug method
const debugMock = jest.spyOn(console, 'debug').mockImplementation(() => {});


// Manually create an output first, before generation, with additional custom file to validate if later it is still there, not overwritten
const generator = new Generator(cleanReactTemplate, outputDir, {
forceWrite: true,
noOverwriteGlobs: [`**/${testOutputFile}`],
debug: true,
});


console.log('Generator options:', generator.options);

await generator.generateFromFile(dummySpecPath);

// Read the file to confirm it was not overwritten
Expand All @@ -152,5 +159,9 @@ describe('Integration testing generateFromFile() to make sure the result of the
/*TODO:
Include log message test in the future to ensure that the log.debug for skipping overwrite is called
*/

expect(debugMock).toHaveBeenCalledWith(expect.stringContaining('Skipping overwrite for'));
// Restore the original console.debug method
debugMock.mockRestore();
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9162ffd

Please sign in to comment.