Skip to content

Commit 8497665

Browse files
committed
Add missing unit tests
1 parent c85830e commit 8497665

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

src/tasks/generator/generator-command.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,86 @@ describe('GeneratorCommand', () => {
9494
expect(mockGetCreatedChannelByName?.show).toHaveBeenCalled();
9595
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith('Failed to launch Generator gen-fail!');
9696
});
97+
98+
it('does not show output channel on success', async () => {
99+
solutionManager.getCsolution.mockReturnValue(csolutionFactory({ solutionPath: 'mock/path.csolution.yml' }));
100+
cmsisToolboxManager.runCmsisTool.mockResolvedValue([0, undefined]);
101+
102+
await generatorCommand.handleRunGenerator('gen-ok', 'release');
103+
104+
const channel = outputChannelProvider.mockGetCreatedChannelByName(CMSIS_SOLUTION_OUTPUT_CHANNEL);
105+
expect(channel?.show).not.toHaveBeenCalled();
106+
});
107+
108+
it('appends the start message to the output channel', async () => {
109+
solutionManager.getCsolution.mockReturnValue(csolutionFactory({ solutionPath: 'mock/path.csolution.yml' }));
110+
111+
await generatorCommand.handleRunGenerator('my-gen', 'debug');
112+
113+
const channel = outputChannelProvider.mockGetCreatedChannelByName(CMSIS_SOLUTION_OUTPUT_CHANNEL);
114+
expect(channel?.mockAppendedStrings).toContain('Starting generator my-gen for context debug...');
115+
});
116+
117+
describe('command dispatch', () => {
118+
beforeEach(async () => {
119+
await generatorCommand.activate(context as unknown as vscode.ExtensionContext);
120+
solutionManager.getCsolution.mockReturnValue(csolutionFactory({ solutionPath: 'mock/path.csolution.yml' }));
121+
});
122+
123+
it('dispatches handleRunGenerator when input is a component-gen COutlineItem-like node', async () => {
124+
const node = {
125+
getAttribute: (name: string) => ({ type: 'component-gen', generator: 'STM32CubeMX', 'cbuild-context': '.debug+target' }[name]),
126+
};
127+
128+
await commandsProvider.mockRunRegistered(GeneratorCommand.runGeneratorCommandType, node);
129+
130+
expect(cmsisToolboxManager.runCmsisTool).toHaveBeenCalledWith(
131+
'csolution',
132+
expect.arrayContaining(['-g', 'STM32CubeMX', '-c', '.debug+target']),
133+
expect.any(Function),
134+
undefined,
135+
undefined,
136+
true
137+
);
138+
});
139+
140+
it('does not dispatch when input is a COutlineItem-like node with wrong type', async () => {
141+
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined);
142+
const node = {
143+
getAttribute: (name: string) => ({ type: 'component', generator: 'STM32CubeMX', 'cbuild-context': '.debug+target' }[name]),
144+
};
145+
146+
await commandsProvider.mockRunRegistered(GeneratorCommand.runGeneratorCommandType, node);
147+
148+
expect(cmsisToolboxManager.runCmsisTool).not.toHaveBeenCalled();
149+
expect(consoleSpy).toHaveBeenCalled();
150+
consoleSpy.mockRestore();
151+
});
152+
153+
it('dispatches handleRunGenerator when input is a plain RunGeneratorRequest object', async () => {
154+
await commandsProvider.mockRunRegistered(GeneratorCommand.runGeneratorCommandType, {
155+
generator: 'plain-gen',
156+
context: 'test+board',
157+
});
158+
159+
expect(cmsisToolboxManager.runCmsisTool).toHaveBeenCalledWith(
160+
'csolution',
161+
expect.arrayContaining(['-g', 'plain-gen', '-c', 'test+board']),
162+
expect.any(Function),
163+
undefined,
164+
undefined,
165+
true
166+
);
167+
});
168+
169+
it('logs an error and does not dispatch when input is invalid', async () => {
170+
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined);
171+
172+
await commandsProvider.mockRunRegistered(GeneratorCommand.runGeneratorCommandType, undefined);
173+
174+
expect(cmsisToolboxManager.runCmsisTool).not.toHaveBeenCalled();
175+
expect(consoleSpy).toHaveBeenCalled();
176+
consoleSpy.mockRestore();
177+
});
178+
});
97179
});

0 commit comments

Comments
 (0)