Skip to content

Commit d45888e

Browse files
committed
clone test variation to capability branch duting the first test execution
1 parent d74e90e commit d45888e

File tree

2 files changed

+86
-25
lines changed

2 files changed

+86
-25
lines changed

src/test-runs/test-runs.service.ts

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,58 @@ export class TestRunsService {
5555
imageBuffer: Buffer;
5656
}): Promise<TestRunResultDto> {
5757
const project = await this.prismaService.project.findUnique({ where: { id: createTestRequestDto.projectId } });
58+
if (createTestRequestDto.baselineBranchName === project.mainBranchName) {
59+
// Capability branch can not be equal to main branch
60+
createTestRequestDto.baselineBranchName = undefined;
61+
}
5862

59-
//createTestRequestDto.branchName===createTestRequestDto.baselineBranchName
6063
let testVariation = await this.testVariationService.find({
6164
...createTestRequestDto,
6265
sourceBranch: createTestRequestDto.baselineBranchName,
6366
});
64-
// creates variatioin if does not exist
67+
68+
// Creates variation if does not exist
6569
if (!testVariation) {
66-
testVariation = await this.testVariationService.create({
67-
createTestRequestDto,
68-
});
70+
// TODO: Rename baselineBranchName to capabilityBranchName
71+
if (createTestRequestDto.baselineBranchName) {
72+
// This is either capability branch or feature branch based on capability branch.
73+
// If test variation does not exist in these branches,
74+
// then try to find it in the main branch and copy it to capability branch.
75+
// If testVariation still not found, then it is actually a new screenshot.
76+
testVariation = await this.testVariationService.findAndClone(
77+
createTestRequestDto,
78+
project.mainBranchName,
79+
createTestRequestDto.baselineBranchName
80+
);
81+
}
82+
if (!testVariation) {
83+
testVariation = await this.testVariationService.create({
84+
createTestRequestDto,
85+
});
86+
}
6987
}
7088

71-
// delete previous test run if exists
89+
// Delete previous test run in the build if exists
90+
await this.deletePreviousTestRun(createTestRequestDto);
91+
92+
// Create test run result
93+
const testRun = await this.create({ testVariation, createTestRequestDto, imageBuffer });
94+
95+
// Calculate diff
96+
let testRunWithResult = await this.calculateDiff(createTestRequestDto.projectId, testRun);
97+
98+
// Try auto approve
99+
if (project.autoApproveFeature) {
100+
this.tryAutoApprove(testVariation, testRunWithResult);
101+
}
102+
return new TestRunResultDto(testRunWithResult, testVariation);
103+
}
104+
105+
//===============================================================================================
106+
// If the build already contains test run for the screenshot and it is NOT approved/autoapproved,
107+
// then delete it.
108+
//===============================================================================================
109+
private async deletePreviousTestRun(createTestRequestDto: CreateTestRequestDto) {
72110
const [previousTestRun] = await this.prismaService.testRun.findMany({
73111
where: {
74112
buildId: createTestRequestDto.buildId,
@@ -80,19 +118,6 @@ export class TestRunsService {
80118
if (!!previousTestRun) {
81119
await this.delete(previousTestRun.id);
82120
}
83-
84-
// create test run result
85-
const testRun = await this.create({ testVariation, createTestRequestDto, imageBuffer });
86-
87-
// calculate diff
88-
let testRunWithResult = await this.calculateDiff(createTestRequestDto.projectId, testRun);
89-
90-
// try auto approve
91-
if (project.autoApproveFeature) {
92-
testRunWithResult = await this.tryAutoApproveByPastBaselines({ testVariation, testRun: testRunWithResult });
93-
testRunWithResult = await this.tryAutoApproveByNewBaselines({ testVariation, testRun: testRunWithResult });
94-
}
95-
return new TestRunResultDto(testRunWithResult, testVariation);
96121
}
97122

98123
/**
@@ -194,7 +219,7 @@ export class TestRunsService {
194219
data: {
195220
image: testRun.imageName,
196221
baseline: testRun.baselineName,
197-
ignoreAreas: this.getAllIgnoteAreas(testRun),
222+
ignoreAreas: this.getAllIgnoreAreas(testRun),
198223
diffTollerancePercent: testRun.diffTollerancePercent,
199224
saveDiffAsFile: true,
200225
},
@@ -301,14 +326,23 @@ export class TestRunsService {
301326
});
302327
}
303328

304-
private getAllIgnoteAreas(testRun: TestRun): IgnoreAreaDto[] {
329+
private getAllIgnoreAreas(testRun: TestRun): IgnoreAreaDto[] {
305330
const ignoreAreas: IgnoreAreaDto[] = JSON.parse(testRun.ignoreAreas) ?? [];
306331
const tempIgnoreAreas: IgnoreAreaDto[] = JSON.parse(testRun.tempIgnoreAreas) ?? [];
307332
return ignoreAreas.concat(tempIgnoreAreas);
308333
}
309334

335+
//===============================================================================================
336+
// Try to auto approve test run using different auto-approval techniques
337+
//===============================================================================================
338+
private async tryAutoApprove(testVariation: TestVariation, testRunWithResult: TestRun): Promise<TestRun> {
339+
testRunWithResult = await this.tryAutoApproveByPastBaselines({ testVariation, testRun: testRunWithResult });
340+
testRunWithResult = await this.tryAutoApproveByNewBaselines({ testVariation, testRun: testRunWithResult });
341+
return testRunWithResult;
342+
}
343+
310344
/**
311-
* Reason: not rebased code from feature branch is compared agains new main branch baseline thus diff is expected
345+
* Reason: not rebased code from feature branch is compared against new main branch baseline thus diff is expected
312346
* Tries to find past baseline in main branch and autoApprove in case matched
313347
* @param testVariation
314348
* @param testRun
@@ -381,7 +415,7 @@ export class TestRunsService {
381415
data: {
382416
image: testRun.imageName,
383417
baseline: baseline.baselineName,
384-
ignoreAreas: this.getAllIgnoteAreas(testRun),
418+
ignoreAreas: this.getAllIgnoreAreas(testRun),
385419
diffTollerancePercent: testRun.diffTollerancePercent,
386420
saveDiffAsFile: false,
387421
},

src/test-variations/test-variations.service.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable, Inject, forwardRef, Logger } from '@nestjs/common';
22
import { PrismaService } from '../prisma/prisma.service';
3-
import { TestVariation, Baseline, Prisma, Build } from '@prisma/client';
3+
import { TestVariation, Baseline, Prisma, Build, Project } from '@prisma/client';
44
import { StaticService } from '../shared/static/static.service';
55
import { BuildsService } from '../builds/builds.service';
66
import { TestRunsService } from '../test-runs/test-runs.service';
@@ -83,7 +83,9 @@ export class TestVariationsService {
8383
createTestRequestDto: BaselineDataDto & { projectId: string; sourceBranch?: string }
8484
): Promise<TestVariation | null> {
8585
const project = await this.prismaService.project.findUnique({ where: { id: createTestRequestDto.projectId } });
86-
const baselineBranchName = createTestRequestDto.sourceBranch ?? project.mainBranchName;
86+
const baselineBranchName = createTestRequestDto.sourceBranch
87+
? createTestRequestDto.sourceBranch
88+
: project.mainBranchName;
8789

8890
const [baselineBranchTestVariation, currentBranchTestVariation] = await Promise.all([
8991
// search main branch variation
@@ -248,4 +250,29 @@ export class TestVariationsService {
248250
where: { id: baseline.id },
249251
});
250252
}
253+
254+
async findAndClone(
255+
createTestRequestDto: CreateTestRequestDto,
256+
sourceBranch: string,
257+
targetBranch: string
258+
): Promise<TestVariation> {
259+
const testVariation = await this.findUnique({
260+
projectId: createTestRequestDto.projectId,
261+
branchName: sourceBranch,
262+
...getTestVariationUniqueData(createTestRequestDto),
263+
});
264+
if (testVariation) {
265+
return this.cloneToBranch(testVariation, targetBranch);
266+
}
267+
}
268+
269+
async cloneToBranch(testVariation: TestVariation, branchName: string): Promise<TestVariation> {
270+
return this.prismaService.testVariation.create({
271+
data: {
272+
...testVariation,
273+
branchName: branchName,
274+
project: { connect: { id: testVariation.projectId } },
275+
},
276+
});
277+
}
251278
}

0 commit comments

Comments
 (0)