From 466920f27a5d31482dabb89a8df191aa5d6e8e1c Mon Sep 17 00:00:00 2001 From: James Parrott <80779630+JamesParrott@users.noreply.github.com> Date: Fri, 18 Jul 2025 23:07:29 +0100 Subject: [PATCH] Make flakey network dependent tests optional This reverts commit 2dc17a78c4eb90196b36169e897ec36f55a6223e. Run on this branch Instead of plain pytest, run pytest -m "not network" Filter out pytest tests and doctests requiring internet downloads Run Ruff format Make skipping network tests optional --- .github/actions/test/action.yml | 9 ++++- shapefile.py | 65 ++++++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/.github/actions/test/action.yml b/.github/actions/test/action.yml index 86ec93f3..10206063 100644 --- a/.github/actions/test/action.yml +++ b/.github/actions/test/action.yml @@ -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: @@ -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 @@ -24,7 +29,7 @@ runs: - name: Pytest shell: bash run: | - pytest + pytest ${{ inputs.extra_args }} - name: Show versions for logs. shell: bash diff --git a/shapefile.py b/shapefile.py index 3a6bbe7e..be3650db 100644 --- a/shapefile.py +++ b/shapefile.py @@ -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 @@ -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: @@ -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)