diff --git a/src/tekton/clustertask.ts b/src/tekton/clustertask.ts index fa714c7e..65631df4 100644 --- a/src/tekton/clustertask.ts +++ b/src/tekton/clustertask.ts @@ -5,6 +5,8 @@ import { TektonItem } from './tektonitem'; import { TektonNode, Command } from '../tkn'; +import { Progress } from '../util/progress'; +import { window } from 'vscode'; export class ClusterTask extends TektonItem { @@ -16,8 +18,15 @@ export class ClusterTask extends TektonItem { if (clustertasks) { ClusterTask.tkn.executeInTerminal(Command.listClusterTasksinTerminal()); } } - static async delete(clustertask: TektonNode): Promise { - if (clustertask) { ClusterTask.tkn.executeInTerminal(Command.deleteClusterTask(clustertask.getName())); } + static async delete(clustertask: TektonNode): Promise { + const value = await window.showWarningMessage(`Do you want to delete the ClusterTask '${clustertask.getName()}\'?`, 'Yes', 'Cancel'); + if (value === 'Yes') { + return Progress.execFunctionWithProgress(`Deleting the ClusterTask '${clustertask.getName()}'.`, () => + ClusterTask.tkn.execute(Command.deleteClusterTask(clustertask.getName()))) + .then(() => `The ClusterTask '${clustertask.getName()}' successfully deleted.`) + .catch((err) => Promise.reject(`Failed to delete the ClusterTask '${clustertask.getName()}': '${err}'.`)); + } + return null; } } \ No newline at end of file diff --git a/src/tekton/pipeline.ts b/src/tekton/pipeline.ts index 3407c17d..cabdf21a 100644 --- a/src/tekton/pipeline.ts +++ b/src/tekton/pipeline.ts @@ -12,7 +12,6 @@ import * as cliInstance from '../cli'; import { Cli } from '../cli'; import * as k8s from 'vscode-kubernetes-tools-api'; - export interface NameType { name: string; type: string; @@ -271,8 +270,15 @@ export class Pipeline extends TektonItem { if (pipeline) { Pipeline.tkn.executeInTerminal(Command.listPipelinesInTerminal(pipeline.getName())); } } - static async delete(pipeline: TektonNode): Promise { - if (pipeline) {Pipeline.tkn.executeInTerminal(Command.deletePipeline(pipeline.getName())); } + static async delete(pipeline: TektonNode): Promise { + const value = await window.showWarningMessage(`Do you want to delete the Pipeline '${pipeline.getName()}\'?`, 'Yes', 'Cancel'); + if (value === 'Yes') { + return Progress.execFunctionWithProgress(`Deleting the Pipeline '${pipeline.getName()}'.`, () => + Pipeline.tkn.execute(Command.deletePipeline(pipeline.getName()))) + .then(() => `The Pipeline '${pipeline.getName()}' successfully deleted.`) + .catch((err) => Promise.reject(`Failed to delete the Pipeline '${pipeline.getName()}': '${err}'.`)); + } + return null; } } \ No newline at end of file diff --git a/src/tekton/pipelineresource.ts b/src/tekton/pipelineresource.ts index acbebbc9..9875eecb 100644 --- a/src/tekton/pipelineresource.ts +++ b/src/tekton/pipelineresource.ts @@ -6,6 +6,8 @@ import { TektonItem } from './tektonitem'; import { TektonNode, Command } from '../tkn'; import * as k8s from 'vscode-kubernetes-tools-api'; +import { Progress } from '../util/progress'; +import { window } from 'vscode'; export class PipelineResource extends TektonItem { @@ -18,8 +20,15 @@ export class PipelineResource extends TektonItem { if (pipelineresource) { PipelineResource.tkn.executeInTerminal(Command.listPipelineResourcesInTerminal(pipelineresource.getName())); } } - static async delete(pipelineresource: TektonNode): Promise { - if (pipelineresource) { PipelineResource.tkn.executeInTerminal(Command.deletePipelineResource(pipelineresource.getName())); } + static async delete(pipelineresource: TektonNode): Promise { + const value = await window.showWarningMessage(`Do you want to delete the Resource '${pipelineresource.getName()}\'?`, 'Yes', 'Cancel'); + if (value === 'Yes') { + return Progress.execFunctionWithProgress(`Deleting the Resource '${pipelineresource.getName()}'.`, () => + PipelineResource.tkn.execute(Command.deletePipelineResource(pipelineresource.getName()))) + .then(() => `The Resource '${pipelineresource.getName()}' successfully deleted.`) + .catch((err) => Promise.reject(`Failed to delete the Resource '${pipelineresource.getName()}': '${err}'.`)); + } + return null; } } \ No newline at end of file diff --git a/src/tekton/pipelinerun.ts b/src/tekton/pipelinerun.ts index ff6ebe0a..7a1a166b 100644 --- a/src/tekton/pipelinerun.ts +++ b/src/tekton/pipelinerun.ts @@ -5,7 +5,8 @@ import { TektonItem } from './tektonitem'; import { TektonNode, Command } from '../tkn'; -import * as k8s from 'vscode-kubernetes-tools-api'; +import { window } from 'vscode'; +import { Progress } from '../util/progress'; export class PipelineRun extends TektonItem { @@ -48,8 +49,15 @@ export class PipelineRun extends TektonItem { if (pipelinerun) { PipelineRun.tkn.executeInTerminal(Command.cancelPipelineRun(pipelinerun.getName())); } } - static async delete(pipelinerun: TektonNode): Promise { - if (pipelinerun) { PipelineRun.tkn.executeInTerminal(Command.deletePipelineRun(pipelinerun.getName())); } + static async delete(pipelinerun: TektonNode): Promise { + const value = await window.showWarningMessage(`Do you want to delete the PipelineRun '${pipelinerun.getName()}\'?`, 'Yes', 'Cancel'); + if (value === 'Yes') { + return Progress.execFunctionWithProgress(`Deleting the PipelineRun '${pipelinerun.getName()}'.`, () => + PipelineRun.tkn.execute(Command.deletePipelineRun(pipelinerun.getName()))) + .then(() => `The PipelineRun '${pipelinerun.getName()}' successfully deleted.`) + .catch((err) => Promise.reject(`Failed to delete the PipelineRun '${pipelinerun.getName()}': '${err}'.`)); + } + return null; } } \ No newline at end of file diff --git a/src/tekton/task.ts b/src/tekton/task.ts index 07860574..532cc81d 100644 --- a/src/tekton/task.ts +++ b/src/tekton/task.ts @@ -5,6 +5,8 @@ import { TektonItem } from './tektonitem'; import { TektonNode, Command } from '../tkn'; +import { window } from 'vscode'; +import { Progress } from '../util/progress'; export class Task extends TektonItem { @@ -18,7 +20,14 @@ export class Task extends TektonItem { if (task) { Task.tkn.executeInTerminal(Command.listTasksinTerminal()); } } - static async delete(task: TektonNode): Promise { - if (task) { Task.tkn.executeInTerminal(Command.deleteTask(task.getName())); } + static async delete(task: TektonNode): Promise { + const value = await window.showWarningMessage(`Do you want to delete the Task '${task.getName()}\'?`, 'Yes', 'Cancel'); + if (value === 'Yes') { + return Progress.execFunctionWithProgress(`Deleting the Task '${task.getName()}'.`, () => + Task.tkn.execute(Command.deleteTask(task.getName()))) + .then(() => `The Task '${task.getName()}' successfully deleted.`) + .catch((err) => Promise.reject(`Failed to delete the Task '${task.getName()}': '${err}'.`)); + } + return null; } } \ No newline at end of file diff --git a/src/tekton/taskrun.ts b/src/tekton/taskrun.ts index b5833a19..deb86eb9 100644 --- a/src/tekton/taskrun.ts +++ b/src/tekton/taskrun.ts @@ -5,6 +5,8 @@ import { TektonItem } from './tektonitem'; import { TektonNode, Command } from '../tkn'; +import { window } from 'vscode'; +import { Progress } from '../util/progress'; export class TaskRun extends TektonItem { @@ -24,7 +26,14 @@ export class TaskRun extends TektonItem { TaskRun.tkn.executeInTerminal(Command.showTaskRunFollowLogs(taskrun.getName())); } - static async delete(taskrun: TektonNode): Promise { - if (taskrun) { TaskRun.tkn.executeInTerminal(Command.deleteTaskRun(taskrun.getName())); } + static async delete(taskrun: TektonNode): Promise { + const value = await window.showWarningMessage(`Do you want to delete the TaskRun '${taskrun.getName()}\'?`, 'Yes', 'Cancel'); + if (value === 'Yes') { + return Progress.execFunctionWithProgress(`Deleting the TaskRun '${taskrun.getName()}'.`, () => + TaskRun.tkn.execute(Command.deleteTaskRun(taskrun.getName()))) + .then(() => `The TaskRun '${taskrun.getName()}' successfully deleted.`) + .catch((err) => Promise.reject(`Failed to delete the TaskRun '${taskrun.getName()}': '${err}'.`)); + } + return null; } } \ No newline at end of file diff --git a/src/tkn.ts b/src/tkn.ts index 6f9351b9..2f0466ab 100644 --- a/src/tkn.ts +++ b/src/tkn.ts @@ -110,7 +110,7 @@ export class Command { } @verbose static deletePipeline(name: string) { - return `tkn pipeline delete ${name}`; + return `tkn pipeline delete ${name} -f`; } @verbose static listPipelineResources() { @@ -126,7 +126,7 @@ export class Command { } @verbose static deletePipelineResource(name: string) { - return `tkn resource delete ${name}`; + return `tkn resource delete ${name} -f`; } @verbose static listPipelines() { @@ -158,7 +158,7 @@ export class Command { } @verbose static deletePipelineRun(name: string) { - return `tkn pipelinerun delete ${name}`; + return `tkn pipelinerun delete ${name} -f`; } @verbose static showPipelineRunLogs(name: string) { @@ -182,7 +182,7 @@ export class Command { } @verbose static deleteTask(name: string) { - return `tkn task delete ${name}`; + return `tkn task delete ${name} -f`; } @verbose static listClusterTasks(namespace?: string) { @@ -193,7 +193,7 @@ export class Command { } @verbose static deleteClusterTask(name: string) { - return `tkn task delete ${name}`; + return `tkn clustertask delete ${name} -f`; } @verbose static showTaskRunLogs(name: string) { @@ -201,7 +201,7 @@ export class Command { } @verbose static deleteTaskRun(name: string) { - return `tkn taskrun delete ${name}`; + return `tkn taskrun delete ${name} -f`; } @verbose static printTknVersion() { diff --git a/test/tekton/clustertask.test.ts b/test/tekton/clustertask.test.ts index 3c747db9..80052c26 100644 --- a/test/tekton/clustertask.test.ts +++ b/test/tekton/clustertask.test.ts @@ -18,6 +18,7 @@ const expect = chai.expect; chai.use(sinonChai); suite('Tekton/Clustertask', () => { + let execStub: sinon.SinonStub; let sandbox: sinon.SinonSandbox; let getClusterTaskStub: sinon.SinonStub; const clustertaskNode = new TestItem(TknImpl.ROOT, 'test-clustertask', ContextType.CLUSTERTASK, null); @@ -26,6 +27,7 @@ suite('Tekton/Clustertask', () => { setup(() => { sandbox = sinon.createSandbox(); + execStub = sandbox.stub(TknImpl.prototype, 'execute').resolves({error: null, stdout: '', stderr: ''}); sandbox.stub(TknImpl.prototype, 'getClusterTasks').resolves([clustertaskItem]); getClusterTaskStub = sandbox.stub(TektonItem, 'getClusterTaskNames').resolves([clustertaskItem]); sandbox.stub(vscode.window, 'showInputBox'); @@ -68,16 +70,47 @@ suite('Tekton/Clustertask', () => { }); - suite('delete', () => { - let termStub: sinon.SinonStub; + suite('delete command', () => { + let warnStub: sinon.SinonStub; - setup(() => { - termStub = sandbox.stub(TknImpl.prototype, 'executeInTerminal').resolves(); + setup(() => { + warnStub = sandbox.stub(vscode.window, 'showWarningMessage'); }); - test('delete calls the correct tkn command in terminal', async () => { + test('calls the appropriate tkn command if confirmed', async () => { + warnStub.resolves('Yes'); + await ClusterTask.delete(clustertaskItem); - expect(termStub).calledOnceWith(Command.deleteTask(clustertaskItem.getName())); + + expect(execStub).calledOnceWith(Command.deleteClusterTask(clustertaskItem.getName())); + }); + + test('returns a confirmation message text when successful', async () => { + warnStub.resolves('Yes'); + + const result = await ClusterTask.delete(clustertaskItem); + + expect(result).equals(`The ClusterTask '${clustertaskItem.getName()}' successfully deleted.`); + }); + + test('returns null when cancelled', async() => { + warnStub.resolves('Cancel'); + + const result = await ClusterTask.delete(clustertaskItem); + + expect(result).null; + }); + + test('throws an error message when command failed', async () => { + warnStub.resolves('Yes'); + execStub.rejects('ERROR'); + let expectedError; + try { + await ClusterTask.delete(clustertaskItem); + } catch (err) { + expectedError = err; + } + expect(expectedError).equals(`Failed to delete the ClusterTask '${clustertaskItem.getName()}': 'ERROR'.`); }); }); diff --git a/test/tekton/pipeline.test.ts b/test/tekton/pipeline.test.ts index 7f71fdb7..d26419eb 100644 --- a/test/tekton/pipeline.test.ts +++ b/test/tekton/pipeline.test.ts @@ -107,13 +107,49 @@ suite('Tekton/Pipeline', () => { }); }); - suite('delete', () => { - test('describe calls the correct tkn command in terminal', async () => { + suite('delete command', () => { + let warnStub: sinon.SinonStub; + + setup(() => { + warnStub = sandbox.stub(vscode.window, 'showWarningMessage'); + }); + + test('calls the appropriate tkn command if confirmed', async () => { + warnStub.resolves('Yes'); + await Pipeline.delete(pipelineItem); - expect(termStub).calledOnceWith(Command.deletePipeline(pipelineItem.getName())); + + expect(execStub).calledOnceWith(Command.deletePipeline(pipelineItem.getName())); + }); + + test('returns a confirmation message text when successful', async () => { + warnStub.resolves('Yes'); + + const result = await Pipeline.delete(pipelineItem); + + expect(result).equals(`The Pipeline '${pipelineItem.getName()}' successfully deleted.`); + }); + + test('returns null when cancelled', async() => { + warnStub.resolves('Cancel'); + + const result = await Pipeline.delete(pipelineItem); + + expect(result).null; }); + test('throws an error message when command failed', async () => { + warnStub.resolves('Yes'); + execStub.rejects('ERROR'); + let expectedError; + try { + await Pipeline.delete(pipelineItem); + } catch (err) { + expectedError = err; + } + expect(expectedError).equals(`Failed to delete the Pipeline '${pipelineItem.getName()}': 'ERROR'.`); + }); }); suite('restart', () => { diff --git a/test/tekton/pipelineresource.test.ts b/test/tekton/pipelineresource.test.ts index be6dc6fa..4f8761fa 100644 --- a/test/tekton/pipelineresource.test.ts +++ b/test/tekton/pipelineresource.test.ts @@ -11,7 +11,6 @@ import * as sinonChai from 'sinon-chai'; import * as sinon from 'sinon'; import { TknImpl, Command, ContextType } from '../../src/tkn'; import { PipelineResource } from '../../src/tekton/pipelineresource'; -import { Pipeline } from '../../src/tekton/pipeline'; import { TestItem } from './testTektonitem'; import { TektonItem } from '../../src/tekton/tektonitem'; @@ -98,20 +97,48 @@ suite('Tekton/PipelineResource', () => { }); - suite('delete', () => { - - - test('returns null when cancelled', async () => { - const result = await PipelineResource.delete(null); + suite('delete command', () => { + let warnStub: sinon.SinonStub; - expect(result).undefined; + setup(() => { + warnStub = sandbox.stub(vscode.window, 'showWarningMessage'); }); - test('describe calls the correct tkn command in terminal', async () => { + test('calls the appropriate tkn command if confirmed', async () => { + warnStub.resolves('Yes'); + await PipelineResource.delete(pipelineresourceItem); - expect(termStub).calledOnceWith(Command.deletePipelineResource(pipelineresourceItem.getName())); + + expect(execStub).calledOnceWith(Command.deletePipelineResource(pipelineresourceItem.getName())); }); + test('returns a confirmation message text when successful', async () => { + warnStub.resolves('Yes'); + + const result = await PipelineResource.delete(pipelineresourceItem); + + expect(result).equals(`The Resource '${pipelineresourceItem.getName()}' successfully deleted.`); + }); + + test('returns null when cancelled', async() => { + warnStub.resolves('Cancel'); + + const result = await PipelineResource.delete(pipelineresourceItem); + + expect(result).null; + }); + + test('throws an error message when command failed', async () => { + warnStub.resolves('Yes'); + execStub.rejects('ERROR'); + let expectedError; + try { + await PipelineResource.delete(pipelineresourceItem); + } catch (err) { + expectedError = err; + } + expect(expectedError).equals(`Failed to delete the Resource '${pipelineresourceItem.getName()}': 'ERROR'.`); + }); }); }); diff --git a/test/tekton/pipelinerun.test.ts b/test/tekton/pipelinerun.test.ts index cc27fc35..43fe4591 100644 --- a/test/tekton/pipelinerun.test.ts +++ b/test/tekton/pipelinerun.test.ts @@ -11,7 +11,6 @@ import * as sinonChai from 'sinon-chai'; import * as sinon from 'sinon'; import { TknImpl, Command, ContextType } from '../../src/tkn'; import { PipelineRun } from '../../src/tekton/pipelinerun'; -import { Pipeline } from '../../src/tekton/pipeline'; import { TestItem } from './testTektonitem'; import { TektonItem } from '../../src/tekton/tektonitem'; @@ -144,13 +143,48 @@ suite('Tekton/PipelineRun', () => { }); - suite('delete', () => { - - test('describe calls the correct tkn command in terminal', async () => { - await PipelineRun.delete(pipelineItem); - expect(termStub).calledOnceWith(Command.deletePipelineRun(pipelineItem.getName())); + suite('delete command', () => { + let warnStub: sinon.SinonStub; + + setup(() => { + warnStub = sandbox.stub(vscode.window, 'showWarningMessage'); + }); + + test('calls the appropriate tkn command if confirmed', async () => { + warnStub.resolves('Yes'); + + await PipelineRun.delete(pipelinerunItem); + + expect(execStub).calledOnceWith(Command.deletePipelineRun(pipelinerunItem.getName())); + }); + + test('returns a confirmation message text when successful', async () => { + warnStub.resolves('Yes'); + + const result = await PipelineRun.delete(pipelinerunItem); + + expect(result).equals(`The PipelineRun '${pipelinerunItem.getName()}' successfully deleted.`); + }); + + test('returns null when cancelled', async() => { + warnStub.resolves('Cancel'); + + const result = await PipelineRun.delete(pipelinerunItem); + + expect(result).null; + }); + + test('throws an error message when command failed', async () => { + warnStub.resolves('Yes'); + execStub.rejects('ERROR'); + let expectedError; + try { + await PipelineRun.delete(pipelinerunItem); + } catch (err) { + expectedError = err; + } + expect(expectedError).equals(`Failed to delete the PipelineRun '${pipelinerunItem.getName()}': 'ERROR'.`); + }); }); - - }); }); }); diff --git a/test/tekton/task.test.ts b/test/tekton/task.test.ts index 85f73bea..466af0e5 100644 --- a/test/tekton/task.test.ts +++ b/test/tekton/task.test.ts @@ -66,11 +66,47 @@ suite('Tekton/Task', () => { }); }); - suite('delete', () => { + suite('delete command', () => { + let warnStub: sinon.SinonStub; + + setup(() => { + warnStub = sandbox.stub(vscode.window, 'showWarningMessage'); + }); + + test('calls the appropriate tkn command if confirmed', async () => { + warnStub.resolves('Yes'); - test('delete calls the correct tkn command in terminal', async () => { await Task.delete(taskItem); - expect(termStub).calledOnceWith(Command.deleteTask(taskItem.getName())); + + expect(execStub).calledOnceWith(Command.deleteTask(taskItem.getName())); + }); + + test('returns a confirmation message text when successful', async () => { + warnStub.resolves('Yes'); + + const result = await Task.delete(taskItem); + + expect(result).equals(`The Task '${taskItem.getName()}' successfully deleted.`); + }); + + test('returns null when cancelled', async() => { + warnStub.resolves('Cancel'); + + const result = await Task.delete(taskItem); + + expect(result).null; + }); + + test('throws an error message when command failed', async () => { + warnStub.resolves('Yes'); + execStub.rejects('ERROR'); + let expectedError; + try { + await Task.delete(taskItem); + } catch (err) { + expectedError = err; + } + expect(expectedError).equals(`Failed to delete the Task '${taskItem.getName()}': 'ERROR'.`); }); }); }); diff --git a/test/tekton/taskrun.test.ts b/test/tekton/taskrun.test.ts index 7d2b4b79..97066042 100644 --- a/test/tekton/taskrun.test.ts +++ b/test/tekton/taskrun.test.ts @@ -19,6 +19,7 @@ chai.use(sinonChai); suite('Tekton/TaskRun', () => { let sandbox: sinon.SinonSandbox; + let execStub: sinon.SinonStub; let getTaskRunsStub: sinon.SinonStub; let getPipelineRunNamesStub: sinon.SinonStub; const pipelineItem = new TestItem(null, 'pipeline', ContextType.PIPELINE); @@ -27,6 +28,7 @@ suite('Tekton/TaskRun', () => { setup(() => { sandbox = sinon.createSandbox(); + execStub = sandbox.stub(TknImpl.prototype, 'execute').resolves({error: null, stdout: '', stderr: ''}); sandbox.stub(TknImpl.prototype, 'getTaskRuns').resolves([taskrunItem]); getPipelineRunNamesStub = sandbox.stub(TektonItem, 'getPipelinerunNames').resolves([pipelinerunItem]); sandbox.stub(vscode.window, 'showInputBox'); @@ -139,11 +141,47 @@ suite('Tekton/TaskRun', () => { }); - suite('delete', () => { - - test('delete calls the correct tkn command in terminal', async () => { + suite('delete command', () => { + let warnStub: sinon.SinonStub; + + setup(() => { + warnStub = sandbox.stub(vscode.window, 'showWarningMessage'); + }); + + test('calls the appropriate tkn command if confirmed', async () => { + warnStub.resolves('Yes'); + await TaskRun.delete(taskrunItem); - expect(termStub).calledOnceWith(Command.deleteTaskRun(taskrunItem.getName())); + + expect(execStub).calledOnceWith(Command.deleteTaskRun(taskrunItem.getName())); + }); + + test('returns a confirmation message text when successful', async () => { + warnStub.resolves('Yes'); + + const result = await TaskRun.delete(taskrunItem); + + expect(result).equals(`The TaskRun '${taskrunItem.getName()}' successfully deleted.`); + }); + + test('returns null when cancelled', async() => { + warnStub.resolves('Cancel'); + + const result = await TaskRun.delete(taskrunItem); + + expect(result).null; + }); + + test('throws an error message when command failed', async () => { + warnStub.resolves('Yes'); + execStub.rejects('ERROR'); + let expectedError; + try { + await TaskRun.delete(taskrunItem); + } catch (err) { + expectedError = err; + } + expect(expectedError).equals(`Failed to delete the TaskRun '${taskrunItem.getName()}': 'ERROR'.`); }); }); });