8000 Adds unit tests for utils.py module by JayjeetAtGithub · Pull Request #719 · getpopper/popper · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Adds unit tests for utils.py module #719

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

Merged
merged 2 commits into from
Jul 23, 2019
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
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ before_install:
- ci/scripts/install_scripts.sh
- pip install git+https://github.com/virtuald/pyhcl.git@0.3.12#egg=pyhcl
install:
- pip install coverage
- pip install cli/
script:
- python -m unittest discover --start-directory cli/
- coverage run -m unittest discover --start-directory cli/
- popper run
after_success:
- bash <(curl -s https://codecov.io/bash)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![Downloads](https://pepy.tech/badge/popper)](https://pepy.tech/project/popper)
[![Build Status](https://travis-ci.org/systemslab/popper.svg?branch=master)](https://travis-ci.org/systemslab/popper)
[![codecov](https://codecov.io/gh/systemslab/popper/branch/master/graph/badge.svg)](https://codecov.io/gh/systemslab/popper)
[![Join the chat at https://gitter.im/systemslab/popper](https://badges.gitter.im/systemslab/popper.svg)](https://gitter.im/falsifiable-us/popper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![PyPI version](https://badge.fury.io/py/popper.svg)](https://badge.fury.io/py/popper)
[![GitHub license](https://img.shields.io/github/license/systemslab/popper.svg)](https://github.com/systemslab/popper/blob/master/LICENSE)
Expand Down
87 changes: 40 additions & 47 deletions cli/popper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re
import sys
import threading
from collections import defaultdict

import yaml
import click
Expand All @@ -23,14 +22,14 @@ def setup_cache():


def decode(line):
"""Make treatment of stdout Python 2/3 compatible"""
"""Make treatment of stdout Python 2/3 compatible."""
if isinstance(line, bytes):
return line.decode('utf-8')
return line


def get_items(dict_object):
"""Python 2/3 compatible way of iterating over a dictionary"""
"""Python 2/3 compatible way of iterating over a dictionary."""
for key in dict_object:
yield key, dict_object[key]

Expand Down Expand Up @@ -83,10 +82,16 @@ def g(*args, **kwargs):
def find_default_wfile(wfile=None):
"""
Used to find `main.workflow` in $PWD or in `.github`
And returns error if not found
And returns error if not found.

Args:
wfile (str): The path to a workflow file.
The default value of this is None, when the
function searches for `main.workflow` or
`.github/main.workflow'.

Returns:
path of wfile
str: Path of wfile.
"""
if not wfile:
if os.path.isfile("main.workflow"):
Expand All @@ -100,18 +105,17 @@ def find_default_wfile(wfile=None):
".github/main.workflow"))
if not os.path.isfile(wfile):
log.fail("File {} not found.".format(wfile))
exit(1)

return wfile


def find_recursive_wfile():
"""
Used to search for `.workflow` files in $PWD and
then recursively in sub directories
then recursively in sub directories.

Returns:
list of path of workflow files
list: List of path of workflow files.
"""
wfile_list = list()
for root, _, files in os.walk('.'):
Expand All @@ -123,42 +127,21 @@ def find_recursive_wfile():
return wfile_list


def get_gh_headers():
"""Method for getting the headers required for making authorized
GitHub API requests.
Returns:
headers (dict): a dictionary representing HTTP-headers and their
values.
"""
gh_token = os.environ.get('POPPER_GITHUB_API_TOKEN', None)

headers = {}

if gh_token:
headers = {
'Authorization': 'token ' + gh_token
}

return headers


def make_gh_request(url, err=True, msg=None):
"""Method for making GET requests to GitHub API
"""Method for making GET requests to GitHub API.
Args:
url (str): URL on which the API request is to be made.
err (bool): Checks if an error message needs to be printed or not.
msg (str): Error message to be printed for a failed request.
Returns:
Response object: contains a server's response to an HTTP request.
Response object: Contains a server's response to an HTTP request.
"""
if not msg:
msg = (
"Unable to connect. If your network is working properly, you might"
" have reached Github's API request limit. Try adding a Github API"
" token to the 'POPPER_GITHUB_API_TOKEN' variable."
"Unable to connect. Please check your network connection."
)

response = requests.get(url, headers=get_gh_headers())
response = requests.get(url)
if err and response.status_code != 200:
log.fail(msg)
else:
Expand All @@ -169,7 +152,7 @@ def read_search_sources():
"""Method to fetch the list of actions.

Returns:
list : The list of actions.
list: The list of actions.
"""
response = make_gh_request(
'https://raw.githubusercontent.com/systemslab/popper/'
Expand All @@ -178,16 +161,16 @@ def read_search_sources():
return yaml.load(response.text, Loader=yaml.FullLoader)


def fetch_metadata(update_cache):
def fetch_metadata(update_cache=False):
"""Fetch metatdata of the repositories from the
search_sources on which to run the search.

Args:
update_cache (bool) : Flag variable to decide whether to update
update_cache (bool): Flag variable to decide whether to update
the cache or not.

Returns:
dict : All metadata related to the actions.
dict: All metadata related to the actions.
"""
cache_file = setup_cache()

Expand All @@ -210,7 +193,7 @@ def fetch_metadata(update_cache):
_, _, user, repo, path_to_action, version = scm.parse(url)
source_list.append((user, repo, path_to_action, version))

metadata = defaultdict(dict)
metadata = dict()
with click.progressbar(
source_list,
show_eta=False,
Expand All @@ -235,32 +218,32 @@ def fetch_repo_metadata(user, repo, path_to_action, version):
"""Returns the metadata for a repo.

Args:
user (str) : The user to which the actions belongs to.
repo (str) : The parent repository name.
user (str): The user to which the actions belongs to.
repo (str): The parent repository name.
path_to_action (str): The path to the action from the root.
version (str) : The branch where the action resides.
version (str): The branch where the action resides.

Returns:
dict : Metadata of the repo.
dict: Metadata of the repo.
"""
readme = fetch_readme_for_repo(user, repo, path_to_action, version)
meta = dict()
meta['repo_readme'] = readme
return meta


def fetch_readme_for_repo(user, repo, path_to_action, version):
def fetch_readme_for_repo(user, repo, path_to_action, version=None):
"""Method to fetch the README for the repo
if present.

Args:
user (str) : The user to which the actions belongs to.
repo (str) : The parent repository name.
user (str): The user to which the actions belongs to.
repo (str): The parent repository name.
path_to_action (str): The path to the action from the root.
version (str) : The branch where the action resides.
version (str): The branch where the action resides.

Returns:
str : The contents of the README file.
str: The contents of the README file.

"""
if not version:
Expand All @@ -272,4 +255,14 @@ def fetch_readme_for_repo(user, repo, path_to_action, version):


def sanitized_name(name):
"""Clean an action name and change it to
proper format. It replaces all the unwanted
characters with `_`.

Args:
name (str): The crude action name.

Returns:
str: The sanitized action name.
"""
return re.sub('[^a-zA-Z0-9_.-]', '_', name)
1 change: 1 addition & 0 deletions cli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
packages=['popper', 'popper.commands'],
include_package_data=True,
install_requires=[
'requests-mock',
'requests',
'GitPython',
'future; python_version == "2.7"',
Expand Down
Loading
0