Skip to content

test(shell-env): cover noisy JSON and exit status - #85

Merged
kirillDevPro merged 1 commit into
masterfrom
test/shell-env-parse
Sep 24, 2026
Merged

kirillDevPro merged 1 commit into
masterfrom
test/shell-env-parse

Conversation

@kirillDevPro

Copy link
Copy Markdown
Collaborator

What & why

shell_env::capture is what load_login_shell_environment calls, and the part that can be wrong without spawning a shell is the parser: a { in the banner, or a zero exit paired with bad output, chooses a different result than a non-zero exit paired with good JSON. The existing test only covers valid JSON with a non-zero exit.

Covered in crates/moon-util/src/shell_env.rs:

  • parse_env_map_from_noisy_output — a later JSON object wins when an earlier brace is not a map, and output with no object is an error.
  • parse_env_output — a zero exit returns a parsed map; a non-zero exit with no JSON includes the shell error; a zero exit with no JSON does not.

Still untested here: capture, capture_unix, capture_windows, spawn_and_read_fd, and print_env (process and filesystem). The non-zero-exit success path was already covered and was not changed. The warning log on that path is not asserted, because log::warn does not run its arguments when that level is disabled.

crates/moon-perf/src/implementation.rs has more untested called functions, but compare_perf's sign and Output::sort disagree with their comments. That was not encoded as a test. See #84.

Mutations

Each test was green, then one production edit turned that test red for the reason below, then the edit was reverted and the test was green again. git diff against the parsers is empty apart from mod shell_env_tests.

  • later_json_object_is_used_when_an_earlier_brace_is_not_a_map — .take(1) on the brace scan. Red: the first {not-json} failed and the later PATH/HOME object was never read.
  • missing_json_is_an_error — return an empty map instead of bailing. Red: expect_err failed because the function returned Ok.
  • successful_exit_returns_parsed_env_without_either_callback — bail with discarded parsed environment when the exit is zero. Red: the expect reported that error instead of the map. Inverting only the log::warn condition stays green, because the closure is not called when warn is disabled; the test pins the returned map.
  • failed_exit_with_unparseable_output_includes_the_shell_error — drop failed_capture_error() from the non-zero parse-failure message. Red: the message was the deserialize error and did not contain shell died.
  • successful_exit_with_unparseable_output_omits_the_shell_error — prefix the zero-exit parse-failure message with failed_capture_error(). Red: the message contained shell died.

Nothing was dropped. A clean-context review kept all five.

How to verify

cargo test -p moon-util --lib shell_env

parse_env_output decides from two inputs that can disagree: whether
the shell printed a JSON object, and whether that process exited
zero. A banner brace, or a zero exit treated as failure, would drop
or mis-report the captured environment with no failing test.
@kirillDevPro
kirillDevPro merged commit bda2e9a into master Sep 24, 2026
4 checks passed
@kirillDevPro
kirillDevPro deleted the test/shell-env-parse branch September 24, 2026 06:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant