From 9162ffd2fcf1f9e758bb83959f4128f3e6810941 Mon Sep 17 00:00:00 2001 From: Light13008 Date: Thu, 19 Dec 2024 21:50:35 +0530 Subject: [PATCH] Todo from apps/generator/test/integration.test.js line 153 is completed --- apps/generator/lib/generator.js | 27 ++++++++++++++++--- apps/generator/lib/logMessages.js | 4 +-- apps/generator/lib/renderer/react.js | 14 +++++++++- apps/generator/test/integration.test.js | 11 ++++++++ .../__transpiled/test-file.md.js | 4 +-- package-lock.json | 2 +- 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/apps/generator/lib/generator.js b/apps/generator/lib/generator.js index 077634e1c..9a8bf0848 100644 --- a/apps/generator/lib/generator.js +++ b/apps/generator/lib/generator.js @@ -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); @@ -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({ diff --git a/apps/generator/lib/logMessages.js b/apps/generator/lib/logMessages.js index a02f3f1f1..8a775865e 100644 --- a/apps/generator/lib/logMessages.js +++ b/apps/generator/lib/logMessages.js @@ -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}`; } @@ -64,5 +64,5 @@ module.exports = { relativeSourceFileNotGenerated, conditionalFilesMatched, compileEnabled, - skipOverwrite + write }; diff --git a/apps/generator/lib/renderer/react.js b/apps/generator/lib/renderer/react.js index 5a8171b8b..69ee31ce2 100644 --- a/apps/generator/lib/renderer/react.js +++ b/apps/generator/lib/renderer/react.js @@ -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. * @@ -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 diff --git a/apps/generator/test/integration.test.js b/apps/generator/test/integration.test.js index c2df3fac5..21428b682 100644 --- a/apps/generator/test/integration.test.js +++ b/apps/generator/test/integration.test.js @@ -135,6 +135,10 @@ 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, @@ -142,6 +146,9 @@ describe('Integration testing generateFromFile() to make sure the result of the debug: true, }); + + console.log('Generator options:', generator.options); + await generator.generateFromFile(dummySpecPath); // Read the file to confirm it was not overwritten @@ -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(); }); }); diff --git a/apps/generator/test/test-templates/react-template/__transpiled/test-file.md.js b/apps/generator/test/test-templates/react-template/__transpiled/test-file.md.js index 93c5b0245..4c46bafdf 100644 --- a/apps/generator/test/test-templates/react-template/__transpiled/test-file.md.js +++ b/apps/generator/test/test-templates/react-template/__transpiled/test-file.md.js @@ -1,8 +1,8 @@ 'use strict'; require('source-map-support/register'); -const generatorReactSdk = require('@asyncapi/generator-react-sdk'); -const jsxRuntime = require('react/cjs/react-jsx-runtime.production.min'); +var generatorReactSdk = require('@asyncapi/generator-react-sdk'); +var jsxRuntime = require('/media/sarvesh-patil/all-Set-Up/AsyncApi/generator/apps/generator/node_modules/react/cjs/react-jsx-runtime.production.min.js'); function testFile_md ({ asyncapi, diff --git a/package-lock.json b/package-lock.json index d543520d0..32df19e15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ }, "apps/generator": { "name": "@asyncapi/generator", - "version": "2.4.1", + "version": "2.5.0", "license": "Apache-2.0", "dependencies": { "@asyncapi/generator-hooks": "*",