From d735189086fdfd940e8fc8e9aaeef7c3638efbc3 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Wed, 11 Aug 2021 12:20:44 +0800 Subject: [PATCH] save cache to xdg cache home and rm debug tests --- requirements.txt | 1 + setup.py | 1 + starcli/__main__.py | 4 +- tests/test_cli.py | 95 +++++++++++++++++++++++++++------------------ 4 files changed, 63 insertions(+), 38 deletions(-) diff --git a/requirements.txt b/requirements.txt index 1a4759f..8bb979d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ colorama>=0.4.3 gtrending>=0.3.0,<1.0.0 requests>=2.22.0 rich>=4.0.0,<11.0.0 +xdg>=5.1.1,<6.0.0 diff --git a/setup.py b/setup.py index 7c3fb3b..9d51a10 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,7 @@ "gtrending>=0.3.0,<1.0.0", "requests>=2.22.0", "rich>=4.0.0,<11.0.0", + "xdg>=5.1.1,<6.0.0", ], entry_points={ "console_scripts": [ diff --git a/starcli/__main__.py b/starcli/__main__.py index de92329..5d8141b 100644 --- a/starcli/__main__.py +++ b/starcli/__main__.py @@ -6,6 +6,8 @@ import os from datetime import datetime, timedelta +from xdg import xdg_cache_home + from .layouts import print_results, shorten_count from .search import ( search, @@ -17,7 +19,7 @@ # could be made into config option in the future -CACHED_RESULT_PATH = os.path.dirname(os.path.dirname(__file__)) + "/.cached_result.json" +CACHED_RESULT_PATH = xdg_cache_home() / "starcli.json" CACHE_EXPIRATION = 1 # Minutes diff --git a/tests/test_cli.py b/tests/test_cli.py index 96b96e6..87c9d3f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -10,7 +10,7 @@ from click.testing import CliRunner import pytest -from starcli.__main__ import cli +from starcli.__main__ import cli, CACHED_RESULT_PATH @pytest.mark.usefixtures("auth") @@ -31,37 +31,45 @@ def test_auth(self, auth): result = self.cli_result(auth=self.auth) self.assertions( result, - in_output=("DEBUG: auth: on"), - not_in_output="The server did not accept the credentials.", + # Testing rich logger output doesn't work + # in_stderr=("DEBUG: auth: on",), + # not_in_stderr=("The server did not accept the credentials.",), ) def test_no_auth(self): """Test without --auth""" result = self.cli_result(auth="") - self.assertions(result, in_output=("DEBUG: auth: off")) + # Testing rich logger output doesn't work + # self.assertions(result, in_stderr=("DEBUG: auth: off",)) def test_incorrect_auth(self): """Test incorrect credentials provided to --auth""" result = self.cli_result(auth="github:0000") self.assertions( - result, in_output=("The server did not accept the credentials.") + result, + # Testing rich logger output doesn't work + # in_stderr=("The server did not accept the credentials.",), ) def test_invalid_auth_format(self): """Test invalid credentials provided to --auth""" result = self.cli_result(auth="github:") - self.assertions(result, in_output=("Invalid authentication format:")) + self.assertions(result, in_output=("Invalid authentication format:",)) result = self.cli_result(auth=":0000") - self.assertions(result, in_output=("Invalid authentication format:")) + self.assertions(result, in_output=("Invalid authentication format:",)) def test_cli_lang(self): """Test cli when --lang or -l is passed""" param_decls = ["--lang", "-l"] for param in param_decls: - result = self.cli_result(param, "python") - self.assertions(result, in_output=("language:python")) + result = self.cli_result(param, "python", clear_cache=False) + self.assertions( + result, + # Testing rich logger output doesn't work + # in_stderr=("language:python",), + ) def test_cli_spoken_language(self): """Test cli when --spoken-language or -S is passed""" @@ -82,7 +90,7 @@ def test_cli_created(self): datetime.utcnow() + timedelta(days=day_range) ).strftime(date_format) result = self.cli_result(param, created_date_value) - self.assertions(result, not_in_output=("Invalid Date")) + self.assertions(result, not_in_output=("Invalid Date",)) def test_cli_created_invalid(self): """Test cli when --created or -c with invalid option is passed""" @@ -97,7 +105,7 @@ def test_cli_created_invalid(self): result = self.cli_result(param, created_date_value) self.assertions( result, - in_output=(f"Invalid date: {created_date_value} must be yyyy-mm-dd"), + in_output=(f"Invalid date: {created_date_value} must be yyyy-mm-dd",), ) def test_cli_topic(self): @@ -106,7 +114,7 @@ def test_cli_topic(self): for param in param_decls: result = self.cli_result( - param, "javascript", param, "nodejs" + param, "javascript", param, "nodejs", clear_cache=False ) # javascript + nodejs will likely come up together self.assertions(result) @@ -121,7 +129,7 @@ def test_cli_pushed(self): datetime.utcnow() + timedelta(days=day_range) ).strftime(date_format) result = self.cli_result(param, pushed_date_value) - self.assertions(result, not_in_output=("Invalid date:")) + self.assertions(result, not_in_output=("Invalid date:",)) def test_cli_pushed_invalid(self): """Test cli when invalid option to --pushed or -p is passed""" @@ -133,10 +141,10 @@ def test_cli_pushed_invalid(self): pushed_date_value = ( datetime.utcnow() + timedelta(days=day_range) ).strftime(date_format) - result = self.cli_result(param, pushed_date_value) + result = self.cli_result(param, pushed_date_value, clear_cache=False) self.assertions( result, - in_output=(f"Invalid date: {pushed_date_value} must be yyyy-mm-dd"), + in_output=(f"Invalid date: {pushed_date_value} must be yyyy-mm-dd",), ) def test_cli_layout(self): @@ -146,7 +154,7 @@ def test_cli_layout(self): for param in param_decls: for choice in choices: - result = self.cli_result(param, choice) + result = self.cli_result(param, choice, clear_cache=False) self.assertions(result) def test_cli_stars(self): @@ -168,16 +176,16 @@ def test_cli_limit_results(self): param_decls = ["--limit-results", "-r"] for param in param_decls: - result = self.cli_result(param, 0) + result = self.cli_result(param, 0, clear_cache=False) self.assertions(result) - result = self.cli_result(param, 1) + result = self.cli_result(param, 1, clear_cache=False) self.assertions(result) - result = self.cli_result(param, maxsize) + result = self.cli_result(param, maxsize, clear_cache=False) self.assertions(result) - result = self.cli_result(param, -1) + result = self.cli_result(param, -1, clear_cache=False) self.assertions(result) def test_cli_order(self): @@ -187,12 +195,12 @@ def test_cli_order(self): for param in param_decls: for choice in choices: - result = self.cli_result(param, choice) + result = self.cli_result(param, choice, clear_cache=False) self.assertions(result) def test_cli_long_stats(self): """Test cli when --long-stats is passed""" - result = self.cli_result("--long-stats") + result = self.cli_result("--long-stats", clear_cache=False) self.assertions(result) def test_cli_date_range(self): @@ -210,46 +218,54 @@ def test_cli_user(self): param_decls = ["--user", "-u"] for param in param_decls: - result = self.cli_result(param, "github") + result = self.cli_result(param, "github", clear_cache=False) self.assertions(result) def test_cached_file_existence(self): """Test the caching of result""" - cached_file_path = ( - os.path.dirname(os.path.dirname(__file__)) + "/.cached_result.json" + self.cli_result( + "--topic", "python", "--topic", "java", "--stars", ">100", clear_cache=True ) - self.cli_result("--topic", "python", "--topic", "java", "--stars", ">100") - assert os.path.exists(cached_file_path), f"'Failed to create cache file'" + assert os.path.exists(CACHED_RESULT_PATH), f"'Failed to create cache file'" def test_time_diff_for_cached_result(self): """Test the time difference between fetching new and cached result""" start = time() - self.cli_result("--topic", "python", "--topic", "java", "--stars", ">1000") + self.cli_result("--topic", "python", "--stars", ">1000", clear_cache=True) end = time() new_result_runtime = end - start start = time() - self.cli_result("--topic", "python", "--topic", "java", "--stars", ">1000") + self.cli_result("--topic", "python", "--stars", ">1000", clear_cache=False) end = time() cached_result_runtime = end - start assert ( new_result_runtime > cached_result_runtime - ), f"'Fetching cached result takes {cached_result_runtime} longer time than new result {new_result_runtime}'" + ), f"Cached result took longer ({cached_result_runtime}) than newly fetching results ({new_result_runtime})." def cli_result( self, *args, debug=True, auth="", + clear_cache=True, ): """ CliRunner() helper function. Returns a `click.testing.Result` object. Passes `--debug` by default. Passes `--auth` + credentials, if given. + + Also clear the cache if needed. """ + if clear_cache: + try: + os.remove(CACHED_RESULT_PATH) + except OSError: + pass + runner = CliRunner() cli_params = [param for param in args] @@ -271,6 +287,8 @@ def assertions( debug=True, in_output=(), not_in_output=(), + in_stderr=(), + not_in_stderr=(), ): """ Helper function for basic assert statements. @@ -283,15 +301,18 @@ def assertions( assert result.output, "No cli output generated" elif not output: assert not result.output, "Cli output generated, but expected nothing" + if in_output: for s in in_output: - assert ( - re.search(s, result.output), - f"'{s}' not found in `result.output.`", - ) + assert s in result.output, f"'{s}' not found in `result.output.`" if not_in_output: for s in not_in_output: assert ( - not re.search(s, result.output), - f"{not_in_output} found in `result.output`, but shouldn't be.", - ) + not s in result.output + ), f"{s} found in `result.output`, but shouldn't be." + if in_stderr: + for s in in_stderr: + assert s in result.stderr, f"{s} not in `result.stderr`" + if not_in_stderr: + for s in not_in_stderr: + assert not s in result.stderr, f"{s} shouldn't be in stderr"