Skip to content

Commit

Permalink
Check if a module outputs to a temporary file using "Path().stat().st…
Browse files Browse the repository at this point in the history
…_size > 0" (GenericMappingTools#2224)
  • Loading branch information
seisman authored and Josh Sixsmith committed Dec 21, 2022
1 parent fa37285 commit dd7f40b
Show file tree
Hide file tree
Showing 29 changed files with 62 additions and 58 deletions.
4 changes: 2 additions & 2 deletions pygmt/tests/test_binstats.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for binstats.
"""
import os
from pathlib import Path

import numpy.testing as npt
from pygmt import binstats
Expand All @@ -23,7 +23,7 @@ def test_binstats_outgrid():
region="g",
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists


def test_binstats_no_outgrid():
Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_blockm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for blockmean and blockmode.
"""
import os
from pathlib import Path

import numpy as np
import numpy.testing as npt
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_blockmean_input_filename():
outfile=tmpfile.name,
)
assert output is None # check that output is None since outfile is set
assert os.path.exists(path=tmpfile.name) # check that outfile exists at path
assert Path(tmpfile.name).stat().st_size > 0 # check that outfile exists
output = pd.read_csv(tmpfile.name, sep="\t", header=None)
assert output.shape == (5849, 3)
npt.assert_allclose(output.iloc[0], [245.888877, 29.978707, -384.0])
Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_blockmedian.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for blockmedian.
"""
import os
from pathlib import Path

import numpy.testing as npt
import pandas as pd
Expand Down Expand Up @@ -82,7 +82,7 @@ def test_blockmedian_input_filename():
outfile=tmpfile.name,
)
assert output is None # check that output is None since outfile is set
assert os.path.exists(path=tmpfile.name) # check that outfile exists at path
assert Path(tmpfile.name).stat().st_size > 0 # check that outfile exists
output = pd.read_csv(tmpfile.name, sep="\t", header=None)
assert output.shape == (5849, 3)
npt.assert_allclose(output.iloc[0], [245.88819, 29.97895, -385.0])
Expand Down
3 changes: 2 additions & 1 deletion pygmt/tests/test_clib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
from contextlib import contextmanager
from itertools import product
from pathlib import Path

import numpy as np
import numpy.testing as npt
Expand Down Expand Up @@ -159,7 +160,7 @@ def test_call_module():
with clib.Session() as lib:
with GMTTempFile() as out_fname:
lib.call_module("info", f"{data_fname} -C ->{out_fname.name}")
assert os.path.exists(out_fname.name)
assert Path(out_fname.name).stat().st_size > 0
output = out_fname.read().strip()
assert output == "11.5309 61.7074 -2.9289 7.8648 0.1412 0.9338"

Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_dimfilter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for dimfilter.
"""
import os
from pathlib import Path

import pytest
import xarray as xr
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_dimfilter_outgrid(grid, expected_grid):
region=[-55, -51, -24, -19],
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)

Expand Down
3 changes: 2 additions & 1 deletion pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Doesn't include the plotting commands which have their own test files.
"""
import os
from pathlib import Path

try:
import IPython
Expand Down Expand Up @@ -151,7 +152,7 @@ def test_figure_savefig_filename_with_spaces():
with GMTTempFile(prefix="pygmt-filename with spaces", suffix=".png") as imgfile:
fig.savefig(fname=imgfile.name)
assert r"\040" not in os.path.abspath(imgfile.name)
assert os.path.exists(imgfile.name)
assert Path(imgfile.name).stat().st_size > 0


def test_figure_savefig():
Expand Down
7 changes: 3 additions & 4 deletions pygmt/tests/test_filter1d.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
Tests for filter1d.
"""

import os
from pathlib import Path

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -38,7 +37,7 @@ def test_filter1d_file_output(data):
data=data, filter_type="g5", outfile=tmpfile.name, output_type="file"
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outfile exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outfile exists


def test_filter1d_invalid_format(data):
Expand Down Expand Up @@ -77,7 +76,7 @@ def test_filter1d_outfile_incorrect_output_type(data):
data=data, filter_type="g5", outfile=tmpfile.name, output_type="numpy"
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outfile exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outfile exists


def test_filter1d_format(data):
Expand Down
6 changes: 3 additions & 3 deletions pygmt/tests/test_grd2xyz.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for grd2xyz.
"""
import os
from pathlib import Path

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_grd2xyz_file_output(grid):
with GMTTempFile(suffix=".xyz") as tmpfile:
result = grd2xyz(grid=grid, outfile=tmpfile.name, output_type="file")
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outfile exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outfile exists


def test_grd2xyz_invalid_format(grid):
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_grd2xyz_outfile_incorrect_output_type(grid):
with GMTTempFile(suffix=".xyz") as tmpfile:
result = grd2xyz(grid=grid, outfile=tmpfile.name, output_type="numpy")
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outfile exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outfile exists


def test_grd2xyz_pandas_output_with_o(grid):
Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_grdclip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for grdclip.
"""
import os
from pathlib import Path

