Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion tests/test_tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import io
import types
import unittest
import unittest.mock
import tldr
import pytest


class TLDRTests(unittest.TestCase):
Expand All @@ -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)
Expand All @@ -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
19 changes: 9 additions & 10 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -223,17 +222,18 @@ def get_page(command, remote=None, platforms=None, languages=None):

COMMAND_SPLIT_REGEX = re.compile(r'(?P<param>{{.+?}})')
PARAM_REGEX = re.compile(r'(?:{{)(?P<param>.+?)(?:}})')
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):
Expand Down Expand Up @@ -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 + \
Expand Down Expand Up @@ -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()
Expand Down