Skip to content

Commit

Permalink
Merge pull request #365 from stephenfin/issue-364
Browse files Browse the repository at this point in the history
Normalize paths
  • Loading branch information
mtreinish authored Nov 23, 2024
2 parents 1dd16b3 + 8659f2a commit 7951fa1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 25 deletions.
25 changes: 17 additions & 8 deletions stestr/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import functools
import io
import os
import os.path
import subprocess
import sys

Expand Down Expand Up @@ -521,11 +522,15 @@ def run_command(

if no_discover:
ids = no_discover
klass = None
if "::" in ids:
ids = ids.replace("::", ".")
if ids.find("/") != -1:
root = ids.replace(".py", "")
ids = root.replace("/", ".")
ids, klass = ids.split("::", 1)
klass = ".".join(klass.split("::"))
if ids.find(os.path.sep) != -1:
root, _ = os.path.splitext(os.path.normpath(ids))
ids = ".".join(root.split(os.path.sep))
if klass:
ids = f"{ids}.{klass}"
stestr_python = sys.executable
if os.environ.get("PYTHON"):
python_bin = os.environ.get("PYTHON")
Expand Down Expand Up @@ -584,11 +589,15 @@ def run_tests():

if pdb:
ids = pdb
klass = None
if "::" in ids:
ids = ids.replace("::", ".")
if ids.find("/") != -1:
root = ids.replace(".py", "")
ids = root.replace("/", ".")
ids, klass = ids.split("::", 1)
klass = ".".join(klass.split("::"))
if ids.find(os.path.sep) != -1:
root, _ = os.path.splitext(os.path.normpath(ids))
ids = ".".join(root.split(os.path.sep))
if klass:
ids = f"{ids}.{klass}"
runner = subunit_run.SubunitTestRunner
stream = io.BytesIO()
program.TestProgram(
Expand Down
50 changes: 41 additions & 9 deletions stestr/tests/test_return_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,17 @@ def assertRunExit(self, cmd, expected, subunit=False, stdin=None):

if not subunit:
self.assertEqual(
p.returncode, expected, "Stdout: {}; Stderr: {}".format(out, err)
p.returncode,
expected,
"Command: {}; Stdout: {}; Stderr: {}".format(cmd, out, err),
)
return (out, err)
else:
self.assertEqual(
p.returncode,
expected,
"Expected return code: %s doesn't match actual "
"return code of: %s" % (expected, p.returncode),
"Expected return code: {} doesn't match actual "
"return code of: {}. Command: {}".format(expected, p.returncode, cmd),
)
output_stream = io.BytesIO(out)
stream = subunit_lib.ByteStreamToStreamResult(output_stream)
Expand Down Expand Up @@ -471,30 +473,60 @@ def test_list_from_func(self):
self.assertEqual(0, list_cmd.list_command(stdout=stdout.stream))

def test_run_no_discover_pytest_path(self):
passing_string = "tests/test_passing.py::FakeTestClass::test_pass_list"
passing_string = "::".join(
[
os.path.join("tests", "test_passing.py"),
"FakeTestClass",
"test_pass_list",
]
)
out, err = self.assertRunExit("stestr run -n %s" % passing_string, 0)
lines = out.decode("utf8").splitlines()
self.assertIn(" - Passed: 1", lines)
self.assertIn(" - Failed: 0", lines)

def test_run_no_discover_pytest_path_failing(self):
passing_string = "tests/test_failing.py::FakeTestClass::test_pass_list"
out, err = self.assertRunExit("stestr run -n %s" % passing_string, 1)
failing_string = "::".join(
[
os.path.join("tests", "test_failing.py"),
"FakeTestClass",
"test_pass_list",
]
)
out, err = self.assertRunExit("stestr run -n %s" % failing_string, 1)
lines = out.decode("utf8").splitlines()
self.assertIn(" - Passed: 0", lines)
self.assertIn(" - Failed: 1", lines)

def test_run_no_discover_file_path(self):
passing_string = "tests/test_passing.py"
passing_string = os.path.join("tests", "test_passing.py")
out, err = self.assertRunExit("stestr run -n %s" % passing_string, 0)
lines = out.decode("utf8").splitlines()
self.assertIn(" - Passed: 2", lines)
self.assertIn(" - Failed: 0", lines)
self.assertIn(" - Expected Fail: 1", lines)

def test_run_no_discover_file_path_failing(self):
passing_string = "tests/test_failing.py"
out, err = self.assertRunExit("stestr run -n %s" % passing_string, 1)
failing_string = os.path.join("tests", "test_failing.py")
out, err = self.assertRunExit("stestr run -n %s" % failing_string, 1)
lines = out.decode("utf8").splitlines()
self.assertIn(" - Passed: 0", lines)
self.assertIn(" - Failed: 2", lines)
self.assertIn(" - Unexpected Success: 1", lines)

def test_run_no_discover_unnormalized_file_path(self):
# we don't use os.path.join since we want an unnormalized path
passing_string = f"tests{os.path.sep}{os.path.sep}test_passing.py"
out, err = self.assertRunExit("stestr run -n %s" % passing_string, 0)
lines = out.decode("utf8").splitlines()
self.assertIn(" - Passed: 2", lines)
self.assertIn(" - Failed: 0", lines)
self.assertIn(" - Expected Fail: 1", lines)

def test_run_no_discover_unnormalized_file_path_failing(self):
# we don't use os.path.join since we want an unnormalized path
failing_string = f"tests{os.path.sep}{os.path.sep}test_failing.py"
out, err = self.assertRunExit("stestr run -n %s" % failing_string, 1)
lines = out.decode("utf8").splitlines()
self.assertIn(" - Passed: 0", lines)
self.assertIn(" - Failed: 2", lines)
Expand Down
9 changes: 1 addition & 8 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
[tox]
minversion = 1.6
minversion = 3.18.0
envlist = py312,py311,py310,py39,py38,pep8
skipsdist = True

[testenv]
usedevelop = True
install_command = pip install -U --force-reinstall {opts} {packages}
setenv = VIRTUAL_ENV={envdir}
allowlist_externals =
find
stestr
Expand All @@ -23,18 +21,14 @@ commands =
black --check {posargs} stestr tools doc setup.py

[testenv:black]
envdir = .tox/pep8
commands =
black {posargs} stestr tools doc setup.py



[testenv:venv]
commands = {posargs}

[testenv:cover]
setenv =
VIRTUAL_ENV={envdir}
PYTHON=coverage run --source stestr
commands =
coverage run stestr/cli.py run {posargs}
Expand All @@ -43,7 +37,6 @@ commands =

[testenv:coverxml]
setenv =
VIRTUAL_ENV={envdir}
PYTHON=coverage run --source stestr
commands =
coverage run stestr/cli.py run {posargs}
Expand Down

0 comments on commit 7951fa1

Please sign in to comment.