Skip to content

Commit 202fcb8

Browse files
authored
Merge pull request #2867 from opossum-tool/bug/missing_file_error_message_missing
Bug/missing file error message missing
2 parents ff0b32c + b95205a commit 202fcb8

19 files changed

+508
-252
lines changed

src/ElectronBackend/errorHandling/errorHandling.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { AllowedFrontendChannels } from '../../shared/ipc-channels';
1515
import { loadInputAndOutputFromFilePath } from '../input/importFromFile';
1616
import { getGlobalBackendState } from '../main/globalBackendState';
1717
import logger from '../main/logger';
18+
import { ProcessingStatusUpdater } from '../main/ProcessingStatusUpdater';
1819
import { getLoadedFilePath } from '../utils/getLoadedFile';
1920

2021
export async function showListenerErrorInMessageBox(
@@ -41,14 +42,13 @@ export async function showListenerErrorInMessageBox(
4142
}
4243

4344
export function sendListenerErrorToFrontend(
44-
_: BrowserWindow,
45+
processingStatusUpdater: ProcessingStatusUpdater,
4546
error: unknown,
4647
): void {
47-
// NOTE: these log messages are forwarded to the frontend
4848
if (error instanceof Error) {
49-
logger.error(error.message);
49+
processingStatusUpdater.error(error.message);
5050
} else {
51-
logger.error('Unexpected internal error');
51+
processingStatusUpdater.error('Unexpected internal error');
5252
}
5353
}
5454

src/ElectronBackend/input/__tests__/importFromFile.test.ts

+116-39
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,46 @@ jest.mock('uuid', () => ({
5353
v4: (): string => manualAttributionUuid,
5454
}));
5555

56+
type SendCall = {
57+
channel: string;
58+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
59+
args: Array<any>;
60+
};
61+
62+
class MockWebContents {
63+
#calls: Array<SendCall> = [];
64+
65+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
66+
send(channel: string, args: Array<any>): void {
67+
this.#calls.push({ channel, args });
68+
}
69+
70+
#callsFromChannel(channel: AllowedFrontendChannels): Array<SendCall> {
71+
return this.#calls.filter(
72+
(sendCall) => sendCall.channel === String(channel),
73+
);
74+
}
75+
76+
numberOfCallsFromChannel(channel: AllowedFrontendChannels): number {
77+
return this.#callsFromChannel(channel).length;
78+
}
79+
80+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
81+
lastArgumentFromChannel(channel: AllowedFrontendChannels): any {
82+
const callsFromChannel = this.#callsFromChannel(channel);
83+
if (callsFromChannel.length) {
84+
return callsFromChannel[callsFromChannel.length - 1].args;
85+
}
86+
return undefined;
87+
}
88+
89+
reset(): void {
90+
this.#calls = [];
91+
}
92+
}
93+
5694
const mainWindow = {
57-
webContents: {
58-
send: jest.fn(),
59-
},
95+
webContents: new MockWebContents(),
6096
setTitle: jest.fn(),
6197
} as unknown as BrowserWindow;
6298

