-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathtest_tldr.py
More file actions
178 lines (136 loc) 路 5.78 KB
/
Copy pathtest_tldr.py
File metadata and controls
178 lines (136 loc) 路 5.78 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import io
from pathlib import Path
import pytest
import sys
import tldr
import types
from unittest import mock
# gem is a basic test of page rendering
# jq is a more complicated test for token parsing
page_names = ('gem', 'jq')
@pytest.mark.parametrize("page_name", page_names)
def test_whole_page(page_name, monkeypatch):
monkeypatch.setenv("FORCE_COLOR", "1")
with open(f"tests/data/{page_name}.md", "rb") as f_original:
with open(f"tests/data/{page_name}_rendered", "rb") as f_rendered:
old_stdout = sys.stdout
sys.stdout = io.StringIO()
sys.stdout.buffer = types.SimpleNamespace()
sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8"))
tldr.output(f_original, "both")
sys.stdout.seek(0)
tldr_output = sys.stdout.read().encode("utf-8")
sys.stdout = old_stdout
correct_output = f_rendered.read()
assert tldr_output == correct_output
@pytest.mark.parametrize("page_name", page_names)
def test_markdown_mode(page_name):
with open(f"tests/data/{page_name}.md", "rb") as f_original:
d_original = f_original.read()
old_stdout = sys.stdout
sys.stdout = io.StringIO()
sys.stdout.buffer = types.SimpleNamespace()
sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8"))
tldr.output(d_original.splitlines(), "both", plain=True)
sys.stdout.seek(0)
tldr_output = sys.stdout.read().encode("utf-8")
sys.stdout = old_stdout
# tldr adds a trailing newline
assert tldr_output == d_original + b"\n"
def test_error_message():
with mock.patch("sys.argv", ["tldr", "73eb6f19cd6f"]):
with pytest.raises(SystemExit) as pytest_wrapped_e:
tldr.main()
correct_output = "`73eb6f19cd6f` documentation is not available.\nIf you want to contribute it, feel free to send a pull request to: https://github.com/tldr-pages/tldr" # noqa
print("Test {}".format(pytest_wrapped_e))
assert pytest_wrapped_e.type == SystemExit
assert str(pytest_wrapped_e.value) == correct_output
@pytest.mark.parametrize("language,expected", [
("en_US.UTF-8", "en"),
("en_US", "en"),
("en", "en"),
("pt_BR", "pt_BR"),
("pt_PT", "pt_PT"),
("zh_TW", "zh_TW"),
("pt", "pt_PT")
])
def test_get_language_code(language, expected):
assert tldr.get_language_code(language) == expected
@pytest.mark.parametrize("language,expected", [
("en_US.UTF-8", "en"),
("POSIX", None),
("C", None)
])
def test_get_default_language(language, expected, monkeypatch):
monkeypatch.setenv("LANG", language)
assert tldr.get_default_language() == expected
def test_get_default_language_unset(monkeypatch):
monkeypatch.delenv("LANG", raising=False)
assert tldr.get_default_language() is None
@pytest.mark.parametrize("tldr_language, language, lang, expected", [
("en", None, "fr_FR", ["en", "fr"]),
("de", "ja_JA:cz_CZ", "cz_CZ", ["de", "ja", "cz", "en"]),
("it", None, "C", ["it", "en"]),
])
def test_tldr_language(tldr_language, language, lang, expected, monkeypatch):
for name, var in [("TLDR_LANGUAGE", tldr_language),
("LANGUAGE", language),
("LANG", lang)]:
# Unset environment variable if their value is given as None
if var is None:
monkeypatch.delenv(name, raising=False)
else:
monkeypatch.setenv(name, var)
assert tldr.get_language_list() == expected
@pytest.mark.parametrize("platform, expected", [
("linux2", "linux"),
("win32", "windows"),
("darwin", "osx"),
("sunos", "sunos"),
("freebsd", "freebsd"),
("openbsd", "openbsd"),
("netbsd", "netbsd"),
("aix", "linux"),
])
def test_get_platform(platform, expected):
with mock.patch("sys.platform", platform):
assert tldr.get_platform() == expected
def test_get_cache_dir_xdg(monkeypatch):
monkeypatch.setenv("XDG_CACHE_HOME", "/tmp/cache")
assert tldr.get_cache_dir() == Path("/tmp/cache/tldr")
def test_get_cache_dir_home(monkeypatch):
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
monkeypatch.setenv("HOME", "/tmp/home")
assert tldr.get_cache_dir() == Path("/tmp/home/.cache/tldr")
def test_get_cache_dir_default(monkeypatch):
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
monkeypatch.delenv("HOME", raising=False)
monkeypatch.setattr(Path, 'home', lambda: Path('/tmp/expanduser'))
assert tldr.get_cache_dir() == Path("/tmp/expanduser/.cache/tldr")
def test_get_commands(monkeypatch, tmp_path):
cache_default = tmp_path / ".cache" / "tldr" / "pages" / "linux"
Path.mkdir(cache_default, parents=True)
Path.touch(cache_default / "lspci.md")
monkeypatch.setenv("HOME", str(tmp_path))
result = tldr.get_commands(platforms=["linux"])
assert isinstance(result, list)
assert "lspci" in result
cache_zh = tmp_path / ".cache" / "tldr" / "pages.zh" / "linux"
Path.mkdir(cache_zh, parents=True)
Path.touch(cache_zh / "lspci.md")
result = tldr.get_commands(platforms=["linux"], language=["zh_CN"])
assert "lspci" in result
def test_debug_respects_verbose_flag(capsys, monkeypatch):
monkeypatch.setattr(tldr, "VERBOSE", False)
tldr.debug("hidden")
assert capsys.readouterr().err == ""
monkeypatch.setattr(tldr, "VERBOSE", True)
tldr.debug("visible")
assert capsys.readouterr().err == "[tldr-debug] visible\n"
def test_verbose_flag_outputs_debug_info(monkeypatch, capsys):
monkeypatch.setattr(tldr, "VERBOSE", False)
with mock.patch("sys.argv", ["tldr", "--verbose", "--list"]):
tldr.main()
captured = capsys.readouterr()
assert "[tldr-debug] Verbose output enabled" in captured.err
assert "[tldr-debug] cache_dir=" in captured.err