Ensure we reset sys.stdout before/after redirection; add test runs.