|
| 1 | +/** |
| 2 | + * Copyright 2026 Arm Limited |
| 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 | + * https://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 | + |
| 17 | +import fs from 'node:fs'; |
| 18 | +import os from 'node:os'; |
| 19 | +import path from 'node:path'; |
| 20 | +import * as vscode from 'vscode'; |
| 21 | +import { EditCommand } from './edit-command'; |
| 22 | +import { extensionContextFactory } from '../../../vscode-api/extension-context.factories'; |
| 23 | +import { commandsProviderFactory, MockCommandsProvider } from '../../../vscode-api/commands-provider.factories'; |
| 24 | +import { COutlineItem } from '../tree-structure/solution-outline-item'; |
| 25 | + |
| 26 | +describe('EditCommand', () => { |
| 27 | + let commandsProvider: MockCommandsProvider; |
| 28 | + let tmpDir: string; |
| 29 | + let cprojectPath: string; |
| 30 | + let yamlContent: string; |
| 31 | + let positionAt: jest.Mock; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + commandsProvider = commandsProviderFactory(); |
| 35 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'edit-command-')); |
| 36 | + cprojectPath = path.join(tmpDir, 'test.cproject.yml'); |
| 37 | + |
| 38 | + yamlContent = `project: |
| 39 | + groups: |
| 40 | + - group: App |
| 41 | + files: |
| 42 | + - file: src/main.c |
| 43 | + groups: |
| 44 | + - group: Drivers |
| 45 | + files: |
| 46 | + - file: drv.c |
| 47 | +`; |
| 48 | + |
| 49 | + fs.writeFileSync(cprojectPath, yamlContent, 'utf8'); |
| 50 | + |
| 51 | + positionAt = jest.fn().mockReturnValue(new vscode.Position(0, 0)); |
| 52 | + jest.spyOn(vscode.workspace, 'openTextDocument').mockResolvedValue({ positionAt } as unknown as vscode.TextDocument); |
| 53 | + jest.spyOn(vscode.window, 'showTextDocument').mockResolvedValue({} as vscode.TextEditor); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + jest.restoreAllMocks(); |
| 58 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('registers the edit command on activation', async () => { |
| 62 | + const editCommand = new EditCommand(commandsProvider); |
| 63 | + |
| 64 | + await editCommand.activate(extensionContextFactory()); |
| 65 | + |
| 66 | + expect(commandsProvider.registerCommand).toHaveBeenCalledWith( |
| 67 | + EditCommand.editCommandId, |
| 68 | + expect.any(Function), |
| 69 | + editCommand, |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it('opens cproject.yml at the selected group entry', async () => { |
| 74 | + const editCommand = new EditCommand(commandsProvider); |
| 75 | + await editCommand.activate(extensionContextFactory()); |
| 76 | + |
| 77 | + const groupNode = new COutlineItem('group'); |
| 78 | + groupNode.setAttribute('groupPath', 'App;Drivers'); |
| 79 | + groupNode.setAttribute('projectUri', cprojectPath); |
| 80 | + |
| 81 | + await commandsProvider.mockRunRegistered(EditCommand.editCommandId, groupNode); |
| 82 | + |
| 83 | + expect(vscode.workspace.openTextDocument).toHaveBeenCalledWith(vscode.Uri.file(cprojectPath)); |
| 84 | + expect(positionAt).toHaveBeenCalledTimes(1); |
| 85 | + const actualOffset = positionAt.mock.calls[0][0] as number; |
| 86 | + expect(actualOffset).toBeGreaterThanOrEqual(0); |
| 87 | + expect(yamlContent.slice(actualOffset)).toContain('group: Drivers'); |
| 88 | + expect(vscode.window.showTextDocument).toHaveBeenCalled(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('opens cproject.yml at the selected file entry', async () => { |
| 92 | + const editCommand = new EditCommand(commandsProvider); |
| 93 | + await editCommand.activate(extensionContextFactory()); |
| 94 | + |
| 95 | + const groupNode = new COutlineItem('group'); |
| 96 | + groupNode.setAttribute('groupPath', 'App;Drivers'); |
| 97 | + groupNode.setAttribute('projectUri', cprojectPath); |
| 98 | + |
| 99 | + const fileNode = groupNode.createChild('file'); |
| 100 | + fileNode.setAttribute('projectUri', cprojectPath); |
| 101 | + fileNode.setAttribute('fileUri', 'drv.c'); |
| 102 | + |
| 103 | + await commandsProvider.mockRunRegistered(EditCommand.editCommandId, fileNode); |
| 104 | + |
| 105 | + expect(vscode.workspace.openTextDocument).toHaveBeenCalledWith(vscode.Uri.file(cprojectPath)); |
| 106 | + expect(positionAt).toHaveBeenCalledTimes(1); |
| 107 | + const actualOffset = positionAt.mock.calls[0][0] as number; |
| 108 | + expect(actualOffset).toBeGreaterThanOrEqual(0); |
| 109 | + expect(yamlContent.slice(actualOffset)).toContain('file: drv.c'); |
| 110 | + expect(vscode.window.showTextDocument).toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('ignores file nodes outside editable groups', async () => { |
| 114 | + const editCommand = new EditCommand(commandsProvider); |
| 115 | + await editCommand.activate(extensionContextFactory()); |
| 116 | + |
| 117 | + const nonEditableNode = new COutlineItem('file'); |
| 118 | + nonEditableNode.setAttribute('fileUri', 'out/test.map'); |
| 119 | + |
| 120 | + await commandsProvider.mockRunRegistered(EditCommand.editCommandId, nonEditableNode); |
| 121 | + |
| 122 | + expect(vscode.workspace.openTextDocument).not.toHaveBeenCalled(); |
| 123 | + expect(vscode.window.showTextDocument).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | +}); |
0 commit comments