|
15 | 15 | __all__ = ()
|
16 | 16 |
|
17 | 17 |
|
| 18 | +class Pep257Env(): |
| 19 | + |
| 20 | + """An isolated environment where pep257.py can be run. |
| 21 | +
|
| 22 | + Since running pep257.py as a script is affected by local config files, it's |
| 23 | + important that tests will run in an isolated environment. This class should |
| 24 | + be used as a context manager and offers utility methods for adding files |
| 25 | + to the environment and changing the environment's configuration. |
| 26 | +
|
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + self.tempdir = None |
| 31 | + |
| 32 | + def write_config(self, ignore, verbose): |
| 33 | + """Change the environment's config file.""" |
| 34 | + with open(os.path.join(self.tempdir, 'tox.ini'), 'wt') as conf: |
| 35 | + conf.write(textwrap.dedent("""\ |
| 36 | + [pep257] |
| 37 | + ignore = {0} |
| 38 | + verbose = {1} |
| 39 | + """.format(ignore, verbose))) |
| 40 | + |
| 41 | + def open(self, path, *args, **kwargs): |
| 42 | + """Open a file in the environment. |
| 43 | +
|
| 44 | + The file path should be relative to the base of the environment. |
| 45 | +
|
| 46 | + """ |
| 47 | + return open(os.path.join(self.tempdir, path), *args, **kwargs) |
| 48 | + |
| 49 | + def invoke_pep257(self, args=""): |
| 50 | + """Run pep257.py on the environment base folder with the given args.""" |
| 51 | + pep257_location = os.path.join(os.path.dirname(__file__), 'pep257.py') |
| 52 | + cmd = shlex.split("python {0} {1} {2}" |
| 53 | + .format(pep257_location, self.tempdir, args)) |
| 54 | + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 55 | + stderr=subprocess.PIPE) |
| 56 | + out, err = p.communicate() |
| 57 | + return out.decode('utf-8'), err.decode('utf-8') |
| 58 | + |
| 59 | + def __enter__(self): |
| 60 | + self.tempdir = tempfile.mkdtemp() |
| 61 | + return self |
| 62 | + |
| 63 | + def __exit__(self, *args, **kwargs): |
| 64 | + shutil.rmtree(self.tempdir) |
| 65 | + |
| 66 | + |
18 | 67 | def test_pep257_conformance():
|
19 | 68 | errors = list(pep257.check(['pep257.py', 'test_pep257.py']))
|
20 | 69 | print(errors)
|
@@ -48,54 +97,46 @@ def test_config_file():
|
48 | 97 | that pep257 gives the correct output.
|
49 | 98 |
|
50 | 99 | """
|
51 |
| - tempdir = tempfile.mkdtemp() |
52 |
| - |
53 |
| - def write_config(ignore, verbose): |
54 |
| - with open(os.path.join(tempdir, 'tox.ini'), 'wt') as conf: |
55 |
| - conf.write(textwrap.dedent("""\ |
56 |
| - [pep257] |
57 |
| - ignore = {0} |
58 |
| - verbose = {1} |
59 |
| - """.format(ignore, verbose))) |
60 |
| - |
61 |
| - def invoke_pep257(): |
62 |
| - pep257_location = os.path.join(os.path.dirname(__file__), 'pep257.py') |
63 |
| - cmd = shlex.split("python {0} {1}".format(pep257_location, tempdir)) |
64 |
| - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
65 |
| - stderr=subprocess.PIPE) |
66 |
| - out, err = p.communicate() |
67 |
| - return out.decode('utf-8'), err.decode('utf-8') |
68 |
| - |
69 |
| - try: |
70 |
| - with open(os.path.join(tempdir, 'example.py'), 'wt') as example: |
| 100 | + with Pep257Env() as env: |
| 101 | + with env.open('example.py', 'wt') as example: |
71 | 102 | example.write(textwrap.dedent("""\
|
72 | 103 | def foo():
|
73 | 104 | pass
|
74 | 105 | """))
|
75 | 106 |
|
76 |
| - write_config(ignore='D100', verbose=True) |
77 |
| - out, err = invoke_pep257() |
| 107 | + env.write_config(ignore='D100', verbose=True) |
| 108 | + out, err = env.invoke_pep257() |
78 | 109 | assert 'D100' not in err
|
79 | 110 | assert 'D103' in err
|
80 | 111 | assert 'example.py' in out
|
81 | 112 |
|
82 |
| - write_config(ignore='', verbose=True) |
83 |
| - out, err = invoke_pep257() |
| 113 | + env.write_config(ignore='', verbose=True) |
| 114 | + out, err = env.invoke_pep257() |
84 | 115 | assert 'D100' in err
|
85 | 116 | assert 'D103' in err
|
86 | 117 | assert 'example.py' in out
|
87 | 118 |
|
88 |
| - write_config(ignore='D100,D103', verbose=False) |
89 |
| - out, err = invoke_pep257() |
| 119 | + env.write_config(ignore='D100,D103', verbose=False) |
| 120 | + out, err = env.invoke_pep257() |
90 | 121 | assert 'D100' not in err
|
91 | 122 | assert 'D103' not in err
|
92 | 123 | assert 'example.py' not in out
|
93 | 124 |
|
94 |
| - write_config(ignore='', verbose=False) |
95 |
| - out, err = invoke_pep257() |
| 125 | + env.write_config(ignore='', verbose=False) |
| 126 | + out, err = env.invoke_pep257() |
96 | 127 | assert 'D100' in err
|
97 | 128 | assert 'D103' in err
|
98 | 129 | assert 'example.py' not in out
|
99 | 130 |
|
100 |
| - finally: |
101 |
| - shutil.rmtree(tempdir) |
| 131 | + |
| 132 | +def test_count(): |
| 133 | + """Test that passing --count to pep257 correctly prints the error num.""" |
| 134 | + with Pep257Env() as env: |
| 135 | + with env.open('example.py', 'wt') as example: |
| 136 | + example.write(textwrap.dedent("""\ |
| 137 | + def foo(): |
| 138 | + pass |
| 139 | + """)) |
| 140 | + |
| 141 | + out, err = env.invoke_pep257(args='--count') |
| 142 | + assert out == '2\n' |
0 commit comments