tests.test_execution module¶

tests.test_execution.test_operation_timeouts_cover_all_operations()[source]¶

Every routable operation has a per-operation timeout default.

The map and the Operations enum then never drift apart.

tests.test_execution.test_operation_name_sets_match_enum()[source]¶

The literal operation-name sets stay subsets of the Operations vocabulary.

_MUTATING_OPERATIONS (plan-mode capture) and the internal_sudo_operations of each manager (watchdog arming) are spelled as string literals: renaming an Operations member would otherwise silently desync them, stopping plan capture or watchdog arming for the renamed operation.

Only a mutating operation can run the tool’s own sudo, so the second set stays inside the first on every manager.

tests.test_execution.test_read_only_tier_is_shorter_than_mutating_tier()[source]¶

Read-only probes fail faster than state-changing operations.

tests.test_execution.test_resolve_timeout_explicit_wins(active_operation)[source]¶

An explicit timeout wins for every operation, including unknown ones.

That timeout comes from --timeout or from a per-manager override.

tests.test_execution.test_resolve_timeout_per_operation(active_operation, expected)[source]¶

With no explicit timeout, each operation resolves to its tier default.

tests.test_execution.test_make_spinner_live_mode_follows_progress(progress, live)[source]¶
tests.test_execution.plain_labels(monkeypatch)[source]¶

Force spinner labels to plain text, whatever the host terminal answers.

Every recognized color variable is cleared and NO_COLOR set, so the label carries no ANSI and the assertions below compare bare strings. Without it a developer’s FORCE_COLOR would style the label and fail them.

tests.test_execution.test_lean_command_strips_manager_plumbing(argv, cli_path, pre_args, post_args, expected)[source]¶

Static plumbing is dropped; every operation-specific argument is kept.

tests.test_execution.test_lean_command_keeps_unmatched_plumbing()[source]¶

The strip matches the actual prefix and suffix, never slices by length.

A call built with auto_pre_args=False carries none of the declared plumbing, so a length-based slice would eat a real argument instead.

tests.test_execution.test_lean_command_empty_argv()[source]¶
tests.test_execution.test_spinner_label_is_plain_without_styling(plain_labels)[source]¶
tests.test_execution.test_styling_gate_reaches_worker_threads(monkeypatch)[source]¶

--no-color reaches a label composed on a worker thread.

Trail lines are composed on the dispatch’s workers, where the thread-local command context is absent. The gate reads the color the invocation published for every thread instead, and that answer outranks the environment.

tests.test_execution.test_spinner_label_subject_alone_without_styling(plain_labels)[source]¶
tests.test_execution.test_spinner_label_italic_follows_terminal_capability(monkeypatch, term, italic)[source]¶

The subject is always painted; the command takes italic where it renders.

The subject is compared against the theme’s own rendering rather than a literal escape, the invoked_command slot resolving to different codes on a light and a dark background.

tests.test_execution.test_make_spinner_label_names_operation_and_command(plain_labels)[source]¶
tests.test_execution.test_make_spinner_label_without_operation(plain_labels)[source]¶
tests.test_execution.test_make_spinner_uses_configured_delay()[source]¶
tests.test_execution.test_spinner_clock_dims_the_parentheses_too(color_enabled, expected)[source]¶

The whole fragment is faint, brackets included, or none of it is.

Painting the duration alone through the timer callable would leave the parentheses upstream adds around it at full weight, which is why the spinner takes a timer_style.

tests.test_execution.test_spinner_clock_is_plain_before_start()[source]¶

A spinner that never started reports color off, so it never dims.

tests.test_execution.FAIL_ON_STDOUT = "import sys; sys.stdout.write('boom'); sys.exit(8)"¶

Fails the steamcmd way: a non-zero exit with its message on <stdout> and an empty <stderr>.

tests.test_execution.FAIL_ON_STDERR = "import sys; sys.stderr.write('boom'); sys.exit(8)"¶

