For example, a wrapper should deliver two words as one argument and $(printf marker) as literal text. A refactor from an argument array to a formatted shell command can change both behaviors while leaving a test using only hello green.
Test that boundary by launching a small child process that prints sys.argv[1:] as JSON, then comparing it with an explicit expected array. Include spaces and shell syntax so the check catches a wrapper that turns arguments into an interpreted command string. When reviewing a wrapper change, inspect the code and record what the child receives: they provide different evidence.
Check the argument array, not the displayed command
A logged command is a representation, not proof of what the child received. Quotes in that representation might be shell syntax, literal characters, or formatting added by the logger.
The child process provides a more direct observation:
| Input supplied as one argument | Expected child argument array |
|---|---|
two words |
["two words"] |
say "hi" |
["say \"hi\""] |
left; : |
["left; :"] |
$(printf marker) |
["$(printf marker)"] |
These cases probe different transformations: splitting on whitespace, consuming quotes, treating a semicolon as a command separator, and expanding command substitution.
Quotes inside an argument are data when no shell interprets them.
Save this as a self-contained boundary test
Create test_subprocess_arguments.py with the following code. These tests exercise demonstration wrappers until you connect your real launcher. A green demonstration suite is not application coverage.
The fixture uses Python's standard library and a temporary child script, without depending on an application command's output format or option parser.
import json
import os
from pathlib import Path
import shlex
import subprocess
import sys
import tempfile
import unittest
CASES = (
("space", "two words", ["two words"]),
("quotes", 'say "hi"', ['say "hi"']),
("semicolon", "left; :", ["left; :"]),
("substitution", "$(printf marker)", ["$(printf marker)"]),
)
def record_arguments(command, directory, *, shell=False):
result = subprocess.run(
command,
cwd=directory,
shell=shell,
check=True,
capture_output=True,
encoding="utf-8",
timeout=5,
)
return json.loads(result.stdout)
def preserved_arguments(child, values):
return record_arguments(
[sys.executable, str(child), *values],
child.parent,
shell=False,
)
def broken_arguments(child, values):
# Deliberately broken POSIX-shell implementation.
# Quote the fixed executable and script, but concatenate raw inputs.
prefix = " ".join(
shlex.quote(part) for part in (sys.executable, str(child))
)
command = prefix + " " + " ".join(values)
return record_arguments(command, child.parent, shell=True)
class ArgumentBoundaryTests(unittest.TestCase):
def setUp(self):
temporary = tempfile.TemporaryDirectory()
self.addCleanup(temporary.cleanup)
self.child = Path(temporary.name).resolve() / "record_args.py"
self.child.write_text(
"import json, sys\n"
"print(json.dumps(sys.argv[1:]))\n",
encoding="utf-8",
)
def test_space_argument(self):
self.assertEqual(
preserved_arguments(self.child, ["two words"]),
["two words"],
)
def test_each_literal_argument(self):
for name, value, expected in CASES:
with self.subTest(case=name):
self.assertEqual(
preserved_arguments(self.child, [value]),
expected,
)
def test_arguments_stay_separate_and_ordered(self):
values = [value for _, value, _ in CASES]
expected = ["two words", 'say "hi"', "left; :",
"$(printf marker)"]
self.assertEqual(
preserved_arguments(self.child, values),
expected,
)
@unittest.skipUnless(os.name == "posix", "POSIX-shell control")
def test_broken_control_splits_one_argument(self):
observed = broken_arguments(self.child, ["two words"])
self.assertEqual(observed, ["two", "words"])
self.assertNotEqual(observed, ["two words"])
if __name__ == "__main__":
unittest.main()
Connect the fixture to the wrapper you actually ship
Replace preserved_arguments with an adapter that makes your real wrapper launch the recorder. Use this template, replacing the marked call and its parameters with your application's API:
def preserved_arguments(child, values):
# APPLICATION-WRAPPER CALL: replace with your real launcher.
result = application_launch(
executable=sys.executable,
arguments=[str(child), *values],
cwd=child.parent,
capture_output=True,
encoding="utf-8",
check=True,
timeout=5,
)
# Adapt this access if your wrapper returns stdout differently.
return json.loads(result.stdout)
application_launch is a placeholder, not a provided function. The template assumes the wrapper can return captured standard output. Preserve the timeout and failure handling through the wrapper's supported interface.
Pass sys.executable as the executable, the recorder script as its first argument, and the test values after it. If the executable is fixed, consider extracting the launch boundary into a function that accepts an executable for testing, while keeping production selection separate.
Keep the recorder as the launched program. Mocking subprocess.run can verify that code constructs a list and requests shell=False, but it cannot observe arguments arriving in a real child process.
Run the tests locally or as a CI step using the project's Python interpreter:
python -m unittest -v test_subprocess_arguments.py
The five-second timeout bounds each invocation; it is not a performance target. The negative control deliberately invokes a POSIX shell with only the fixed two words input. Do not turn the broken wrapper into a general-purpose payload runner. A temporary working directory is not a security sandbox.
The broken control checks whether your test can detect failure
The passing control proves that the recorder observes splitting: it expects two and words as separate arguments. Substituting the broken implementation into the preservation assertion checks something different: that the assertion rejects this regression.
A green suite matters only if the check rejects the regression you care about.
On POSIX only, temporarily replace the preserved_arguments call inside the existing test_space_argument assertion with:
broken_arguments(self.child, ["two words"]),
Run that dedicated test, which does not iterate over CASES:
python -m unittest -v \
test_subprocess_arguments.ArgumentBoundaryTests.test_space_argument
The expected ['two words'] differs from the broken wrapper's ['two', 'words'], so the assertion should fail. Revert the temporary change afterward.
The shell-specific control is skipped on Windows. The argument-recording tests still exercise the Python child on Windows, but they do not characterize every Windows executable's command-line parsing rules.
Use the boundary checklist when reviewing a change
| Signal | Evidence to require |
|---|---|
| Argument construction changed | Exact child-array assertions for spaces and punctuation |
| Shell execution was introduced | A documented need for shell syntax and a separate safety review |
| Only simple inputs were tested | At least one whitespace case and one literal shell-expression case |
| The invoked program changed | Tests for that program's option and operand rules |
I would keep the sequence-based launch for passing user-supplied values to an executable. If a task genuinely needs shell operators, I would treat that as a different interface rather than silently extending this wrapper.
Preserved boundaries do not make every argument safe
An argument can remain perfectly intact and still be dangerous to the invoked program. A literal value beginning with --, for instance, might be interpreted as an option rather than a filename.
Literal does not mean harmless to the receiving program.
This fixture answers one narrow question: did the wrapper preserve the argument values? It does not establish that the executable is trusted, the options are authorized, or the requested operation is safe. Use an option terminator such as -- only where the target program documents support for it, and test those rules separately.
For a five-minute first check, connect preserved_arguments to your real launcher and run the dedicated space test, then the full suite. Target-program safety still needs separate tests.