summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--igor.py25
-rw-r--r--tests/test_execfile.py10
-rw-r--r--tests/test_html.py36
-rw-r--r--tests/test_phystokens.py4
-rw-r--r--tests/test_process.py16
5 files changed, 54 insertions, 37 deletions
diff --git a/igor.py b/igor.py
index 6c1c5d1..4df94cf 100644
--- a/igor.py
+++ b/igor.py
@@ -168,18 +168,19 @@ def do_check_eol():
checked.add(fname)
line = None
- for n, line in enumerate(open(fname, "rb"), start=1):
- if crlf:
- if "\r" in line:
- print("%s@%d: CR found" % (fname, n))
- return
- if trail_white:
- line = line[:-1]
- if not crlf:
- line = line.rstrip('\r')
- if line.rstrip() != line:
- print("%s@%d: trailing whitespace found" % (fname, n))
- return
+ with open(fname, "rb") as f:
+ for n, line in enumerate(f, start=1):
+ if crlf:
+ if "\r" in line:
+ print("%s@%d: CR found" % (fname, n))
+ return
+ if trail_white:
+ line = line[:-1]
+ if not crlf:
+ line = line.rstrip('\r')
+ if line.rstrip() != line:
+ print("%s@%d: trailing whitespace found" % (fname, n))
+ return
if line is not None and not line.strip():
print("%s: final blank line" % (fname,))
diff --git a/tests/test_execfile.py b/tests/test_execfile.py
index 3a92ff7..63fb919 100644
--- a/tests/test_execfile.py
+++ b/tests/test_execfile.py
@@ -66,7 +66,8 @@ class RunFileTest(CoverageTest):
a = 1
print("a is %r" % a)
#""")
- abrupt = open("abrupt.py").read()
+ with open("abrupt.py") as f:
+ abrupt = f.read()
self.assertEqual(abrupt[-1], '#')
run_python_file("abrupt.py", ["abrupt.py"])
self.assertEqual(self.stdout(), "a is 1\n")
@@ -113,10 +114,9 @@ class RunPycFileTest(CoverageTest):
pycfile = self.make_pyc()
# Jam Python 2.1 magic number into the .pyc file.
- fpyc = open(pycfile, "r+b")
- fpyc.seek(0)
- fpyc.write(binary_bytes([0x2a, 0xeb, 0x0d, 0x0a]))
- fpyc.close()
+ with open(pycfile, "r+b") as fpyc:
+ fpyc.seek(0)
+ fpyc.write(binary_bytes([0x2a, 0xeb, 0x0d, 0x0a]))
with self.assertRaisesRegex(NoCode, "Bad magic number in .pyc file"):
run_python_file(pycfile, [pycfile])
diff --git a/tests/test_html.py b/tests/test_html.py
index 8e43e7c..948a422 100644
--- a/tests/test_html.py
+++ b/tests/test_html.py
@@ -81,7 +81,8 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest):
# In this case, helper1 changes because its source is different.
self.create_initial_files()
self.run_coverage()
- index1 = open("htmlcov/index.html").read()
+ with open("htmlcov/index.html") as f:
+ index1 = f.read()
self.remove_html_files()
# Now change a file and do it again
@@ -98,7 +99,8 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest):
self.assert_exists("htmlcov/helper1.html")
self.assert_doesnt_exist("htmlcov/main_file.html")
self.assert_doesnt_exist("htmlcov/helper2.html")
- index2 = open("htmlcov/index.html").read()
+ with open("htmlcov/index.html") as f:
+ index2 = f.read()
self.assertMultiLineEqual(index1, index2)
def test_html_delta_from_coverage_change(self):
@@ -129,7 +131,8 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest):
# changed.
self.create_initial_files()
self.run_coverage(covargs=dict(omit=[]))
- index1 = open("htmlcov/index.html").read()
+ with open("htmlcov/index.html") as f:
+ index1 = f.read()
self.remove_html_files()
self.run_coverage(covargs=dict(omit=['xyzzy*']))
@@ -139,7 +142,8 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest):
self.assert_exists("htmlcov/helper1.html")
self.assert_exists("htmlcov/main_file.html")
self.assert_exists("htmlcov/helper2.html")
- index2 = open("htmlcov/index.html").read()
+ with open("htmlcov/index.html") as f:
+ index2 = f.read()
self.assertMultiLineEqual(index1, index2)
def test_html_delta_from_coverage_version_change(self):
@@ -148,7 +152,8 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest):
# changed.
self.create_initial_files()
self.run_coverage()
- index1 = open("htmlcov/index.html").read()
+ with open("htmlcov/index.html") as f:
+ index1 = f.read()
self.remove_html_files()
# "Upgrade" coverage.py!
@@ -161,7 +166,8 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest):
self.assert_exists("htmlcov/helper1.html")
self.assert_exists("htmlcov/main_file.html")
self.assert_exists("htmlcov/helper2.html")
- index2 = open("htmlcov/index.html").read()
+ with open("htmlcov/index.html") as f:
+ index2 = f.read()
fixed_index2 = index2.replace("XYZZY", self.real_coverage_version)
self.assertMultiLineEqual(index1, fixed_index2)
@@ -172,7 +178,8 @@ class HtmlTitleTest(HtmlTestHelpers, CoverageTest):
def test_default_title(self):
self.create_initial_files()
self.run_coverage()
- index = open("htmlcov/index.html").read()
+ with open("htmlcov/index.html") as f:
+ index = f.read()
self.assertIn("<title>Coverage report</title>", index)
self.assertIn("<h1>Coverage report:", index)
@@ -180,7 +187,8 @@ class HtmlTitleTest(HtmlTestHelpers, CoverageTest):
self.create_initial_files()
self.make_file(".coveragerc", "[html]\ntitle = Metrics & stuff!\n")
self.run_coverage()
- index = open("htmlcov/index.html").read()
+ with open("htmlcov/index.html") as f:
+ index = f.read()
self.assertIn("<title>Metrics &amp; stuff!</title>", index)
self.assertIn("<h1>Metrics &amp; stuff!:", index)
@@ -190,7 +198,8 @@ class HtmlTitleTest(HtmlTestHelpers, CoverageTest):
"[html]\ntitle = «ταБЬℓσ» numbers"
)
self.run_coverage()
- index = open("htmlcov/index.html").read()
+ with open("htmlcov/index.html") as f:
+ index = f.read()
self.assertIn(
"<title>&#171;&#964;&#945;&#1041;&#1068;&#8467;&#963;&#187;"
" numbers", index
@@ -204,7 +213,8 @@ class HtmlTitleTest(HtmlTestHelpers, CoverageTest):
self.create_initial_files()
self.make_file(".coveragerc", "[html]\ntitle = Good title\n")
self.run_coverage(htmlargs=dict(title="«ταБЬℓσ» & stüff!"))
- index = open("htmlcov/index.html").read()
+ with open("htmlcov/index.html") as f:
+ index = f.read()
self.assertIn(
"<title>&#171;&#964;&#945;&#1041;&#1068;&#8467;&#963;&#187;"
" &amp; st&#252;ff!</title>", index
@@ -339,7 +349,8 @@ class HtmlStaticFileTest(CoverageTest):
self.start_import_stop(cov, "main")
cov.html_report()
- jquery = open("htmlcov/jquery.min.js").read()
+ with open("htmlcov/jquery.min.js") as f:
+ jquery = f.read()
self.assertEqual(jquery, "Not Really JQuery!")
def test_copying_static_files_from_system_in_dir(self):
@@ -361,7 +372,8 @@ class HtmlStaticFileTest(CoverageTest):
for fpath in INSTALLED:
the_file = os.path.basename(fpath)
- contents = open(os.path.join("htmlcov", the_file)).read()
+ with open(os.path.join("htmlcov", the_file)) as f:
+ contents = f.read()
self.assertEqual(contents, "Not real.")
def test_cant_find_static_files(self):
diff --git a/tests/test_phystokens.py b/tests/test_phystokens.py
index 4755c16..c528955 100644
--- a/tests/test_phystokens.py
+++ b/tests/test_phystokens.py
@@ -40,7 +40,9 @@ class PhysTokensTest(CoverageTest):
def check_file_tokenization(self, fname):
"""Use the contents of `fname` for `check_tokenization`."""
- self.check_tokenization(open(fname).read())
+ with open(fname) as f:
+ source = f.read()
+ self.check_tokenization(source)
def test_simple(self):
self.assertEqual(list(source_token_lines(SIMPLE)),
diff --git a/tests/test_process.py b/tests/test_process.py
index 4669a62..ac5c6e1 100644
--- a/tests/test_process.py
+++ b/tests/test_process.py
@@ -319,7 +319,8 @@ class ProcessTest(CoverageTest):
def test_coverage_run_is_like_python(self):
tryfile = os.path.join(here, "try_execfile.py")
- self.make_file("run_me.py", open(tryfile).read())
+ with open(tryfile) as f:
+ self.make_file("run_me.py", f.read())
out_cov = self.run_command("coverage run run_me.py")
out_py = self.run_command("python run_me.py")
self.assertMultiLineEqual(out_cov, out_py)
@@ -334,7 +335,8 @@ class ProcessTest(CoverageTest):
# https://bitbucket.org/ned/coveragepy/issue/242
tryfile = os.path.join(here, "try_execfile.py")
self.make_file("sub/__init__.py", "")
- self.make_file("sub/run_me.py", open(tryfile).read())
+ with open(tryfile) as f:
+ self.make_file("sub/run_me.py", f.read())
out_cov = self.run_command("coverage run -m sub.run_me")
out_py = self.run_command("python -m sub.run_me")
self.assertMultiLineEqual(out_cov, out_py)
@@ -651,9 +653,8 @@ class ProcessStartupTest(CoverageTest):
""")
# sub.py will write a few lines.
self.make_file("sub.py", """\
- f = open("out.txt", "w")
- f.write("Hello, world!\\n")
- f.close()
+ with open("out.txt", "w") as f:
+ f.write("Hello, world!\\n")
""")
self.make_file("coverage.ini", """\
[run]
@@ -662,10 +663,11 @@ class ProcessStartupTest(CoverageTest):
self.set_environ("COVERAGE_PROCESS_START", "coverage.ini")
import main # pylint: disable=F0401,W0612
- self.assertEqual(open("out.txt").read(), "Hello, world!\n")
+ with open("out.txt") as f:
+ self.assertEqual(f.read(), "Hello, world!\n")
# Read the data from .coverage
self.assert_exists(".mycovdata")
data = coverage.CoverageData()
data.read_file(".mycovdata")
- self.assertEqual(data.summary()['sub.py'], 3)
+ self.assertEqual(data.summary()['sub.py'], 2)