forked from nedbat/coveragepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mixins.py
80 lines (63 loc) · 2.83 KB
/
test_mixins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
"""Tests of code in tests/mixins.py"""
import pytest
from coverage.misc import import_local_file
from tests.mixins import TempDirMixin, RestoreModulesMixin
class TempDirMixinTest(TempDirMixin):
"""Test the methods in TempDirMixin."""
def file_text(self, fname):
"""Return the text read from a file."""
with open(fname, "rb") as f:
return f.read().decode('ascii')
def test_make_file(self):
# A simple file.
self.make_file("fooey.boo", "Hello there")
assert self.file_text("fooey.boo") == "Hello there"
# A file in a sub-directory
self.make_file("sub/another.txt", "Another")
assert self.file_text("sub/another.txt") == "Another"
# A second file in that sub-directory
self.make_file("sub/second.txt", "Second")
assert self.file_text("sub/second.txt") == "Second"
# A deeper directory
self.make_file("sub/deeper/evenmore/third.txt")
assert self.file_text("sub/deeper/evenmore/third.txt") == ""
# Dedenting
self.make_file("dedented.txt", """\
Hello
Bye
""")
assert self.file_text("dedented.txt") == "Hello\nBye\n"
def test_make_file_newline(self):
self.make_file("unix.txt", "Hello\n")
assert self.file_text("unix.txt") == "Hello\n"
self.make_file("dos.txt", "Hello\n", newline="\r\n")
assert self.file_text("dos.txt") == "Hello\r\n"
self.make_file("mac.txt", "Hello\n", newline="\r")
assert self.file_text("mac.txt") == "Hello\r"
def test_make_file_non_ascii(self):
self.make_file("unicode.txt", "tablo: «ταБℓσ»")
with open("unicode.txt", "rb") as f:
text = f.read()
assert text == b"tablo: \xc2\xab\xcf\x84\xce\xb1\xd0\x91\xe2\x84\x93\xcf\x83\xc2\xbb"
def test_make_bytes_file(self):
self.make_file("binary.dat", bytes=b"\x99\x33\x66hello\0")
with open("binary.dat", "rb") as f:
data = f.read()
assert data == b"\x99\x33\x66hello\0"
class RestoreModulessMixinTest(TempDirMixin, RestoreModulesMixin):
"""Tests of SysPathModulesMixin."""
@pytest.mark.parametrize("val", [17, 42])
def test_module_independence(self, val):
self.make_file("xyzzy.py", f"A = {val}")
import xyzzy # pylint: disable=import-error
assert xyzzy.A == val
def test_cleanup_and_reimport(self):
self.make_file("xyzzy.py", "A = 17")
xyzzy = import_local_file("xyzzy")
assert xyzzy.A == 17
self.clean_local_file_imports()
self.make_file("xyzzy.py", "A = 42")
xyzzy = import_local_file("xyzzy")
assert xyzzy.A == 42