-
-
Notifications
You must be signed in to change notification settings - Fork 443
Expand file tree
/
Copy pathgrass_script_run_command_test.py
More file actions
175 lines (133 loc) · 5.22 KB
/
Copy pathgrass_script_run_command_test.py
File metadata and controls
175 lines (133 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""Tests of run_command family of functions"""
import pytest
import grass.script as gs
from grass.exceptions import CalledModuleError
def test_run_command(session_2x2):
"""Check run_command runs again with overwrite parameter"""
gs.run_command("r.random.surface", output="surface", seed=42, env=session_2x2.env)
gs.run_command(
"r.random.surface",
output="surface",
seed=42,
overwrite=True,
env=session_2x2.env,
)
def test_run_command_status(session_2x2):
"""Check that run_command gives returncode (aka status)"""
assert (
gs.run_command("r.mask.status", flags="t", env=session_2x2.env, errors="status")
== 1
)
def test_run_command_ignore_default(session_2x2):
"""Check that run_command raises by default"""
with pytest.raises(CalledModuleError, match=r"r\.slope\.aspect"):
gs.run_command(
"r.slope.aspect",
elevation="does_not_exist",
slope="slope",
env=session_2x2.env,
)
def test_run_command_ignore_raise(session_2x2):
"""Check that run_command raises when set to raise"""
with pytest.raises(CalledModuleError, match=r"r\.slope\.aspect"):
gs.run_command(
"r.slope.aspect",
elevation="does_not_exist",
slope="slope",
env=session_2x2.env,
errors="raise",
)
def test_run_command_ignore(session_2x2):
"""Check that run_command ignores errors"""
gs.run_command(
"r.slope.aspect",
elevation="does_not_exist",
slope="slope",
env=session_2x2.env,
errors="ignore",
)
def test_run_command_fatal(session_2x2):
"""Check that run_command results in a fatal error (which calls in sys.exit)"""
with pytest.raises(SystemExit):
gs.run_command(
"r.slope.aspect",
elevation="does_not_exist",
slope="slope",
env=session_2x2.env,
errors="fatal",
)
def test_run_command_exit(session_2x2):
"""Check that run_command calls sys.exit"""
with pytest.raises(SystemExit):
gs.run_command(
"r.slope.aspect",
elevation="does_not_exist",
slope="slope",
env=session_2x2.env,
errors="exit",
)
def test_read_command_flag(session_2x2):
"""Check a single flag character"""
assert "north" in gs.read_command(
"g.region", flags="p", format="json", env=session_2x2.env
)
def test_read_command_flag_with_dash(session_2x2):
"""Check flag with dash"""
assert "north" in gs.read_command(
"g.region", flags="-p", format="json", env=session_2x2.env
)
def test_read_command_multiple_flags(session_2x2):
"""Check multiple flag characters included a number"""
assert "north" in gs.read_command(
"g.region", flags="p3u", format="json", env=session_2x2.env
)
def test_read_command_multiple_flags_with_dash(session_2x2):
"""Check multiple flag characters included a number with dash"""
assert "north" in gs.read_command(
"g.region", flags="-p3u", format="json", env=session_2x2.env
)
def test_read_command_multiple_flags_with_multiple_dashes(session_2x2):
with pytest.raises(CalledModuleError, match=r"g\.region"):
gs.read_command("g.region", flags="-p-u", format="json", env=session_2x2.env)
def test_read_command_multiple_flags_with_multiple_dashes_and_spaces(session_2x2):
with pytest.raises(CalledModuleError, match=r"g\.region"):
gs.read_command("g.region", flags="-p -u", format="json", env=session_2x2.env)
def test_read_command_number_as_a_string_flag(session_2x2):
"""Number is a character just as any letter is"""
assert "top" in gs.read_command(
"g.region", flags="-3", format="json", env=session_2x2.env
)
def test_read_command_number_as_a_int_flag(session_2x2):
"""Integer is supported, because we support ints and floats for options"""
assert "top" in gs.read_command(
"g.region", flags=3, format="json", env=session_2x2.env
)
def test_read_command_number_as_a_float_flag(session_2x2):
"""Float is not supported as a flag even if its integer part is a valid flag"""
with pytest.raises(CalledModuleError, match=r"g\.region"):
gs.read_command("g.region", flags=3.0, env=session_2x2.env)
def test_read_command_number_as_a_negative_int_flag(session_2x2):
"""
Negative number works only as as a side effect of other processing,
but we test for it anyway since it is expected from the current implementation.
However, it is not how users should use it.
"""
assert "top" in gs.read_command("g.region", flags=-3, env=session_2x2.env)
def test_parse_command_key_value(session_2x2):
"""Check parse_command parses shell-script style key-value pairs
Values are always strings even for numbers.
"""
assert (
gs.parse_command("g.region", flags="p", format="shell", env=session_2x2.env)[
"nsres"
]
== "0.5"
)
def test_parse_command_json(session_2x2):
"""Check parse_command parses JSON output"""
assert (
gs.parse_command("g.region", flags="p", format="json", env=session_2x2.env)[
"nsres"
]
== 0.5
)