Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): resolve TODO in integration test for log message validation #1326

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.

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.

Loading