Skip to content

Commit

Permalink
Fix test and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian committed May 1, 2024
1 parent f09b48a commit 2928d3f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[run]
cover_pylib = True
source = coredumpy
source_pkgs = coredumpy
8 changes: 1 addition & 7 deletions src/coredumpy/coredumpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ def __init__(self, target):
if not getattr(sys.flags, "safe_path", None):
sys.path[0] = os.path.dirname(self._target)

def __repr__(self):
return self._target

@property
def filename(self):
return self._target
Expand Down Expand Up @@ -80,14 +77,11 @@ def __init__(self, target):
except ImportError as e:
print(f"ImportError: {e}")
sys.exit(1)
except Exception:
except Exception: # pragma: no cover
import traceback
traceback.print_exc()
sys.exit(1)

def __repr__(self):
return self._target

@property
def filename(self):
return self._code.co_filename
Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/gaogaotiantian/coredumpy/blob/master/NOTICE.txt
2 changes: 2 additions & 0 deletions tests/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/gaogaotiantian/coredumpy/blob/master/NOTICE.txt
16 changes: 16 additions & 0 deletions tests/data/failed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/gaogaotiantian/coredumpy/blob/master/NOTICE.txt


import unittest


class TestUnittest(unittest.TestCase):
def test_bool(self):
self.assertTrue(False)
def test_eq(self):
self.assertEqual(1, 2)
def test_pass(self):
self.assertEqual(1, 1)
def test_error(self):
raise ValueError()
3 changes: 3 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def f(x):
self.assertIn("142857", stdout)

def test_cli_invalid(self):
stdout, _ = self.run_run([])
self.assertIn("Error", stdout)

stdout, _ = self.run_run(["notexist.py"])
self.assertIn("Error", stdout)

Expand Down
27 changes: 4 additions & 23 deletions tests/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tempfile
import textwrap

import unittest

from .base import TestBase

Expand Down Expand Up @@ -38,31 +39,11 @@ def test_error(self):

def test_unittest_with_cli(self):
with tempfile.TemporaryDirectory() as tempdir:
script = textwrap.dedent("""
import unittest
class TestUnittest(unittest.TestCase):
def test_bool(self):
self.assertTrue(False)
def test_eq(self):
self.assertEqual(1, 2)
def test_pass(self):
self.assertEqual(1, 1)
def test_error(self):
raise ValueError()
""")
with open(f"{tempdir}/script.py", "w") as f:
f.write(script)

try:
curdir = os.getcwd()
os.chdir(tempdir)
stdout, stderr = self.run_run(["-m", "unittest", "script",
"--directory", os.path.join(tempdir, "dump")])
finally:
os.chdir(curdir)
stdout, stderr = self.run_run(["-m", "unittest", "tests.data.failed",
"--directory", tempdir])
self.assertIn("FAIL: test_bool", stderr)
self.assertIn("FAIL: test_eq", stderr)
self.assertIn("ERROR: test_error", stderr)
self.assertNotIn("test_pass", stderr)
self.assertEqual(stdout.count(tempdir), 3)
self.assertEqual(len(os.listdir(os.path.join(tempdir, "dump"))), 3)
self.assertEqual(len(os.listdir(tempdir)), 3)
4 changes: 2 additions & 2 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def normalize_commands(commands):
if os.getenv("COVERAGE_RUN"):
if commands[0] == "python" or commands[0] == sys.executable:
commands = ["coverage", "run", "--parallel-mode"] + commands[1:]
commands = [sys.executable, "-m", "coverage", "run", "--parallel-mode"] + commands[1:]
elif commands[0] == "coredumpy":
commands = ["coverage", "run", "--parallel-mode", "-m", "coredumpy"] + commands[1:]
commands = [sys.executable, "-m", "coverage", "run", "--parallel-mode", "-m", "coredumpy"] + commands[1:]
return commands

0 comments on commit 2928d3f

Please sign in to comment.