Fails the conventional way: a non-zero exit with its message on <stderr>.

tests.test_execution.test_run_failure_gate(stop_on_error, must_succeed, script, expectation)[source]¶

The failure gate decides to raise, accumulate or tolerate.

It reads the exit code, the <stderr> content, and the stop_on_error/must_succeed context.

tests.test_execution.NOISY_STATUS = "import sys; sys.stdout.write('boom'); sys.stderr.write('chatter\\n'); sys.exit(8)"¶

Exits non-zero as a status, with only routine chatter on <stderr>.

tests.test_execution.NOISY_FAILURE = "import sys; sys.stderr.write('chatter\\nbroken\\n'); sys.exit(8)"¶

Fails for real, its diagnosis written after the same routine chatter.

tests.test_execution.test_run_failure_gate_tolerates_stderr_noise(monkeypatch)[source]¶

A <stderr> holding only declared noise is as good as a silent one.

tests.test_execution.test_run_failure_gate_drops_stderr_noise_from_the_diagnosis(monkeypatch)[source]¶

A real failure still raises, its diagnosis cleared of the noise.

tests.test_execution.DIAGNOSIS_CAP = 13¶

Longest report CLIError.diagnosis relays whole.

tests.test_execution.test_cli_error_diagnosis(error, output, expected)[source]¶

CLIError.diagnosis prefers <stderr>, then falls back on <stdout>.

It states a silent death, and keeps the head and the tail around a truncation counter.

tests.test_execution.test_cli_error_repr(code, error, expected)[source]¶

The repr folds the error onto one line, and shortens it.

tests.test_execution.test_cli_error_diagnosis_keeps_an_opening_error()[source]¶

A report that opens with its error and trails a backtrace keeps the error.

The tail alone would hold frames and no reason.

tests.test_execution.test_failed_run_relays_diagnosis(stop_on_error, script, expected, caplog)[source]¶

The failure gate logs the diagnosis at WARNING, tagged with the manager.

It does so for accumulated and raised failures alike.

tests.test_execution.test_no_diagnosis_relay(script, level, caplog)[source]¶

No WARNING relay without a genuine failure.

There is none either when DEBUG already streamed the raw output inline.

tests.test_execution.test_exempt_operations_skip_diagnosis_relay(operation, caplog)[source]¶

Version probes and doctor fail without the WARNING relay.

Version probes fire per candidate on selection, and doctor relays its report verbatim.

tests.test_execution.test_version_probe_failure_stays_out_of_cli_errors()[source]¶

A binary rejecting the version verb records no error.

The probe is how mpm settles that a PATH hit is not the manager it names, so its rejection is a verdict unavailable_reason already reports. Left in cli_errors, it named a manager mpm had correctly dropped in the end-of-run summary of every command: issue 2119, where PearCleaner’s pear symlink had mpm outdated warn about a PHP manager the user never installed.

tests.test_execution.test_version_probe_incomplete_run_stays_an_error()[source]¶

A probe that never completed is plumbing, not a verdict.

It stays in the tally, mirroring doctor’s own split.

tests.test_execution.test_run_cache_replays_identical_command(tmp_path)[source]¶

With a shared cache, a byte-identical command runs the subprocess once.

tests.test_execution.test_run_cache_disabled_by_default(tmp_path)[source]¶

Without a cache, identical commands each spawn their own subprocess.

tests.test_execution.test_run_cache_replays_failure_to_every_member(tmp_path)[source]¶

A cached failure is re-attributed to each peer, though the command runs once.

tests.test_execution.test_run_cache_keeps_distinct_commands_apart(tmp_path)[source]¶

Only byte-identical invocations collapse; different args each run.

tests.test_execution.test_run_cache_collapses_dry_run(caplog)[source]¶

Under –dry-run a family’s identical command is announced once.

tests.test_execution.test_run_cache_drops_reads_once_the_lane_changes_state(tmp_path, plan, runs)[source]¶

