From 6d7973363100a55983beb4e844d8b51091d6b6c2 Mon Sep 17 00:00:00 2001 From: Derrick Date: Fri, 31 Jul 2020 07:40:34 -0700 Subject: [PATCH] Gh actions (#4) * fix parsing issue * add cli to get default module list names --- README.md | 4 +- obspy_github_api/cli.py | 27 +++++++++- obspy_github_api/obspy_github_api.py | 51 ++++++++++++------- obspy_github_api/tests/test_cli.py | 9 ++++ .../tests/test_obspy_github_api.py | 37 +++++++++----- 5 files changed, 93 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index a965583..225e13a 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,10 @@ The easiest way to use obspy_github_api is via its command line interface. obshub make_config 101 --path obspy_config.json # Read a specified option. -obshub read_config_value module_list --path obspy_config.json +obshub read-config-value module_list --path obspy_config.json # Use a value in the config in another command line utility. -export BUILDDOCS=`bshub read_config_value module_list --path obspy_config.json` +export BUILDDOCS=`obshub read-config-value module_list --path obspy_config.json` some-other-command --docs $BUILDDOCS ``` diff --git a/obspy_github_api/cli.py b/obspy_github_api/cli.py index 990cd58..bab3d44 100644 --- a/obspy_github_api/cli.py +++ b/obspy_github_api/cli.py @@ -6,7 +6,11 @@ import typer -from obspy_github_api.obspy_github_api import make_ci_json_config +from obspy_github_api.obspy_github_api import ( + make_ci_json_config, + get_obspy_module_lists, + _append_obspy, +) app = typer.Typer() @@ -44,6 +48,27 @@ def read_config_value(name: str, path: str = DEFAULT_CONFIG_PATH): return value +@app.command() +def get_module_list(group: str = "default", sep=" "): + """ + Print and return module lists for use with coverage. + + Parameters + ---------- + group + The name of the module group. Options are: + default + all + network + sep + Character to separate modules, ' ' else ',' + """ + mod_list = get_obspy_module_lists()[group] + with_obspy = _append_obspy(mod_list) + print(sep.join(with_obspy)) + return with_obspy + + def main(): app() diff --git a/obspy_github_api/obspy_github_api.py b/obspy_github_api/obspy_github_api.py index 962991d..648b9cf 100644 --- a/obspy_github_api/obspy_github_api.py +++ b/obspy_github_api/obspy_github_api.py @@ -4,7 +4,6 @@ import json import os import re -import time import warnings from functools import lru_cache from pathlib import Path @@ -38,7 +37,7 @@ def get_github_client(token=None): return gh -def check_specific_module_tests_requested(issue_number, token=None): +def get_requested_modules(issue_number, token=None): """ Checks if tests of specific modules are requested for given issue number (e.g. by magic string '+TESTS:clients.fdsn,clients.arclink' or '+TESTS:ALL' @@ -79,8 +78,21 @@ def check_specific_module_tests_requested(issue_number, token=None): return modules_to_test +def get_obspy_module_lists(module_path="./obspy/core/util/base.py"): + """Return a dict of lists of obspy's default, network, and all modules.""" + try: # If ObsPy is installed just use module list from expected place. + from obspy.core.util.base import DEFAULT_MODULES, ALL_MODULES, NETWORK_MODULES + except (ImportError, ModuleNotFoundError): # Else parse the module. + names = {"DEFAULT_MODULES", "NETWORK_MODULES"} + values = get_values_from_module(module_path, names) + DEFAULT_MODULES = values["DEFAULT_MODULES"] + NETWORK_MODULES = values["NETWORK_MODULES"] + ALL_MODULES = DEFAULT_MODULES + NETWORK_MODULES + return dict(all=ALL_MODULES, default=DEFAULT_MODULES, network=NETWORK_MODULES) + + def get_module_test_list( - issue_number, token=None, module_path="./obspy/core/util/base.py" + issue_number, token=None, module_path="./obspy/core/util/base.py", ): """ Gets the list of modules that should be tested for the given issue number. @@ -92,23 +104,16 @@ def get_module_test_list( :rtype: list :returns: List of modules names to test for given issue number. """ - try: # If ObsPy is installed just use module list from expected place. - from obspy.core.util.base import DEFAULT_MODULES, ALL_MODULES - except (ImportError, ModuleNotFoundError): # Else parse the module. - names = {"DEFAULT_MODULES", "NETWORK_MODULES"} - values = get_values_from_module(module_path, names) - DEFAULT_MODULES = values["DEFAULT_MODULES"] - NETWORK_MODULES = values["NETWORK_MODULES"] - ALL_MODULES = DEFAULT_MODULES + NETWORK_MODULES - - modules_to_test = check_specific_module_tests_requested(issue_number, token) - + mod_dict = get_obspy_module_lists(module_path) + modules_to_test = get_requested_modules(issue_number, token) + # Set to default or all if modules_to_test is False: - return DEFAULT_MODULES + modules_to_test = mod_dict["default"] elif modules_to_test is True: - return ALL_MODULES - else: - return sorted(list(set.union(set(DEFAULT_MODULES), modules_to_test))) + modules_to_test = mod_dict["all"] + # filter out any modules which don't exist + modules_to_test = set(modules_to_test) & set(mod_dict["all"]) + return sorted(list(set.union(set(mod_dict["default"]), modules_to_test))) def get_values_from_module(node, names): @@ -449,6 +454,14 @@ def get_docker_build_targets( return " ".join(targets) +def _append_obspy(module_list): + """ + Append the string 'obspy.' to each string in module list for use in coverage. + """ + module_list_obspy_prepended = [f"obspy.{x}" for x in module_list] + return module_list_obspy_prepended + + def make_ci_json_config(issue_number, path="obspy_ci_conf.json", token=None): """ Make a json file for configuring additional actions in CI. @@ -459,7 +472,7 @@ def make_ci_json_config(issue_number, path="obspy_ci_conf.json", token=None): # comment string to use for later actions. module_list = get_module_test_list(issue_number, token=token) docs = check_docs_build_requested(issue_number, token=token) - module_list_obspy_prepended = [f"obspy.{x}" for x in module_list] + module_list_obspy_prepended = _append_obspy(module_list) out = dict( module_list=",".join(module_list_obspy_prepended), diff --git a/obspy_github_api/tests/test_cli.py b/obspy_github_api/tests/test_cli.py index 5768486..001c65d 100644 --- a/obspy_github_api/tests/test_cli.py +++ b/obspy_github_api/tests/test_cli.py @@ -45,3 +45,12 @@ def test_read_config_value(self, populated_config): run_str = f"obshub read-config-value docs --path {populated_config}" out = run(run_str, shell=True, capture_output=True) assert out.stdout.decode("utf8").rstrip() == "False" + + def test_get_module_lists(self): + """Ensure module lists are retrievable. """ + run_str = f"obshub get-module-list --sep ' '" + out = run(run_str, shell=True, capture_output=True) + mod_list = out.stdout.decode("utf8").rstrip().split(" ") + assert len(mod_list) > 5 + for mod in mod_list: + assert mod.startswith("obspy.") diff --git a/obspy_github_api/tests/test_obspy_github_api.py b/obspy_github_api/tests/test_obspy_github_api.py index bc0dc60..2474a15 100644 --- a/obspy_github_api/tests/test_obspy_github_api.py +++ b/obspy_github_api/tests/test_obspy_github_api.py @@ -3,7 +3,7 @@ from obspy_github_api import ( check_docs_build_requested, - check_specific_module_tests_requested, + get_requested_modules, get_commit_status, get_commit_time, get_issue_numbers_that_request_docs_build, @@ -23,9 +23,9 @@ def test_check_docs_build_requested(): def test_check_specific_module_tests_requested(): - assert check_specific_module_tests_requested(100) is False - assert check_specific_module_tests_requested(101) is True - assert check_specific_module_tests_requested(102) == [ + assert get_requested_modules(100) is False + assert get_requested_modules(101) is True + assert get_requested_modules(102) == [ "clients.arclink", "clients.fdsn", ] @@ -34,8 +34,8 @@ def test_check_specific_module_tests_requested(): @mock.patch("obspy.core.util.base.DEFAULT_MODULES", MOCK_DEFAULT_MODULES) @mock.patch("obspy.core.util.base.ALL_MODULES", MOCK_ALL_MODULES) def test_get_module_test_list(): - assert get_module_test_list(100) == MOCK_DEFAULT_MODULES - assert get_module_test_list(101) == MOCK_ALL_MODULES + assert get_module_test_list(100) == sorted(MOCK_DEFAULT_MODULES) + assert get_module_test_list(101) == sorted(MOCK_ALL_MODULES) assert get_module_test_list(102) == sorted( set.union(set(MOCK_DEFAULT_MODULES), ["clients.arclink", "clients.fdsn"]) ) @@ -73,10 +73,21 @@ def test_get_issue_numbers_that_request_docs_build(): assert isinstance(issue, int) -def test_json_ci_config(): - """Tests contents of configuration dict.""" - config_dict = make_ci_json_config(100, path=None) - assert isinstance(config_dict, dict) - # ensure module list elements don't end in '.' - module_list_split = config_dict["module_list"].split(",") - assert all([not x.endswith(".") for x in module_list_split]) +class TestConfig: + """Tests for creating the configuration file""" + + def test_json_ci_config(self): + """Tests contents of configuration dict.""" + config_dict = make_ci_json_config(100, path=None) + assert isinstance(config_dict, dict) + # ensure module list elements don't end in '.' + module_list_split = config_dict["module_list"].split(",") + assert all([not x.endswith(".") for x in module_list_split]) + + def test_no_ellipses(self): + """Ensure the literal 'obspy....' is not in the module list. """ + config_dict = make_ci_json_config(2591, path=None) + module_list = config_dict["module_list"] + module_list_split = module_list.split(",") + # There should never be more than one consecutive dot + assert not any([".." in x for x in module_list_split])