summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/cmd/env.py25
-rw-r--r--lib/spack/spack/test/cmd/env.py19
2 files changed, 40 insertions, 4 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