Skip to content

Commit

Permalink
Speed up slow YAML mapPushTests test
Browse files Browse the repository at this point in the history
It was intermittently failing.  Slowness was due to deep cloning and
Project object creation with a large number of files.

[changelog:fixed]
  • Loading branch information
David Dooling committed Jun 24, 2020
1 parent 88bc5a6 commit 91b5f0b
Showing 1 changed file with 57 additions and 72 deletions.
129 changes: 57 additions & 72 deletions test/core/machine/yaml/mapPushTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@ import { GitCommandGitProject } from "@atomist/automation-client/lib/project/git
import { InMemoryProject } from "@atomist/automation-client/lib/project/mem/InMemoryProject";
import * as assert from "power-assert";
import { pushTest } from "../../../../lib/api/mapping/PushTest";
import {
mapTests,
PushTestMaker,
} from "../../../../lib/core/machine/yaml/mapPushTests";
import { mapTests, PushTestMaker } from "../../../../lib/core/machine/yaml/mapPushTests";

// tslint:disable:no-unused-expression
describe("mapPushTests", () => {

describe("HasFile", () => {

it("should detect hasFile", async () => {
const yaml = {
hasFile: "package.json",
Expand All @@ -39,7 +34,7 @@ describe("mapPushTests", () => {
assert(await test.mapping({ project: p1 } as any));

const p2 = InMemoryProject.of({ path: "pom.xml", content: "<pom></pom>" });
assert(!await test.mapping({ project: p2 } as any));
assert(!(await test.mapping({ project: p2 } as any)));
});

it("should not detect hasFile without missing file name", async () => {
Expand All @@ -51,29 +46,27 @@ describe("mapPushTests", () => {
assert(e.message === "Unable to construct push test from '\"hasFile\"'");
}
});

});

describe("IsRepo", () => {

it("should detect isRepo with regexp", async () => {
const yaml = {
isRepo: /^atomist\/sdm$/,
};
const test = (await mapTests(yaml, {}, {}))[0];

assert(await test.mapping({ id: { owner: "atomist", repo: "sdm" } } as any));
assert(!await test.mapping({ id: { owner: "atomist", repo: "client" } } as any));
assert(!(await test.mapping({ id: { owner: "atomist", repo: "client" } } as any)));
});

it("should detect isRepo with string", async () => {
const yaml = {
isRepo: "atomist\/sdm",
isRepo: "atomist/sdm",
};
const test = (await mapTests(yaml, {}, {}))[0];

assert(await test.mapping({ id: { owner: "atomist", repo: "sdm" } } as any));
assert(!await test.mapping({ id: { owner: "atomist", repo: "client" } } as any));
assert(!(await test.mapping({ id: { owner: "atomist", repo: "client" } } as any)));
});

it("should not detect isRepo without missing regexp", async () => {
Expand All @@ -85,19 +78,17 @@ describe("mapPushTests", () => {
assert(e.message === "Unable to construct push test from '\"isRepo\"'");
}
});

});

describe("IsBranch", () => {

it("should detect isBranch with regexp", async () => {
const yaml = {
isBranch: /^dev-.*$/,
};
const test = (await mapTests(yaml, {}, {}))[0];

assert(await test.mapping({ push: { branch: "dev-pr-1001" } } as any));
assert(!await test.mapping({ push: { branch: "master" } } as any));
assert(!(await test.mapping({ push: { branch: "master" } } as any)));
});

it("should detect isBranch with string", async () => {
Expand All @@ -107,7 +98,7 @@ describe("mapPushTests", () => {
const test = (await mapTests(yaml, {}, {}))[0];

assert(await test.mapping({ push: { branch: "dev-pr-1001" } } as any));
assert(!await test.mapping({ push: { branch: "master" } } as any));
assert(!(await test.mapping({ push: { branch: "master" } } as any)));
});

it("should not detect isBranch without missing regexp", async () => {
Expand All @@ -119,23 +110,21 @@ describe("mapPushTests", () => {
assert(e.message === "Unable to construct push test from '\"isBranch\"'");
}
});

});

describe("IsDefaultBranch", () => {

it("should detect isBranch with regexp", async () => {
const yaml = "is_default_branch";
const test = (await mapTests(yaml, {}, {}))[0];

assert(!await test.mapping({ push: { branch: "dev-pr-1001", repo: { defaultBranch: "master" } } } as any));
assert(
!(await test.mapping({ push: { branch: "dev-pr-1001", repo: { defaultBranch: "master" } } } as any)),
);
assert(await test.mapping({ push: { branch: "master", repo: { defaultBranch: "master" } } } as any));
});

});

describe("IsGoal", () => {

it("should detect isGoal", async () => {
const yaml = {
isGoal: {
Expand All @@ -149,66 +138,75 @@ describe("mapPushTests", () => {
});

describe("IsMaterialChange", () => {

it("should detect isMaterialChange", async () => {
const sha = "3a0087db2a7e1849b0ece33651b5717b1a0616b7";
const sha = "24d42b889c3636770cce22e86bd31396ee3f213d";
const branch = "test-branch-do-not-delete";
const project = await GitCommandGitProject.cloned(
undefined,
GitHubRepoRef.from({
owner: "atomist",
repo: "sdm",
sha,
} as any));
branch,
} as any),
{ depth: 5 },
);

let yaml: any = {
isMaterialChange: {
extensions: ["ts", "js"],
extensions: ["md", "json"],
},
};
let test = (await mapTests(yaml, {}, {}))[0];

assert(await test.mapping({
id: project.id,
project,
push: { after: { sha } },
} as any));
assert(
await test.mapping({
id: project.id,
project,
push: { after: { sha } },
} as any),
);

yaml = {
isMaterialChange: {
files: ["lib/api/mapping/goalTest.ts"],
files: ["package.json"],
},
};
test = (await mapTests(yaml, {}, {}))[0];
assert(await test.mapping({
id: project.id,
project,
push: { after: { sha } },
} as any));
assert(
await test.mapping({
id: project.id,
project,
push: { after: { sha } },
} as any),
);

yaml = {
isMaterialChange: {
pattern: ["**/goalTest.ts"],
pattern: ["**/FindReferencedGitHubIssueTest.ts"],
},
};
test = (await mapTests(yaml, {}, {}))[0];
assert(await test.mapping({
id: project.id,
project,
push: { after: { sha } },
} as any));
assert(
await test.mapping({
id: project.id,
project,
push: { after: { sha } },
} as any),
);

yaml = {
isMaterialChange: {
directories: ["lib"],
directories: ["test"],
},
};
test = (await mapTests(yaml, {}, {}))[0];
assert(await test.mapping({
id: project.id,
project,
push: { after: { sha } },
} as any));
}).timeout(20000);
assert(
await test.mapping({
id: project.id,
project,
push: { after: { sha } },
} as any),
);
}).timeout(10000);

it("should not detect isMaterialChange without missing parameters", async () => {
const yaml = "isMaterialChange";
Expand All @@ -219,11 +217,9 @@ describe("mapPushTests", () => {
assert(e.message === "Unable to construct push test from '\"isMaterialChange\"'");
}
});

});

describe("HasFileContaining", () => {

it("should detect hasFileContaining", async () => {
const yaml = {
hasFileContaining: {
Expand All @@ -233,11 +229,11 @@ describe("mapPushTests", () => {
};
const test = (await mapTests(yaml, {}, {}))[0];

const p1 = InMemoryProject.of({ path: "package.json", content: "{ \"name:\": \"@atomist/atomist-foo\" }" });
const p1 = InMemoryProject.of({ path: "package.json", content: '{ "name:": "@atomist/atomist-foo" }' });
assert(await test.mapping({ project: p1 } as any));

const p2 = InMemoryProject.of({ path: "pom.xml", content: "<pom></pom>" });
assert(!await test.mapping({ project: p2 } as any));
assert(!(await test.mapping({ project: p2 } as any)));
});

it("should not detect hasFileContaining without missing parameters", async () => {
Expand All @@ -263,25 +259,22 @@ describe("mapPushTests", () => {
assert(e.message === "Push test 'hasFileContaining' can't be used without 'content' property");
}
});

});

describe("AdditionalTest", () => {

it("should find test", async () => {
const alwaysTrue = pushTest("always true", async () => true);
const alwaysFalse = pushTest("always false", async () => false);
const yaml = { use: "alwaysTrue" };
const test = (await mapTests(yaml, { alwaysTrue, alwaysFalse }, {}))[0];
assert(await test.mapping({} as any));
});

});

describe("ExtensionTest", () => {

it("should find test with parameters", async () => {
const sometimesTrue: PushTestMaker = (params: any) => pushTest("always true", async () => params.shouldbeTrue);
const sometimesTrue: PushTestMaker = (params: any) =>
pushTest("always true", async () => params.shouldbeTrue);
const yaml = {
use: "sometimesTrue",
parameters: { shouldbeTrue: true },
Expand All @@ -305,21 +298,19 @@ describe("mapPushTests", () => {
const alwaysFalse = () => pushTest("always false", async () => false);
const yaml = { use: "alwaysFalse" };
const test = (await mapTests(yaml, {}, { alwaysTrue, alwaysFalse }))[0];
assert(!await test.mapping({} as any));
assert(!(await test.mapping({} as any)));
});

});

describe("And", () => {

it("should correctly evaluate true and false", async () => {
const alwaysTrue = pushTest("always true", async () => true);
const alwaysFalse = pushTest("always false", async () => false);
const yaml = {
and: [{ use: "alwaysTrue" }, { use: "alwaysFalse" }],
};
const test = (await mapTests(yaml, { alwaysTrue, alwaysFalse }, {}))[0];
assert(!await test.mapping({} as any));
assert(!(await test.mapping({} as any)));
});

it("should correctly evaluate true and true", async () => {
Expand All @@ -334,7 +325,6 @@ describe("mapPushTests", () => {
});

describe("Or", () => {

it("should correctly evaluate true and false", async () => {
const alwaysTrue = pushTest("always true", async () => true);
const alwaysFalse = pushTest("always false", async () => false);
Expand All @@ -357,14 +347,13 @@ describe("mapPushTests", () => {
});

describe("Not", () => {

it("should correctly evaluate true", async () => {
const alwaysTrue = pushTest("always true", async () => true);
const yaml = {
not: { use: "alwaysTrue" },
};
const test = (await mapTests(yaml, { alwaysTrue }, {}))[0];
assert(!await test.mapping({} as any));
assert(!(await test.mapping({} as any)));
});

it("should correctly evaluate true and true", async () => {
Expand All @@ -378,7 +367,6 @@ describe("mapPushTests", () => {
});

describe("Logical combinations", () => {

it("should correctly evaluate combination of and and not", async () => {
const alwaysTrue = pushTest("always true", async () => true);
const alwaysFalse = pushTest("always false", async () => false);
Expand All @@ -401,10 +389,7 @@ describe("mapPushTests", () => {
and: [
{ use: "alwaysTrue" },
{
or: [
{ use: "alwaysFalse" },
{ use: "alwaysTrue" },
],
or: [{ use: "alwaysFalse" }, { use: "alwaysTrue" }],
},
],
};
Expand Down

0 comments on commit 91b5f0b

Please sign in to comment.