Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
Signed-off-by: lstocchi <[email protected]>
  • Loading branch information
lstocchi committed May 20, 2024
1 parent 58f77f8 commit 0789ab5
Show file tree
Hide file tree
Showing 6 changed files with 1,208 additions and 230 deletions.
24 changes: 24 additions & 0 deletions __mocks__/@podman-desktop/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**********************************************************************
* Copyright (C) 2022 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

/**
* Mock the extension API for vitest.
* This file is referenced from vitest.config.js file.
*/
const plugin = {};
module.exports = plugin;
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@
"format:fix": "prettier --write src/**",
"desk:build": "ts-node-esm ./scripts/run.mts build",
"desk:prepare": "ts-node-esm ./scripts/run.mts prepare",
"desk:run": "ts-node-esm ./scripts/run.mts run"
"desk:run": "ts-node-esm ./scripts/run.mts run",
"test": "vitest run --coverage --passWithNoTests"
},
"dependencies": {},
"devDependencies": {
"7zip-min": "^1.4.4",
"@podman-desktop/api": "next",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
Expand All @@ -79,6 +79,8 @@
"@types/node": "^18.14.6",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"@vitest/coverage-v8": "^1.6.0",
"7zip-min": "^1.4.4",
"compare-versions": "^6.0.0-rc.1",
"eslint": "^8.36.0",
"got": "^12.5.3",
Expand All @@ -89,6 +91,7 @@
"ts-node": "^10.9.1",
"tslib": "^2.5.0",
"typescript": "^4.9.5",
"vitest": "^1.6.0",
"which": "^3.0.0",
"zip-local": "^0.3.5"
}
Expand Down
33 changes: 33 additions & 0 deletions src/crc-setup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import { expect, test, vi } from 'vitest';
import * as crcCli from './crc-cli';
import { needSetup } from './crc-setup';

test('needSetup should return true if setup --check-only command fails', async () => {
vi.spyOn(crcCli, 'execPromise').mockRejectedValue('daemon not running');
const isNeeded = await needSetup();
expect(isNeeded).toBeTruthy();
});

test('needSetup should return false if setup --check-only command succeed', async () => {
vi.spyOn(crcCli, 'execPromise').mockResolvedValue('');
const isNeeded = await needSetup();
expect(isNeeded).toBeFalsy();
});
75 changes: 75 additions & 0 deletions src/crc-start.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import * as extensionApi from '@podman-desktop/api';

Check failure on line 19 in src/crc-start.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests

All imports in the declaration are only used as types. Use `import type`
import { expect, test, vi } from 'vitest';
import * as crcCli from './crc-cli';
import * as crcSetup from './crc-setup';
import { startCrc } from './crc-start';
import * as logProvider from './log-provider';
import * as daemon from './daemon-commander';
import { StartInfo } from './types';

Check failure on line 26 in src/crc-start.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests

All imports in the declaration are only used as types. Use `import type`

vi.mock('@podman-desktop/api', async () => {
return {
EventEmitter: vi.fn(),
};
});

test('setUpCRC is skipped if already setup, it just perform the daemon start command', async () => {
vi.spyOn(crcCli, 'execPromise').mockResolvedValue('');
vi.spyOn(logProvider.crcLogProvider, 'startSendingLogs').mockImplementation((logger: extensionApi.Logger) => {

Check failure on line 36 in src/crc-start.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests

'logger' is defined but never used
return Promise.resolve();
});
const startDaemon = vi.spyOn(daemon.commander, 'start').mockResolvedValue({
Status: 'Running',
} as unknown as StartInfo);
const setUpMock = vi.spyOn(crcSetup, 'setUpCrc');
await startCrc(
{
updateStatus: (status: extensionApi.ProviderStatus) => {},

Check failure on line 45 in src/crc-start.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests

'status' is defined but never used

Check failure on line 45 in src/crc-start.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests

Unexpected empty method 'updateStatus'
} as unknown as extensionApi.Provider,
{} as extensionApi.Logger,
{ logUsage: vi.fn() } as unknown as extensionApi.TelemetryLogger,
);
expect(setUpMock).not.toBeCalled();
expect(startDaemon).toBeCalled();
});

test('set up CRC and then start the daemon', async () => {
vi.spyOn(crcCli, 'execPromise').mockRejectedValue('daemon not running');

vi.spyOn(logProvider.crcLogProvider, 'startSendingLogs').mockImplementation((logger: extensionApi.Logger) => {

Check failure on line 57 in src/crc-start.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests

'logger' is defined but never used
return Promise.resolve();
});
const startDaemon = vi.spyOn(daemon.commander, 'start').mockResolvedValue({
Status: 'Running',
} as unknown as StartInfo);
const setUpMock = vi
.spyOn(crcSetup, 'setUpCrc')
.mockImplementation((logger: extensionApi.Logger) => Promise.resolve(true));

Check failure on line 65 in src/crc-start.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests

'logger' is defined but never used
await startCrc(
{
updateStatus: (status: extensionApi.ProviderStatus) => {},

Check failure on line 68 in src/crc-start.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests

'status' is defined but never used

Check failure on line 68 in src/crc-start.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests

Unexpected empty method 'updateStatus'
} as unknown as extensionApi.Provider,
{} as extensionApi.Logger,
{ logUsage: vi.fn() } as unknown as extensionApi.TelemetryLogger,
);
expect(setUpMock).toBeCalled();
expect(startDaemon).toBeCalled();
});
37 changes: 37 additions & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import path from 'node:path';

const config = {
test: {
include: ['**/*.{test,spec}.?(c|m)[jt]s?(x)'],
coverage: {
provider: 'v8',
reporter: ['lcov', 'text'],
extension: '.ts',
},
},
resolve: {
alias: {
'@podman-desktop/api': path.resolve(__dirname, '__mocks__/@podman-desktop/api.js'),
},
},
};

export default config;
Loading

0 comments on commit 0789ab5

Please sign in to comment.