Skip to content
Open
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: 2 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ def mutable_config(tmpdir_factory, configuration_dir):
ramble.config.ConfigScope(name, str(mutable_dir.join(name)))
for name in ["site", "system", "user"]
]
scopes.append(ramble.config.InternalConfigScope("command_line"))

with ramble.config.use_configuration(*scopes) as cfg:
yield cfg
Expand All @@ -442,6 +443,7 @@ def mutable_empty_config(tmpdir_factory, configuration_dir):
ramble.config.ConfigScope(name, str(mutable_dir.join(name)))
for name in ["site", "system", "user"]
]
scopes.append(ramble.config.InternalConfigScope("command_line"))

with ramble.config.use_configuration(*scopes) as cfg:
yield cfg
Expand Down
1 change: 1 addition & 0 deletions lib/ramble/ramble/cmd/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ def help(parser, args):
parser.parse_args([args.help_command, "-h"])
else:
sys.stdout.write(parser.format_help(level=args.all))
return 0
1 change: 0 additions & 1 deletion lib/ramble/ramble/cmd/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,6 @@ def workspace_push_to_cache(args):
pipeline = pipeline_cls(ws, filters, spack_cache_path=args.cache_path)

workspace_run_pipeline(args, pipeline)
pipeline.run()


def workspace_push_to_cache_setup_parser(subparser):
Expand Down
2 changes: 2 additions & 0 deletions lib/ramble/ramble/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ def make_argument_parser(**kwargs):
# See https://docs.python.org/3/library/argparse.html#color.
kwargs.pop("color")

kwargs.setdefault("prog", "ramble")

parser = RambleArgumentParser(
formatter_class=RambleHelpFormatter,
add_help=False,
Expand Down
30 changes: 21 additions & 9 deletions lib/ramble/ramble/test/cmd/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,41 @@ def __call__(self, *args, **kwargs):
monkeypatch.setattr(ramble.caches.fetch_cache, "destroy", Counter("downloads"), raising=False)
monkeypatch.setattr(ramble.caches.misc_cache, "destroy", Counter("caches"))
monkeypatch.setattr(ramble.cmd.clean, "remove_python_caches", Counter("python_caches"))
monkeypatch.setattr(ramble.cmd.clean, "remove_reports_files", Counter("reports"))

yield counts


all_effects = ["downloads", "caches", "python_caches"]
# All possible effect categories monitored by the test harness
all_monitored_effects = ["downloads", "caches", "python_caches", "reports"]

# '-a' / '--all' is defined as AllClean (equivalent to -dmp) and excludes 'reports'
all_flag_effects = ["downloads", "caches", "python_caches"]


@pytest.mark.usefixtures("config")
@pytest.mark.parametrize(
"command_line,effects",
"args,effects",
[
("-d", ["downloads"]),
("-m", ["caches"]),
("-p", ["python_caches"]),
("-a", all_effects),
([], ["downloads"]),
(["-d"], ["downloads"]),
(["--downloads"], ["downloads"]),
(["-m"], ["caches"]),
(["--misc-cache"], ["caches"]),
(["-p"], ["python_caches"]),
(["--python-cache"], ["python_caches"]),
(["-r"], ["reports"]),
(["--reports"], ["reports"]),
(["-a"], all_flag_effects),
(["--all"], all_flag_effects),
],
)
def test_function_calls(command_line, effects, mock_calls_for_clean):
def test_function_calls(args, effects, mock_calls_for_clean):

# Call the command with the supplied command line
clean(command_line)
clean(*args)

# Assert that we called the expected functions the correct
# number of times
for name in all_effects:
for name in all_monitored_effects:
assert mock_calls_for_clean[name] == (1 if name in effects else 0)
87 changes: 87 additions & 0 deletions lib/ramble/ramble/test/cmd/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright 2022-2026 The Ramble Authors
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

import os
from unittest.mock import patch

import pytest

import ramble.cmd
import ramble.config
from ramble.error import RambleCommandError
from ramble.main import RambleCommand
from ramble.util.logger import logger

command = RambleCommand("commands")


def test_missing_command():
with pytest.raises(RambleCommandError) as err_info:
RambleCommand("missing-command")

assert "does not exist" in str(err_info.value)


def test_available_command():
for cmd in ramble.cmd.all_commands():
logger.msg(f"Command = {cmd}")
RambleCommand(cmd)


def test_command_output(tmpdir):
formats = ["subcommands", "rst", "names", "bash"]
for f in formats:
file = os.path.join(tmpdir, f"outfile.{f}")
command("--format", f, "--update", file)
assert os.path.isfile(file)

target = os.path.join(tmpdir, "outfile.names")
header = os.path.join(tmpdir, "outfile.subcommands")
command("--update", target, "--header", header, "-a")
assert os.path.isfile(target)


def test_command_alias_output(mutable_config):
with ramble.config.override("config:aliases", {"ws": "workspace"}):
out = command("-a", output=str)
assert "ws" in out
assert "workspace" in out


def test_command_invalid_header(tmpdir):
missing_header = os.path.join(tmpdir, "nonexistent_header.txt")
out = command("--header", missing_header, fail_on_error=False)
assert "No such file" in out


def test_command_update_completion_conflict():
out = command("--update-completion", "-a", fail_on_error=False)
assert "--update-completion can only be specified alone" in out


def test_command_update_completion(tmpdir):
bash_no_aliases = str(tmpdir.join("ramble-completion.bash"))
base_with_aliases = str(tmpdir.join("custom-ramble-completion.bash"))
mock_args = {
"bash_no_aliases": {
"aliases": False,
"format": "bash",
"header": os.path.join(ramble.paths.share_path, "bash", "ramble-completion.in"),
"update": bash_no_aliases,
},
"base_with_aliases": {
"aliases": True,
"format": "bash",
"header": os.path.join(ramble.paths.share_path, "bash", "ramble-completion.in"),
"update": base_with_aliases,
},
}
with patch.dict("ramble.cmd.commands.update_completion_args", mock_args):
command("--update-completion")
assert os.path.isfile(bash_no_aliases)
assert os.path.isfile(base_with_aliases)
15 changes: 15 additions & 0 deletions lib/ramble/ramble/test/cmd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,3 +831,18 @@ def test_command_alias(mutable_config):
output = f.getvalue()
assert ret == 0
assert "ramble info" not in output


def test_config_blame(mock_low_high_config):
low_path = mock_low_high_config.scopes["low"].path
fs.mkdirp(low_path)
with open(os.path.join(low_path, "config.yaml"), "w", encoding="utf-8") as f:
f.write("""\
config:
verbose: true
""")

output = config("blame", "config")
assert "config:" in output
assert "verbose: true" in output
assert "config.yaml:" in output
32 changes: 32 additions & 0 deletions lib/ramble/ramble/test/cmd/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,44 @@
# option. This file may not be copied, modified, or distributed
# except according to those terms.

import pytest

from ramble.main import RambleCommand

help_cmd = RambleCommand("help")


def test_help_default():
"""Test that `ramble help` gives short help."""
help_cmd = RambleCommand("help")
output = help_cmd()
assert "A flexible benchmark experiment manager" in output


def test_help_all():
"""Test that `ramble help --all` and `-a` list all available commands."""
help_cmd = RambleCommand("help")
out_all = help_cmd("--all")
assert "Complete list of ramble commands:" in out_all
assert "workspace" in out_all

help_cmd2 = RambleCommand("help")
out_short = help_cmd2("-a")
assert "Complete list of ramble commands:" in out_short
assert "workspace" in out_short


def test_help_spec():
"""Test that `ramble help --spec` prints the spec guide."""
help_cmd = RambleCommand("help")
output = help_cmd("--spec")
assert "spec expression syntax:" in output
assert "application [constraint]" in output


@pytest.mark.parametrize("subcmd", ["config", "info", "list", "workspace", "help"])
def test_help_command(subcmd):
"""Test that `ramble help <cmd>` prints help for the given command."""
help_cmd = RambleCommand("help")
output = help_cmd(subcmd)
assert f"usage: ramble {subcmd}" in output or "usage: ramble" in output
38 changes: 38 additions & 0 deletions lib/ramble/ramble/test/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,41 @@ def test_command_argument_before_command_is_rejected(capsys):
captured = capsys.readouterr()
assert ret == 1
assert "unrecognized arguments: --dry-run" in captured.err


@pytest.mark.parametrize("flag", ["-V", "--version"])
def test_global_version(flag, capsys):
ret = ramble.main.main(argv=[flag])
captured = capsys.readouterr()
assert ret == 0
assert str(ramble.ramble_version) in captured.out


@pytest.mark.parametrize("flag", ["-h", "--help"])
def test_global_help(flag, capsys):
ret = ramble.main.main(argv=[flag])
captured = capsys.readouterr()
assert ret == 0
assert "A flexible benchmark experiment manager" in captured.out


@pytest.mark.parametrize("flag", ["-H", "--all-help"])
def test_global_all_help(flag, capsys):
ret = ramble.main.main(argv=[flag])
captured = capsys.readouterr()
assert ret == 0
assert "Complete list of ramble commands:" in captured.out


def test_global_config_var(capsys):
ret = ramble.main.main(argv=["-c", "config:debug:true", "help"])
captured = capsys.readouterr()
assert ret == 0
assert "A flexible benchmark experiment manager" in captured.out


def test_global_config_scope(tmpdir, capsys):
ret = ramble.main.main(argv=["-C", str(tmpdir), "help"])
captured = capsys.readouterr()
assert ret == 0
assert "A flexible benchmark experiment manager" in captured.out
20 changes: 20 additions & 0 deletions lib/ramble/ramble/test/cmd/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,23 @@ def test_python_with_module():
def test_python_raises():
out = python("--foobar", fail_on_error=False)
assert "Error: Unknown arguments" in out


def test_python_script(tmpdir):
script = tmpdir.join("test_script.py")
script.write("""
import sys
import ramble
print(f"ARG:{sys.argv[1]}")
print(f"VER:{ramble.ramble_version}")
""")
out = python(str(script), "hello")
assert "ARG:hello" in out
assert f"VER:{ramble.ramble_version}" in out


def test_python_command_and_script(tmpdir):
script = tmpdir.join("dummy.py")
script.write("print('hello')")
out = python("-c", "print('cmd')", str(script), fail_on_error=False)
assert "You can only specify a command OR script" in out
Loading
Loading