Skip to content

Commit 1dd7e3f

Browse files
authored
Fix "documentation is not available.", refactor get_commands (#132)
1 parent 09dd82e commit 1dd7e3f

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

tests/test_tldr.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import io
33
import types
44
import unittest
5+
import unittest.mock
56
import tldr
7+
import pytest
68

79

810
class TLDRTests(unittest.TestCase):
@@ -12,7 +14,7 @@ def test_whole_page(self):
1214
old_stdout = sys.stdout
1315
sys.stdout = io.StringIO()
1416
sys.stdout.buffer = types.SimpleNamespace()
15-
sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode('utf-8'))
17+
sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8"))
1618
tldr.output(f_original)
1719

1820
sys.stdout.seek(0)
@@ -21,3 +23,12 @@ def test_whole_page(self):
2123

2224
correct_output = f_rendered.read()
2325
self.assertEqual(tldr_output, correct_output)
26+
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

tldr.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from termcolor import colored
1616
import colorama # Required for Windows
1717
import argcomplete
18-
from glob import glob
1918

2019
__version__ = "1.0.0"
2120
__client_specification__ = "1.3"
@@ -223,17 +222,18 @@ def get_page(command, remote=None, platforms=None, languages=None):
223222

224223
COMMAND_SPLIT_REGEX = re.compile(r'(?P<param>{{.+?}})')
225224
PARAM_REGEX = re.compile(r'(?:{{)(?P<param>.+?)(?:}})')
226-
CACHE_FILE_REGEX = re.compile(r'.*\/(.*)\.md')
227225

228226

229227
def get_commands(platforms=None):
230228
if platforms is None:
231229
platforms = get_platform_list()
232230

233-
cache_files = []
234-
for platform in platforms:
235-
cache_files += glob(os.path.join(get_cache_dir(), 'pages', platform, '*.md'))
236-
return [re.search(CACHE_FILE_REGEX, x).group(1) for x in cache_files]
231+
commands = []
232+
if os.path.exists(get_cache_dir()):
233+
for platform in platforms:
234+
path = os.path.join(get_cache_dir(), 'pages', platform)
235+
commands += [file[:-3] for file in os.listdir(path) if file.endswith(".md")]
236+
return commands
237237

238238

239239
def colors_of(key):
@@ -269,7 +269,7 @@ def output(page):
269269
colored(
270270
line.replace('>', '').replace('<', ''),
271271
*colors_of('description')
272-
)
272+
)
273273
sys.stdout.buffer.write(line.encode('utf-8'))
274274
elif line[0] == '-':
275275
line = '\n' + ' ' * LEADING_SPACES_NUM + \
@@ -381,9 +381,8 @@ def main():
381381
help='Override the default language')
382382

383383
parser.add_argument(
384-
'command', type=str, nargs='*', help="command to lookup", metavar='command',
385-
choices=get_commands() + [[]]
386-
)
384+
'command', type=str, nargs='*', help="command to lookup", metavar='command'
385+
).completer = argcomplete.completers.ChoicesCompleter(get_commands() + [[]])
387386

388387
argcomplete.autocomplete(parser)
389388
options = parser.parse_args()

0 commit comments

Comments
 (0)