Skip to content

Commit

Permalink
Replace os.path.exists() with Path().stat().st_size > 0
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Dec 5, 2022
1 parent c5c404b commit fb79af2
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
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_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
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
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
6 changes: 3 additions & 3 deletions pygmt/tests/test_triangulate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for triangulate.
"""
import os
from pathlib import Path

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_delaunay_triples_outfile(dataframe, expected_dataframe):
result = triangulate.delaunay_triples(data=dataframe, outfile=tmpfile.name)
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", header=None)
pd.testing.assert_frame_equal(left=temp_df, right=expected_dataframe)

Expand Down Expand Up @@ -152,7 +152,7 @@ def test_regular_grid_with_outgrid_param(dataframe, expected_grid):
data=data, spacing=1, region=[2, 4, 5, 6], outgrid=tmpfile.name
)
assert output is None # check that output is None since outgrid is set
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
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)
assert grid.gmt.registration == 0 # Gridline registration
Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_xyz2grd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Tests for xyz2grd.
"""
import os
from pathlib import Path

import numpy as np
import pytest
Expand Down Expand Up @@ -63,7 +63,7 @@ def test_xyz2grd_input_array_file_out(ship_data, expected_grid):
outgrid=tmpfile.name,
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name)
assert Path(tmpfile.name).stat().st_size > 0
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)

Expand Down

0 comments on commit fb79af2

Please sign in to comment.