Skip to content

Commit bdfd451

Browse files
committed
Run url tests on real urls on one runner (3.14 on Ubuntu)
Try usgs.gov instead of github.com for doctests Put https://github.com/nvkelso/natural-earth-vector tests in a separate localhost only test function Avoid nvkelson's repo (natural-earth-vector) don't do doctests Try true / false instead of yes / no Try to test remote urls from one job in the test matrix only Add extra url of .zipped shapefile and test it
1 parent bf968a0 commit bdfd451

5 files changed

Lines changed: 65 additions & 27 deletions

File tree

.github/actions/test/action.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ inputs:
1010
default: '-m "not network"'
1111
required: false
1212
replace_remote_urls_with_localhost:
13-
description: yes or no. Test loading shapefiles from a url, without overloading an external server from 30 parallel workflows.
14-
default: 'no'
13+
description: true or false. Test loading shapefiles from a url, without overloading an external server from 30 parallel workflows.
14+
default: 'false'
1515
required: false
1616
pyshp_repo_directory:
1717
description: Path to where the PyShp repo was checked out to (to keep separate from Shapefiles & artefacts repo).
@@ -40,7 +40,7 @@ runs:
4040
# uses: ./Pyshp/.github/actions/test
4141
# with:
4242
# extra_args: ""
43-
# replace_remote_urls_with_localhost: 'yes'
43+
# replace_remote_urls_with_localhost: 'true'
4444
# pyshp_repo_directory: ./Pyshp
4545

4646
# The Python to be tested with is required to already be setup,
@@ -51,14 +51,14 @@ runs:
5151

5252

5353
- name: Checkout shapefiles and zip file artefacts repo
54-
if: ${{ inputs.replace_remote_urls_with_localhost == 'yes' }}
54+
if: ${{ inputs.replace_remote_urls_with_localhost == 'true' }}
5555
uses: actions/checkout@v6
5656
with:
5757
repository: JamesParrott/PyShp_test_shapefile
5858
path: ./PyShp_test_shapefile
5959

6060
- name: Serve shapefiles and zip file artefacts on localhost
61-
if: ${{ inputs.replace_remote_urls_with_localhost == 'yes' }}
61+
if: ${{ inputs.replace_remote_urls_with_localhost == 'true' }}
6262
shell: bash
6363
working-directory: ./PyShp_test_shapefile
6464
run: |
@@ -105,12 +105,12 @@ runs:
105105
# - name: Test http server
106106
# # (needs a full Github Actions runner or a Python non-slim Docker image,
107107
# # as the slim Debian images don't have curl or wget).
108-
# if: ${{ inputs.replace_remote_urls_with_localhost == 'yes' }}
108+
# if: ${{ inputs.replace_remote_urls_with_localhost == 'true' }}
109109
# shell: bash
110110
# run: curl http://localhost:8000/ne_110m_admin_0_tiny_countries.shp
111111

112112
- name: Stop http server
113-
if: ${{ inputs.replace_remote_urls_with_localhost == 'yes' }}
113+
if: ${{ inputs.replace_remote_urls_with_localhost == 'true' }}
114114
shell: bash
115115
run: |
116116
echo Killing http server process ID: ${{ env.HTTP_SERVER_PID }}

.github/workflows/run_checks_build_and_test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ jobs:
8787
with:
8888
pyshp_repo_directory: ./Pyshp
8989

90-
- name: Network tests
90+
- name: "Network tests (on Localhost ${{ !(matrix.os == 'ubuntu-24.04' && matrix.python-version == '3.14') }})"
9191
uses: ./Pyshp/.github/actions/test
9292
with:
9393
extra_args: '-m network'
94-
replace_remote_urls_with_localhost: 'yes'
94+
replace_remote_urls_with_localhost: ${{ !(matrix.os == 'ubuntu-24.04' && matrix.python-version == '3.14') }}
9595
# Checkout to ./PyShp, as the test job also needs to check out the artefact repo
9696
pyshp_repo_directory: ./Pyshp

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ Finally, you can use all of the above methods to read shapefiles directly from t
435435
>>> sf = shapefile.Reader("https://github.com/JamesParrott/PyShp_test_shapefile/raw/main/gis_osm_natural_a_free_1.zip")
436436

437437
>>> # from a shapefile collection of files in a github repository
438-
>>> sf = shapefile.Reader("https://github.com/nvkelso/natural-earth-vector/blob/master/110m_cultural/ne_110m_admin_0_tiny_countries.shp?raw=true")
438+
>>> sf = shapefile.Reader("https://pubs.usgs.gov/of/2000/of00-006/gisdata/coverage/airports.shp")
439439

440440
This will automatically download the file(s) to a temporary location before reading, saving you a lot of time and repetitive boilerplate code when you just want quick access to some external data.
441441

