diff --git a/tests/test_tldr.py b/tests/test_tldr.py index 53279b1..82061ac 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -2,7 +2,9 @@ import io import types import unittest +import unittest.mock import tldr +import pytest class TLDRTests(unittest.TestCase): @@ -12,7 +14,7 @@ def test_whole_page(self): 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')) + sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8")) tldr.output(f_original) sys.stdout.seek(0) @@ -21,3 +23,12 @@ def test_whole_page(self): correct_output = f_rendered.read() self.assertEqual(tldr_output, correct_output) + + def test_error_message(self): + with unittest.mock.patch("sys.argv", ["tldr", "73eb6f19cd6f"]): + with pytest.raises(SystemExit) as pytest_wrapped_e: + tldr.main() + correct_output = "`73eb6f19cd6f` documentation is not available. Consider contributing 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 diff --git a/tldr.py b/tldr.py index bccf57b..7ca0233 100755 --- a/tldr.py +++ b/tldr.py @@ -15,7 +15,6 @@ from termcolor import colored import colorama # Required for Windows import argcomplete -from glob import glob __version__ = "1.0.0" __client_specification__ = "1.3" @@ -223,17 +222,18 @@ def get_page(command, remote=None, platforms=None, languages=None): COMMAND_SPLIT_REGEX = re.compile(r'(?P{{.+?}})') PARAM_REGEX = re.compile(r'(?:{{)(?P.+?)(?:}})') -CACHE_FILE_REGEX = re.compile(r'.*\/(.*)\.md') def get_commands(platforms=None): if platforms is None: platforms = get_platform_list() - cache_files = [] - for platform in platforms: - cache_files += glob(os.path.join(get_cache_dir(), 'pages', platform, '*.md')) - return [re.search(CACHE_FILE_REGEX, x).group(1) for x in cache_files] + commands = [] + if os.path.exists(get_cache_dir()): + for platform in platforms: + path = os.path.join(get_cache_dir(), 'pages', platform) + commands += [file[:-3] for file in os.listdir(path) if file.endswith(".md")] + return commands def colors_of(key): @@ -269,7 +269,7 @@ def output(page): colored( line.replace('>', '').replace('<', ''), *colors_of('description') - ) + ) sys.stdout.buffer.write(line.encode('utf-8')) elif line[0] == '-': line = '\n' + ' ' * LEADING_SPACES_NUM + \ @@ -381,9 +381,8 @@ def main(): help='Override the default language') parser.add_argument( - 'command', type=str, nargs='*', help="command to lookup", metavar='command', - choices=get_commands() + [[]] - ) + 'command', type=str, nargs='*', help="command to lookup", metavar='command' + ).completer = argcomplete.completers.ChoicesCompleter(get_commands() + [[]]) argcomplete.autocomplete(parser) options = parser.parse_args()