test(shell-env): cover noisy JSON and exit status - #85
Merged
Merged
Conversation
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.
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.
What & why
shell_env::captureis whatload_login_shell_environmentcalls, 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, andprint_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, becauselog::warndoes not run its arguments when that level is disabled.crates/moon-perf/src/implementation.rshas more untested called functions, butcompare_perf's sign andOutput::sortdisagree 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 diffagainst the parsers is empty apart frommod 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 laterPATH/HOMEobject was never read.missing_json_is_an_error— return an empty map instead of bailing. Red:expect_errfailed because the function returnedOk.successful_exit_returns_parsed_env_without_either_callback— bail withdiscarded parsed environmentwhen the exit is zero. Red: theexpectreported that error instead of the map. Inverting only thelog::warncondition 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— dropfailed_capture_error()from the non-zero parse-failure message. Red: the message was the deserialize error and did not containshell died.successful_exit_with_unparseable_output_omits_the_shell_error— prefix the zero-exit parse-failure message withfailed_capture_error(). Red: the message containedshell died.Nothing was dropped. A clean-context review kept all five.
How to verify
cargo test -p moon-util --lib shell_env