Skip to content

Commit

Permalink
Consistently use sys.executable to run subprocesses
Browse files Browse the repository at this point in the history
In some scenarios, like running pytest outside of an active virtualenv,
it could happen that "coverage" would refer to a different Python
interpreter than the one running the tests. Admittedly that does not
happen in standard development scenarios where you use a virtualenv. But
it easily happens when packaging for Linux distributions that support
multiple versions of Python and it can also happen when running pytest
from a virtualenv without activating it. The latter is something that's
convenient when testing versus multiple Python versions without using
tox.

Explicitly invoking coverage as a module on sys.executable ensures that
the same binary, not process, that's also running the tests is used.
This was in fact already done in some tests but not consistently across
all of them.
  • Loading branch information
theMarix committed Jul 18, 2022
1 parent fac64ca commit b2ed091
Show file tree
Hide file tree
Showing 117 changed files with 336 additions and 156 deletions.
3 changes: 2 additions & 1 deletion tests/test_compat/test_option_get_help.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import subprocess
import sys

import typer.core
from typer.testing import CliRunner
Expand Down Expand Up @@ -40,7 +41,7 @@ def test_coverage_call():

def test_completion():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand Down
16 changes: 8 additions & 8 deletions tests/test_completion/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_install_completion():

def test_completion_invalid_instruction():
result = subprocess.run(
["coverage", "run", mod.__file__],
[sys.executable, "-m", "coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -63,7 +63,7 @@ def test_completion_invalid_instruction():

def test_completion_source_bash():
result = subprocess.run(
["coverage", "run", mod.__file__],
[sys.executable, "-m", "coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -81,7 +81,7 @@ def test_completion_source_bash():

def test_completion_source_invalid_shell():
result = subprocess.run(
["coverage", "run", mod.__file__],
[sys.executable, "-m", "coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -96,7 +96,7 @@ def test_completion_source_invalid_shell():

def test_completion_source_invalid_instruction():
result = subprocess.run(
["coverage", "run", mod.__file__],
[sys.executable, "-m", "coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -111,7 +111,7 @@ def test_completion_source_invalid_instruction():

def test_completion_source_zsh():
result = subprocess.run(
["coverage", "run", mod.__file__],
[sys.executable, "-m", "coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -126,7 +126,7 @@ def test_completion_source_zsh():

def test_completion_source_fish():
result = subprocess.run(
["coverage", "run", mod.__file__],
[sys.executable, "-m", "coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -141,7 +141,7 @@ def test_completion_source_fish():

def test_completion_source_powershell():
result = subprocess.run(
["coverage", "run", mod.__file__],
[sys.executable, "-m", "coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -159,7 +159,7 @@ def test_completion_source_powershell():

def test_completion_source_pwsh():
result = subprocess.run(
["coverage", "run", mod.__file__],
[sys.executable, "-m", "coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand Down
21 changes: 11 additions & 10 deletions tests/test_completion/test_completion_complete.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import subprocess
import sys

from docs_src.commands.help import tutorial001 as mod


def test_completion_complete_subcommand_bash():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -23,7 +24,7 @@ def test_completion_complete_subcommand_bash():

def test_completion_complete_subcommand_bash_invalid():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -40,7 +41,7 @@ def test_completion_complete_subcommand_bash_invalid():

def test_completion_complete_subcommand_zsh():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -59,7 +60,7 @@ def test_completion_complete_subcommand_zsh():

def test_completion_complete_subcommand_zsh_files():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -75,7 +76,7 @@ def test_completion_complete_subcommand_zsh_files():

def test_completion_complete_subcommand_fish():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -95,7 +96,7 @@ def test_completion_complete_subcommand_fish():

def test_completion_complete_subcommand_fish_should_complete():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -112,7 +113,7 @@ def test_completion_complete_subcommand_fish_should_complete():

def test_completion_complete_subcommand_fish_should_complete_no():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -129,7 +130,7 @@ def test_completion_complete_subcommand_fish_should_complete_no():

def test_completion_complete_subcommand_powershell():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -147,7 +148,7 @@ def test_completion_complete_subcommand_powershell():

def test_completion_complete_subcommand_pwsh():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -165,7 +166,7 @@ def test_completion_complete_subcommand_pwsh():

def test_completion_complete_subcommand_noshell():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand Down
9 changes: 5 additions & 4 deletions tests/test_completion/test_completion_complete_no_help.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import subprocess
import sys

from docs_src.commands.index import tutorial002 as mod


def test_completion_complete_subcommand_zsh():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -23,7 +24,7 @@ def test_completion_complete_subcommand_zsh():

def test_completion_complete_subcommand_fish():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -40,7 +41,7 @@ def test_completion_complete_subcommand_fish():

def test_completion_complete_subcommand_powershell():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -56,7 +57,7 @@ def test_completion_complete_subcommand_powershell():

def test_completion_complete_subcommand_pwsh():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand Down
33 changes: 29 additions & 4 deletions tests/test_completion/test_completion_install.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import subprocess
import sys
from pathlib import Path
from unittest import mock

Expand All @@ -16,7 +17,7 @@

def test_completion_install_no_shell():
result = subprocess.run(
["coverage", "run", mod.__file__, "--install-completion"],
[sys.executable, "-m", "coverage", "run", mod.__file__, "--install-completion"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -39,7 +40,15 @@ def test_completion_install_bash():
if bash_completion_path.is_file():
text = bash_completion_path.read_text()
result = subprocess.run(
["coverage", "run", mod.__file__, "--install-completion", "bash"],
[
sys.executable,
"-m",
"coverage",
"run",
mod.__file__,
"--install-completion",
"bash",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand Down Expand Up @@ -74,7 +83,15 @@ def test_completion_install_zsh():
if completion_path.is_file():
text = completion_path.read_text()
result = subprocess.run(
["coverage", "run", mod.__file__, "--install-completion", "zsh"],
[
sys.executable,
"-m",
"coverage",
"run",
mod.__file__,
"--install-completion",
"zsh",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand Down Expand Up @@ -103,7 +120,15 @@ def test_completion_install_fish():
Path.home() / f".config/fish/completions/{script_path.name}.fish"
)
result = subprocess.run(
["coverage", "run", mod.__file__, "--install-completion", "fish"],
[
sys.executable,
"-m",
"coverage",
"run",
mod.__file__,
"--install-completion",
"fish",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand Down
53 changes: 47 additions & 6 deletions tests/test_completion/test_completion_show.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import subprocess
import sys

from docs_src.first_steps import tutorial001 as mod


def test_completion_show_no_shell():
result = subprocess.run(
["coverage", "run", mod.__file__, "--show-completion"],
[sys.executable, "-m", "coverage", "run", mod.__file__, "--show-completion"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -25,7 +26,15 @@ def test_completion_show_no_shell():

def test_completion_show_bash():
result = subprocess.run(
["coverage", "run", mod.__file__, "--show-completion", "bash"],
[
sys.executable,
"-m",
"coverage",
"run",
mod.__file__,
"--show-completion",
"bash",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -43,7 +52,15 @@ def test_completion_show_bash():

def test_completion_source_zsh():
result = subprocess.run(
["coverage", "run", mod.__file__, "--show-completion", "zsh"],
[
sys.executable,
"-m",
"coverage",
"run",
mod.__file__,
"--show-completion",
"zsh",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -58,7 +75,15 @@ def test_completion_source_zsh():

def test_completion_source_fish():
result = subprocess.run(
["coverage", "run", mod.__file__, "--show-completion", "fish"],
[
sys.executable,
"-m",
"coverage",
"run",
mod.__file__,
"--show-completion",
"fish",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -73,7 +98,15 @@ def test_completion_source_fish():

def test_completion_source_powershell():
result = subprocess.run(
["coverage", "run", mod.__file__, "--show-completion", "powershell"],
[
sys.executable,
"-m",
"coverage",
"run",
mod.__file__,
"--show-completion",
"powershell",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand All @@ -91,7 +124,15 @@ def test_completion_source_powershell():

def test_completion_source_pwsh():
result = subprocess.run(
["coverage", "run", mod.__file__, "--show-completion", "pwsh"],
[
sys.executable,
"-m",
"coverage",
"run",
mod.__file__,
"--show-completion",
"pwsh",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
Expand Down
Loading

0 comments on commit b2ed091

Please sign in to comment.