Skip to content

Commit

Permalink
fix: use mockSteps with specific workflowFile passed to Act (#26)
Browse files Browse the repository at this point in the history
* fix cwd in step mocker object initialization

* updated test cases

* if workflow file is defined then use it directly instead of getting a list of all available workflow files when mocking a step

* add more tests

* bump version
  • Loading branch information
shubhbapna authored Apr 14, 2023
1 parent f63d621 commit 4d5add8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kie/act-js",
"version": "2.0.6",
"version": "2.0.7",
"description": "nodejs wrapper for nektos/act",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
Expand Down
22 changes: 16 additions & 6 deletions src/act/act.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,24 @@ export class Act {
) {
if (opts?.mockSteps) {
// there could multiple workflow files with same event triggers or job names. Act executes them all
const workflowNames = (
await this.list(undefined, opts.cwd, opts.workflowFile)
).filter(filter);
let workflowFiles: string[] = [];

// if workflow file was defined then no need to consider all possible options
if (opts.workflowFile) {
workflowFiles = [path.basename(opts.workflowFile)];
} else if (this.workflowFile !== this.cwd) {
workflowFiles = [path.basename(this.workflowFile)];
} else {
workflowFiles = (
await this.list(undefined, opts.cwd, opts.workflowFile)
).filter(filter).map(l => l.workflowFile);
}

return Promise.all(
workflowNames.map(name => {
workflowFiles.map(workflowFile => {
const stepMocker = new StepMocker(
name.workflowFile,
opts.workflowFile ?? this.workflowFile
workflowFile,
opts.cwd ?? this.cwd
);
return stepMocker.mock(opts.mockSteps!);
})
Expand Down
15 changes: 11 additions & 4 deletions test/unit/act/act.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,14 @@ describe("run", () => {
]);
});

test("run job with mocked step", async () => {
test.each([
["no specific workflow file", undefined],
["specific workflow file", path.join(resources, "push1.yml")],
])("run job with mocked step: %p", async (_title: string, workflowFile: string | undefined) => {
const original = fs.readFileSync(path.join(resources, "push1.yml"), "utf8");
const act = new Act(__dirname, resources);
const act = new Act(resources);
const output = await act.runJob("push1", {
workflowFile,
mockSteps: {
push1: [
{
Expand Down Expand Up @@ -185,12 +189,15 @@ describe("run", () => {
fs.writeFileSync(path.join(resources, "push1.yml"), original);
});

test("run event with mocked step", async () => {
test.each([
["no specific workflow file", undefined],
["specific workflow file", path.join(resources, "pull_request.yml")],
])("run event with mocked step: %p", async (_title: string, workflowFile: string | undefined) => {
const original = fs.readFileSync(
path.join(resources, "pull_request.yml"),
"utf8"
);
const act = new Act(undefined, resources);
const act = new Act(resources, workflowFile);
const output = await act.runEvent("pull_request", {
mockSteps: {
pr: [
Expand Down

0 comments on commit 4d5add8

Please sign in to comment.