-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from CMIP-REF/invoke-cli-helper
- Loading branch information
Showing
7 changed files
with
128 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added the `invoke_cli` pytest fixture to provide a consistent interface for running CLI tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,43 @@ | ||
import os | ||
|
||
from typer.testing import CliRunner | ||
|
||
from ref.cli import app | ||
def test_without_subcommand(invoke_cli): | ||
# exit code 2 denotes a user error | ||
result = invoke_cli(["config"], expected_exit_code=2) | ||
assert "Missing command." in result.stderr | ||
|
||
runner = CliRunner() | ||
|
||
def test_config_help(invoke_cli): | ||
result = invoke_cli(["config", "--help"]) | ||
|
||
def test_without_subcommand(): | ||
result = runner.invoke(app, ["config"]) | ||
assert result.exit_code == 2 | ||
assert "Missing command." in result.output | ||
|
||
|
||
def test_config_help(): | ||
result = runner.invoke(app, ["config", "--help"]) | ||
assert result.exit_code == 0 | ||
|
||
assert "View and update the REF configuration" in result.output | ||
assert "View and update the REF configuration" in result.stdout | ||
|
||
|
||
class TestConfigList: | ||
def test_config_list(self, config): | ||
result = runner.invoke(app, ["config", "list"]) | ||
assert result.exit_code == 0 | ||
def test_config_list(self, config, invoke_cli): | ||
result = invoke_cli(["config", "list"]) | ||
|
||
config_dir = os.environ.get("REF_CONFIGURATION") | ||
assert f'data = "{config_dir}/data"\n' in result.output | ||
assert 'database_url = "sqlite://' in result.output | ||
|
||
def test_config_list_custom_missing(self, config): | ||
result = runner.invoke( | ||
app, | ||
def test_config_list_custom_missing(self, config, invoke_cli): | ||
result = invoke_cli( | ||
[ | ||
"--configuration-directory", | ||
"missing", | ||
"config", | ||
"list", | ||
], | ||
expected_exit_code=1, | ||
) | ||
assert result.exit_code == 1, result.output | ||
|
||
assert "Configuration file not found" in result.stdout | ||
|
||
|
||
class TestConfigUpdate: | ||
def test_config_update(self): | ||
result = runner.invoke(app, ["config", "update"]) | ||
assert result.exit_code == 0 | ||
def test_config_update(self, invoke_cli): | ||
result = invoke_cli(["config", "update"]) | ||
|
||
# TODO: actually implement this functionality | ||
assert "config" in result.output | ||
assert "config" in result.stdout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,55 @@ | ||
from typer.testing import CliRunner | ||
|
||
from ref import __core_version__, __version__ | ||
from ref.cli import app | ||
|
||
runner = CliRunner( | ||
mix_stderr=False, | ||
) | ||
|
||
|
||
def test_without_subcommand(): | ||
result = runner.invoke(app, []) | ||
assert result.exit_code == 0 | ||
def test_without_subcommand(invoke_cli): | ||
result = invoke_cli([]) | ||
assert "Usage:" in result.stdout | ||
assert "ref [OPTIONS] COMMAND [ARGS]" in result.stdout | ||
assert "ref: A CLI for the CMIP Rapid Evaluation Framework" in result.stdout | ||
|
||
|
||
def test_version(): | ||
result = runner.invoke(app, ["--version"]) | ||
assert result.exit_code == 0 | ||
assert f"ref: {__version__}\nref-core: {__core_version__}" in result.output | ||
def test_version(invoke_cli): | ||
result = invoke_cli(["--version"]) | ||
assert f"ref: {__version__}\nref-core: {__core_version__}" in result.stdout | ||
|
||
|
||
def test_verbose(): | ||
def test_verbose(invoke_cli): | ||
exp_log = "| DEBUG | ref.config:default:178 - Loading default configuration from" | ||
result = runner.invoke( | ||
app, | ||
result = invoke_cli( | ||
["--verbose", "config", "list"], | ||
) | ||
assert exp_log in result.stderr | ||
|
||
result = runner.invoke( | ||
app, | ||
result = invoke_cli( | ||
["config", "list"], | ||
) | ||
# Only info and higher messages logged | ||
assert exp_log not in result.stderr | ||
|
||
|
||
def test_config_directory_custom(config): | ||
def test_config_directory_custom(config, invoke_cli): | ||
config.paths.tmp = "test-value" | ||
config.save() | ||
|
||
result = runner.invoke( | ||
app, | ||
result = invoke_cli( | ||
[ | ||
"--configuration-directory", | ||
str(config._config_file.parent), | ||
"config", | ||
"list", | ||
], | ||
) | ||
assert result.exit_code == 0 | ||
assert 'tmp = "test-value"\n' in result.output | ||
|
||
|
||
def test_config_directory_append(config): | ||
def test_config_directory_append(config, invoke_cli): | ||
# configuration directory must be passed before command | ||
result = runner.invoke( | ||
app, | ||
invoke_cli( | ||
[ | ||
"config", | ||
"list", | ||
"--configuration-directory", | ||
str(config._config_file.parent), | ||
], | ||
expected_exit_code=2, | ||
) | ||
assert result.exit_code == 2 |
Oops, something went wrong.