-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'IdentifierRegistry' mechanism for debugging IDs
Whenever an ID is generated, we will optionally log it to a file with the data used to generate it. This will help us debug generated identifiers on artefacts or runtime errors. Test Plan: N/A Pull Request: #333
- Loading branch information
Showing
6 changed files
with
136 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// @flow strict-local | ||
|
||
import fs from 'fs'; | ||
|
||
export class IdentifierRegistry { | ||
#enabled: boolean; | ||
#createdDirectory: boolean = false; | ||
|
||
constructor(enabled: boolean) { | ||
this.#enabled = enabled; | ||
} | ||
|
||
addIdentifier(type: string, identifier: string, data: mixed) { | ||
if (this.#enabled) { | ||
if (!this.#createdDirectory) { | ||
fs.mkdirSync('./.atlaspack', {recursive: true}); | ||
this.#createdDirectory = true; | ||
} | ||
|
||
fs.appendFile( | ||
'./.atlaspack/atlaspack-identifiers.txt', | ||
// $FlowFixMe | ||
`${type} ${identifier} ${JSON.stringify(data)}\n`, | ||
() => {}, | ||
); | ||
} | ||
} | ||
} | ||
|
||
export const identifierRegistry: IdentifierRegistry = new IdentifierRegistry( | ||
process.env.ATLASPACK_IDENTIFIER_DEBUG === 'true', | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// @flow strict-local | ||
|
||
import assert from 'assert'; | ||
import fs from 'fs'; | ||
import sinon from 'sinon'; | ||
import {IdentifierRegistry} from '../src/IdentifierRegistry'; | ||
|
||
describe('IdentifierRegistry', () => { | ||
let mkdirSyncStub; | ||
let fsStub; | ||
|
||
beforeEach(() => { | ||
mkdirSyncStub = sinon.stub(fs, 'mkdirSync'); | ||
fsStub = sinon.stub(fs, 'appendFile'); | ||
}); | ||
|
||
afterEach(() => { | ||
mkdirSyncStub.restore(); | ||
fsStub.restore(); | ||
}); | ||
|
||
it("does nothing if the identifers flag isn't set", () => { | ||
const identifierRegistry = new IdentifierRegistry(false); | ||
identifierRegistry.addIdentifier('type', 'identifier', 'data'); | ||
assert(!mkdirSyncStub.called); | ||
assert(!fsStub.called); | ||
}); | ||
|
||
it('appends the identifier to the file', () => { | ||
const identifierRegistry = new IdentifierRegistry(true); | ||
identifierRegistry.addIdentifier('type', 'identifier', 'data'); | ||
assert( | ||
fsStub.calledWith( | ||
'./.atlaspack/atlaspack-identifiers.txt', | ||
'type identifier "data"\n', | ||
sinon.match.func, | ||
), | ||
); | ||
}); | ||
|
||
it('only creates the directory once', () => { | ||
const identifierRegistry = new IdentifierRegistry(true); | ||
identifierRegistry.addIdentifier('type', 'identifier', '1'); | ||
identifierRegistry.addIdentifier('type', 'identifier', '2'); | ||
|
||
assert(mkdirSyncStub.calledOnce); | ||
|
||
assert(mkdirSyncStub.calledWith('./.atlaspack', {recursive: true})); | ||
assert( | ||
fsStub.calledWith( | ||
'./.atlaspack/atlaspack-identifiers.txt', | ||
'type identifier "1"\n', | ||
sinon.match.func, | ||
), | ||
); | ||
assert( | ||
fsStub.calledWith( | ||
'./.atlaspack/atlaspack-identifiers.txt', | ||
'type identifier "2"\n', | ||
sinon.match.func, | ||
), | ||
); | ||
}); | ||
}); |