Skip to content

Commit 0789ab5

Browse files
committed
chore: add tests
Signed-off-by: lstocchi <[email protected]>
1 parent 58f77f8 commit 0789ab5

File tree

6 files changed

+1208
-230
lines changed

6 files changed

+1208
-230
lines changed

__mocks__/@podman-desktop/api.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**********************************************************************
2+
* Copyright (C) 2022 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
19+
/**
20+
* Mock the extension API for vitest.
21+
* This file is referenced from vitest.config.js file.
22+
*/
23+
const plugin = {};
24+
module.exports = plugin;

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@
6666
"format:fix": "prettier --write src/**",
6767
"desk:build": "ts-node-esm ./scripts/run.mts build",
6868
"desk:prepare": "ts-node-esm ./scripts/run.mts prepare",
69-
"desk:run": "ts-node-esm ./scripts/run.mts run"
69+
"desk:run": "ts-node-esm ./scripts/run.mts run",
70+
"test": "vitest run --coverage --passWithNoTests"
7071
},
7172
"dependencies": {},
7273
"devDependencies": {
73-
"7zip-min": "^1.4.4",
7474
"@podman-desktop/api": "next",
7575
"@rollup/plugin-commonjs": "^24.0.1",
7676
"@rollup/plugin-json": "^6.0.0",
@@ -79,6 +79,8 @@
7979
"@types/node": "^18.14.6",
8080
"@typescript-eslint/eslint-plugin": "^5.55.0",
8181
"@typescript-eslint/parser": "^5.55.0",
82+
"@vitest/coverage-v8": "^1.6.0",
83+
"7zip-min": "^1.4.4",
8284
"compare-versions": "^6.0.0-rc.1",
8385
"eslint": "^8.36.0",
8486
"got": "^12.5.3",
@@ -89,6 +91,7 @@
8991
"ts-node": "^10.9.1",
9092
"tslib": "^2.5.0",
9193
"typescript": "^4.9.5",
94+
"vitest": "^1.6.0",
9295
"which": "^3.0.0",
9396
"zip-local": "^0.3.5"
9497
}

src/crc-setup.spec.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**********************************************************************
2+
* Copyright (C) 2024 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
19+
import { expect, test, vi } from 'vitest';
20+
import * as crcCli from './crc-cli';
21+
import { needSetup } from './crc-setup';
22+
23+
test('needSetup should return true if setup --check-only command fails', async () => {
24+
vi.spyOn(crcCli, 'execPromise').mockRejectedValue('daemon not running');
25+
const isNeeded = await needSetup();
26+
expect(isNeeded).toBeTruthy();
27+
});
28+
29+
test('needSetup should return false if setup --check-only command succeed', async () => {
30+
vi.spyOn(crcCli, 'execPromise').mockResolvedValue('');
31+
const isNeeded = await needSetup();
32+
expect(isNeeded).toBeFalsy();
33+
});

src/crc-start.spec.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**********************************************************************
2+
* Copyright (C) 2024 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
19+
import * as extensionApi from '@podman-desktop/api';
20+
import { expect, test, vi } from 'vitest';
21+
import * as crcCli from './crc-cli';
22+
import * as crcSetup from './crc-setup';
23+
import { startCrc } from './crc-start';
24+
import * as logProvider from './log-provider';
25+
import * as daemon from './daemon-commander';
26+
import { StartInfo } from './types';
27+
28+
vi.mock('@podman-desktop/api', async () => {
29+
return {
30+
EventEmitter: vi.fn(),
31+
};
32+
});
33+
34+
test('setUpCRC is skipped if already setup, it just perform the daemon start command', async () => {
35+
vi.spyOn(crcCli, 'execPromise').mockResolvedValue('');
36+
vi.spyOn(logProvider.crcLogProvider, 'startSendingLogs').mockImplementation((logger: extensionApi.Logger) => {
37+
return Promise.resolve();
38+
});
39+
const startDaemon = vi.spyOn(daemon.commander, 'start').mockResolvedValue({
40+
Status: 'Running',
41+
} as unknown as StartInfo);
42+
const setUpMock = vi.spyOn(crcSetup, 'setUpCrc');
43+
await startCrc(
44+
{
45+
updateStatus: (status: extensionApi.ProviderStatus) => {},
46+
} as unknown as extensionApi.Provider,
47+
{} as extensionApi.Logger,
48+
{ logUsage: vi.fn() } as unknown as extensionApi.TelemetryLogger,
49+
);
50+
expect(setUpMock).not.toBeCalled();
51+
expect(startDaemon).toBeCalled();
52+
});
53+
54+
test('set up CRC and then start the daemon', async () => {
55+
vi.spyOn(crcCli, 'execPromise').mockRejectedValue('daemon not running');
56+
57+
vi.spyOn(logProvider.crcLogProvider, 'startSendingLogs').mockImplementation((logger: extensionApi.Logger) => {
58+
return Promise.resolve();
59+
});
60+
const startDaemon = vi.spyOn(daemon.commander, 'start').mockResolvedValue({
61+
Status: 'Running',
62+
} as unknown as StartInfo);
63+
const setUpMock = vi
64+
.spyOn(crcSetup, 'setUpCrc')
65+
.mockImplementation((logger: extensionApi.Logger) => Promise.resolve(true));
66+
await startCrc(
67+
{
68+
updateStatus: (status: extensionApi.ProviderStatus) => {},
69+
} as unknown as extensionApi.Provider,
70+
{} as extensionApi.Logger,
71+
{ logUsage: vi.fn() } as unknown as extensionApi.TelemetryLogger,
72+
);
73+
expect(setUpMock).toBeCalled();
74+
expect(startDaemon).toBeCalled();
75+
});

vitest.config.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**********************************************************************
2+
* Copyright (C) 2024 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
19+
import path from 'node:path';
20+
21+
const config = {
22+
test: {
23+
include: ['**/*.{test,spec}.?(c|m)[jt]s?(x)'],
24+
coverage: {
25+
provider: 'v8',
26+
reporter: ['lcov', 'text'],
27+
extension: '.ts',
28+
},
29+
},
30+
resolve: {
31+
alias: {
32+
'@podman-desktop/api': path.resolve(__dirname, '__mocks__/@podman-desktop/api.js'),
33+
},
34+
},
35+
};
36+
37+
export default config;

0 commit comments

Comments
 (0)