|
1 | 1 | import sys |
2 | 2 | import io |
3 | 3 | import types |
4 | | -import unittest |
5 | | -import unittest.mock |
| 4 | +from unittest import mock |
6 | 5 | import tldr |
7 | 6 | import pytest |
8 | 7 |
|
9 | 8 |
|
10 | | -class TLDRTests(unittest.TestCase): |
11 | | - def test_whole_page(self): |
12 | | - with open("tests/data/gem.md", "rb") as f_original: |
13 | | - with open("tests/data/gem_rendered", "rb") as f_rendered: |
14 | | - old_stdout = sys.stdout |
15 | | - sys.stdout = io.StringIO() |
16 | | - sys.stdout.buffer = types.SimpleNamespace() |
17 | | - sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8")) |
18 | | - tldr.output(f_original) |
| 9 | +def test_whole_page(): |
| 10 | + with open("tests/data/gem.md", "rb") as f_original: |
| 11 | + with open("tests/data/gem_rendered", "rb") as f_rendered: |
| 12 | + old_stdout = sys.stdout |
| 13 | + sys.stdout = io.StringIO() |
| 14 | + sys.stdout.buffer = types.SimpleNamespace() |
| 15 | + sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8")) |
| 16 | + tldr.output(f_original) |
19 | 17 |
|
20 | | - sys.stdout.seek(0) |
21 | | - tldr_output = sys.stdout.read().encode("utf-8") |
22 | | - sys.stdout = old_stdout |
| 18 | + sys.stdout.seek(0) |
| 19 | + tldr_output = sys.stdout.read().encode("utf-8") |
| 20 | + sys.stdout = old_stdout |
23 | 21 |
|
24 | | - correct_output = f_rendered.read() |
25 | | - self.assertEqual(tldr_output, correct_output) |
| 22 | + correct_output = f_rendered.read() |
| 23 | + assert tldr_output == correct_output |
26 | 24 |
|
27 | | - def test_error_message(self): |
28 | | - with unittest.mock.patch("sys.argv", ["tldr", "73eb6f19cd6f"]): |
29 | | - with pytest.raises(SystemExit) as pytest_wrapped_e: |
30 | | - tldr.main() |
31 | | - correct_output = "`73eb6f19cd6f` documentation is not available. Consider contributing Pull Request to https://github.com/tldr-pages/tldr" # noqa |
32 | | - print("Test {}".format(pytest_wrapped_e)) |
33 | | - assert pytest_wrapped_e.type == SystemExit |
34 | | - assert str(pytest_wrapped_e.value) == correct_output |
| 25 | + |
| 26 | +def test_error_message(): |
| 27 | + with mock.patch("sys.argv", ["tldr", "73eb6f19cd6f"]): |
| 28 | + with pytest.raises(SystemExit) as pytest_wrapped_e: |
| 29 | + tldr.main() |
| 30 | + correct_output = "`73eb6f19cd6f` documentation is not available. Consider contributing Pull Request to https://github.com/tldr-pages/tldr" # noqa |
| 31 | + print("Test {}".format(pytest_wrapped_e)) |
| 32 | + assert pytest_wrapped_e.type == SystemExit |
| 33 | + assert str(pytest_wrapped_e.value) == correct_output |
0 commit comments