Skip to content
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
9 changes: 7 additions & 2 deletions .github/actions/test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ name:
description:
Run pytest, and run the doctest runner (shapefile.py as a script).

inputs:
extra_args:
type: string
default: '-m "not network"'

runs:
using: "composite"
steps:
Expand All @@ -13,7 +18,7 @@ runs:

- name: Doctests
shell: bash
run: python shapefile.py
run: python shapefile.py ${{ inputs.extra_args }}

- name: Install test dependencies.
shell: bash
Expand All @@ -24,7 +29,7 @@ runs:
- name: Pytest
shell: bash
run: |
pytest
pytest ${{ inputs.extra_args }}

- name: Show versions for logs.
shell: bash
Expand Down
65 changes: 51 additions & 14 deletions shapefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2776,13 +2776,56 @@ def field(self, name, fieldType="C", size="50", decimal=0):


# Begin Testing
def test(**kwargs):
def _get_doctests():
import doctest

doctest.NORMALIZE_WHITESPACE = 1
verbosity = kwargs.get("verbose", 0)

# run tests
with open("README.md", "rb") as fobj:
tests = doctest.DocTestParser().get_doctest(
string=fobj.read().decode("utf8").replace("\r\n", "\n"),
globs={},
name="README",
filename="README.md",
lineno=0,
)

return tests


def _get_no_network_doctests(examples):
globals_from_network_doctests = set()
for example in examples:
if 'sf = shapefile.Reader("https://' in example.source:
globals_from_network_doctests.add("sf")
continue
lhs = example.source.partition("=")[0]

for target in lhs.split(","):
target = target.strip()
if target in globals_from_network_doctests:
globals_from_network_doctests.remove(target)

if globals_from_network_doctests:
continue

yield example


def _test(verbosity=0):
if verbosity == 0:
print("Running doctests...")
print("Getting doctests...")
tests = _get_doctests()

if len(sys.argv) >= 3 and sys.argv[1:3] == ["-m", "not network"]:
if verbosity == 0:
print("Removing doctests requiring internet access...")
tests.examples = list(_get_no_network_doctests(tests.examples))

import doctest

doctest.NORMALIZE_WHITESPACE = 1

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

# run tests
runner = doctest.DocTestRunner(checker=Py23DocChecker(), verbose=verbosity)
with open("README.md", "rb") as fobj:
test = doctest.DocTestParser().get_doctest(
string=fobj.read().decode("utf8").replace("\r\n", "\n"),
globs={},
name="README",
filename="README.md",
lineno=0,
)
failure_count, test_count = runner.run(test)

if verbosity == 0:
print("Running %s doctests..." % len(tests.examples))
failure_count, test_count = runner.run(tests)

# print results
if verbosity:
Expand All @@ -2827,5 +2864,5 @@ def summarize(self):
Doctests are contained in the file 'README.md', and are tested using the built-in
testing libraries.
"""
failure_count = test()
failure_count = _test()
sys.exit(failure_count)
Loading