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

test: add an --offline option to pytest #1555

Merged
merged 1 commit into from
May 18, 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
17 changes: 17 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
import pytest

# This file lists files which should be ignored by pytest
collect_ignore = ["setup.py", "sopel.py", "sopel/modules/ipython.py"]


def pytest_addoption(parser):
parser.addoption('--offline', action='store_true', default=False)


def pytest_collection_modifyitems(config, items):
if not config.getoption('--offline'):
# nothing to skip
return

skip_online = pytest.mark.skip(reason='deactivated when --offline is used')
for item in items:
if 'online' in item.keywords:
item.add_marker(skip_online)
11 changes: 10 additions & 1 deletion sopel/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,12 @@ class example(object):
user_help:
Whether this example should be displayed in user-facing help output
such as `.help command`.
online:
If true, pytest will mark it as "online".
"""
def __init__(self, msg, result=None, privmsg=False, admin=False,
owner=False, repeat=1, re=False, ignore=None, user_help=False):
owner=False, repeat=1, re=False, ignore=None,
user_help=False, online=True):
# Wrap result into a list for get_example_test
if isinstance(result, list):
self.result = result
Expand All @@ -565,6 +568,7 @@ def __init__(self, msg, result=None, privmsg=False, admin=False,
self.admin = admin
self.owner = owner
self.repeat = repeat
self.online = online

if isinstance(ignore, list):
self.ignore = ignore
Expand All @@ -580,6 +584,7 @@ def __call__(self, func):
func.example = []

import sys
import pytest

import sopel.test_tools # TODO: fix circular import with sopel.bot and sopel.test_tools

Expand All @@ -590,6 +595,10 @@ def __call__(self, func):
func, self.msg, self.result, self.privmsg, self.admin,
self.owner, self.repeat, self.use_re, self.ignore
)

if self.online:
test = pytest.mark.online(test)

sopel.test_tools.insert_into_module(
test, func.__module__, func.__name__, 'test_example'
)
Expand Down
2 changes: 1 addition & 1 deletion sopel/modules/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def c(bot, trigger):


@commands('py')
@example('.py len([1,2,3])', '3')
@example('.py len([1,2,3])', '3', online=True)
def py(bot, trigger):
"""Evaluate a Python expression."""
if not trigger.group(2):
Expand Down
3 changes: 2 additions & 1 deletion sopel/modules/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def _find_geoip_db(bot):
@example('.ip 8.8.8.8',
r'\[IP\/Host Lookup\] Hostname: \S*dns\S*\.google\S* \| Location: United States \| ISP: AS15169 Google LLC',
re=True,
ignore='Downloading GeoIP database, please wait...')
ignore='Downloading GeoIP database, please wait...',
online=True)
def ip(bot, trigger):
"""IP Lookup tool"""
# Check if there is input at all
Expand Down
17 changes: 12 additions & 5 deletions sopel/modules/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,16 @@ def duck_api(query):

@commands('duck', 'ddg', 'g')
# test for bad Unicode handling in py2
@example('.duck grandorder.wiki chulainn alter', 'https://grandorder.wiki/Cú_Chulainn_(Alter)')
@example(
'.duck grandorder.wiki chulainn alter',
'https://grandorder.wiki/Cú_Chulainn_(Alter)',
online=True)
# the last example (in source line order) is what .help displays
@example('.duck sopel irc bot', r'https?:\/\/sopel\.chat\/?', re=True)
@example(
'.duck sopel irc bot',
r'https?:\/\/sopel\.chat\/?',
re=True,
online=True)
def duck(bot, trigger):
"""Queries DuckDuckGo for the specified input."""
query = trigger.group(2)
Expand Down Expand Up @@ -167,9 +174,9 @@ def search(bot, trigger):


@commands('suggest')
@example('.suggest wikip', 'wikipedia')
@example('.suggest ', 'No query term.')
@example('.suggest lkashdfiauwgeaef', 'Sorry, no result.')
@example('.suggest wikip', 'wikipedia', online=True)
@example('.suggest ', 'No query term.', online=True)
@example('.suggest lkashdfiauwgeaef', 'Sorry, no result.', online=True)
def suggest(bot, trigger):
"""Suggests terms starting with given input"""
if not trigger.group(2):
Expand Down
10 changes: 7 additions & 3 deletions sopel/modules/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ def tr(bot, trigger):


@commands('translate', 'tr')
@example('.tr :en :fr my dog', '"mon chien" (en to fr, translate.google.com)')
@example('.tr היי', '"Hey" (iw to en, translate.google.com)')
@example('.tr mon chien', '"my dog" (fr to en, translate.google.com)')
@example('.tr :en :fr my dog',
'"mon chien" (en to fr, translate.google.com)',
online=True)
@example('.tr היי', '"Hey" (iw to en, translate.google.com)', online=True)
@example('.tr mon chien',
'"my dog" (fr to en, translate.google.com)',
online=True)
def tr2(bot, trigger):
"""Translates a phrase, with an optional language hint."""
command = trigger.group(2)
Expand Down
5 changes: 4 additions & 1 deletion sopel/modules/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def setup(bot):


@module.commands('title')
@module.example('.title https://www.google.com', '[ Google ] - www.google.com')
@module.example(
'.title https://www.google.com',
'[ Google ] - www.google.com',
online=True)
def title_command(bot, trigger):
"""
Show the title or URL information for the given URL, or the last URL seen
Expand Down