@@ -184,6 +220,7 @@ const validMetadata = {
184220
describe('Test of loading function', () => {
185221
afterEach(() => {
186222
jest.resetAllMocks();
223+
(mainWindow.webContents as unknown as MockWebContents).reset();
187224
});
188225

189226
it('handles Parsing error correctly', async () => {
@@ -209,10 +246,15 @@ describe('Test of loading function', () => {
209246

210247
await loadInputAndOutputFromFilePath(mainWindow, corruptJsonPath);
211248

212-
const expectedNumberOfCalls = 3;
213-
expect(mainWindow.webContents.send).toHaveBeenCalledTimes(
214-
expectedNumberOfCalls,
215-
);
249+
const webContents = mainWindow.webContents as unknown as MockWebContents;
250+
expect(
251+
webContents.numberOfCallsFromChannel(
252+
AllowedFrontendChannels.ResetLoadedFile,
253+
),
254+
).toBe(2);
255+
expect(
256+
webContents.numberOfCallsFromChannel(AllowedFrontendChannels.FileLoaded),
257+
).toBe(1);
216258

217259
expect(getGlobalBackendState()).toEqual(expectedBackendState);
218260
});
@@ -273,11 +315,18 @@ describe('Test of loading function', () => {
273315
setGlobalBackendState({});
274316
await loadInputAndOutputFromFilePath(mainWindow, opossumPath);
275317

276-
expect(mainWindow.webContents.send).toHaveBeenLastCalledWith(
277-
AllowedFrontendChannels.FileLoaded,
278-
expectedFileContent,
279-
);
280-
expect(mainWindow.webContents.send).toHaveBeenCalledTimes(2);
318+
const webContents = mainWindow.webContents as unknown as MockWebContents;
319+
expect(
320+
webContents.lastArgumentFromChannel(AllowedFrontendChannels.FileLoaded),
321+
).toEqual(expectedFileContent);
322+
expect(
323+
webContents.numberOfCallsFromChannel(AllowedFrontendChannels.FileLoaded),
324+
).toBe(1);
325+
expect(
326+
webContents.numberOfCallsFromChannel(
327+
AllowedFrontendChannels.ResetLoadedFile,
328+
),
329+
).toBe(1);
281330

282331
expect(dialog.showMessageBox).not.toHaveBeenCalled();
283332
});
@@ -292,11 +341,20 @@ describe('Test of loading function', () => {
292341
setGlobalBackendState({});
293342
await loadInputAndOutputFromFilePath(mainWindow, jsonPath);
294343

295-
expect(mainWindow.webContents.send).toHaveBeenCalledTimes(2);
296-
expect(mainWindow.webContents.send).toHaveBeenLastCalledWith(
297-
AllowedFrontendChannels.FileLoaded,
298-
expectedFileContent,
299-
);
344+
const webContents = mainWindow.webContents as unknown as MockWebContents;
345+
expect(
346+
webContents.numberOfCallsFromChannel(
347+
AllowedFrontendChannels.FileLoaded,
348+
),
349+
).toBe(1);
350+
expect(
351+
webContents.lastArgumentFromChannel(AllowedFrontendChannels.FileLoaded),
352+
).toEqual(expectedFileContent);
353+
expect(
354+
webContents.numberOfCallsFromChannel(
355+
AllowedFrontendChannels.ResetLoadedFile,
356+
),
357+
).toBe(1);
300358

301359
expect(dialog.showMessageBox).not.toHaveBeenCalled();
302360
});
@@ -313,11 +371,20 @@ describe('Test of loading function', () => {
313371
setGlobalBackendState({});
314372
await loadInputAndOutputFromFilePath(mainWindow, jsonPath);
315373

316-
expect(mainWindow.webContents.send).toHaveBeenCalledTimes(2);
317-
expect(mainWindow.webContents.send).toHaveBeenLastCalledWith(
318-
AllowedFrontendChannels.FileLoaded,
319-
expectedFileContent,
320-
);
374+
const webContents = mainWindow.webContents as unknown as MockWebContents;
375+
expect(
376+
webContents.numberOfCallsFromChannel(
377+
AllowedFrontendChannels.FileLoaded,
378+
),
379+
).toBe(1);
380+
expect(
381+
webContents.lastArgumentFromChannel(AllowedFrontendChannels.FileLoaded),
382+
).toEqual(expectedFileContent);
383+
expect(
384+
webContents.numberOfCallsFromChannel(
385+
AllowedFrontendChannels.ResetLoadedFile,
386+
),
387+
).toBe(1);
321388
expect(dialog.showMessageBox).not.toHaveBeenCalled();
322389
});
323390
});
@@ -519,10 +586,10 @@ describe('Test of loading function', () => {
519586
},
520587
};
521588

522-
expect(mainWindow.webContents.send).toHaveBeenLastCalledWith(
523-
AllowedFrontendChannels.FileLoaded,
524-
expectedLoadedFile,
525-
);
589+
const webContents = mainWindow.webContents as unknown as MockWebContents;
590+
expect(
591+
webContents.lastArgumentFromChannel(AllowedFrontendChannels.FileLoaded),
592+
).toEqual(expectedLoadedFile);
526593
expect(dialog.showMessageBox).not.toHaveBeenCalled();
527594
},
528595
);
@@ -558,10 +625,10 @@ describe('Test of loading function', () => {
558625
metadata: inputFileContentWithCustomMetadata.metadata,
559626
};
560627

561-
expect(mainWindow.webContents.send).toHaveBeenLastCalledWith(
562-
AllowedFrontendChannels.FileLoaded,
563-
expectedLoadedFile,
564-
);
628+
const webContents = mainWindow.webContents as unknown as MockWebContents;
629+
expect(
630+
webContents.lastArgumentFromChannel(AllowedFrontendChannels.FileLoaded),
631+
).toEqual(expectedLoadedFile);
565632
expect(dialog.showMessageBox).not.toHaveBeenCalled();
566633
});
567634

