Skip to content

Commit

Permalink
fix(tests): Cleaning up gocdHelpers and tests (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanWoodard authored Sep 17, 2024
1 parent 01535e8 commit efacead
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 37 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
"typescript.enablePromptUseWorkspaceTsdk": true,
"terminal.integrated.shellArgs.linux": ["-l"],
"terminal.integrated.shellArgs.osx": ["-l"],
"jest.runMode": "on-demand",
}
101 changes: 70 additions & 31 deletions src/utils/gocdHelpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GoCDPipeline } from '@/types';
import {
filterBuildCauses,
firstGitMaterialSHA,
Expand All @@ -13,7 +14,7 @@ describe('firstGitMaterialSHA', () => {
});

it('return nothing for no deploy', async function () {
const got = firstGitMaterialSHA(null);
const got = firstGitMaterialSHA(undefined);
expect(got).toEqual(null);
});

Expand All @@ -28,8 +29,14 @@ describe('firstGitMaterialSHA', () => {
const got = firstGitMaterialSHA({
pipeline_build_cause: [
{
changed: true,
material: {
type: 'git',
'git-configuration': {
'shallow-clone': false,
branch: 'master',
url: 'https://example.com/repo.git',
},
},
modifications: [],
},
Expand All @@ -42,12 +49,14 @@ describe('firstGitMaterialSHA', () => {
const got = firstGitMaterialSHA({
pipeline_build_cause: [
{
changed: true,
material: {
type: 'other',
type: 'pipeline',
},
modifications: [
{
revision: 'example-pipeline/1/example-stage',
'modified-time': '2021-01-01T00:00:00Z',
},
],
},
Expand All @@ -60,12 +69,19 @@ describe('firstGitMaterialSHA', () => {
const got = firstGitMaterialSHA({
pipeline_build_cause: [
{
changed: true,
material: {
type: 'git',
'git-configuration': {
'shallow-clone': false,
branch: 'master',
url: 'https://example.com/repo.git',
},
},
modifications: [
{
revision: 'abc123',
'modified-time': '2021-01-01T00:00:00Z',
},
],
},
Expand All @@ -77,31 +93,41 @@ describe('firstGitMaterialSHA', () => {

describe('filterBuildCauses', () => {
it('filter build causes', function () {
const pipeline = {
const pipeline: Pick<GoCDPipeline, 'build-cause'> = {
'build-cause': [
{
id: 1,
changed: true,
material: {
type: 'git',
},
modifications: [{}],
modifications: [
{
revision: 'abc123',
'modified-time': '2021-01-01T00:00:00Z',
},
],
},
{
id: 2,
changed: true,
material: {
type: 'git',
},
modifications: [],
},
{
id: 3,
changed: true,
material: {
type: 'pipeline',
},
modifications: [{}],
modifications: [
{
revision: 'example-pipeline-name/123/pipeline-complete/1',
'modified-time': '2021-01-01T00:00:00Z',
},
],
},
{
id: 4,
changed: true,
material: {
type: 'pipeline',
},
Expand All @@ -110,47 +136,38 @@ describe('filterBuildCauses', () => {
],
};

const expectedGit = [pipeline['build-cause'][0]];

const gotGit = filterBuildCauses(pipeline, 'git');
expect(gotGit.length).toEqual(1);
expect(gotGit).toEqual([
{
id: 1,
material: {
type: 'git',
},
modifications: [{}],
},
]);
expect(gotGit).toEqual(expectedGit);

const expectedPipeline = [pipeline['build-cause'][2]];

const gotPipeline = filterBuildCauses(pipeline, 'pipeline');
expect(gotPipeline.length).toEqual(1);
expect(gotPipeline).toEqual([
{
id: 3,
material: {
type: 'pipeline',
},
modifications: [{}],
},
]);
expect(gotPipeline).toEqual(expectedPipeline);
});

describe('getBaseAndHeadCommit', () => {
it('return nothing when there is no build cause', async function () {
const got = await getBaseAndHeadCommit({
'build-cause': [],
group: 'example-pipeline-group',
name: 'example-pipeline-name',
});
expect(got).toEqual([null, null]);
});

it('return nothing when there is no git build cause', async function () {
const got = await getBaseAndHeadCommit({
group: 'example-pipeline-group',
name: 'example-pipeline-name',
'build-cause': [
{
changed: true,
material: {
type: 'other',
type: 'pipeline',
},
modifications: [{}],
modifications: [],
},
],
});
Expand All @@ -159,10 +176,18 @@ describe('filterBuildCauses', () => {

it('return nothing when there is no modifications', async function () {
const got = await getBaseAndHeadCommit({
group: 'example-pipeline-group',
name: 'example-pipeline-name',
'build-cause': [
{
changed: true,
material: {
type: 'git',
'git-configuration': {
'shallow-clone': false,
branch: 'master',
url: 'https://example.com/repo.git',
},
},
modifications: [],
},
Expand All @@ -180,12 +205,19 @@ describe('filterBuildCauses', () => {
name: 'example-pipeline-name',
'build-cause': [
{
changed: true,
material: {
type: 'git',
'git-configuration': {
'shallow-clone': false,
branch: 'master',
url: 'https://example.com/repo.git',
},
},
modifications: [
{
revision: 'abc123',
'modified-time': '2021-01-01T00:00:00Z',
},
],
},
Expand Down Expand Up @@ -218,12 +250,19 @@ describe('filterBuildCauses', () => {
name: 'example-pipeline-name',
'build-cause': [
{
changed: true,
material: {
type: 'git',
'git-configuration': {
branch: 'master',
'shallow-clone': false,
url: 'https://example.com/repo.git',
},
},
modifications: [
{
revision: 'abc123',
'modified-time': '2021-01-01T00:00:00Z',
},
],
},
Expand Down
15 changes: 9 additions & 6 deletions src/utils/gocdHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ export function getProgressColor(pipeline: GoCDPipeline) {
}

export function firstGitMaterialSHA(
deploy: DBGoCDDeployment | undefined
deploy?: Pick<DBGoCDDeployment, 'pipeline_build_cause'>
): string | null {
if (!deploy) {
if (deploy === undefined) {
return null;
}
if (deploy.pipeline_build_cause.length === 0) {
if (
deploy.pipeline_build_cause === undefined ||
deploy.pipeline_build_cause.length === 0
) {
return null;
}
for (const bc of deploy.pipeline_build_cause) {
Expand All @@ -112,11 +115,11 @@ export function firstGitMaterialSHA(
}

export function filterBuildCauses(
pipeline: GoCDPipeline,
pipeline: Pick<GoCDPipeline, 'build-cause'>,
type: GoCDBuildType
): Array<GoCDBuildCause> {
const buildCauses = pipeline['build-cause'];
if (!buildCauses || buildCauses.length === 0) {
if (buildCauses.length === 0) {
return [];
}

Expand All @@ -135,7 +138,7 @@ export function filterBuildCauses(
}

export async function getBaseAndHeadCommit(
pipeline: GoCDPipeline
pipeline: Pick<GoCDPipeline, 'build-cause' | 'group' | 'name'>
): Promise<[string | null, string | null]> {
const buildCauses = filterBuildCauses(pipeline, 'git');
if (buildCauses.length === 0) {
Expand Down

0 comments on commit efacead

Please sign in to comment.