Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python_linter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install python3 python3-pip netcdf-bin
sudo pip3 install pylint==2.6
sudo pip3 install pylint==2.16

# Run python unittests
- name: Lint the test directory
Expand Down
4 changes: 2 additions & 2 deletions tests/test_python/test_generate_FV3LAM_wflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" Defines an integration test for generate_FV3LAM_wflow script in the
ush directory """

#pylint: disable=invalid-name, no-self-use
#pylint: disable=invalid-name
import os
import sys
import unittest
Expand Down Expand Up @@ -68,7 +68,7 @@ def run_workflow(USHdir, logfile):
}
update_dict(cfg_updates, nco_test_config)

with open(f"{USHdir}/config.yaml", "w") as cfg_file:
with open(f"{USHdir}/config.yaml", "w", encoding="utf-8") as cfg_file:
cfg_file.write(cfg_to_yaml_str(nco_test_config))

run_workflow(USHdir, logfile)
Expand Down
1 change: 1 addition & 0 deletions tests/test_python/test_link_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def setUp(self):

# Create a space to link that data into. It need not be in the
# same space.
# pylint: disable=consider-using-with
self.tmp_dir = tempfile.TemporaryDirectory(
dir=os.path.abspath("."),
prefix="expt_fix_lam",
Expand Down
80 changes: 39 additions & 41 deletions tests/test_python/test_python_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_pattern_finding(self):
self.assertEqual(("lsm_ruc",), match)

# Test given a string
with open(test_file) as file_:
with open(test_file, encoding='utf-8') as file_:
content = file_.read()

util.find_pattern_in_str(pattern, content)
Expand All @@ -66,27 +66,26 @@ def test_check_for_preexist_dir_file(self):
""" Test that when an existing directory should be renamed, it
still exists and that a new directory is made"""

tmp_dir = tempfile.TemporaryDirectory(
with tempfile.TemporaryDirectory(
dir=os.path.abspath("."),
prefix="preexist_space",
)
) as tmp_dir:

# Check cmd_vrfy works
existing_dir = os.path.join(tmp_dir.name, "dir")
util.cmd_vrfy(f"mkdir -p {existing_dir}")
self.assertTrue(os.path.exists(existing_dir))
# Check cmd_vrfy works
existing_dir = os.path.join(tmp_dir, "dir")
util.cmd_vrfy(f"mkdir -p {existing_dir}")
self.assertTrue(os.path.exists(existing_dir))

# Given a preexisting directory, move it and test that they both
# exist.
util.check_for_preexist_dir_file(existing_dir, "rename")
dirs = glob.glob(f"{existing_dir}_*")
self.assertEqual(len(dirs), 1)
# Given a preexisting directory, move it and test that they both
# exist.
util.check_for_preexist_dir_file(existing_dir, "rename")
dirs = glob.glob(f"{existing_dir}_*")
self.assertEqual(len(dirs), 1)

# Clean up the older version, and test rm_vrfy
util.rm_vrfy(f"-rf {existing_dir}_*")
dirs = glob.glob(f"{existing_dir}_*")
self.assertEqual(len(dirs), 0)
tmp_dir.cleanup()
# Clean up the older version, and test rm_vrfy
util.rm_vrfy(f"-rf {existing_dir}_*")
dirs = glob.glob(f"{existing_dir}_*")
self.assertEqual(len(dirs), 0)

def test_check_var_valid_value(self):
""" Test that a string is available in a given list. """
Expand All @@ -95,28 +94,28 @@ def test_check_var_valid_value(self):
def test_filesys_cmds(self):
""" Test the functions that perform filesystem commands"""

tmp_dir = tempfile.TemporaryDirectory(
with tempfile.TemporaryDirectory(
dir=os.path.abspath("."),
prefix="filesys_space",
)
testable_path = os.path.join(
tmp_dir.name,
"dir",
)
) as tmp_dir:

# Make sure a desired path is created
util.mkdir_vrfy(testable_path)
self.assertTrue(os.path.exists(testable_path))
testable_path = os.path.join(
tmp_dir,
"dir",
)

# Make sure a file is copied
util.cp_vrfy(f"{self.ushdir}/python_utils/misc.py", f"{testable_path}/miscs.py")
self.assertTrue(os.path.exists(f"{testable_path}/miscs.py"))
# Make sure a desired path is created
util.mkdir_vrfy(testable_path)
self.assertTrue(os.path.exists(testable_path))

# Run a platform native command
util.cmd_vrfy(f"rm -rf {testable_path}")
# Make sure a file is copied
util.cp_vrfy(f"{self.ushdir}/python_utils/misc.py", f"{testable_path}/miscs.py")
self.assertTrue(os.path.exists(f"{testable_path}/miscs.py"))

self.assertFalse(os.path.exists(testable_path))
tmp_dir.cleanup()
# Run a platform native command
util.cmd_vrfy(f"rm -rf {testable_path}")

self.assertFalse(os.path.exists(testable_path))

def test_run_command(self):
""" Test the return of the run_command task is as expected."""
Expand All @@ -126,17 +125,16 @@ def test_create_symlink_to_file(self):
""" Test that a simlink is created as expected."""

target = f"{self.test_dir}/test_python_utils.py"
tmp_dir = tempfile.TemporaryDirectory(
with tempfile.TemporaryDirectory(
dir=os.path.abspath("."),
prefix="simlink_space",
)
) as tmp_dir:

symlink = os.path.join(
tmp_dir.name,
"test_python_utils.py"
)
util.create_symlink_to_file(target, symlink)
tmp_dir.cleanup()
symlink = os.path.join(
tmp_dir,
"test_python_utils.py"
)
util.create_symlink_to_file(target, symlink)

def test_define_macos_utilities(self):
""" Test that environment setting and getting utils work. Also,
Expand Down
1 change: 1 addition & 0 deletions tests/test_python/test_set_FV3nml_ens_stoch_seeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def setUp(self):
PARMdir = os.path.join(USHdir, "..", "parm")