A read cached before a state-changing command spawns again after it.

tests.test_execution.test_run_cache_drops_reads_when_the_state_change_times_out(tmp_path)[source]¶

A command killed on its timeout may have changed the system all the same.

tests.test_execution.test_run_cache_keeps_state_changes_for_the_peers(tmp_path)[source]¶

A state-changing result outlives the next one, so a peer replays both.

tests.test_execution.test_run_cache_never_serves_a_read_from_a_state_change(tmp_path)[source]¶

A read sharing the command line of a state change still runs on its own.

tests.test_execution.test_format_plan_command_shell_quotes_env_and_args()[source]¶

A captured plan command renders as a plain, shell-quoted, runnable line.

tests.test_execution.test_plan_captures_mutating_command_without_running_it(tmp_path)[source]¶

Plan mode records a state-changing command instead of executing it.

tests.test_execution.test_plan_runs_read_only_operations(tmp_path)[source]¶

Plan mode executes read-only queries so the plan resolves against real state.

tests.test_execution.test_plan_synthesized_upgrade_all_resolves_reads(invoke, patch_pool_with)[source]¶

mpm --plan upgrade on a synthesized upgrade-all prints each command.

Those are the per-package upgrade commands, resolved against a really-executed outdated read.

Guards the read-stamp of the synthesized fallback in PackageManager.upgrade: the whole run is stamped upgrade_all (a mutating operation), so without re-stamping the inner listing as outdated, plan mode captured the read itself — leaving the plan without the actual mutating commands it exists to disclose.

tests.test_execution.test_plan_ignores_force_exec(tmp_path)[source]¶

A force_exec read runs for real even under plan mode (version probes, etc.).

tests.test_execution.test_run_registers_live_process_then_discards_it()[source]¶

run() tracks its subprocess while it runs, and drops it once done.

A background call parks in a real subprocess. Once it is registered, terminate_live_processes() unblocks it, and run_cli()’s finally clears the registry: this is the exact path the SIGINT handler drives on Ctrl+C.

tests.test_execution.test_build_cli_escalates_with_sudo_n_when_policy_on()[source]¶

A privileged op escalates to sudo --non-interactive on the manager’s opt-in.

The escalator is pinned rather than detected: a Windows runner carries no sudo on PATH, and detection finding none would skip the wrapping this test asserts.

tests.test_execution.test_build_cli_no_escalation_as_root()[source]¶

Root runs a privileged op bare, whatever escalator the host carries.

Wrapping it would only add ways to fail: pkexec needs a running polkit authority, which an OpenRC host may lack.

tests.test_execution.test_build_cli_passes_the_options_the_release_accepts(options)[source]¶

The gated options sit between the escalator and the command.

tests.test_execution.test_build_cli_no_escalation_when_policy_off()[source]¶

--no-sudo (policy False) drops escalation even on a privileged op.

tests.test_execution.test_build_cli_escalation_follows_the_escalator_not_the_platform()[source]¶

Carrying an escalator is what decides escalation, on any platform.

A host with no escalator cannot escalate whatever it runs, and one that has it can, Windows included: the gsudo entry exists for that one platform.

tests.test_execution.test_build_cli_no_escalation_without_an_escalator()[source]¶

A host carrying none runs unprivileged on any platform.

It does not crash on a missing binary.

tests.test_execution.test_build_cli_marker_required_for_escalation()[source]¶

Policy on but no per-op marker (sudo=False default) means no escalation.

tests.test_execution.test_npm_sudo_marker_dormant_until_opted_in()[source]¶

npm marks its global installs privileged, but stays unescalated.

It escalates only once the user opts in via –sudo / config (default_sudo is False).

tests.test_execution.test_dormant_marker_permission_failure_hints_the_opt_in(caplog)[source]¶

A dormant privileged op dying on a permission error draws the hint.

The hint names the scoped opt-in, labeled and placed after the tool’s own account of the refusal.

