Skip to content
This repository was archived by the owner on Oct 30, 2020. It is now read-only.

Commit ec6656f

Browse files
author
Paulo Sérgio Baima
authored
Merge pull request #7 from psbds/dev-padasil
Additional Unit tests for UnifyReleaseService
2 parents 5fd836b + 2e3a0f5 commit ec6656f

File tree

2 files changed

+245
-31
lines changed

2 files changed

+245
-31
lines changed

unify-release-build-task/src/services/unifyReleaseService.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default class UnifyReleaseService implements IUnifyReleaseService {
3838
continue;
3939
}
4040

41-
if (build.result == BuildResult.Canceled && !this.configuration.releaseOnCancel) {
41+
if ((build.result == BuildResult.Canceled || build.status == BuildStatus.Cancelling) && !this.configuration.releaseOnCancel) {
4242
shouldCreateTag = false;
4343
break;
4444
}
@@ -48,6 +48,11 @@ export default class UnifyReleaseService implements IUnifyReleaseService {
4848
shouldCreateTag = false;
4949
break;
5050
}
51+
52+
if (build.status == BuildStatus.Postponed || build.status == BuildStatus.InProgress || build.status == BuildStatus.NotStarted) {
53+
shouldCreateTag = false;
54+
break;
55+
}
5156
}
5257

