Skip to content

Commit

Permalink
update generator.js, react.js, integration.test.js
Browse files Browse the repository at this point in the history
- delete the log debug in shouldOverwriteFile
- in react, add a log debug to see if the problem is with variable
- mock imported log instead of console.log
  • Loading branch information
lmgyuan committed May 21, 2024
1 parent 15629dc commit 5e49121
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
8 changes: 1 addition & 7 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,18 +961,12 @@ class Generator {
async shouldOverwriteFile(filePath) {
const fullPath = path.resolve(this.targetDir, filePath);
// Additional logging for debugging purposes
log.debug(`Checking if file should be overwritten: ${fullPath}`);

if (!Array.isArray(this.noOverwriteGlobs)) return true;
const fileExists = await exists(path.resolve(this.targetDir, filePath));
if (!fileExists) return true;

if (this.noOverwriteGlobs.some(globExp => minimatch(filePath, globExp))) {
log.debug("file shouldn't be overwritten");
log.debug(logMessage.fileNotOverwritten(fullPath));
return false;
}
return true;
return !this.noOverwriteGlobs.some(globExp => minimatch(filePath, globExp));
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/renderer/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const saveContentToFile = async (renderedContent, outputPath, noOverwriteGlobs =
mode: permissions
});
} else {
log.debug(`noOverwriteGlobs matched`);
log.debug(`Skipping overwrite for: ${filePath}`);
}
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
"devDependencies": {
"eslint": "^6.8.0",
"eslint-plugin-jest": "^23.8.2",
"eslint-plugin-sonarjs": "^0.5.0",
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-sonarjs": "^0.5.0",
"jest": "^25.5.0",
"jsdoc-to-markdown": "^7.1.1",
"markdown-toc": "^1.2.0",
Expand Down
12 changes: 6 additions & 6 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Integration testing generateFromFile() to make sure the result of the
});

it('should ignore specified files with noOverwriteGlobs', async () => {
log.debug = jest.fn();
const logSpyDebug = jest.spyOn(log, 'debug').mockImplementation(() => {});

const outputDir = generateFolderName();
// Manually create a file to test if it's not overwritten
Expand All @@ -91,18 +91,18 @@ describe('Integration testing generateFromFile() to make sure the result of the

// Read the file to confirm it was not overwritten
const fileContent = await readFile(testFilePath, 'utf8');
// Check if the files have been overwritte
// Check if the files have been overwritten
await expect(fileContent).toBe(testContent);
await expect(log).toHaveBeenCalledWith(`Checking if file should be overwritten:`);
await expect(log).toHaveBeenCalledWith("file shouldn't be overwritten");
// Check if the log message was printed
await expect(log).toHaveBeenCalledWith(`${testFilePath} was not generated because it already exists and noOverwriteGlobs configuration in template configuration matched.`);
// Check if the log debug message was printed
await expect(logSpyDebug).toHaveBeenCalledWith(`noOverwriteGlobs matched`);
await expect(logSpyDebug).toHaveBeenCalledWith(`Skipping overwrite for: ${testFilePath}`);
console.log('All console.log calls:');

// Print all console.log calls
await log.mock.calls.forEach((call, index) => {
console.log(`${index + 1}:`, call);
});

log.mockRestore();
});
});

0 comments on commit 5e49121

Please sign in to comment.