import pytest
import xarray as xr
Expand Down Expand Up @@ -47,7 +47,7 @@ def test_grdclip_outgrid(grid, expected_grid):
region=[-53, -49, -19, -16],
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
temp_grid = load_dataarray(tmpfile.name)
assert temp_grid.dims == ("lat", "lon")
assert temp_grid.gmt.gtype == 1 # Geographic grid
Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_grdfill.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for grdfill.
"""
import os
from pathlib import Path

import numpy as np
import pytest
Expand Down Expand Up @@ -115,7 +115,7 @@ def test_grdfill_file_out(grid, expected_grid):
with GMTTempFile(suffix=".nc") as tmpfile:
result = grdfill(grid=grid, mode="c20", outgrid=tmpfile.name)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outfile exists
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)

Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_grdfilter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for grdfilter.
"""
import os
from pathlib import Path

import numpy as np
import pytest
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_grdfilter_dataarray_in_file_out(grid, expected_grid):
region=[-53, -49, -20, -17],
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)

Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_grdgradient.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for grdgradient.
"""
import os
from pathlib import Path

import pytest
import xarray as xr
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_grdgradient_outgrid(grid, expected_grid):
grid=grid, outgrid=tmpfile.name, azimuth=10, region=[-53, -49, -20, -17]
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)

Expand Down
6 changes: 3 additions & 3 deletions pygmt/tests/test_grdhisteq.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for grdhisteq.
"""
import os
from pathlib import Path

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_equalize_grid_outgrid_file(grid, expected_grid, region):
grid=grid, divisions=2, region=region, outgrid=tmpfile.name
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)

Expand Down Expand Up @@ -112,7 +112,7 @@ def test_compute_bins_outfile(grid, expected_df, region):
)
assert len(record) == 1 # check that only one warning was raised
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name)
assert Path(tmpfile.name).stat().st_size > 0
temp_df = pd.read_csv(
filepath_or_buffer=tmpfile.name,
sep="\t",
Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_grdlandmask.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for grdlandmask.
"""
import os
from pathlib import Path

import pytest
import xarray as xr
Expand Down Expand Up @@ -39,7 +39,7 @@ def test_grdlandmask_outgrid(expected_grid):
with GMTTempFile(suffix=".nc") as tmpfile:
result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[125, 130, 30, 35])
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)

Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_grdproject.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for grdproject.
"""
import os
from pathlib import Path

import pytest
import xarray as xr
Expand Down Expand Up @@ -53,7 +53,7 @@ def test_grdproject_file_out(grid, expected_grid):
region=[-53, -51, -20, -17],
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)

Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_grdsample.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for grdsample.
"""
import os
from pathlib import Path

import pytest
import xarray as xr
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_grdsample_file_out(grid, expected_grid, region, spacing):
grid=grid, outgrid=tmpfile.name, spacing=spacing, region=region
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)

Expand Down
3 changes: 2 additions & 1 deletion pygmt/tests/test_grdtrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tests for grdtrack.
"""
import os
from pathlib import Path

import numpy as np
import numpy.testing as npt
Expand Down Expand Up @@ -72,7 +73,7 @@ def test_grdtrack_input_csvfile_and_dataarray(dataarray, expected_array):
with GMTTempFile() as tmpfile:
output = grdtrack(points=POINTS_DATA, grid=dataarray, outfile=tmpfile.name)
assert output is None # check that output is None since outfile is set
assert os.path.exists(path=tmpfile.name) # check that outfile exists at path
assert Path(tmpfile.name).stat().st_size > 0 # check that outfile exists
output = np.loadtxt(tmpfile.name)
npt.assert_allclose(np.array(output), expected_array)

Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_grdvolume.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for grdvolume.
"""
import os
from pathlib import Path

import numpy as np
import numpy.testing as npt
Expand Down Expand Up @@ -98,4 +98,4 @@ def test_grdvolume_outgrid(grid, region):
region=region,
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outfile exists
assert Path(tmpfile.name).stat().st_size > 0 # check that outfile exists
3 changes: 2 additions & 1 deletion pygmt/tests/test_makecpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tests for makecpt.
"""
import os
from pathlib import Path

import numpy as np
import pytest
Expand Down Expand Up @@ -76,7 +77,7 @@ def test_makecpt_output_cpt_file():
"""
with GMTTempFile(suffix=".cpt") as cptfile:
makecpt(output=cptfile.name)
assert os.path.exists(cptfile.name)
assert Path(cptfile.name).stat().st_size > 0


def test_makecpt_blank_output():
Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_nearneighbor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for nearneighbor.
"""
import os
from pathlib import Path

import numpy as np
import numpy.testing as npt
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_nearneighbor_with_outgrid_param(ship_data):
search_radius="10m",
)
assert output is None # check that output is None since outgrid is set
assert os.path.exists(path=tmpfile.name) # check that outgrid exists at path
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
with xr.open_dataarray(tmpfile.name) as grid:
assert isinstance(grid, xr.DataArray) # ensure netcdf grid loads ok
assert grid.shape == (121, 121)
Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_project.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for project.
"""
import os
from pathlib import Path

import numpy as np
import numpy.testing as npt
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_project_output_filename(dataframe):
outfile=tmpfile.name,
)
assert output is None # check that output is None since outfile is set
assert os.path.exists(path=tmpfile.name) # check that outfile exists at path
assert Path(tmpfile.name).stat().st_size > 0 # check that outfile exists
output = pd.read_csv(tmpfile.name, sep="\t", header=None)
assert output.shape == (1, 6)
npt.assert_allclose(
Expand Down
Loading

0 comments on commit dd7f40b

Please sign in to comment.