@@ -613,11 +680,18 @@ describe('Test of loading function', () => {
613680
},
614681
};
615682

616-
expect(mainWindow.webContents.send).toHaveBeenCalledTimes(2);
617-
expect(mainWindow.webContents.send).toHaveBeenLastCalledWith(
618-
AllowedFrontendChannels.FileLoaded,
619-
expectedLoadedFile,
620-
);
683+
const webContents = mainWindow.webContents as unknown as MockWebContents;
684+
expect(
685+
webContents.lastArgumentFromChannel(AllowedFrontendChannels.FileLoaded),
686+
).toEqual(expectedLoadedFile);
687+
expect(
688+
webContents.numberOfCallsFromChannel(AllowedFrontendChannels.FileLoaded),
689+
).toBe(1);
690+
expect(
691+
webContents.numberOfCallsFromChannel(
692+
AllowedFrontendChannels.ResetLoadedFile,
693+
),
694+
).toBe(1);
621695
});
622696
});
623697

@@ -644,10 +718,13 @@ function assertFileLoadedCorrectly(testUuid: string): void {
644718
},
645719
};
646720

647-
expect(mainWindow.webContents.send).toHaveBeenCalledWith(
648-
AllowedFrontendChannels.FileLoaded,
649-
expectedLoadedFile,
650-
);
721+
const webContents = mainWindow.webContents as unknown as MockWebContents;
722+
expect(
723+
webContents.lastArgumentFromChannel(AllowedFrontendChannels.FileLoaded),
724+
).toEqual(expectedLoadedFile);
725+
expect(
726+
webContents.numberOfCallsFromChannel(AllowedFrontendChannels.FileLoaded),
727+
).toBe(1);
651728
expect(dialog.showMessageBox).not.toHaveBeenCalled();
652729
}
653730

src/ElectronBackend/input/__tests__/refineConfiguration.test.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
RawProjectConfig,
1111
} from '../../../shared/shared-types';
1212
import { faker } from '../../../testing/Faker';
13+
import { ProcessingStatusUpdater } from '../../main/ProcessingStatusUpdater';
1314
import { refineConfiguration } from '../refineConfiguration';
1415

1516
function fakePackagesWithClassifications(
@@ -36,9 +37,21 @@ function fakeConfigWithClassificationIds(
3637
return { classifications };
3738
}
3839

40+
function fakeProcessingStatusUpdater(): ProcessingStatusUpdater {
41+
return {
42+
warn: () => {},
43+
info: () => {},
44+
error: () => {},
45+
} as unknown as ProcessingStatusUpdater;
46+
}
47+
3948
describe('check and update configuration', () => {
4049
it('returns configuration with empty classifications if no configuration set', () => {
41-
const result = refineConfiguration(undefined, {});
50+
const result = refineConfiguration(
51+
undefined,
52+
{},
53+
fakeProcessingStatusUpdater(),
54+
);
4255

4356
expect(result).toEqual(EMPTY_PROJECT_CONFIG);
4457
});
@@ -48,7 +61,11 @@ describe('check and update configuration', () => {
4861
const expectedConfiguration: RawProjectConfig = { ...configuration };
4962
const externalAttributions = fakePackagesWithClassifications(1, 0, 1, 0);
5063

51-
const result = refineConfiguration(configuration, externalAttributions);
64+
const result = refineConfiguration(
65+
configuration,
66+
externalAttributions,
67+
fakeProcessingStatusUpdater(),
68+
);
5269

5370
expect(result).toEqual(expectedConfiguration);
5471
});
@@ -58,7 +75,11 @@ describe('check and update configuration', () => {
5875

5976
const externalAttributions = fakePackagesWithClassifications(0, 1, 22);
6077

61-
const result = refineConfiguration(configuration, externalAttributions);
78+
const result = refineConfiguration(
79+
configuration,
80+
externalAttributions,
81+
fakeProcessingStatusUpdater(),
82+
);
6283

6384
const expectedConfiguration: RawProjectConfig = {
6485
classifications: {

0 commit comments

Comments
 (0)