# Create an temporary experiment directory
# pylint: disable=consider-using-with

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a reason to not use "with" here for the temporary directory?

self.tmp_dir = tempfile.TemporaryDirectory(
dir=os.path.abspath("."),
prefix="expt",
Expand Down
3 changes: 2 additions & 1 deletion tests/test_python/test_set_FV3nml_sfc_climo_filenames.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class Testing(unittest.TestCase):
""" Define the tests """
def test_set_FV3nml_sfc_climo_filenames(self): #pylint: disable=no-self-use
def test_set_FV3nml_sfc_climo_filenames(self):
""" Call the function and don't raise an Exception. """
set_FV3nml_sfc_climo_filenames()

Expand All @@ -30,6 +30,7 @@ def setUp(self):

# Create a temporary experiment directory structure
here = os.getcwd()
# pylint: disable=consider-using-with
self.tmp_dir = tempfile.TemporaryDirectory(
dir=os.path.abspath(here),
prefix="expt",
Expand Down
8 changes: 4 additions & 4 deletions tests/test_python/test_set_gridparams_ESGgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def test_set_gridparams_ESGgrid(self):
halo_width=6,
delx=3000.0,
dely=3000.0,
constants=dict(
RADIUS_EARTH=6371200.0,
DEGS_PER_RADIAN=57.29577951308232087679,
),
constants={
"RADIUS_EARTH": 6371200.0,
"DEGS_PER_RADIAN": 57.29577951308232087679,
},
)

self.assertEqual(
Expand Down
32 changes: 16 additions & 16 deletions tests/test_python/test_set_predef_grid_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ def test_set_predef_grid_params(self):
in the parameter dict."""
test_dir = os.path.dirname(os.path.abspath(__file__))
ushdir = os.path.join(test_dir, "..", "..", "ush")
fcst_config = dict(
PREDEF_GRID_NAME="RRFS_CONUS_3km",
QUILTING=False,
DT_ATMOS=36,
LAYOUT_X=18,
LAYOUT_Y=36,
BLOCKSIZE=28,
)
fcst_config = {
"PREDEF_GRID_NAME": "RRFS_CONUS_3km",
"QUILTING": False,
"DT_ATMOS": 36,
"LAYOUT_X": 18,
"LAYOUT_Y": 36,
"BLOCKSIZE": 28,
}
params_dict = set_predef_grid_params(
ushdir,
fcst_config["PREDEF_GRID_NAME"],
fcst_config["QUILTING"],
)
self.assertEqual(params_dict["GRID_GEN_METHOD"], "ESGgrid")
self.assertEqual(params_dict["ESGgrid_LON_CTR"], -97.5)
fcst_config = dict(
PREDEF_GRID_NAME="RRFS_CONUS_3km",
QUILTING=True,
DT_ATMOS=36,
LAYOUT_X=18,
LAYOUT_Y=36,
BLOCKSIZE=28,
)
fcst_config = {
"PREDEF_GRID_NAME": "RRFS_CONUS_3km",
"QUILTING": True,
"DT_ATMOS": 36,
"LAYOUT_X": 18,
"LAYOUT_Y": 36,
"BLOCKSIZE": 28,
}
params_dict = set_predef_grid_params(
ushdir,
fcst_config["PREDEF_GRID_NAME"],
Expand Down