From 2ecbfeee6f6be539eab7adcb26318160d64999f4 Mon Sep 17 00:00:00 2001 From: samuel-w Date: Mon, 3 Aug 2020 23:29:53 -0500 Subject: [PATCH 1/8] Fix 'Consider contributing a Pull Request... not showing up --- tldr.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tldr.py b/tldr.py index bccf57b..d21e6e1 100755 --- a/tldr.py +++ b/tldr.py @@ -14,7 +14,7 @@ from urllib.error import HTTPError, URLError from termcolor import colored import colorama # Required for Windows -import argcomplete +from argcomplete.completers import ChoicesCompleter from glob import glob __version__ = "1.0.0" @@ -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,11 +381,9 @@ 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 = ChoicesCompleter(get_commands() + [[]]) - argcomplete.autocomplete(parser) options = parser.parse_args() colorama.init(strip=options.color) From 27ffc54b28a62d8614d3584b29568f578e7726fc Mon Sep 17 00:00:00 2001 From: samuel-w Date: Mon, 3 Aug 2020 23:49:49 -0500 Subject: [PATCH 2/8] Fix autocomplete --- tldr.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tldr.py b/tldr.py index d21e6e1..841bb40 100755 --- a/tldr.py +++ b/tldr.py @@ -15,6 +15,7 @@ from termcolor import colored import colorama # Required for Windows from argcomplete.completers import ChoicesCompleter +import argcomplete from glob import glob __version__ = "1.0.0" @@ -384,6 +385,8 @@ def main(): 'command', type=str, nargs='*', help="command to lookup", metavar='command' ).completer = ChoicesCompleter(get_commands() + [[]]) + argcomplete.autocomplete(parser) + options = parser.parse_args() colorama.init(strip=options.color) From 1e60f2805b6cc608296933fd1368a808e82fbdc6 Mon Sep 17 00:00:00 2001 From: samuel-w Date: Tue, 4 Aug 2020 00:24:41 -0500 Subject: [PATCH 3/8] Remove double import --- tldr.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tldr.py b/tldr.py index 841bb40..062c6fc 100755 --- a/tldr.py +++ b/tldr.py @@ -14,7 +14,6 @@ from urllib.error import HTTPError, URLError from termcolor import colored import colorama # Required for Windows -from argcomplete.completers import ChoicesCompleter import argcomplete from glob import glob @@ -383,7 +382,7 @@ def main(): parser.add_argument( 'command', type=str, nargs='*', help="command to lookup", metavar='command' - ).completer = ChoicesCompleter(get_commands() + [[]]) + ).completer = argcomplete.completers.ChoicesCompleter(get_commands() + [[]]) argcomplete.autocomplete(parser) From eab5edbb28764d4cb96474c018a0145f52516b60 Mon Sep 17 00:00:00 2001 From: samuel-w Date: Tue, 4 Aug 2020 01:05:45 -0500 Subject: [PATCH 4/8] Add error message test --- .github/workflows/test.yml | 2 +- tests/test_tldr.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e12f6f2..cb1252d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,7 +25,7 @@ jobs: - name: Install Dependencies run: | python3 -m pip install -U pip - python3 -m pip install -U pytest pytest-runner flake8 + python3 -m pip install -U pytest pytest-runner flake8 mock - name: Lint codebase run: python3 -m flake8 diff --git a/tests/test_tldr.py b/tests/test_tldr.py index 53279b1..4c26501 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -3,6 +3,8 @@ import types import unittest import tldr +import pytest +from mock import patch 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,13 @@ def test_whole_page(self): correct_output = f_rendered.read() self.assertEqual(tldr_output, correct_output) + + def test_error_message(self): + testargs = ["tldr", "73eb6f19cd6f"] + with patch.object(sys, "argv", testargs): + 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 From d0ef752f6be1909c70b70faf0fc8abb26a192f31 Mon Sep 17 00:00:00 2001 From: Samuel Woon Date: Tue, 4 Aug 2020 13:33:00 -0500 Subject: [PATCH 5/8] Update tldr.py --- tldr.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tldr.py b/tldr.py index 062c6fc..e8acd97 100755 --- a/tldr.py +++ b/tldr.py @@ -385,7 +385,6 @@ def main(): ).completer = argcomplete.completers.ChoicesCompleter(get_commands() + [[]]) argcomplete.autocomplete(parser) - options = parser.parse_args() colorama.init(strip=options.color) From 8c4ac6d01022b0f1dcf1641a055b04d1e1db27e3 Mon Sep 17 00:00:00 2001 From: samuel-w Date: Tue, 4 Aug 2020 16:47:25 -0500 Subject: [PATCH 6/8] Remove dependency on mock --- .github/workflows/test.yml | 2 +- tests/test_tldr.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cb1252d..e12f6f2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,7 +25,7 @@ jobs: - name: Install Dependencies run: | python3 -m pip install -U pip - python3 -m pip install -U pytest pytest-runner flake8 mock + python3 -m pip install -U pytest pytest-runner flake8 - name: Lint codebase run: python3 -m flake8 diff --git a/tests/test_tldr.py b/tests/test_tldr.py index 4c26501..82061ac 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -2,9 +2,9 @@ import io import types import unittest +import unittest.mock import tldr import pytest -from mock import patch class TLDRTests(unittest.TestCase): @@ -25,8 +25,7 @@ def test_whole_page(self): self.assertEqual(tldr_output, correct_output) def test_error_message(self): - testargs = ["tldr", "73eb6f19cd6f"] - with patch.object(sys, "argv", testargs): + 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 From a786c4a5ac2b3b2a41be3e529f207f2336d05345 Mon Sep 17 00:00:00 2001 From: samuel-w Date: Tue, 4 Aug 2020 19:27:16 -0500 Subject: [PATCH 7/8] Simplify get_commands --- tldr.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tldr.py b/tldr.py index e8acd97..67a796d 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,17 @@ 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 = [] + commands = [] 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] + 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): From 5472b3a9878f73df18cde6dea6380a588a6571de Mon Sep 17 00:00:00 2001 From: samuel-w Date: Tue, 4 Aug 2020 19:58:20 -0500 Subject: [PATCH 8/8] Check if cache dir exists --- tldr.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tldr.py b/tldr.py index 67a796d..7ca0233 100755 --- a/tldr.py +++ b/tldr.py @@ -229,9 +229,10 @@ def get_commands(platforms=None): platforms = get_platform_list() commands = [] - 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")] + 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