/
test_scripts.py
136 lines (113 loc) · 3.96 KB
/
test_scripts.py
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
"""Test scripts."""
import socket
import sys
from pathlib import Path
from subprocess import Popen, PIPE
import pytest
def is_connected(hostname):
"""See https://stackoverflow.com/a/20913928/ to test hostname."""
try:
host = socket.gethostbyname(hostname)
s = socket.create_connection((host, 80), 2)
s.close()
return True
except Exception:
pass
return False
requires_github = pytest.mark.skipif(
not is_connected("github.com"), reason="github.com is required."
)
flopy_dir = Path(__file__).parents[1] / "flopy"
get_modflow_script = flopy_dir / "utils" / "get_modflow.py"
@pytest.fixture(scope="session")
def downloads_dir(tmp_path_factory):
downloads_dir = tmp_path_factory.mktemp("Downloads")
return downloads_dir
def run_py_script(script, *args):
"""Run a Python script, return tuple (stdout, stderr, returncode)."""
args = [sys.executable, str(script)] + [str(g) for g in args]
print("running: " + " ".join(args))
p = Popen(args, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
stdout = stdout.decode()
stderr = stderr.decode()
returncode = p.returncode
return stdout, stderr, returncode
def run_get_modflow_script(*args):
return run_py_script(get_modflow_script, *args)
def test_script_usage():
assert get_modflow_script.exists()
stdout, stderr, returncode = run_get_modflow_script("-h")
assert "usage" in stdout
assert len(stderr) == 0
assert returncode == 0
@requires_github
def test_get_modflow(tmp_path, downloads_dir):
# exit if extraction directory does not exist
bindir = tmp_path / "bin1"
assert not bindir.exists()
stdout, stderr, returncode = run_get_modflow_script(bindir)
assert "does not exist" in stderr
assert returncode == 1
# ensure extraction directory exists
bindir.mkdir()
assert bindir.exists()
# attempt to fetch a non-existing release-id
stdout, stderr, returncode = run_get_modflow_script(
bindir, "--release-id", "1.9", "--downloads-dir", downloads_dir
)
assert "Release '1.9' not found" in stderr
assert returncode == 1
# fetch latest
stdout, stderr, returncode = run_get_modflow_script(
bindir, "--downloads-dir", downloads_dir
)
assert len(stderr) == returncode == 0
files = [item.name for item in bindir.iterdir() if item.is_file()]
assert len(files) > 20
# take only a few files using --subset, starting with invalid
bindir = tmp_path / "bin2"
bindir.mkdir()
stdout, stderr, returncode = run_get_modflow_script(
bindir, "--subset", "mfnwt,mpx", "--downloads-dir", downloads_dir
)
assert "subset item not found: mpx" in stderr
assert returncode == 1
# now valid subset
stdout, stderr, returncode = run_get_modflow_script(
bindir, "--subset", "mfnwt,mp6", "--downloads-dir", downloads_dir
)
assert len(stderr) == returncode == 0
files = [item.stem for item in bindir.iterdir() if item.is_file()]
assert sorted(files) == ["mfnwt", "mfnwtdbl", "mp6"]
# similar as before, but also specify a ostag
bindir = tmp_path / "bin3"
bindir.mkdir()
stdout, stderr, returncode = run_get_modflow_script(
bindir,
"--subset",
"mfnwt",
"--release-id",
"2.0",
"--ostag",
"win64",
"--downloads-dir",
downloads_dir,
)
assert len(stderr) == returncode == 0
files = [item.name for item in bindir.iterdir() if item.is_file()]
assert sorted(files) == ["mfnwt.exe", "mfnwtdbl.exe"]
@requires_github
def test_get_mf6_nightly(tmp_path, downloads_dir):
bindir = tmp_path / "bin1"
bindir.mkdir()
stdout, stderr, returncode = run_get_modflow_script(
bindir,
"--repo",
"modflow6-nightly-build",
"--downloads-dir",
downloads_dir,
)
assert len(stderr) == returncode == 0
files = [item.name for item in bindir.iterdir() if item.is_file()]
assert len(files) >= 4