-
-
Notifications
You must be signed in to change notification settings - Fork 207
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
feat: support noOverwriteGlobs
for templates based on react
#1191
base: master
Are you sure you want to change the base?
Changes from 58 commits
fc81484
94c04b5
4bccad3
57ec2ee
8333600
d467476
4999885
19e11df
c0b9ec8
d147b9f
7dd724f
1ff0f22
98a1e71
e9f5bd0
695e960
3bb9e6f
6aa3b59
380e210
1b638a2
30bf2f6
a7930b8
2e8dda6
1047cda
a3c0a86
95d44f8
2e6cd81
36815ce
655d39b
e075fff
a94fd00
cd22f3e
2117147
6ae0b80
33eb3e5
ca40710
332a4ed
214387a
e5db0f6
3505053
c4f29c2
038c130
b2fa883
15629dc
5e49121
a23636a
55f9ae7
900faa9
3e7afe7
b3b29a1
6854f6b
fbd41c8
09e7882
49c8952
eebca77
2f3a3ef
f5a6251
0385f26
be28dea
12ada05
06848d4
1ec96a4
61d795a
f5180b7
4305a9c
ce41fe0
c741598
2ee44db
2d77e3c
47ae19b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,25 +3,38 @@ | |
*/ | ||
|
||
const { readFile } = require('fs').promises; | ||
const { existsSync } = require('fs'); | ||
const path = require('path'); | ||
const Generator = require('../lib/generator'); | ||
const dummySpecPath = path.resolve(__dirname, './docs/dummy.yml'); | ||
const refSpecPath = path.resolve(__dirname, './docs/apiwithref.json'); | ||
const refSpecFolder = path.resolve(__dirname, './docs/'); | ||
const crypto = require('crypto'); | ||
const {mkdirSync} = require('fs'); | ||
const {exists, writeFile} = require('../lib/utils'); | ||
const mainTestResultPath = 'test/temp/integrationTestResult'; | ||
const reactTemplate = 'test/test-templates/react-template'; | ||
const nunjucksTemplate = 'test/test-templates/nunjucks-template'; | ||
const logMessage = require('../lib/logMessages.js'); | ||
const log = require('loglevel'); | ||
|
||
describe('Integration testing generateFromFile() to make sure the result of the generation is not changend comparing to snapshot', () => { | ||
const generateFolderName = () => { | ||
//you always want to generate to new directory to make sure test runs in clear environment | ||
// you always want to generate to new directory to make sure test runs in clear environment | ||
// normalize the path for cross-platform compatibility | ||
// return path.normalize(path.resolve(mainTestResultPath, crypto.randomBytes(4).toString('hex'))); | ||
return path.resolve(mainTestResultPath, crypto.randomBytes(4).toString('hex')); | ||
}; | ||
|
||
jest.setTimeout(60000); | ||
const testOutputFile = 'test-file.md'; | ||
|
||
beforeAll(() => { | ||
if (!existsSync(path.join(reactTemplate, 'package.json'))) { | ||
throw new Error(`React template not found at ${reactTemplate}`); | ||
} | ||
}); | ||
|
||
it('generated using Nunjucks template', async () => { | ||
const outputDir = generateFolderName(); | ||
const generator = new Generator(nunjucksTemplate, outputDir, { | ||
|
@@ -55,4 +68,39 @@ describe('Integration testing generateFromFile() to make sure the result of the | |
const file = await readFile(path.join(outputDir, testOutputFile), 'utf8'); | ||
expect(file).toMatchSnapshot(); | ||
}); | ||
|
||
it('should ignore specified files with noOverwriteGlobs', async () => { | ||
const logSpyDebug = jest.spyOn(log, 'debug').mockImplementation(() => {}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried that. jest.fn() does not work but spyOn() does. The former does not actually track the log.debug() calls in other files, but the latter does by creating a layer of observation around the existing methods. |
||
// log.debug = jest.fn(); | ||
|
||
const outputDir = generateFolderName(); | ||
// Manually create a file to test if it's not overwritten | ||
lmgyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!await exists(outputDir)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually I don't get why we need to validate if it exists, if a line earlier it is created There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If I understand it correctly, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that in other tests you have written, you did not check and create the directory. Did you not have directory or path not found issue? If I comment out line 78-80, I have
|
||
mkdirSync(outputDir, { recursive: true }); | ||
} | ||
// Create a variable to store the file content | ||
const testContent = '<script>const initialContent = "This should not change";</script>'; | ||
// eslint-disable-next-line sonarjs/no-duplicate-string | ||
const testFilePath = path.normalize(path.resolve(outputDir, testOutputFile)); | ||
await writeFile(testFilePath, testContent); | ||
|
||
// 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(reactTemplate, outputDir, { | ||
forceWrite: true, | ||
derberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
noOverwriteGlobs: [`**/${testOutputFile}`], | ||
debug: true | ||
}); | ||
|
||
await generator.generateFromFile(dummySpecPath); | ||
|
||
// Read the file to confirm it was not overwritten | ||
const fileContent = await readFile(testFilePath, 'utf8'); | ||
// Check if the files have been overwritten | ||
await expect(fileContent).toBe(testContent); | ||
// Check if the log debug message was printed | ||
await expect(logSpyDebug).toHaveBeenCalledWith(logMessage.skipOverwrite(testFilePath)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expect is not async call, so no need for |
||
|
||
// Clean up | ||
logSpyDebug.mockRestore(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted. I used it during development because I could not find the package.json sometimes.