Skip to content

Commit

Permalink
Test: Add unit tests for devtool ide-sdk command
Browse files Browse the repository at this point in the history
  • Loading branch information
deribaucourt committed Dec 29, 2023
1 parent 5145443 commit c23aac7
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
84 changes: 84 additions & 0 deletions client/src/__tests__/unit-tests/ui/bitbake-commands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2023 Savoir-faire Linux. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as vscode from 'vscode'
import { BitbakeWorkspace } from '../../../ui/BitbakeWorkspace'
import { BitBakeProjectScanner } from '../../../driver/BitBakeProjectScanner'
import { BitbakeDriver } from '../../../driver/BitbakeDriver'
import { registerDevtoolCommands } from '../../../ui/BitbakeCommands'
import { clientNotificationManager } from '../../../ui/ClientNotificationManager'
import * as BitbakeTerminal from '../../../ui/BitbakeTerminal'
import * as ProcessUtils from '../../../lib/src/utils/ProcessUtils'

jest.mock('vscode')

function mockExtensionContext (bitbakeWorkspace: BitbakeWorkspace, bitBakeProjectScanner: BitBakeProjectScanner): any {
const contextMock: vscode.ExtensionContext = {
subscriptions: {
push: jest.fn()
}
} as any

let ideSDKCommand: any
vscode.commands.registerCommand = jest.fn().mockImplementation(
(command: string, callback: (...args: any[]) => any, thisArg?: any): void => {
if (command === 'bitbake.devtool-ide-sdk') {
ideSDKCommand = callback
}
})
registerDevtoolCommands(contextMock, bitbakeWorkspace, bitBakeProjectScanner)
expect(ideSDKCommand).toBeDefined()
return ideSDKCommand
}

describe('Devtool ide-sdk command', () => {
afterEach(() => {
jest.clearAllMocks()
})

it('should detect missing settings', async () => {
const bitbakeWorkspace = new BitbakeWorkspace()
const bitBakeProjectScanner = new BitBakeProjectScanner(new BitbakeDriver())
const ideSDKCommand = mockExtensionContext(bitbakeWorkspace, bitBakeProjectScanner)

clientNotificationManager.showSDKConfigurationError = jest.fn()
await ideSDKCommand('busybox')
expect(clientNotificationManager.showSDKConfigurationError).toHaveBeenCalled()
})

it('should detect ide-sdk missing', async () => {
const bitbakeWorkspace = new BitbakeWorkspace()
const bitBakeProjectScanner = new BitBakeProjectScanner(new BitbakeDriver())
bitBakeProjectScanner.bitbakeDriver.bitbakeSettings = {
pathToBitbakeFolder: '',
sdkImage: 'core-image-minimal'
}
const ideSDKCommand = mockExtensionContext(bitbakeWorkspace, bitBakeProjectScanner)

jest.spyOn(BitbakeTerminal, 'runBitbakeTerminalCustomCommand').mockReturnValue(undefined as any)
jest.spyOn(ProcessUtils, 'finishProcessExecution').mockReturnValue({ status: 1 } as any)

clientNotificationManager.showSDKUnavailableError = jest.fn()
await ideSDKCommand('busybox')
expect(clientNotificationManager.showSDKUnavailableError).toHaveBeenCalled()
})

it('should properly format ide-sdk command', async () => {
const bitbakeWorkspace = new BitbakeWorkspace()
const bitBakeProjectScanner = new BitBakeProjectScanner(new BitbakeDriver())
bitBakeProjectScanner.bitbakeDriver.bitbakeSettings = {
pathToBitbakeFolder: '',
sdkImage: 'core-image-minimal',
sshTarget: '[email protected]'
}
const ideSDKCommand = mockExtensionContext(bitbakeWorkspace, bitBakeProjectScanner)

const commandSpy = jest.spyOn(BitbakeTerminal, 'runBitbakeTerminalCustomCommand').mockReturnValue(undefined as any)
jest.spyOn(ProcessUtils, 'finishProcessExecution').mockReturnValue({ status: 0 } as any)

await ideSDKCommand('busybox')
expect(commandSpy).toHaveBeenCalledWith(expect.anything(), 'devtool ide-sdk -i code busybox core-image-minimal -t [email protected]', expect.anything())
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { BitbakeDriver } from '../../../driver/BitbakeDriver'
jest.mock('vscode')

describe('BitbakeDriver Recipes View', () => {
afterEach(() => {
jest.clearAllMocks()
})

it('should list recipes', (done) => {
const bitbakeWorkspace = new BitbakeWorkspace()
const bitBakeProjectScanner = new BitBakeProjectScanner(new BitbakeDriver())
Expand Down

0 comments on commit c23aac7

Please sign in to comment.