kiegroup / act-js

A node.js wrapper for nektos/act to programmatically run your github actions locally
Apache License 2.0
54 stars 9 forks source link

Cannot use `mockSteps` with specific `workflowFile` passed to Act #24

Closed shivshav closed 1 year ago

shivshav commented 1 year ago

Hello there! First of all, thank you for this lib, it's turning out to be very useful so far so I appreciate the work put it into it!

Describe the bug I believe there's a bug if you try to use both step mocking and passing a specific workflow file to the act runner together

To Reproduce

Workflow file ```yaml name: Test on: [push] jobs: repro: name: bug repro runs-on: ubuntu-latest steps: - name: step 1 (mocked) run: echo "this is the real command" - name: step 2 (not mocked) run: echo "this is the real command" ```
Minimal test file ```javascript const path = require("path"); const { Act } = require("@kie/act-js"); const { MockGithub } = require("@kie/mock-github"); const { beforeEach, test, afterEach, expect} = require("@jest/globals"); let mockGithub; beforeEach(async () => { mockGithub = new MockGithub({ repo: { test: { files: [ { src: path.join(__dirname, "workflowfile-plus-mocksteps-repro.yaml"), dest: "/.github/workflows/workflow-file-plus-mocksteps-repro.yaml", } ] }, } }) await mockGithub.setup(); }) afterEach(async () => { await mockGithub.teardown(); }) test("workflow file + mockSteps repro", async () => { const act = new Act(mockGithub.repo.getPath("test"), ".github/workflows/workflow-file-plus-mocksteps-repro.yaml"); const result = await act.runEvent("push", { logFile: process.env.ACT_LOG ? "workflow.log" : undefined, mockSteps: { "repro": [{name: "step 1 (mocked)", mockWith: "echo this is the mocked command"}] } }) expect(result).toStrictEqual([ {name: "Main step 1 (mocked)", status: 0, output: "this is the mocked command"}, {name: "Main step 2 (not mocked)", status: 0, output: "this is the real command"} ]) }) ```

Expected behavior

Both options are able to be used in conjunction and the given workflow file executes with the proper step mocked

Logs Never makes it far enough for the act to actually execute, but the jest output is noted below.

 FAIL  action/deploy/v3/test/repro.test.js
  ✕ workflow file + mockSteps repro (1268 ms)

  ● workflow file + mockSteps repro

    Could not locate workflow-file-plus-mocksteps-repro.yaml

      30 | test("workflow file + mockSteps repro", async () => {
      31 |     const act = new Act(mockGithub.repo.getPath("test"), ".github/workflows/workflow-file-plus-mocksteps-repro.yaml");
    > 32 |     const result = await act.runEvent("push", {
         |                    ^
      33 |         logFile: process.env.ACT_LOG ? "workflow.log" : undefined,
      34 |         mockSteps: {
      35 |             "repro": [{name: "step 1 (mocked)", mockWith: "echo this is the mocked command"}]

      at StepMocker.getWorkflowPath (node_modules/@kie/act-js/build/src/step-mocker/step-mocker.js:63:19)
      at StepMocker.mock (node_modules/@kie/act-js/build/src/step-mocker/step-mocker.js:17:31)
      at node_modules/@kie/act-js/build/src/act/act.js:131:35
          at Array.map (<anonymous>)
      at Act.handleStepMocking (node_modules/@kie/act-js/build/src/act/act.js:129:46)
      at Act.runEvent (node_modules/@kie/act-js/build/src/act/act.js:118:9)
      at Object.<anonymous> (action/deploy/v3/test/repro.test.js:32:20)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.556 s, estimated 2 s
Ran all test suites matching /action\/deploy\/v3\/test\/repro.test.js/i.

Additional context I suspect this is a problem with the arguments passed to the StepMocker from the Act class. The StepMocker expects the workflowFile + cwd here but what actually gets passed from Act's handleStepMocking call is the path to the workflow file 2x so that when this function tries to find the workflow file, none of the if branches are a match.

shubhbapna commented 1 year ago

You are absolutely right! Thank you for reporting this. I will have a fix out asap