Skip to content

Commit 61fa5d9

Browse files
pytest: add reckless regex search
1 parent 9d73ec2 commit 61fa5d9

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

tests/test_reckless.py

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pyln.testing.utils import VALGRIND
66
import pytest
77
import os
8+
import re
89
import shutil
910
import time
1011
import unittest
@@ -91,25 +92,51 @@ def canned_github_server(directory):
9192
server.terminate()
9293

9394

95+
class RecklessResult:
96+
def __init__(self, process, returncode, stdout, stderr):
97+
self.process = process
98+
self.returncode = returncode
99+
self.stdout = stdout
100+
self.stderr = stderr
101+
102+
def __repr__(self):
103+
return f'self.returncode, self.stdout, self.stderr'
104+
105+
def search_stdout(self, regex):
106+
"""return the matching regex line from reckless output."""
107+
ex = re.compile(regex)
108+
matching = []
109+
for line in self.stdout:
110+
if ex.search(line):
111+
matching.append(line)
112+
return matching
113+
114+
94115
def reckless(cmds: list, dir: PosixPath = None,
95-
autoconfirm=True, timeout: int = 15):
116+
autoconfirm=True, timeout: int = 60):
96117
'''Call the reckless executable, optionally with a directory.'''
97118
if dir is not None:
98119
cmds.insert(0, "-l")
99120
cmds.insert(1, str(dir))
100121
cmds.insert(0, "tools/reckless")
122+
if autoconfirm:
123+
process_input = 'Y\n'
124+
else:
125+
process_input = None
101126
r = subprocess.run(cmds, capture_output=True, encoding='utf-8', env=my_env,
102-
input='Y\n')
127+
input=process_input, timeout=timeout)
128+
stdout = r.stdout.splitlines()
129+
stderr = r.stderr.splitlines()
103130
print(" ".join(r.args), "\n")
104131
print("***RECKLESS STDOUT***")
105-
for l in r.stdout.splitlines():
132+
for l in stdout:
106133
print(l)
107134
print('\n')
108135
print("***RECKLESS STDERR***")
109-
for l in r.stderr.splitlines():
136+
for l in stderr:
110137
print(l)
111138
print('\n')
112-
return r
139+
return RecklessResult(r, r.returncode, stdout, stderr)
113140

114141

115142
def get_reckless_node(node_factory):
@@ -119,28 +146,13 @@ def get_reckless_node(node_factory):
119146
return node
120147

121148

122-
def check_stderr(stderr):
123-
def output_okay(out):
124-
for warning in ['[notice]', 'WARNING:', 'npm WARN',
125-
'npm notice', 'DEPRECATION:', 'Creating virtualenv',
126-
'config file not found:', 'press [Y]']:
127-
if out.startswith(warning):
128-
return True
129-
return False
130-
for e in stderr.splitlines():
131-
if len(e) < 1:
132-
continue
133-
# Don't err on verbosity from pip, npm
134-
assert output_okay(e)
135-
136-
137149
def test_basic_help():
138150
'''Validate that argparse provides basic help info.
139151
This requires no config options passed to reckless.'''
140152
r = reckless(["-h"])
141153
assert r.returncode == 0
142-
assert "positional arguments:" in r.stdout.splitlines()
143-
assert "options:" in r.stdout.splitlines() or "optional arguments:" in r.stdout.splitlines()
154+
assert r.search_stdout("positional arguments:")
155+
assert r.search_stdout("options:") or r.search_stdout("optional arguments:")
144156

145157

146158
def test_contextual_help(node_factory):
@@ -149,7 +161,7 @@ def test_contextual_help(node_factory):
149161
'enable', 'disable', 'source']:
150162
r = reckless([subcmd, "-h"], dir=n.lightning_dir)
151163
assert r.returncode == 0
152-
assert "positional arguments:" in r.stdout.splitlines()
164+
assert r.search_stdout("positional arguments:")
153165

154166

155167
def test_sources(node_factory):
@@ -194,17 +206,17 @@ def test_search(node_factory):
194206
n = get_reckless_node(node_factory)
195207
r = reckless([f"--network={NETWORK}", "search", "testplugpass"], dir=n.lightning_dir)
196208
assert r.returncode == 0
197-
assert 'found testplugpass in source: https://github.com/lightningd/plugins' in r.stdout
209+
assert r.search_stdout('found testplugpass in source: https://github.com/lightningd/plugins')
198210

