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

Bugfix/RECGEN-59 duplicate incremental data #33

Merged
merged 3 commits into from
Aug 24, 2023
Merged
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
1 change: 1 addition & 0 deletions generator/src/models/incremental-data-template-piece.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface IncrementalDataTemplatePiece {
id: string
generatorName?: string
marker: string
body: string
outputFile: string
Expand Down
2 changes: 1 addition & 1 deletion generator/src/rendering/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class Renderer {
const engine = this.getTemplateEngine(file.path)

if (this.context) {
await engine?.setup(this.context!.incrementalDataHandler)
await engine?.setup(this.generator.metaData.name, this.context!.incrementalDataHandler)
}

if (!engine) {
Expand Down
10 changes: 6 additions & 4 deletions generator/src/templating/incremental-data-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export class IncrementalDataHandler {

async renderIncrementalData(context: EntityContext, generator: Generator) {
const engine = new NunjucksTemplateEngine(generator.metaData.templateRoot)
await engine.setup(this)
await engine.setup(generator.metaData.name, this)

for (const piece of this.dataPieces) {
for (const piece of this.dataPieces.filter(d => d.generatorName === undefined || d.generatorName === generator.metaData.name)) {
const file = await this.filesystem.read(piece.outputFile)
const text = file.toString()

Expand All @@ -35,14 +35,16 @@ export class IncrementalDataHandler {
}
}

async registerDataPiece(id: string, body: string, path: string, markerLanguage: string): Promise<string> {
// eslint-disable-next-line max-params
async registerDataPiece(id: string, body: string, path: string, markerLanguage: string, generatorName?: string): Promise<string> {
const marker = this.createMarker(id, markerLanguage)
const piece = {
id,
marker,
body,
outputFile: path,
}
generatorName: generatorName,
} as IncrementalDataTemplatePiece

this.dataPieces.push(piece)

Expand Down
8 changes: 4 additions & 4 deletions generator/src/templating/nunjucks-template-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export class NunjucksTemplateEngine implements TemplateEngine {

constructor(private root: string = '') {}

async setup(incrementalDataHandler: IncrementalDataHandler) {
this.environment = this.buildEnvironment(incrementalDataHandler)
async setup(generatorName: string, incrementalDataHandler: IncrementalDataHandler) {
this.environment = this.buildEnvironment(generatorName, incrementalDataHandler)
}

render(template: string, context: { [p: string]: any }, outputFile?: string): Promise<string> {
Expand Down Expand Up @@ -41,7 +41,7 @@ export class NunjucksTemplateEngine implements TemplateEngine {
return path.replace(/\.njk$/, '')
}

buildEnvironment(incrementalDataHandler?: IncrementalDataHandler): nunjucks.Environment {
buildEnvironment(generatorName?: string, incrementalDataHandler?: IncrementalDataHandler): nunjucks.Environment {
const loader = new nunjucks.FileSystemLoader(this.root)
const environment = new nunjucks.Environment(loader)

Expand Down Expand Up @@ -103,7 +103,7 @@ export class NunjucksTemplateEngine implements TemplateEngine {
return new nodes.CallExtensionAsync(this, 'run', args, [])
},
async run({ctx}: any, {id, lang: markerLanguage}, callback) {
const marker = await incrementalDataHandler?.registerDataPiece(id, this.body[id], ctx.outputFile, markerLanguage ?? 'html')
const marker = await incrementalDataHandler?.registerDataPiece(id, this.body[id], ctx.outputFile, markerLanguage ?? 'html', generatorName)

callback(null, new nunjucks.runtime.SafeString(marker ?? ''))
},
Expand Down
2 changes: 1 addition & 1 deletion generator/src/templating/template-engine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IncrementalDataHandler} from './incremental-data-handler'

export interface TemplateEngine {
setup(incrementalDataHandler: IncrementalDataHandler): Promise<void>;
setup(generatorName: string, incrementalDataHandler: IncrementalDataHandler): Promise<void>;
render(template: string, context: { [key: string]: any }, outputFile?: string): Promise<string>;
supports(path: string): boolean;
transformFilename(path: string): string;
Expand Down
25 changes: 15 additions & 10 deletions generator/test/io/local-filesystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import {expect} from 'chai'
import sinon from 'sinon'
import {promises as fs} from 'node:fs'
import {LocalFilesystem} from '../../src'
import * as os from 'node:os'

describe('local file system', () => {
let localFs: LocalFilesystem
let root: string
let sandbox: sinon.SinonSandbox
let directorySeparator: string = '/'

beforeEach(() => {
root = '/tmp/test'
const tempDir = os.tmpdir() // /tmp
directorySeparator = tempDir.includes('\\') ? '\\' : '/'
root = tempDir + directorySeparator + 'test' + directorySeparator
localFs = new LocalFilesystem(root)
sandbox = sinon.createSandbox()
})
Expand All @@ -29,7 +33,7 @@ describe('local file system', () => {
const result = await localFs.read(path)

sinon.assert.calledOnce(readFileStub)
sinon.assert.calledWithExactly(readFileStub, `${root}/${path}`)
sinon.assert.calledWithExactly(readFileStub, `${root}${path}`)

expect(result.toString()).to.equal(content)
})
Expand All @@ -48,7 +52,7 @@ describe('local file system', () => {
}

sinon.assert.calledOnce(readFileStub)
sinon.assert.calledWithExactly(readFileStub, `${root}/${path}`)
sinon.assert.calledWithExactly(readFileStub, `${root}${path}`)
})
})

Expand All @@ -61,14 +65,14 @@ describe('local file system', () => {
const writeFileStub = sandbox.stub(fs, 'writeFile')
await localFs.write(path, content)

sinon.assert.calledOnceWithExactly(writeFileStub, '/tmp/test/path/to/file.txt', content)
sinon.assert.calledOnceWithExactly(writeFileStub, root + 'path' + directorySeparator + 'to' + directorySeparator + 'file.txt', content)
})

it('should set the permissions if provided', async () => {
const chmodStub = sandbox.stub(fs, 'chmod')
await localFs.write(path, content, permissions)

sinon.assert.calledOnceWithExactly(chmodStub, '/tmp/test/path/to/file.txt', permissions)
sinon.assert.calledOnceWithExactly(chmodStub, root + 'path' + directorySeparator + 'to' + directorySeparator + 'file.txt', permissions)
})

it('should not set the permissions if not provided', async () => {
Expand Down Expand Up @@ -130,7 +134,7 @@ describe('local file system', () => {

await localFs.delete('/path/to/file.txt')

expect(unlinkStub.calledOnceWith('/tmp/test/path/to/file.txt')).to.be.true
expect(unlinkStub.calledOnceWith(root + 'path' + directorySeparator + 'to' + directorySeparator + 'file.txt')).to.be.true

unlinkStub.restore()
})
Expand All @@ -145,7 +149,8 @@ describe('local file system', () => {

await localFs.copy('/path/to/file.txt', '/path/to/other/file.txt')

expect(copyFileStub.calledOnceWith('/tmp/test/path/to/file.txt', '/tmp/test/path/to/other/file.txt')).to.be.true
expect(copyFileStub.calledOnceWith(root + 'path' + directorySeparator + 'to' + directorySeparator + 'file.txt',
root + 'path' + directorySeparator + 'to' + directorySeparator + 'other' + directorySeparator + 'file.txt')).to.be.true
})
})

Expand Down Expand Up @@ -177,7 +182,7 @@ describe('local file system', () => {

await localFs.createDirectory('/path/to/dir')

expect(mkdirStub.calledOnceWith('/tmp/test/path/to/dir')).to.be.true
expect(mkdirStub.calledOnceWith(root + 'path' + directorySeparator + 'to' + directorySeparator + 'dir')).to.be.true
})
})

Expand All @@ -188,7 +193,7 @@ describe('local file system', () => {

await localFs.deleteDirectory('/path/to/dir')

expect(rmdirStub.calledOnceWith('/tmp/test/path/to/dir')).to.be.true
expect(rmdirStub.calledOnceWith(root + 'path' + directorySeparator + 'to' + directorySeparator + 'dir')).to.be.true
})
})

Expand All @@ -199,7 +204,7 @@ describe('local file system', () => {

await localFs.updatePermissions('/path/to/file.txt', 0o644)

expect(chmodStub.calledOnceWith('/tmp/test/path/to/file.txt', 0o644)).to.be.true
expect(chmodStub.calledOnceWith(root + 'path' + directorySeparator + 'to' + directorySeparator + 'file.txt', 0o644)).to.be.true
})
})

Expand Down
32 changes: 30 additions & 2 deletions generator/test/rendering/incremental-data-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import sinon from 'sinon'
import {EntityContext, FakerTestDataManager, Generator, LocalFilesystem, Project} from '../../src'
import {LockFileManager} from '../../src/lock-file/lock-file-manager'
import {expect} from 'chai'
import {EOL} from 'node:os'

describe('incremental data handler', () => {
let handler: IncrementalDataHandler
Expand Down Expand Up @@ -50,13 +51,38 @@ describe('incremental data handler', () => {
marker,
body: 'test',
outputFile: 'test.txt',
generatorName: undefined,
},
])
sinon.assert.calledOnceWithExactly(lockFileManagerStub.addIncrementalData, {
id: '1234567890',
marker,
body: 'test',
outputFile: 'test.txt',
generatorName: undefined,
})
})

it('should register a generator name in data piece when available', async () => {
const marker = await handler.registerDataPiece('1234567890', 'test', 'test.txt', 'html', 'generator1')
const validateMarkerRegex = /<!-- sc3000: 1234567890-[\da-z]+ do not remove this line -->/gi

expect(marker).to.match(validateMarkerRegex)
expect(handler.getDataPieces()).to.deep.equal([
{
id: '1234567890',
marker,
body: 'test',
outputFile: 'test.txt',
generatorName: 'generator1',
},
])
sinon.assert.calledOnceWithExactly(lockFileManagerStub.addIncrementalData, {
id: '1234567890',
marker,
body: 'test',
outputFile: 'test.txt',
generatorName: 'generator1',
})
})

Expand All @@ -71,13 +97,15 @@ describe('incremental data handler', () => {
marker,
body: 'test',
outputFile: 'test.txt',
generatorName: undefined,
},
])
sinon.assert.calledOnceWithExactly(lockFileManagerStub.addIncrementalData, {
id: '1234567890',
marker,
body: 'test',
outputFile: 'test.txt',
generatorName: undefined,
})
})

Expand All @@ -90,6 +118,7 @@ describe('incremental data handler', () => {
marker: '<!-- SC3000: 1234567890 do not remove this line -->',
body: 'body {{ entity.name }}',
id: '1234567890',
generatorName: 'generator1',
},
],
}
Expand Down Expand Up @@ -126,8 +155,7 @@ describe('incremental data handler', () => {

await handler.renderIncrementalData(context, generator)

const result = `body Project
<!-- SC3000: 1234567890 do not remove this line -->`
const result = 'body Project' + EOL + '<!-- SC3000: 1234567890 do not remove this line -->'

sinon.assert.calledOnceWithExactly(filesystemStub.write, 'test.txt', Buffer.from(result))
})
Expand Down