Repository navigation
(1 of 4) Exit through an injectable kernel; add Launcher for Aruba testing base branch: main, new branch: kigster:kig/injectable-kernel-launcher - #175
Open
kigster wants to merge 2 commits into
Conversation
Dry::CLI#call and Command.new take `kernel:`, defaulting to Kernel. Help, errors and typos stop the CLI by raising a private Halt, which #call turns into kernel.exit, so a kernel whose exit returns (as Aruba's does) still stops the CLI. A command's own `exit` goes through its kernel too. Dry::CLI::Launcher[registry] runs a CLI with the arguments, streams and kernel in the order Aruba's in-process launcher passes them, so a CLI can be tested in the same process with StringIO streams.
kigster:kig/injectable-kernel-launcher
kigster:kig/injectable-kernel-launchermain, new branch: kigster:kig/injectable-kernel-launcher
main, new branch: kigster:kig/injectable-kernel-launchermain, new branch: kigster:kig/injectable-kernel-launcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposal 1 of 4 in the dry-cli plugin-hooks series.
Upstream issue/forum thread: pending (per CONTRIBUTING.md this is discussed on the forum before going to dry-rb).
Summary
CLI exits go through an injectable kernel, and a
Launcherruns a CLI with Aruba's in-process signature.Dry::CLI#call(arguments:, stdout:, stderr:, stdin:, kernel: Kernel) perform_registry / perform_command parse help → raise Halt(0) error → raise Halt(1) spell_checker → raise Halt(1) rescue Halt → kernel.exit(status) rescue Signal → kernel.exit(128 + signo)Explanation
Haltis private. It exists because a test kernel'sexitreturns: without it, execution would fall through the help screen into code expecting a parsed command.kernel:is anauto_initializekeyword (Free up#initializefor command subclasses #167), soCommand#kernelis set before#initializeand never reaches it. PrivateCommand#exitgoes through it.Launcher
Launcher is an intermediate class that swaps out standard streams (
stdin,stdout,stderr, and additionally,Kernel) so that in tests the streams could be replaced withStringIO, andKernel.exitbe a no-op.This allows using another gem calleed
arubafor proper CLI integration testing of the command, except without the penalty of a fork(). Given this type of Launcher pattern, Aruba is able to use it to drive the gem in a single process, making the tests much much faster.Example on how to configure Aruba
Launcher[target, stdin:, stdout:, stderr:, kernel:]pins defaults for.new(argv); unpinned ones take the globals when the launcher is created. Aruba passes all five, so its StringIOs win.#execute!exits 0 unless the CLI already exited; exceptions propagate. No global stream swapping.Evidence
Before:
Dry.CLI(registry).call(arguments: ["x", "-h"], kernel: fake)→NoMethodError(nokernel:); in-process Aruba runs end the test process onexit.After:
spec/unit/dry/cli/kernel_spec.rb(14),launcher_spec.rb(14),spec/integration/launcher_spec.rb(3, real Aruba in-process). Full suite: 631 examples, 0 failures; rubocop clean.Notes
arubagemSee
Blog Post on testing CLI Gems with Aruba