tests.test_execution.test_dormant_marker_hint_negative_gates(caplog, marker, stderr, euid)[source]¶

Each gate individually silences the hint.

The gates are a missing privileged marker, a failure that is not a permission refusal, and a process already root.

tests.test_execution.test_failed_run_names_a_prompt_its_sudo_gave_up_on(caplog, stderr, named)[source]¶

A tool whose sudo stopped waiting for a password fails like a timeout.

It gets what a timeout of mpm on that prompt gets: the same advice, after the tool’s own account, and the same cause for the trail line.

The two controls keep the plain relay: a line no sudo signed, and a sudo that refuses for another reason.

tests.test_execution.test_run_hints_when_escalation_cannot_authenticate(tmp_path, monkeypatch, caplog, escalator_id, refusal, returncode)[source]¶

A real escalator that cannot authenticate triggers the actionable hint.

The hint names that escalator and what its policy needs.

A fake escalator on PATH mimics the refusal, so the whole build_cli → subprocess → failure-gate → hint path runs for real, without root.

tests.test_execution.test_escalated_calls_keep_the_controlling_terminal(escalate)[source]¶

An escalated call is never detached into a POSIX session of its own.

sudo keys its credential cache per terminal (tty_tickets), so a child spawned with start_new_session cannot read the cache prime_sudo() probed and warmed: it fails with sudo: interactive authentication is required however fresh the credentials are. Everything mpm does not escalate keeps the session isolation that reaps wedged grandchildren.

tests.test_execution.test_internal_escalator_spends_a_warm_cache_in_the_terminal_session(monkeypatch, internal_sudo, resets, operation, warm, detached)[source]¶

An internal escalator call stays in the terminal session while cached.

This holds while the credential cache is warm, in each operation where its tool may run sudo.

sudo keys a ticket on the terminal and on its session. Detached, the tool’s own sudo finds no ticket and no terminal to prompt on, so its privileged step fails on the very cache meant to serve it.

Each control changes one condition and keeps the session isolation: an operation in which the tool never escalates, a read-only call, a cold cache, a manager that never escalates on its own, and a tool that drops the cache, which has nothing to spend.

tests.test_execution.test_run_forwards_a_forced_environment_across_run0()[source]¶

The environment a call forces reaches the payload run0 escalates.

run0 forks it off the service manager, which inherits nothing, so a variable handed to the escalator’s own process stops there. run() splices the escalator’s own forwarding argument instead, and does it after the cooldown merge so that gate travels too. Measured on Arch with systemd 261: run0 --pipe --no-ask-password printenv LC_ALL exits 1, while the same call carrying --setenv=LC_ALL=C prints C.

tests.test_execution.test_run_names_each_run0_unit_at_spawn_time()[source]¶

Each spawned run0 call carries a unit name of its own.

The serial in that name stays out of the cache key: a peer of the lane still replays the call, and a later spawn draws a new serial.

tests.test_execution.test_run_forwards_the_forced_locale_across_run0()[source]¶

The forced locale reaches the payload run0 escalates, LANGUAGE unset.

An unset variable needs no carrying: the fresh environment run0 builds never had it, and a bare --setenv=LANGUAGE would import the caller’s value.

tests.test_execution.test_run_spawns_in_the_forced_locale(manager_env, expected)[source]¶

Every spawned command gets the forced locale, under the manager’s own.

The disclosed $ line keeps the manager’s variables alone: the locale is disclosed once per process instead of on every line.

tests.test_execution.test_c_locale_falls_back_to_c_without_c_utf8(monkeypatch)[source]¶

A C library without C.UTF-8 gets C, and the probe leaves mpm’s locale.

The process starts in C, as it must on such a host: a runner whose own locale is C.UTF-8 would otherwise ask the stand-in to restore it.

tests.test_execution.test_run_forwards_nothing_across_an_escalator_that_needs_no_help()[source]¶

sudo resets the environment through a sudoers policy the host owns.

