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

[typings-generator] Add some options to typings generation and introduce a Heft plugin for generating loc file typings. #4894

Merged
merged 21 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Introduce a test for StringValuesTypingsGenerator.
  • Loading branch information
iclanton committed Aug 21, 2024
commit 2ba61086ac5af30202f7e824a92dd725cb81069a
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ function convertToTypingsGeneratorOptions<TFileContents>(
let interfaceDocumentationCommentLines: string[] | undefined;
if (typeof exportAsDefaultOptionsOverride === 'boolean') {
if (exportAsDefaultOptionsOverride) {
exportAsDefaultInterfaceName = defaultExportAsDefaultInterfaceName;
exportAsDefaultInterfaceName =
defaultExportAsDefaultInterfaceName ?? EXPORT_AS_DEFAULT_INTERFACE_NAME;
interfaceDocumentationCommentLines = defaultSplitExportAsDefaultDocumentationComment;
}
} else if (exportAsDefaultOptionsOverride) {
const { interfaceName, documentationComment } = exportAsDefaultOptionsOverride;
exportAsDefaultInterfaceName = interfaceName ?? defaultExportAsDefaultInterfaceName;
exportAsDefaultInterfaceName =
interfaceName ?? defaultExportAsDefaultInterfaceName ?? EXPORT_AS_DEFAULT_INTERFACE_NAME;
interfaceDocumentationCommentLines =
documentationComment?.split(/\r?\n/) ?? defaultSplitExportAsDefaultDocumentationComment;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import type { StringBufferTerminalProvider, Terminal } from '@rushstack/terminal';
import type {
IStringValuesTypingsGeneratorBaseOptions,
IStringValueTypings
} from '../StringValuesTypingsGenerator';

let inputFs: Record<string, string>;
let outputFs: Record<string, string>;

jest.mock('@rushstack/node-core-library', () => {
const realNcl: typeof import('@rushstack/node-core-library') = jest.requireActual(
'@rushstack/node-core-library'
);
return {
...realNcl,
FileSystem: {
readFileAsync: async (filePath: string) => {
const result: string | undefined = inputFs[filePath];
if (result === undefined) {
const error: NodeJS.ErrnoException = new Error(
`Cannot read file ${filePath}`
) as NodeJS.ErrnoException;
error.code = 'ENOENT';
throw error;
} else {
return result;
}
},
writeFileAsync: async (filePath: string, contents: string) => {
outputFs[filePath] = contents;
}
}
};
});

describe('StringValuesTypingsGenerator', () => {
beforeEach(() => {
inputFs = {};
outputFs = {};
});

function runTests(
baseOptions: IStringValuesTypingsGeneratorBaseOptions,
extraStringTypings?: Partial<IStringValueTypings>
): void {
it('should generate typings', async () => {
const [{ StringValuesTypingsGenerator }, { Terminal, StringBufferTerminalProvider }] =
await Promise.all([import('../StringValuesTypingsGenerator'), import('@rushstack/terminal')]);
const terminalProvider: StringBufferTerminalProvider = new StringBufferTerminalProvider();
const terminal: Terminal = new Terminal(terminalProvider);

inputFs['/src/test.ext'] = '';

const fileContents: {} = { a: 1 };
const generator = new StringValuesTypingsGenerator({
srcFolder: '/src',
generatedTsFolder: '/out',
readFile: (filePath: string, relativePath: string) => {
expect(relativePath).toEqual('test.ext');
return Promise.resolve(fileContents);
},
fileExtensions: ['.ext'],
parseAndGenerateTypings: (contents: {}, filePath: string, relativePath: string) => {
expect(contents).toBe(fileContents);
return {
typings: [
{
exportName: 'test',
comment: 'test comment\nsecond line'
}
],
...extraStringTypings
};
},
terminal,
...baseOptions
} as ConstructorParameters<
typeof import('../StringValuesTypingsGenerator').StringValuesTypingsGenerator
>[0]);

await generator.generateTypingsAsync(['test.ext']);
expect(outputFs).toMatchSnapshot();

expect(terminalProvider.getOutput()).toEqual('');
expect(terminalProvider.getWarningOutput()).toEqual('');
expect(terminalProvider.getErrorOutput()).toEqual('');
expect(terminalProvider.getVerboseOutput()).toEqual('');
expect(terminalProvider.getDebugOutput()).toEqual('');
});
}

describe('non-default exports', () => {
runTests({});
});

describe('default exports', () => {
describe('with { exportAsDefault: true }', () => {
runTests({ exportAsDefault: true });
});

describe("with { exportAsDefault: true, exportAsDefaultInterfaceName: 'IOverride' }", () => {
runTests({
exportAsDefault: true,
exportAsDefaultInterfaceName: 'IOverride'
});
});

describe("with { exportAsDefault: {}, exportAsDefaultInterfaceName: 'IOverride' }", () => {
runTests({
exportAsDefault: {},
exportAsDefaultInterfaceName: 'IOverride'
});
});

describe("with { exportAsDefault: { interfaceName: 'IOverride' }, exportAsDefaultInterfaceName: 'IDeprecated' }", () => {
runTests({
exportAsDefault: {
interfaceName: 'IOverride'
},
exportAsDefaultInterfaceName: 'IDeprecated'
});
});

describe("with { exportAsDefault: { documentationComment: 'doc-comment\\nsecond line' } }", () => {
runTests({
exportAsDefault: {
documentationComment: 'doc-comment\nsecond line'
}
});
});

describe('overrides for individual files', () => {
describe('with exportAsDefault unset', () => {
describe('overriding with { exportAsDefault: false }', () => {
runTests(
{},
{
exportAsDefault: false
}
);
});

describe("overriding with { interfaceName: 'IOverride' } ", () => {
runTests(
{},
{
exportAsDefault: {
interfaceName: 'IOverride'
}
}
);
});

describe('overriding with a new doc comment ', () => {
runTests(
{},
{
exportAsDefault: {
documentationComment: 'doc-comment\nsecond line'
}
}
);
});
});

describe('with exportAsDefault set to true', () => {
describe('overriding with { exportAsDefault: false }', () => {
runTests(
{
exportAsDefault: true
},
{
exportAsDefault: false
}
);
});

describe("overriding with { interfaceName: 'IOverride' } ", () => {
runTests(
{
exportAsDefault: true
},
{
exportAsDefault: {
interfaceName: 'IOverride'
}
}
);
});

describe('overriding with a new doc comment ', () => {
runTests(
{
exportAsDefault: true
},
{
exportAsDefault: {
documentationComment: 'doc-comment\nsecond line'
}
}
);
});
});

describe('with exportAsDefault set to {}', () => {
describe('overriding with { exportAsDefault: false }', () => {
runTests(
{
exportAsDefault: {}
},
{
exportAsDefault: false
}
);
});

describe("overriding with { interfaceName: 'IOverride' } ", () => {
runTests(
{
exportAsDefault: {}
},
{
exportAsDefault: {
interfaceName: 'IOverride'
}
}
);
});

describe('overriding with a new doc comment ', () => {
runTests(
{
exportAsDefault: {}
},
{
exportAsDefault: {
documentationComment: 'doc-comment\nsecond line'
}
}
);
});
});

describe('with exportAsDefault filled', () => {
describe('overriding with { exportAsDefault: false }', () => {
runTests(
{
exportAsDefault: {
interfaceName: 'IBase',
documentationComment: 'base-comment'
}
},
{
exportAsDefault: false
}
);
});

describe("overriding with { interfaceName: 'IOverride' } ", () => {
runTests(
{
exportAsDefault: {
interfaceName: 'IBase',
documentationComment: 'base-comment'
}
},
{
exportAsDefault: {
interfaceName: 'IOverride'
}
}
);
});

describe('overriding with a new doc comment ', () => {
runTests(
{
exportAsDefault: {
interfaceName: 'IBase',
documentationComment: 'base-comment'
}
},
{
exportAsDefault: {
documentationComment: 'doc-comment\nsecond line'
}
}
);
});
});
});
});
});
Loading