src/shapefile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
# Test config (for the Doctest runner and test_shapefile.py)
6262
REPLACE_REMOTE_URLS_WITH_LOCALHOST = (
63-
os.getenv("REPLACE_REMOTE_URLS_WITH_LOCALHOST", "").lower() == "yes"
63+
os.getenv("REPLACE_REMOTE_URLS_WITH_LOCALHOST", "").lower() == "true"
6464
)
6565

6666
# Constants for shape types

test_shapefile.py

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -479,25 +479,25 @@ def test_shaperecord_geo_interface():
479479
assert json.dumps(shaperec.__geo_interface__)
480480

481481

482+
@pytest.mark.skipif(
483+
not shapefile.REPLACE_REMOTE_URLS_WITH_LOCALHOST,
484+
reason="Flakey test, fails due to Github rate limit",
485+
)
482486
@pytest.mark.network
483-
def test_reader_url():
487+
def test_reader_nvkelso_files_from_localhost_url():
484488
"""
485489
Assert that Reader can open shapefiles from a url.
486490
"""
487491

488-
# Allow testing loading of shapefiles from a url on localhost (to avoid
489-
# overloading external servers, and associated spurious test failures).
492+
# Only test these shapefiles from localhost,
493+
# https://github.com/nvkelso/natural-earth-vector urls throws 426 errors ("too many downloads").
490494
# A suitable repo of test files, and a localhost server setup is
491495
# defined in ./.github/actions/test/actions.yml
492-
if shapefile.REPLACE_REMOTE_URLS_WITH_LOCALHOST:
493496

494-
def Reader(url):
495-
new_url = shapefile._replace_remote_url(url)
496-
print(f"repr(new_url): {repr(new_url)}")
497-
return shapefile.Reader(new_url)
498-
else:
499-
print("Using plain Reader")
500-
Reader = shapefile.Reader
497+
def Reader(url):
498+
new_url = shapefile._replace_remote_url(url)
499+
print(f"repr(new_url): {repr(new_url)}")
500+
return shapefile.Reader(new_url)
501501

502502
# test with extension
503503
url = "https://github.com/nvkelso/natural-earth-vector/blob/master/110m_cultural/ne_110m_admin_0_tiny_countries.shp?raw=true"
@@ -518,19 +518,57 @@ def Reader(url):
518518
assert sf._shx is None or sf.shx.closed
519519
assert sf.dbf.closed
520520

521+
522+
@pytest.mark.network
523+
def test_reader_urls():
524+
"""
525+
Assert that Reader can open shapefiles from a few different urls.
526+
"""
527+
528+
# Allow testing loading of shapefiles from a url on localhost (to avoid
529+
# overloading external servers, and associated spurious test failures).
530+
# A suitable repo of test files, and a localhost server setup is
531+
# defined in ./.github/actions/test/actions.yml
532+
if shapefile.REPLACE_REMOTE_URLS_WITH_LOCALHOST:
533+
534+
def Reader(url):
535+
new_url = shapefile._replace_remote_url(url)
536+
print(f"repr(new_url): {repr(new_url)}")
537+
return shapefile.Reader(new_url)
538+
else:
539+
print("Using plain Reader")
540+
Reader = shapefile.Reader
541+
521542
# test no files found
522543
url = "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/README.md"
523544
with pytest.raises(shapefile.ShapefileException):
524545
with Reader(url) as sf:
525546
pass
526547

527548
# test reading zipfile from url
528-
url = "https://github.com/JamesParrott/PyShp_test_shapefile/raw/main/gis_osm_natural_a_free_1.zip"
529-
with Reader(url) as sf:
530-
for __recShape in sf.iterShapeRecords():
549+
urls = [
550+
"https://github.com/JamesParrott/PyShp_test_shapefile/raw/main/gis_osm_natural_a_free_1.zip",
551+
"http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_admin_0_boundary_lines_land.zip",
552+
]
553+
for url in urls:
554+
try:
555+
with Reader(url) as sf:
556+
for __recShape in sf.iterShapeRecords():
557+
pass
558+
assert len(sf) > 0
559+
except shapefile.HTTPError:
531560
pass
532-
assert len(sf) > 0
533-
assert sf.shp.closed is sf.shx.closed is sf.dbf.closed is True
561+
else:
562+
assert sf.shp.closed is sf.shx.closed is sf.dbf.closed is True
563+
break
564+
else:
565+
raise shapefile.HTTPError(
566+
"\n".join(urls),
567+
"Could not download .zipped shapefiles from any of the test urls",
568+
404,
569+
{},
570+
None,
571+
)
534572

535573

536574
def test_reader_zip():

0 commit comments

Comments
 (0)