-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: consolidate container lifecycle actions #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import * as vscode from 'vscode'; | ||
| import { mock } from 'vitest-mock-extended'; | ||
| import type { MockInstance } from 'vitest'; | ||
| import { ProjectController } from '../controllers/projectController'; | ||
| import { WrappedError } from '../errors/wrappedError'; | ||
| import { ContainerCommands } from '../services/containerCommands'; | ||
| import { ContainerItem } from '../util/types'; | ||
| import { ContainerTreeItem } from '../views/treeItems/containerTreeItem'; | ||
| import { ContainerLifecycle } from './containerLifecycle'; | ||
|
|
||
| type LifecycleCase = { | ||
| operation: 'start' | 'stop' | 'delete'; | ||
| command: 'startContainer' | 'stopContainer' | 'deleteContainer'; | ||
| invoke: ( | ||
| lifecycle: ContainerLifecycle, | ||
| treeItem: ContainerTreeItem, | ||
| ) => Promise<void>; | ||
| }; | ||
|
|
||
| const lifecycleCases = [ | ||
| { | ||
| operation: 'start', | ||
| command: 'startContainer', | ||
| invoke: (lifecycle, treeItem) => | ||
| lifecycle.startContainerCommandHandler(treeItem), | ||
| }, | ||
| { | ||
| operation: 'stop', | ||
| command: 'stopContainer', | ||
| invoke: (lifecycle, treeItem) => | ||
| lifecycle.stopContainerCommandHandler(treeItem), | ||
| }, | ||
| { | ||
| operation: 'delete', | ||
| command: 'deleteContainer', | ||
| invoke: (lifecycle, treeItem) => | ||
| lifecycle.deleteContainerCommandHandler(treeItem), | ||
| }, | ||
| ] satisfies LifecycleCase[]; | ||
|
|
||
| describe('ContainerLifecycle', () => { | ||
| let showErrorMessageSpy: MockInstance; | ||
| const target = 'user@topo.local'; | ||
| const container: ContainerItem = { | ||
| id: 'abc123', | ||
| names: 'my-container', | ||
| image: 'nginx', | ||
| state: 'running', | ||
| status: 'Up', | ||
| processingDomain: 'CoolProcessingDomain', | ||
| address: '1.2.3.4:5678', | ||
| target, | ||
| }; | ||
| const treeItem = new ContainerTreeItem(container); | ||
|
|
||
| beforeEach(() => { | ||
| showErrorMessageSpy = vi | ||
| .spyOn(vscode.window, 'showErrorMessage') | ||
| .mockImplementation(vi.fn()); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it.each(lifecycleCases)( | ||
| '$operation invokes the matching command and refreshes containers', | ||
| async ({ command, invoke }) => { | ||
| const containerCommands = mock<ContainerCommands>(); | ||
| const projectController = mock<ProjectController>(); | ||
| const lifecycle = new ContainerLifecycle( | ||
| containerCommands, | ||
| projectController, | ||
| ); | ||
|
|
||
| await invoke(lifecycle, treeItem); | ||
|
|
||
| expect(containerCommands[command]).toHaveBeenCalledWith( | ||
| container.id, | ||
| target, | ||
| ); | ||
| expect( | ||
| projectController.refreshProjectContainersCommandHandler, | ||
| ).toHaveBeenCalledOnce(); | ||
| }, | ||
| ); | ||
|
|
||
| it.each(lifecycleCases)( | ||
| '$operation reports Docker errors without refreshing containers', | ||
| async ({ operation, command, invoke }) => { | ||
| const containerCommands = mock<ContainerCommands>(); | ||
| const projectController = mock<ProjectController>(); | ||
| containerCommands[command].mockRejectedValue( | ||
| new WrappedError('DOCKER', 'fail'), | ||
| ); | ||
| const lifecycle = new ContainerLifecycle( | ||
| containerCommands, | ||
| projectController, | ||
| ); | ||
|
|
||
| await invoke(lifecycle, treeItem); | ||
|
|
||
| expect(showErrorMessageSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining( | ||
| `Failed to ${operation} the container ${container.id}. fail`, | ||
| ), | ||
| ); | ||
| expect( | ||
| projectController.refreshProjectContainersCommandHandler, | ||
| ).not.toHaveBeenCalled(); | ||
| }, | ||
| ); | ||
|
|
||
| it('rethrows unexpected command errors', async () => { | ||
| const containerCommands = mock<ContainerCommands>(); | ||
| containerCommands.startContainer.mockRejectedValue( | ||
| new Error('generic error'), | ||
| ); | ||
| const lifecycle = new ContainerLifecycle( | ||
| containerCommands, | ||
| mock<ProjectController>(), | ||
| ); | ||
|
|
||
| await expect( | ||
| lifecycle.startContainerCommandHandler(treeItem), | ||
| ).rejects.toThrow('generic error'); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { ProjectController } from '../controllers/projectController'; | ||
| import { isWrappedError } from '../errors/wrappedError'; | ||
| import { ContainerCommands } from '../services/containerCommands'; | ||
| import { showAndLogError } from '../util/showAndLog'; | ||
| import { assertContainerTreeItem } from '../views/treeItems/assertContainerTreeItem'; | ||
|
|
||
| const containerOperationMethods = { | ||
| start: 'startContainer', | ||
| stop: 'stopContainer', | ||
| delete: 'deleteContainer', | ||
| } as const; | ||
|
|
||
| type ContainerOperation = keyof typeof containerOperationMethods; | ||
|
|
||
| export class ContainerLifecycle { | ||
| constructor( | ||
| private readonly containerCommands: ContainerCommands, | ||
| private readonly projectController: ProjectController, | ||
| ) {} | ||
|
|
||
| public async startContainerCommandHandler( | ||
| treeNode: unknown, | ||
| ): Promise<void> { | ||
| await this.runContainerCommand('start', treeNode); | ||
| } | ||
|
|
||
| public async stopContainerCommandHandler(treeNode: unknown): Promise<void> { | ||
| await this.runContainerCommand('stop', treeNode); | ||
| } | ||
|
|
||
| public async deleteContainerCommandHandler( | ||
| treeNode: unknown, | ||
| ): Promise<void> { | ||
| await this.runContainerCommand('delete', treeNode); | ||
| } | ||
|
|
||
| private async runContainerCommand( | ||
| operation: ContainerOperation, | ||
| treeNode: unknown, | ||
| ): Promise<void> { | ||
| assertContainerTreeItem(treeNode); | ||
| const containerId = treeNode.containerItem.id; | ||
| const commandMethod = containerOperationMethods[operation]; | ||
|
|
||
| try { | ||
| await this.containerCommands[commandMethod]( | ||
| containerId, | ||
| treeNode.containerItem.target, | ||
| ); | ||
| } catch (error: unknown) { | ||
| if (isWrappedError(error, ['DOCKER'])) { | ||
| showAndLogError( | ||
| `Failed to ${operation} the container ${containerId}`, | ||
| error, | ||
| ); | ||
| return; | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| await this.projectController.refreshProjectContainersCommandHandler(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allows for deletion of the manual spy and
beforeEach/afterEachblocksThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dc0d337