5358
if (shouldCreateTag) {

unify-release-build-task/tests/services/unifyReleaseServiceTests.ts

Lines changed: 239 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,32 @@ import UnifyReleaseService from "../../src/services/unifyReleaseService";
1111

1212
//# Tests
1313
describe('UnifyReleaseService', () => {
14-
var configStub: StubbedInstance<IAzureDevOpsConfiguration> = null;
14+
var configStub: IAzureDevOpsConfiguration = null;
1515
beforeEach(() => {
16-
configStub = StubInterface<IAzureDevOpsConfiguration>();
17-
configStub.teamFoundationCollectionUri = "organizationUrl";
18-
configStub.teamFoundationProject = "project";
19-
configStub.accessToken = "token";
20-
configStub.waitForAllTriggeredBuilds = true;
21-
configStub.currentBuildId = 1;
22-
configStub.releaseTag = "releaseTag";
23-
configStub.definition1 = "1";
24-
configStub.definition2 = "2";
25-
configStub.definition3 = "3";
26-
configStub.definition4 = "4";
27-
configStub.definition5 = "5";
16+
configStub = {
17+
teamFoundationCollectionUri: "organizationUrl",
18+
teamFoundationProject: "project",
19+
accessToken: "token",
20+
waitForAllTriggeredBuilds: true,
21+
currentBuildId: 1,
22+
releaseTag: "releaseTag",
23+
definition1: "1",
24+
definition2: "2",
25+
definition3: "3",
26+
definition4: "4",
27+
definition5: "5",
28+
releaseOnCancel: false,
29+
releaseOnError: false
30+
}
2831
})
2932

3033
describe('unifyRelease', () => {
31-
it("Should create tag if all last related builds are Completed and Succeeded or Partially Suceeded", async () => {
34+
var buildServiceStub: StubbedInstance<IBuildService> = null
35+
beforeEach(() => {
36+
buildServiceStub = StubInterface<IBuildService>();
37+
})
38+
39+
function setBuildServiceStubs(relatedBuilds: Build[]) {
3240
let triggeredBuild: Build = {
3341
id: 1,
3442
definition: {
@@ -37,6 +45,25 @@ describe('UnifyReleaseService', () => {
3745
status: BuildStatus.InProgress,
3846
sourceVersion: "sourceVersion"
3947
};
48+
var relatedBuildsStub = new Map<string, Build>();
49+
relatedBuildsStub.set(triggeredBuild.definition.id.toString(), triggeredBuild);
50+
relatedBuilds.forEach(build => {
51+
relatedBuildsStub.set(build.definition.id.toString(), build);
52+
});
53+
54+
buildServiceStub.getBuildInfo
55+
.withArgs(
56+
configStub.teamFoundationCollectionUri, configStub.accessToken, configStub.teamFoundationProject, configStub.currentBuildId)
57+
.returns(Promise.resolve(triggeredBuild));
58+
59+
buildServiceStub.listRelatedBuilds
60+
.withArgs(
61+
configStub.teamFoundationCollectionUri, configStub.accessToken, configStub.teamFoundationProject, "sourceVersion", configStub.waitForAllTriggeredBuilds, [configStub.definition1, configStub.definition2, configStub.definition3, configStub.definition4, configStub.definition5])
62+
.returns(Promise.resolve(relatedBuildsStub));
63+
}
64+
65+
it("Should create tag if all last related builds are Completed and Succeeded or Partially Suceeded", async () => {
66+
4067
let build2: Build = {
4168
id: 2,
4269
definition: {
@@ -55,40 +82,222 @@ describe('UnifyReleaseService', () => {
5582
result: BuildResult.PartiallySucceeded,
5683
sourceVersion: "sourceVersion"
5784
};
58-
var relatedBuildsStub = new Map<string, Build>();
59-
relatedBuildsStub.set(triggeredBuild.definition.id.toString(), triggeredBuild);
60-
relatedBuildsStub.set(build2.definition.id.toString(), build2);
61-
relatedBuildsStub.set(build3.definition.id.toString(), build3);
6285

63-
let buildServiceStub: StubbedInstance<IBuildService> = StubInterface<IBuildService>();
86+
setBuildServiceStubs([build2, build3]);
6487

65-
buildServiceStub.getBuildInfo
66-
.withArgs(
67-
configStub.teamFoundationCollectionUri, configStub.accessToken, configStub.teamFoundationProject, configStub.currentBuildId)
68-
.returns(Promise.resolve(triggeredBuild));
88+
let buildServiceCreateTagSpy = buildServiceStub.addBuildTag
89+
.withArgs(configStub.teamFoundationCollectionUri, configStub.teamFoundationProject, configStub.accessToken, configStub.currentBuildId, configStub.releaseTag);
6990

70-
buildServiceStub.listRelatedBuilds
71-
.withArgs(
72-
configStub.teamFoundationCollectionUri, configStub.accessToken, configStub.teamFoundationProject, "sourceVersion", configStub.waitForAllTriggeredBuilds, [configStub.definition1, configStub.definition2, configStub.definition3, configStub.definition4, configStub.definition5])
73-
.returns(Promise.resolve(relatedBuildsStub));
91+
var unifyReleaseService = new UnifyReleaseService(buildServiceStub, configStub);
92+
93+
await unifyReleaseService.unifyRelease();
94+
95+
expect(buildServiceCreateTagSpy.calledOnce).equal(true);
96+
});
97+
98+
it("Should create tag if there's a last Build Cancelling and with flag release on cancel on", async () => {
99+
let build2: Build = {
100+
id: 2,
101+
definition: {
102+
id: 2,
103+
},
104+
status: BuildStatus.Cancelling,
105+
sourceVersion: "sourceVersion"
106+
};
107+
108+
setBuildServiceStubs([build2]);
109+
110+
let buildServiceCreateTagSpy = buildServiceStub.addBuildTag
111+
.withArgs(configStub.teamFoundationCollectionUri, configStub.teamFoundationProject, configStub.accessToken, configStub.currentBuildId, configStub.releaseTag);
112+
113+
configStub.releaseOnCancel = true;
114+
var unifyReleaseService = new UnifyReleaseService(buildServiceStub, configStub);
115+
116+
await unifyReleaseService.unifyRelease();
117+
118+
expect(buildServiceCreateTagSpy.calledOnce).equal(true);
119+
});
120+
121+
it("Should not create tag if there's a last Build Cancelling and with flag release on cancel off", async () => {
122+
let build2: Build = {
123+
id: 2,
124+
definition: {
125+
id: 2,
126+
},
127+
status: BuildStatus.Cancelling,
128+
sourceVersion: "sourceVersion"
129+
};
130+
131+
setBuildServiceStubs([build2]);
132+
133+
let buildServiceCreateTagSpy = buildServiceStub.addBuildTag
134+
.withArgs(configStub.teamFoundationCollectionUri, configStub.teamFoundationProject, configStub.accessToken, configStub.currentBuildId, configStub.releaseTag);
135+
136+
configStub.releaseOnCancel = false;
137+
var unifyReleaseService = new UnifyReleaseService(buildServiceStub, configStub);
138+
139+
await unifyReleaseService.unifyRelease();
140+
141+
expect(buildServiceCreateTagSpy.calledOnce).equal(false);
142+
});
143+
144+
it("Should create tag if there's a last Build Cancelled and with flag release on cancel on", async () => {
145+
let build2: Build = {
146+
id: 2,
147+
definition: {
148+
id: 2,
149+
},
150+
result: BuildResult.Canceled,
151+
sourceVersion: "sourceVersion"
152+
};
153+
154+
setBuildServiceStubs([build2]);
155+
156+
let buildServiceCreateTagSpy = buildServiceStub.addBuildTag
157+
.withArgs(configStub.teamFoundationCollectionUri, configStub.teamFoundationProject, configStub.accessToken, configStub.currentBuildId, configStub.releaseTag);
158+
159+
configStub.releaseOnCancel = true;
160+
var unifyReleaseService = new UnifyReleaseService(buildServiceStub, configStub);
161+
162+
await unifyReleaseService.unifyRelease();
163+
164+
expect(buildServiceCreateTagSpy.calledOnce).equal(true);
165+
});
166+
167+
it("Should not create tag if there's a last Build Cancelled and with flag release on cancel off", async () => {
168+
let build2: Build = {
169+
id: 2,
170+
definition: {
171+
id: 2,
172+
},
173+
result: BuildResult.Canceled,
174+
sourceVersion: "sourceVersion"
175+
};
176+
177+
setBuildServiceStubs([build2]);
74178

75179
let buildServiceCreateTagSpy = buildServiceStub.addBuildTag
76180
.withArgs(configStub.teamFoundationCollectionUri, configStub.teamFoundationProject, configStub.accessToken, configStub.currentBuildId, configStub.releaseTag);
77181

182+
configStub.releaseOnCancel = false;
183+
var unifyReleaseService = new UnifyReleaseService(buildServiceStub, configStub);
184+
185+
await unifyReleaseService.unifyRelease();
186+
187+
expect(buildServiceCreateTagSpy.calledOnce).equal(false);
188+
});
189+
190+
it("Should create tag if there's a last Build failed and with flag release on failure on", async () => {
191+
let build2: Build = {
192+
id: 2,
193+
definition: {
194+
id: 2,
195+
},
196+
result: BuildResult.Failed,
197+
sourceVersion: "sourceVersion"
198+
};
199+
200+
setBuildServiceStubs([build2]);
201+
202+
let buildServiceCreateTagSpy = buildServiceStub.addBuildTag
203+
.withArgs(configStub.teamFoundationCollectionUri, configStub.teamFoundationProject, configStub.accessToken, configStub.currentBuildId, configStub.releaseTag);
204+
205+
configStub.releaseOnError = true;
78206
var unifyReleaseService = new UnifyReleaseService(buildServiceStub, configStub);
79207

80208
await unifyReleaseService.unifyRelease();
81209

82-
debugger;
83210
expect(buildServiceCreateTagSpy.calledOnce).equal(true);
211+
});
212+
213+
it("Should not create tag if there's a last Build failed and with flag release on failure off", async () => {
214+
let build2: Build = {
215+
id: 2,
216+
definition: {
217+
id: 2,
218+
},
219+
result: BuildResult.Failed,
220+
sourceVersion: "sourceVersion"
221+
};
84222

223+
setBuildServiceStubs([build2]);
85224

86-
// throw new Error("Not Implemented");
225+
let buildServiceCreateTagSpy = buildServiceStub.addBuildTag
226+
.withArgs(configStub.teamFoundationCollectionUri, configStub.teamFoundationProject, configStub.accessToken, configStub.currentBuildId, configStub.releaseTag);
87227

228+
configStub.releaseOnError = false;
229+
var unifyReleaseService = new UnifyReleaseService(buildServiceStub, configStub);
230+
231+
await unifyReleaseService.unifyRelease();
232+
233+
expect(buildServiceCreateTagSpy.calledOnce).equal(false);
88234
});
89235

90-
it("Should return last Build from definitions triggered from the same Source Version only for the Build Definitons specified", async () => {
91236

237+
it("Should not create tag if there's a last Build with Status NotStarted", async () => {
238+
let build2: Build = {
239+
id: 2,
240+
definition: {
241+
id: 2,
242+
},
243+
status: BuildStatus.NotStarted,
244+
sourceVersion: "sourceVersion"
245+
};
246+
247+
setBuildServiceStubs([build2]);
248+
249+
let buildServiceCreateTagSpy = buildServiceStub.addBuildTag
250+
.withArgs(configStub.teamFoundationCollectionUri, configStub.teamFoundationProject, configStub.accessToken, configStub.currentBuildId, configStub.releaseTag);
251+
252+
var unifyReleaseService = new UnifyReleaseService(buildServiceStub, configStub);
253+
254+
await unifyReleaseService.unifyRelease();
255+
256+
expect(buildServiceCreateTagSpy.calledOnce).equal(false);
257+
});
258+
259+
it("Should not create tag if there's a last Build with Status InProgress", async () => {
260+
let build2: Build = {
261+
id: 2,
262+
definition: {
263+
id: 2,
264+
},
265+
status: BuildStatus.InProgress,
266+
sourceVersion: "sourceVersion"
267+
};
268+
269+
setBuildServiceStubs([build2]);
270+
271+
let buildServiceCreateTagSpy = buildServiceStub.addBuildTag
272+
.withArgs(configStub.teamFoundationCollectionUri, configStub.teamFoundationProject, configStub.accessToken, configStub.currentBuildId, configStub.releaseTag);
273+
274+
var unifyReleaseService = new UnifyReleaseService(buildServiceStub, configStub);
275+
276+
await unifyReleaseService.unifyRelease();
277+
278+
expect(buildServiceCreateTagSpy.calledOnce).equal(false);
279+
});
280+
281+
it("Should not create tag if there's a last Build with Status PostPoned", async () => {
282+
let build2: Build = {
283+
id: 2,
284+
definition: {
285+
id: 2,
286+
},
287+
status: BuildStatus.Postponed,
288+
sourceVersion: "sourceVersion"
289+
};
290+
291+
setBuildServiceStubs([build2]);
292+
293+
let buildServiceCreateTagSpy = buildServiceStub.addBuildTag
294+
.withArgs(configStub.teamFoundationCollectionUri, configStub.teamFoundationProject, configStub.accessToken, configStub.currentBuildId, configStub.releaseTag);
295+
296+
var unifyReleaseService = new UnifyReleaseService(buildServiceStub, configStub);
297+
298+
await unifyReleaseService.unifyRelease();
299+
300+
expect(buildServiceCreateTagSpy.calledOnce).equal(false);
92301
});
93302
});
94303
});

0 commit comments

Comments
 (0)