199211

200212
def test_install(node_factory):
201213
"""test search, git clone, and installation to folder."""
202214
n = get_reckless_node(node_factory)
203215
r = reckless([f"--network={NETWORK}", "-v", "install", "testplugpass"], dir=n.lightning_dir)
204216
assert r.returncode == 0
205-
assert 'dependencies installed successfully' in r.stdout
206-
assert 'plugin installed:' in r.stdout
207-
assert 'testplugpass enabled' in r.stdout
217+
assert r.search_stdout('dependencies installed successfully')
218+
assert r.search_stdout('plugin installed:')
219+
assert r.search_stdout('testplugpass enabled')
208220
check_stderr(r.stderr)
209221
plugin_path = Path(n.lightning_dir) / 'reckless/testplugpass'
210222
print(plugin_path)
@@ -217,9 +229,9 @@ def test_poetry_install(node_factory):
217229
n = get_reckless_node(node_factory)
218230
r = reckless([f"--network={NETWORK}", "-v", "install", "testplugpyproj"], dir=n.lightning_dir)
219231
assert r.returncode == 0
220-
assert 'dependencies installed successfully' in r.stdout
221-
assert 'plugin installed:' in r.stdout
222-
assert 'testplugpyproj enabled' in r.stdout
232+
assert r.search_stdout('dependencies installed successfully')
233+
assert r.search_stdout('plugin installed:')
234+
assert r.search_stdout('testplugpyproj enabled')
223235
check_stderr(r.stderr)
224236
plugin_path = Path(n.lightning_dir) / 'reckless/testplugpyproj'
225237
print(plugin_path)
@@ -240,7 +252,7 @@ def test_local_dir_install(node_factory):
240252
assert r.returncode == 0
241253
r = reckless([f"--network={NETWORK}", "-v", "install", "testplugpass"], dir=n.lightning_dir)
242254
assert r.returncode == 0
243-
assert 'testplugpass enabled' in r.stdout
255+
assert r.search_stdout('testplugpass enabled')
244256
plugin_path = Path(n.lightning_dir) / 'reckless/testplugpass'
245257
print(plugin_path)
246258
assert os.path.exists(plugin_path)
@@ -249,9 +261,9 @@ def test_local_dir_install(node_factory):
249261
r = reckless(['uninstall', 'testplugpass', '-v'], dir=n.lightning_dir)
250262
assert not os.path.exists(plugin_path)
251263
r = reckless(['source', 'remove', source_dir], dir=n.lightning_dir)
252-
assert 'plugin source removed' in r.stdout
264+
assert r.search_stdout('plugin source removed')
253265
r = reckless(['install', '-v', source_dir], dir=n.lightning_dir)
254-
assert 'testplugpass enabled' in r.stdout
266+
assert r.search_stdout('testplugpass enabled')
255267
assert os.path.exists(plugin_path)
256268

257269

@@ -263,9 +275,9 @@ def test_disable_enable(node_factory):
263275
r = reckless([f"--network={NETWORK}", "-v", "install", "testPlugPass"],
264276
dir=n.lightning_dir)
265277
assert r.returncode == 0
266-
assert 'dependencies installed successfully' in r.stdout
267-
assert 'plugin installed:' in r.stdout
268-
assert 'testplugpass enabled' in r.stdout
278+
assert r.search_stdout('dependencies installed successfully')
279+
assert r.search_stdout('plugin installed:')
280+
assert r.search_stdout('testplugpass enabled')
269281
check_stderr(r.stderr)
270282
plugin_path = Path(n.lightning_dir) / 'reckless/testplugpass'
271283
print(plugin_path)
@@ -278,7 +290,7 @@ def test_disable_enable(node_factory):
278290
r = reckless([f"--network={NETWORK}", "-v", "enable", "testplugpass.py"],
279291
dir=n.lightning_dir)
280292
assert r.returncode == 0
281-
assert 'testplugpass enabled' in r.stdout
293+
assert r.search_stdout('testplugpass enabled')
282294
test_plugin = {'name': str(plugin_path / 'testplugpass.py'),
283295
'active': True, 'dynamic': True}
284296
time.sleep(1)

0 commit comments

Comments
 (0)