From fb79af2233f800534a36c25b9cfa8811521649cf Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 5 Dec 2022 18:53:27 +0800 Subject: [PATCH] Replace os.path.exists() with Path().stat().st_size > 0 --- pygmt/tests/test_grd2xyz.py | 6 +++--- pygmt/tests/test_grdclip.py | 4 ++-- pygmt/tests/test_grdfill.py | 4 ++-- pygmt/tests/test_grdgradient.py | 4 ++-- pygmt/tests/test_grdproject.py | 4 ++-- pygmt/tests/test_makecpt.py | 3 ++- pygmt/tests/test_triangulate.py | 6 +++--- pygmt/tests/test_xyz2grd.py | 4 ++-- 8 files changed, 18 insertions(+), 17 deletions(-) diff --git a/pygmt/tests/test_grd2xyz.py b/pygmt/tests/test_grd2xyz.py index 4cb172d3684..831e0e93d5e 100644 --- a/pygmt/tests/test_grd2xyz.py +++ b/pygmt/tests/test_grd2xyz.py @@ -1,7 +1,7 @@ """ Tests for grd2xyz. """ -import os +from pathlib import Path import numpy as np import pandas as pd @@ -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): @@ -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): diff --git a/pygmt/tests/test_grdclip.py b/pygmt/tests/test_grdclip.py index 85fd2071286..15d90b49452 100644 --- a/pygmt/tests/test_grdclip.py +++ b/pygmt/tests/test_grdclip.py @@ -1,7 +1,7 @@ """ Tests for grdclip. """ -import os +from pathlib import Path import pytest import xarray as xr @@ -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 diff --git a/pygmt/tests/test_grdfill.py b/pygmt/tests/test_grdfill.py index 89e6f3f3129..c6ffe9688d2 100644 --- a/pygmt/tests/test_grdfill.py +++ b/pygmt/tests/test_grdfill.py @@ -1,7 +1,7 @@ """ Tests for grdfill. """ -import os +from pathlib import Path import numpy as np import pytest @@ -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) diff --git a/pygmt/tests/test_grdgradient.py b/pygmt/tests/test_grdgradient.py index 10dd611fd0e..2236c2a7e6e 100644 --- a/pygmt/tests/test_grdgradient.py +++ b/pygmt/tests/test_grdgradient.py @@ -1,7 +1,7 @@ """ Tests for grdgradient. """ -import os +from pathlib import Path import pytest import xarray as xr @@ -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) diff --git a/pygmt/tests/test_grdproject.py b/pygmt/tests/test_grdproject.py index b8bce36cb6a..f4257c0899d 100644 --- a/pygmt/tests/test_grdproject.py +++ b/pygmt/tests/test_grdproject.py @@ -1,7 +1,7 @@ """ Tests for grdproject. """ -import os +from pathlib import Path import pytest import xarray as xr @@ -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) diff --git a/pygmt/tests/test_makecpt.py b/pygmt/tests/test_makecpt.py index 22a9ff1a1b8..7a8413d0aa2 100644 --- a/pygmt/tests/test_makecpt.py +++ b/pygmt/tests/test_makecpt.py @@ -2,6 +2,7 @@ Tests for makecpt. """ import os +from pathlib import Path import numpy as np import pytest @@ -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(): diff --git a/pygmt/tests/test_triangulate.py b/pygmt/tests/test_triangulate.py index d2d3b5e7cd1..618aa612f6f 100644 --- a/pygmt/tests/test_triangulate.py +++ b/pygmt/tests/test_triangulate.py @@ -1,7 +1,7 @@ """ Tests for triangulate. """ -import os +from pathlib import Path import numpy as np import pandas as pd @@ -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) @@ -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 diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index 51747b69c48..ad6c7a0f1f8 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -1,7 +1,7 @@ """ Tests for xyz2grd. """ -import os +from pathlib import Path import numpy as np import pytest @@ -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)