Skip to content

Commit 89a4125

Browse files
authored
Merge pull request #319 from GeospatialPython/skip_network_tests
Make flakey network dependent tests optional
2 parents a9caa32 + 466920f commit 89a4125

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

.github/actions/test/action.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ name:
44
description:
55
Run pytest, and run the doctest runner (shapefile.py as a script).
66

7+
inputs:
8+
extra_args:
9+
type: string
10+
default: '-m "not network"'
11+
712
runs:
813
using: "composite"
914
steps:
@@ -13,7 +18,7 @@ runs:
1318

1419
- name: Doctests
1520
shell: bash
16-
run: python shapefile.py
21+
run: python shapefile.py ${{ inputs.extra_args }}
1722

1823
- name: Install test dependencies.
1924
shell: bash
@@ -24,7 +29,7 @@ runs:
2429
- name: Pytest
2530
shell: bash
2631
run: |
27-
pytest
32+
pytest ${{ inputs.extra_args }}
2833
2934
- name: Show versions for logs.
3035
shell: bash

shapefile.py

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,13 +2776,56 @@ def field(self, name, fieldType="C", size="50", decimal=0):
27762776

27772777

27782778
# Begin Testing
2779-
def test(**kwargs):
2779+
def _get_doctests():
27802780
import doctest
27812781

27822782
doctest.NORMALIZE_WHITESPACE = 1
2783-
verbosity = kwargs.get("verbose", 0)
2783+
2784+
# run tests
2785+
with open("README.md", "rb") as fobj:
2786+
tests = doctest.DocTestParser().get_doctest(
2787+
string=fobj.read().decode("utf8").replace("\r\n", "\n"),
2788+
globs={},
2789+
name="README",
2790+
filename="README.md",
2791+
lineno=0,
2792+
)
2793+
2794+
return tests
2795+
2796+
2797+
def _get_no_network_doctests(examples):
2798+
globals_from_network_doctests = set()
2799+
for example in examples:
2800+
if 'sf = shapefile.Reader("https://' in example.source:
2801+
globals_from_network_doctests.add("sf")
2802+
continue
2803+
lhs = example.source.partition("=")[0]
2804+
2805+
for target in lhs.split(","):
2806+
target = target.strip()
2807+
if target in globals_from_network_doctests:
2808+
globals_from_network_doctests.remove(target)
2809+
2810+
if globals_from_network_doctests:
2811+
continue
2812+
2813+
yield example
2814+
2815+
2816+
def _test(verbosity=0):
27842817
if verbosity == 0:
2785-
print("Running doctests...")
2818+
print("Getting doctests...")
2819+
tests = _get_doctests()
2820+
2821+
if len(sys.argv) >= 3 and sys.argv[1:3] == ["-m", "not network"]:
2822+
if verbosity == 0:
2823+
print("Removing doctests requiring internet access...")
2824+
tests.examples = list(_get_no_network_doctests(tests.examples))
2825+
2826+
import doctest
2827+
2828+
doctest.NORMALIZE_WHITESPACE = 1
27862829

27872830
# ignore py2-3 unicode differences
27882831
import re
@@ -2798,17 +2841,11 @@ def check_output(self, want, got, optionflags):
27982841
def summarize(self):
27992842
doctest.OutputChecker.summarize(True)
28002843

2801-
# run tests
28022844
runner = doctest.DocTestRunner(checker=Py23DocChecker(), verbose=verbosity)
2803-
with open("README.md", "rb") as fobj:
2804-
test = doctest.DocTestParser().get_doctest(
2805-
string=fobj.read().decode("utf8").replace("\r\n", "\n"),
2806-
globs={},
2807-
name="README",
2808-
filename="README.md",
2809-
lineno=0,
2810-
)
2811-
failure_count, test_count = runner.run(test)
2845+
2846+
if verbosity == 0:
2847+
print("Running %s doctests..." % len(tests.examples))
2848+
failure_count, test_count = runner.run(tests)
28122849

28132850
# print results
28142851
if verbosity:
@@ -2827,5 +2864,5 @@ def summarize(self):
28272864
Doctests are contained in the file 'README.md', and are tested using the built-in
28282865
testing libraries.
28292866
"""
2830-
failure_count = test()
2867+
failure_count = _test()
28312868
sys.exit(failure_count)

0 commit comments

Comments
 (0)