|
2 | 2 | Tests for wxvx.cli. |
3 | 3 | """ |
4 | 4 |
|
5 | | -import logging |
6 | 5 | import re |
7 | 6 | from argparse import ArgumentTypeError, Namespace |
8 | 7 | from pathlib import Path |
9 | 8 | from unittest.mock import patch |
10 | 9 |
|
11 | 10 | import yaml |
| 11 | +from iotaa import Asset, external |
12 | 12 | from pytest import mark, raises |
13 | 13 |
|
14 | | -from wxvx import cli |
| 14 | +from wxvx import cli, workflow |
15 | 15 | from wxvx.strings import S |
16 | 16 | from wxvx.types import Config |
17 | 17 | from wxvx.util import WXVXError, pkgname, resource_path |
|
25 | 25 | @mark.parametrize("threads", [1, 2]) |
26 | 26 | def test_cli_main(config_data, logged, switch_c, switch_n, switch_t, threads, tmp_path): |
27 | 27 | cfgfile = tmp_path / "config.yaml" |
28 | | - with cfgfile.open("w") as f: |
29 | | - yaml.safe_dump(config_data, f) |
| 28 | + cfgfile.write_text(yaml.safe_dump(config_data)) |
30 | 29 | argv = [pkgname, switch_c, str(cfgfile), switch_n, str(threads), switch_t, S.plots] |
31 | 30 | with ( |
32 | 31 | patch.object(cli, "_parse_args", wraps=cli._parse_args) as _parse_args, |
@@ -85,9 +84,30 @@ def test_cli_main__exception(logged): |
85 | 84 | assert e.value.code == 1 |
86 | 85 |
|
87 | 86 |
|
| 87 | +@mark.parametrize("switch", ["-f", "--fail", None]) |
| 88 | +def test_cli_main__fail(config_data, switch, tmp_path): |
| 89 | + @external |
| 90 | + def bad(_): |
| 91 | + yield "bad" |
| 92 | + yield Asset(None, lambda: False) |
| 93 | + |
| 94 | + cfgfile = tmp_path / "config.yaml" |
| 95 | + cfgfile.write_text(yaml.safe_dump(config_data)) |
| 96 | + argv = [pkgname, "-c", str(cfgfile), "-t", "bad", switch] |
| 97 | + with ( |
| 98 | + patch.object(cli.sys, "argv", list(filter(None, argv))), |
| 99 | + patch.object(workflow, "bad", create=True, new=bad), |
| 100 | + ): |
| 101 | + if switch: |
| 102 | + with raises(SystemExit) as e: |
| 103 | + cli.main() |
| 104 | + assert e.value.code == 1 |
| 105 | + else: |
| 106 | + cli.main() |
| 107 | + |
| 108 | + |
88 | 109 | @mark.parametrize("switch", ["-l", "--list"]) |
89 | 110 | def test_cli_main__task_list(caplog, switch, tidy): |
90 | | - caplog.set_level(logging.INFO) |
91 | 111 | with ( |
92 | 112 | patch.object( |
93 | 113 | cli.sys, "argv", [pkgname, "-c", str(resource_path("config-grid.yaml")), switch] |
@@ -126,14 +146,13 @@ def test_cli_main__show_config(capsys, check, fs, switch): |
126 | 146 | assert isinstance(yaml.safe_load(capsys.readouterr().out), dict) |
127 | 147 |
|
128 | 148 |
|
129 | | -def test_cli_main__task_missing(caplog): |
130 | | - caplog.set_level(logging.INFO) |
| 149 | +def test_cli_main__task_missing(logged): |
131 | 150 | argv = [pkgname, "-c", str(resource_path("config-grid.yaml")), "-t", "foo"] |
132 | 151 | with patch.object(cli.sys, "argv", argv), patch.object(cli, "use_uwtools_logger"): |
133 | 152 | with raises(SystemExit) as e: |
134 | 153 | cli.main() |
135 | 154 | assert e.value.code == 1 |
136 | | - assert "No such task: foo" in caplog.messages |
| 155 | + assert logged("No such task: foo") |
137 | 156 |
|
138 | 157 |
|
139 | 158 | def test_cli__arg_type_int_greater_than_zero__pass(): |
|
0 commit comments