summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/spack/spack/cmd/env.py25
-rw-r--r--lib/spack/spack/test/cmd/env.py19
-rwxr-xr-xshare/spack/qa/setup-env-test.fish6
-rwxr-xr-xshare/spack/qa/setup-env-test.sh6
-rwxr-xr-xshare/spack/setup-env.sh3
5 files changed, 51 insertions, 8 deletions
diff --git a/lib/spack/spack/cmd/env.py b/lib/spack/spack/cmd/env.py
index d75cf6b262..bf1f29d558 100644
--- a/lib/spack/spack/cmd/env.py
+++ b/lib/spack/spack/cmd/env.py
@@ -5,6 +5,7 @@
import argparse
import os
+import shlex
import shutil
import sys
import tempfile
@@ -144,10 +145,13 @@ def create_temp_env_directory():
return tempfile.mkdtemp(prefix="spack-")
-def env_activate(args):
- if not args.activate_env and not args.dir and not args.temp:
- tty.die("spack env activate requires an environment name, directory, or --temp")
+def _tty_info(msg):
+ """tty.info like function that prints the equivalent printf statement for eval."""
+ decorated = f'{colorize("@*b{==>}")} {msg}\n'
+ print(f"printf {shlex.quote(decorated)};")
+
+def env_activate(args):
if not args.shell:
spack.cmd.common.shell_init_instructions(
"spack env activate", " eval `spack env activate {sh_arg} [...]`"
@@ -160,12 +164,25 @@ def env_activate(args):
env_name_or_dir = args.activate_env or args.dir
+ # When executing `spack env activate` without further arguments, activate
+ # the default environment. It's created when it doesn't exist yet.
+ if not env_name_or_dir and not args.temp:
+ short_name = "default"
+ if not ev.exists(short_name):
+ ev.create(short_name)
+ action = "Created and activated"
+ else:
+ action = "Activated"
+ env_path = ev.root(short_name)
+ _tty_info(f"{action} default environment in {env_path}")
+
# Temporary environment
- if args.temp:
+ elif args.temp:
env = create_temp_env_directory()
env_path = os.path.abspath(env)
short_name = os.path.basename(env_path)
ev.create_in_dir(env).write(regenerate=False)
+ _tty_info(f"Created and activated temporary environment in {env_path}")
# Managed environment
elif ev.exists(env_name_or_dir) and not args.dir:
diff --git a/lib/spack/spack/test/cmd/env.py b/lib/spack/spack/test/cmd/env.py
index 308e0b0e90..e291432a0f 100644
--- a/lib/spack/spack/test/cmd/env.py
+++ b/lib/spack/spack/test/cmd/env.py
@@ -2924,6 +2924,25 @@ def test_activate_temp(monkeypatch, tmpdir):
assert ev.is_env_dir(str(tmpdir))
+def test_activate_default(monkeypatch):
+ """Tests whether `spack env activate` creates / activates the default
+ environment"""
+ assert not ev.exists("default")
+
+ # Activating it the first time should create it
+ env("activate", "--sh")
+ env("deactivate", "--sh")
+ assert ev.exists("default")
+
+ # Activating it while it already exists should work
+ env("activate", "--sh")
+ env("deactivate", "--sh")
+ assert ev.exists("default")
+
+ env("remove", "-y", "default")
+ assert not ev.exists("default")
+
+
def test_env_view_fail_if_symlink_points_elsewhere(tmpdir, install_mockery, mock_fetch):
view = str(tmpdir.join("view"))
# Put a symlink to an actual directory in view
diff --git a/share/spack/qa/setup-env-test.fish b/share/spack/qa/setup-env-test.fish
index 6474917b70..589f4cbfa8 100755
--- a/share/spack/qa/setup-env-test.fish
+++ b/share/spack/qa/setup-env-test.fish
@@ -371,7 +371,6 @@ spt_contains " spack env list " spack env list --help
title 'Testing `spack env activate`'
spt_contains "No such environment:" spack env activate no_such_environment
-spt_contains "env activate requires an environment " spack env activate
spt_contains "usage: spack env activate " spack env activate -h
spt_contains "usage: spack env activate " spack env activate --help
@@ -415,6 +414,11 @@ spt_contains 'spack_test_2_env' 'fish' '-c' 'echo $PATH'
spt_does_not_contain 'spack_test_env' 'fish' '-c' 'echo $PATH'
despacktivate
+echo "Testing default environment"
+spack env activate
+contains "In environment default" spack env status
+despacktivate
+
echo "Correct error exit codes for activate and deactivate"
spt_fails spack env activate nonexisiting_environment
spt_fails spack env deactivate
diff --git a/share/spack/qa/setup-env-test.sh b/share/spack/qa/setup-env-test.sh
index 4172a40155..b26619b9cd 100755
--- a/share/spack/qa/setup-env-test.sh
+++ b/share/spack/qa/setup-env-test.sh
@@ -140,7 +140,6 @@ contains " spack env list " spack env list --help
title 'Testing `spack env activate`'
contains "No such environment:" spack env activate no_such_environment
-contains "env activate requires an environment " spack env activate
contains "usage: spack env activate " spack env activate -h
contains "usage: spack env activate " spack env activate --help
@@ -197,6 +196,11 @@ contains "spack_test_2_env" sh -c 'echo $PATH'
does_not_contain "spack_test_env" sh -c 'echo $PATH'
despacktivate
+echo "Testing default environment"
+spack env activate
+contains "In environment default" spack env status
+despacktivate
+
echo "Correct error exit codes for activate and deactivate"
fails spack env activate nonexisiting_environment
fails spack env deactivate
diff --git a/share/spack/setup-env.sh b/share/spack/setup-env.sh
index 91a601e652..a42882266c 100755
--- a/share/spack/setup-env.sh
+++ b/share/spack/setup-env.sh
@@ -126,8 +126,7 @@ _spack_shell_wrapper() {
# Space needed here to differentiate between `-h`
# argument and environments with "-h" in the name.
# Also see: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion
- if [ -z ${1+x} ] || \
- [ "${_a#* --sh}" != "$_a" ] || \
+ if [ "${_a#* --sh}" != "$_a" ] || \
[ "${_a#* --csh}" != "$_a" ] || \
[ "${_a#* -h}" != "$_a" ] || \
[ "${_a#* --help}" != "$_a" ];