Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh actions #4

Merged
merged 2 commits into from
Jul 31, 2020
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
27 changes: 26 additions & 1 deletion obspy_github_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
51 changes: 32 additions & 19 deletions obspy_github_api/obspy_github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
import os
import re
import time
import warnings
from functools import lru_cache
from pathlib import Path
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions obspy_github_api/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
37 changes: 24 additions & 13 deletions obspy_github_api/tests/test_obspy_github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
]
Expand All @@ -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"])
)
Expand Down Expand Up @@ -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])