oclif / core

Node.js Open CLI Framework. Built by Salesforce.

Home Page:https://oclif.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

this.exit(0) no longer exits in v3

maxbeatty opened this issue · comments

Describe the bug
this.exit(0) used to exit with a code of zero in v2. In v3, this behavior has changed in that a command no longer exits.

To Reproduce
Reproduction repository: https://github.com/maxbeatty/reproduce-oclif-test-exit-zero

Simple command that can exit in different ways:
https://github.com/maxbeatty/reproduce-oclif-test-exit-zero/blob/2dfa70a1ab80b057167c3f4a9eb1f58701b125cc/src/commands/hello/index.ts#L20-L40

Tests for exit logic:
https://github.com/maxbeatty/reproduce-oclif-test-exit-zero/blob/2dfa70a1ab80b057167c3f4a9eb1f58701b125cc/test/commands/hello/index.test.ts#L11-L32

CI failure:
https://github.com/maxbeatty/reproduce-oclif-test-exit-zero/actions/runs/6670925950/job/18131787718

Downgrading to v2 restores expected behavior:

Expected behavior
this.exit(code) should exit the command with the given code.

Environment (please complete the following information):

  • OS & version: observed locally in macOS and in GitHub Actions on Ubuntu
  • Shell/terminal & version: does not seem to be related

@mdonnalley thanks for restoring the exit behavior! I noticed in your PR that you're using .catch(/EEXIT: 0/) in your tests. Is it possible to use .exit(0) instead for more consistency between the command and test (i.e., the command calls this.exit so my test would verify that with test.exit)?

maxbeatty/reproduce-oclif-test-exit-zero#7 is upgrading to the newest version of @oclif/test but failing because I am using .exit(0)

https://github.com/maxbeatty/reproduce-oclif-test-exit-zero/blob/0aca49ec6fb0d1180aef4ca391e8885bec2390ee/test/commands/hello/index.test.ts#L14

https://github.com/maxbeatty/reproduce-oclif-test-exit-zero/blob/0aca49ec6fb0d1180aef4ca391e8885bec2390ee/test/commands/hello/index.test.ts#L29

@maxbeatty Yes, it is possible. Those test are, for some unknown reason, using fancy-test directly instead of @oclif/test, which is what exposes the .exit option.

If I had rewritten it to use @ocilf/test it would have looked like this:

import {test} from '@oclif/test'

test
  .stdout()
  .do(() => {
    class Command extends Base {
      async run() {
        this.exit(0)
      }
    }

    return Command.run([])
    })
  .exit(0)
  .it('exits with 0')

apologies for the oversight on my end. this is all working as expected now. thank you!

maxbeatty/reproduce-oclif-test-exit-zero@532581b