Skip to content

Commit 1f549d9

Browse files
authored
Merge pull request #3403 from samsrabin/fix-py_env_create-tests
Fix py_env_create and tests
2 parents 1b9fbe4 + a627c5c commit 1f549d9

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

py_env_create

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,41 @@ if [ ! -f $condafile ]; then
143143
exit -1
144144
fi
145145

146+
# Given two version number strings (unsigned integers separated by periods), echo "true" if the
147+
# second one is "greater than or equal to" the first. Otherwise, echo "false".
148+
# Based on https://unix.stackexchange.com/a/285928/208738
149+
function is_version2_ge_version1 {
150+
requiredver=$1
151+
currentver=$2
152+
if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
153+
echo "true"
154+
else
155+
echo "false"
156+
fi
157+
}
158+
146159
#
147160
# Handle an environment that already exists
148161
#
149162
function conda_env_exists {
150-
${condamamba} env list | grep -oE "/$1$" | wc -l
163+
# Not using ${condamamba} because, at least as of mamba 2.3.1, it doesn't always show environment
164+
# names. See https://github.com/mamba-org/mamba/issues/4045
165+
conda env list | grep -oE "/$1$" | wc -l
151166
}
152167
function rename_existing_env {
153168
if [[ "$CONDA_DEFAULT_ENV" == *"$1" ]]; then
154169
echo "Not going to let you rename the currently active env" >&2
155170
exit 1
156171
elif [[ $(conda_env_exists $2) -eq 0 ]]; then
157172
echo "Renaming $1 to $2 (this will take a few minutes)..."
158-
yes_tmp=${yes/yes/force}
173+
conda_version=$(conda --version | cut -d" " -f2)
174+
if [[ "$(is_version2_ge_version1 25.3.0 ${conda_version})" == "true" ]]; then
175+
yes_tmp=$yes
176+
else
177+
# Before conda 25.3.0, conda rename needed --force instead of --yes
178+
# https://github.com/conda/conda/blob/408abc3a3826c80750883accb8896005e3ac97b2/CHANGELOG.md?plain=1#L291
179+
yes_tmp=${yes/yes/force}
180+
fi
159181
# Use conda instead of $condamamba because, at least as of mamba 1.5.9 / conda 24.7.1,
160182
# rename is not supported through mamba.
161183
if [[ "${quiet}" == "--quiet" ]]; then

python/ctsm/test/test_sys_py_env_create.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ def setUp(self):
6060
self.py_env_create = os.path.join(path_to_ctsm_root(), "py_env_create")
6161
assert os.path.exists(self.py_env_create)
6262

63-
# Get path to testing condafile
63+
# Get path to testing conda/mambafile:
64+
# conda needs a completely empty file (# comments okay) as of 25.5.1, but mamba as of 2.3.1
65+
# needs at least some YML structure.
6466
self.empty_condafile = os.path.join(path_to_ctsm_root(), "python", "empty.txt")
65-
assert os.path.exists(self.empty_condafile)
67+
self.empty_mambafile = os.path.join(path_to_ctsm_root(), "python", "empty.yml")
6668

6769
# Set up other variables
6870
self.env_names = []
@@ -89,7 +91,12 @@ def _create_empty_env(self, check=None, extra_args=None, expect_error=False, new
8991
self.env_names.append(get_unique_env_name(5))
9092

9193
# Form and run command
92-
cmd = [self.py_env_create, "-n", self.env_names[-1], "-f", self.empty_condafile, "--yes"]
94+
if extra_args is not None and ("-m" in extra_args or "--mamba" in extra_args):
95+
empty_file = self.empty_mambafile
96+
else:
97+
empty_file = self.empty_condafile
98+
assert os.path.exists(empty_file)
99+
cmd = [self.py_env_create, "-n", self.env_names[-1], "-f", empty_file, "--yes"]
93100
if extra_args:
94101
cmd += extra_args
95102
out = subprocess.run(cmd, capture_output=True, text=True, check=False)
@@ -327,7 +334,8 @@ def test_complete_py_env_create(self):
327334
raise e
328335
env_list = get_conda_envs()
329336
for env_name in self.env_names:
330-
assert does_env_exist(env_name, env_list)
337+
if not does_env_exist(env_name, env_list):
338+
raise AssertionError(f"environment not found: {env_name}")
331339

332340
def test_complete_py_env_create_mamba(self):
333341
"""
@@ -358,7 +366,8 @@ def test_complete_py_env_create_mamba(self):
358366
raise e
359367
env_list = get_conda_envs()
360368
for env_name in self.env_names:
361-
assert does_env_exist(env_name, env_list)
369+
if not does_env_exist(env_name, env_list):
370+
raise AssertionError(f"environment not found: {env_name}")
362371

363372

364373
if __name__ == "__main__":

python/empty.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# An empty file for use in testing py_env_create with mamba, which needs some YML structure
2+
# (as opposed to conda, which can use a truly empty file)
3+
#
4+
channels:
5+
- conda-forge
6+
- defaults
7+
dependencies:

0 commit comments

Comments
 (0)