From 96ba630a8d577a7952decfd3a8374803020eb8e0 Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Tue, 24 Sep 2019 22:26:26 +1000 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20allow=20passing=20multip?= =?UTF-8?q?le=20dependencies=20to=20.dependsOn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes: #7 --- .../__snapshots__/command.spec.ts.snap | 58 +++++++++++++++++++ src/__tests__/command.spec.ts | 11 +++- src/base.ts | 10 +++- 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/src/__tests__/__snapshots__/command.spec.ts.snap b/src/__tests__/__snapshots__/command.spec.ts.snap index ea97b2a..e93c38d 100644 --- a/src/__tests__/__snapshots__/command.spec.ts.snap +++ b/src/__tests__/__snapshots__/command.spec.ts.snap @@ -1243,6 +1243,32 @@ subgraph cluster_1 { " `; +exports[`buildkite-graph Steps dependencies step dependency dot 2`] = ` +"digraph \\"whatever\\" { + graph [ compound =true ]; +subgraph cluster_0 { + graph [ color = \\"black\\" ]; + \\"\\" [ color = \\"grey\\" ]; + \\"\\" [ color = \\"grey\\" ]; + \\"\\" [ color = \\"grey\\" ]; +} + +subgraph cluster_1 { + graph [ color = \\"black\\" ]; + \\"\\" [ color = \\"grey\\" ]; +} + + \\"\\"; + \\"\\"; + \\"\\"; + \\"\\"; + \\"\\" -> \\"\\" [ ltail = \\"cluster_0\\", lhead = \\"cluster_1\\" ]; + \\"\\" -> \\"\\" [ ltail = \\"cluster_0\\", lhead = \\"cluster_1\\" ]; + \\"\\" -> \\"\\" [ ltail = \\"cluster_0\\", lhead = \\"cluster_1\\" ]; +} +" +`; + exports[`buildkite-graph Steps dependencies step dependency json 1`] = ` Object { "steps": Array [ @@ -1259,6 +1285,28 @@ Object { } `; +exports[`buildkite-graph Steps dependencies step dependency json 2`] = ` +Object { + "steps": Array [ + Object { + "command": "a", + }, + Object { + "command": "b", + }, + Object { + "command": "c", + }, + Object { + "wait": null, + }, + Object { + "command": "d", + }, + ], +} +`; + exports[`buildkite-graph Steps dependencies step dependency yaml 1`] = ` "steps: - command: a @@ -1266,3 +1314,13 @@ exports[`buildkite-graph Steps dependencies step dependency yaml 1`] = ` - command: b " `; + +exports[`buildkite-graph Steps dependencies step dependency yaml 2`] = ` +"steps: + - command: a + - command: b + - command: c + - wait: ~ + - command: d +" +`; diff --git a/src/__tests__/command.spec.ts b/src/__tests__/command.spec.ts index d7407d4..32402bc 100644 --- a/src/__tests__/command.spec.ts +++ b/src/__tests__/command.spec.ts @@ -4,11 +4,18 @@ import { createTest } from './helpers'; describe('buildkite-graph', () => { describe('Steps', () => { describe('dependencies', () => { - createTest('step dependency', () => + createTest('step dependency', () => [ new Pipeline('whatever').add( new CommandStep('b').dependsOn(new CommandStep('a')), ), - ); + new Pipeline('whatever').add( + new CommandStep('d').dependsOn( + new CommandStep('a'), + new CommandStep('b'), + new CommandStep('c'), + ), + ), + ]); createTest('can depend on itself to produce wait', () => { const c = new CommandStep('c'); return new Pipeline('whatever').add( diff --git a/src/base.ts b/src/base.ts index 4c4d3f7..cc59e4f 100644 --- a/src/base.ts +++ b/src/base.ts @@ -12,8 +12,14 @@ export abstract class Step implements BaseStep { @Exclude() public readonly dependencies: Set = new Set(); - dependsOn(step: Step): this { - this.dependencies.add(step); + dependsOn(...steps: Step[]): this { + ow(steps, ow.array.ofType(ow.object.nonEmpty)); + // iterate in reverse so if dependencies are not added to the graph, yet + // they will be added in the order they are given as dependencies + for (let i = steps.length; i > 0; i--) { + const step = steps[i - 1]; + this.dependencies.add(step); + } return this; }