mpm then splices nothing of its own.

tests.test_execution.test_mutating_subcommands_prime_sudo(invoke, fake_pool, args)[source]¶

Every mutating subcommand authenticates sudo up front.

A future command then cannot silently skip the priming.

tests.test_execution.test_restore_primes_sudo(invoke, fake_pool, tmp_path)[source]¶

restore fans installs out too, so it also primes sudo up front.

tests.test_execution.test_read_subcommand_does_not_prime_sudo(invoke, fake_pool)[source]¶

A read-only command never escalates, so it must not prompt for sudo.

Patched at the source module: the explore subcommands import no sudo machinery at all, so any priming would have to reach it there.

tests.test_execution.test_sourced_operation_stamps_active_operation(invoke, fake_pool, monkeypatch, args, expected_operation)[source]¶

A sourced remove/upgrade <packages> runs under the real operation stamp.

Its mutating action carries the stamp.

Source discovery re-selects the managers with installed, which stamps _active_operation = "installed" on the shared singletons. Each per-package task must therefore re-stamp the mutating operation for the duration of its own attempt (through CLIExecutor.acting_as), or the command would resolve the read-only timeout and skip the escalator stall watchdog (both keyed on _active_operation). This drives the real selection flow and observes the stamp from inside the action itself: it fails if the dispatch stops stamping.

tests.test_execution.test_untied_install_search_runs_under_read_only_stamp(invoke, fake_pool, monkeypatch)[source]¶

The untied-install priority search runs under the search stamp, not install.

The search is read-only and can never escalate, so it must not resolve the mutating timeout nor arm the internal-escalator stall watchdog (both keyed on _active_operation). Without the stamp, a slow brew search of a cask would be misread as a hidden password prompt.

tests.test_execution.test_search_all_cli_skips_inaccessible_directory(tmp_path, monkeypatch, caplog, operation, error_code)[source]¶

An inaccessible PATH entry must not hide a later, usable CLI.

tests.test_execution.test_search_all_cli_skips_inaccessible_file(tmp_path, monkeypatch, caplog, on_windows, error_code)[source]¶

Unreadable or vanished candidates must not abort the remaining search.

A symlink pointing to itself must not abort the remaining search.

tests.test_execution.test_search_all_cli_skips_a_shim_its_owner_disowns(tmp_path, monkeypatch)[source]¶

A class defining owns_missing_shim hides a shim of a missing tool.

CLIExecutor registers the class when it is created, and search_all_cli asks it about each candidate, so a later real binary wins.

tests.test_execution.test_windows_pathext_tracks_cpython()[source]¶

Our copy of CPython’s default PATHEXT still matches the original.

search_all_cli mirrors shutil.which() on Windows by reading shutil._WIN_DEFAULT_PATHEXT, which is private: a release may rename it, drop it, or add a suffix to it, and only Windows would notice. The attribute is defined on every platform, so this canary runs on every cell of the test matrix, including the prerelease interpreter that sees the change first, and turns a Windows-only detection failure into a named failure everywhere.

On divergence: update WIN_DEFAULT_PATHEXT to the new value. If the attribute is gone for good, the fallback is already what the code uses.

tests.test_execution.test_a_file_that_is_not_a_program_raises_enoexec(tmp_path)[source]¶

The premise UNRUNNABLE_ERRNOS rests on.

subprocess execs directly rather than through a shell, so it never falls back to interpreting a file the kernel refused. A wrong-architecture binary and a text file carrying the executable bit both land here.

tests.test_execution.test_unrunnable_cli_degrades_instead_of_crashing(error_code, caplog)[source]¶

A CLI that cannot be spawned marks its manager unavailable and yields “”.

mpm probes every manager it can see, so one unrunnable binary anywhere on PATH aborts every subcommand instead of skipping that single manager. ENOEXEC is the one that reached CI: GitHub’s ubuntu-26.04-arm image carries an x86 pnpm at /usr/local/bin/pnpm.