summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/coveragetest.py43
-rw-r--r--test/test_api.py22
-rw-r--r--test/test_config.py89
-rw-r--r--test/test_farm.py2
4 files changed, 147 insertions, 9 deletions
diff --git a/test/coveragetest.py b/test/coveragetest.py
index 4471392f..fb6a5bcc 100644
--- a/test/coveragetest.py
+++ b/test/coveragetest.py
@@ -38,9 +38,7 @@ class CoverageTest(TestCase):
self.old_dir = os.getcwd()
os.chdir(self.temp_dir)
- # Preserve changes to PYTHONPATH.
- self.old_pypath = os.environ.get('PYTHONPATH', '')
-
+
# Modules should be importable from this temp directory.
self.old_syspath = sys.path[:]
sys.path.insert(0, '')
@@ -48,6 +46,9 @@ class CoverageTest(TestCase):
# Keep a counter to make every call to check_coverage unique.
self.n = 0
+ # Record environment variables that we changed with set_environ.
+ self.environ_undos = {}
+
# Use a Tee to capture stdout.
self.old_stdout = sys.stdout
self.captured_stdout = StringIO()
@@ -55,17 +56,45 @@ class CoverageTest(TestCase):
def tearDown(self):
if self.run_in_temp_dir:
- # Restore the original sys.path and PYTHONPATH
+ # Restore the original sys.path.
sys.path = self.old_syspath
- os.environ['PYTHONPATH'] = self.old_pypath
# Get rid of the temporary directory.
os.chdir(self.old_dir)
shutil.rmtree(self.temp_root)
+ # Restore the environment.
+ self.undo_environ()
+
# Restore stdout.
sys.stdout = self.old_stdout
+ def set_environ(self, name, value):
+ """Set an environment variable `name` to be `value`.
+
+ The environment variable is set, and record is kept that it was set,
+ so that `tearDown` can restore its original value.
+
+ """
+ if name not in self.environ_undos:
+ self.environ_undos[name] = os.environ.get(name)
+ os.environ[name] = value
+
+ def original_environ(self, name):
+ """The environment variable `name` from when the test started."""
+ if name in self.environ_undos:
+ return self.environ_undos[name]
+ else:
+ return os.environ[name]
+
+ def undo_environ(self):
+ """Undo all the changes made by `set_environ`."""
+ for name, value in self.environ_undos.items():
+ if value is None:
+ del os.environ[name]
+ else:
+ os.environ[name] = value
+
def stdout(self):
"""Return the data written to stdout during the test."""
return self.captured_stdout.getvalue()
@@ -254,11 +283,11 @@ class CoverageTest(TestCase):
here = os.path.dirname(self.nice_file(coverage.__file__, ".."))
testmods = self.nice_file(here, 'test/modules')
zipfile = self.nice_file(here, 'test/zipmods.zip')
- pypath = self.old_pypath
+ pypath = self.original_environ('PYTHONPATH')
if pypath:
pypath += os.pathsep
pypath += testmods + os.pathsep + zipfile
- os.environ['PYTHONPATH'] = pypath
+ self.set_environ('PYTHONPATH', pypath)
_, output = run_command(cmd)
print(output)
diff --git a/test/test_api.py b/test/test_api.py
index 2552d114..0df2df4a 100644
--- a/test/test_api.py
+++ b/test/test_api.py
@@ -164,7 +164,7 @@ class ApiTest(CoverageTest):
# Measure without the stdlib.
cov1 = coverage.coverage()
- self.assertEqual(cov1.cover_pylib, False)
+ self.assertEqual(cov1.config.cover_pylib, False)
cov1.start()
self.import_module("mymain")
cov1.stop()
@@ -246,6 +246,26 @@ class ApiTest(CoverageTest):
self.assertSameElements(os.listdir("."),
["datatest3.py", "datatest3.pyc", "cov.data.14"])
+ def testDatafileFromRcFile(self):
+ # You can specify the data file name in the .coveragerc file
+ self.make_file("datatest4.py", """\
+ fooey = 17
+ """)
+ self.make_file(".coveragerc", """\
+ [run]
+ data_file = mydata.dat
+ """)
+
+ self.assertSameElements(os.listdir("."),
+ ["datatest4.py", ".coveragerc"])
+ cov = coverage.coverage()
+ cov.start()
+ self.import_module("datatest4")
+ cov.stop()
+ cov.save()
+ self.assertSameElements(os.listdir("."),
+ ["datatest4.py", "datatest4.pyc", ".coveragerc", "mydata.dat"])
+
def testEmptyReporting(self):
# Used to be you'd get an exception reporting on nothing...
cov = coverage.coverage()
diff --git a/test/test_config.py b/test/test_config.py
new file mode 100644
index 00000000..a1abf51b
--- /dev/null
+++ b/test/test_config.py
@@ -0,0 +1,89 @@
+"""Test the config file handling for coverage.py"""
+
+import os, sys
+import coverage
+
+sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
+from coveragetest import CoverageTest
+
+
+class ConfigTest(CoverageTest):
+ """Tests of the config file support."""
+
+ def test_default_config(self):
+ # Just constructing a coverage() object gets the right defaults.
+ cov = coverage.coverage()
+ self.assertFalse(cov.config.timid)
+ self.assertFalse(cov.config.branch)
+ self.assertEqual(cov.config.data_file, ".coverage")
+
+ def test_arguments(self):
+ # Arguments to the constructor are applied to the configuation.
+ cov = coverage.coverage(timid=True, data_file="fooey.dat")
+ self.assert_(cov.config.timid)
+ self.assertFalse(cov.config.branch)
+ self.assertEqual(cov.config.data_file, "fooey.dat")
+
+ def test_config_file(self):
+ # A .coveragerc file will be read into the configuration.
+ self.make_file(".coveragerc", """\
+ # This is just a bogus .rc file for testing.
+ [run]
+ timid = True
+ data_file = .hello_kitty.data
+ """)
+ cov = coverage.coverage()
+ self.assert_(cov.config.timid)
+ self.assertFalse(cov.config.branch)
+ self.assertEqual(cov.config.data_file, ".hello_kitty.data")
+
+ def test_named_config_file(self):
+ # You can name the config file what you like.
+ self.make_file("my_cov.ini", """\
+ [run]
+ timid = True
+ ; I wouldn't really use this as a data file...
+ data_file = delete.me
+ """)
+ cov = coverage.coverage(config_file="my_cov.ini")
+ self.assert_(cov.config.timid)
+ self.assertFalse(cov.config.branch)
+ self.assertEqual(cov.config.data_file, "delete.me")
+
+ def test_ignored_config_file(self):
+ # You can disable reading the .coveragerc file.
+ self.make_file(".coveragerc", """\
+ [run]
+ timid = True
+ data_file = delete.me
+ """)
+ cov = coverage.coverage(config_file=False)
+ self.assertFalse(cov.config.timid)
+ self.assertFalse(cov.config.branch)
+ self.assertEqual(cov.config.data_file, ".coverage")
+
+ def test_config_file_then_args(self):
+ # The arguments override the .coveragerc file.
+ self.make_file(".coveragerc", """\
+ [run]
+ timid = True
+ data_file = weirdo.file
+ """)
+ cov = coverage.coverage(timid=False, data_file=".mycov")
+ self.assertFalse(cov.config.timid)
+ self.assertFalse(cov.config.branch)
+ self.assertEqual(cov.config.data_file, ".mycov")
+
+ def test_data_file_from_environment(self):
+ # There's an environment variable for the data_file.
+ self.make_file(".coveragerc", """\
+ [run]
+ timid = True
+ data_file = weirdo.file
+ """)
+ self.set_environ("COVERAGE_FILE", "fromenv.dat")
+ cov = coverage.coverage()
+ self.assertEqual(cov.config.data_file, "fromenv.dat")
+ # But the constructor args override the env var.
+ cov = coverage.coverage(data_file="fromarg.dat")
+ self.assertEqual(cov.config.data_file, "fromarg.dat")
diff --git a/test/test_farm.py b/test/test_farm.py
index de07541b..8f7d5712 100644
--- a/test/test_farm.py
+++ b/test/test_farm.py
@@ -3,7 +3,7 @@
import difflib, filecmp, fnmatch, glob, os, re, shutil, sys
sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
-from backtest import run_command, execfile # pylint: disable-msg=W0622
+from backtest import run_command, execfile # pylint: disable-msg=W0622
def test_farm(clean_only=False):