Skip to content

Commit 3ab9ee0

Browse files
committed
Update test.py and workflows
1 parent 4e682dc commit 3ab9ee0

File tree

3 files changed

+96
-22
lines changed

3 files changed

+96
-22
lines changed

.ci/test.py

+87-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
from pathlib import Path
2-
import subprocess
3-
from pprint import pprint
4-
from collections import namedtuple
5-
from typing import Generator
6-
1+
import json
72
import logging
3+
import os
4+
import shlex
85
import shutil
6+
import subprocess
97
import sys
108
import tempfile
11-
import shlex
12-
import os
9+
from collections import namedtuple
10+
from pathlib import Path, PosixPath
11+
from typing import Generator, List
1312

1413
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
1514

@@ -18,8 +17,8 @@
1817
'.ci',
1918
'.git',
2019
'.github',
20+
'archived',
2121
'lightning',
22-
'feeadjuster'
2322
]
2423

2524
global_dependencies = [
@@ -50,13 +49,15 @@ def enumerate_plugins(basedir: Path) -> Generator[Plugin, None, None]:
5049
pip_pytest = [
5150
x for x in plugins if (x / Path('requirements.txt')).exists()
5251
]
52+
print(f'Pip plugins: {", ".join([p.name for p in sorted(pip_pytest)])}')
5353

5454
poetry_pytest = [
5555
x for x in plugins if (x / Path("pyproject.toml")).exists()
5656
]
57-
print(poetry_pytest)
57+
print(f'Poetry plugins: {", ".join([p.name for p in sorted(poetry_pytest)])}')
5858

5959
other_plugins = [x for x in plugins if x not in pip_pytest and x not in poetry_pytest]
60+
print(f'Other plugins: {", ".join([p.name for p in sorted(other_plugins)])}')
6061

6162
for p in sorted(pip_pytest):
6263
yield Plugin(
@@ -227,20 +228,24 @@ def install_pyln_testing(pip_path):
227228
stderr=subprocess.STDOUT,
228229
)
229230

230-
def run_one(p: Plugin) -> bool:
231-
print("Running tests on plugin {p.name}".format(p=p))
232-
233-
testfiles = [
231+
def get_testfiles(p: Plugin) -> List[PosixPath]:
232+
return [
234233
x for x in p.path.iterdir()
235234
if (x.is_dir() and x.name == 'tests')
236235
or (x.name.startswith("test_") and x.name.endswith('.py'))
237236
]
238237

239-
if len(testfiles) == 0:
238+
def has_testfiles(p: Plugin) -> bool:
239+
return len(get_testfiles(p)) > 0
240+
241+
def run_one(p: Plugin) -> bool:
242+
print("Running tests on plugin {p.name}".format(p=p))
243+
244+
if not has_testfiles(p):
240245
print("No test files found, skipping plugin {p.name}".format(p=p))
241246
return True
242247

243-
print("Found {ctestfiles} test files, creating virtualenv and running tests".format(ctestfiles=len(testfiles)))
248+
print("Found {ctestfiles} test files, creating virtualenv and running tests".format(ctestfiles=len(get_testfiles(p))))
244249
print("##[group]{p.name}".format(p=p))
245250

246251
# Create a virtual env
@@ -296,7 +301,7 @@ def run_one(p: Plugin) -> bool:
296301
print("##[endgroup]")
297302

298303

299-
def run_all(args):
304+
def run_all(workflow, update_badges, plugin_names):
300305
root_path = subprocess.check_output([
301306
'git',
302307
'rev-parse',
@@ -306,20 +311,82 @@ def run_all(args):
306311
root = Path(root_path)
307312

308313
plugins = list(enumerate_plugins(root))
309-
if args != []:
310-
plugins = [p for p in plugins if p.name in args]
314+
if plugin_names != []:
315+
plugins = [p for p in plugins if p.name in plugin_names]
311316
print("Testing the following plugins: {names}".format(names=[p.name for p in plugins]))
312317
else:
313318
print("Testing all plugins in {root}".format(root=root))
314319

315320
results = [(p, run_one(p)) for p in plugins]
316321
success = all([t[1] for t in results])
317322

323+
if sys.version_info[0:2] == (3, 12) and update_badges:
324+
push_badges_data(collect_badges_data(results, success), workflow)
325+
318326
if not success:
319327
print("The following tests failed:")
320328
for t in filter(lambda t: not t[1], results):
321329
print(" - {p.name} ({p.path})".format(p=t[0]))
322330
sys.exit(1)
331+
else:
332+
print("All tests passed.")
333+
334+
335+
def collect_badges_data(results, success):
336+
badges_data = {}
337+
for t in results:
338+
p = t[0]
339+
if has_testfiles(p):
340+
if success or t[1]:
341+
badges_data[p.name] = True
342+
else:
343+
badges_data[p.name] = False
344+
return badges_data
345+
346+
347+
def configure_git():
348+
subprocess.run(["git", "config", "--global", "user.email", '"lightningd@plugins.repo"'])
349+
subprocess.run(["git", "config", "--global", "user.name", '"lightningd"'])
350+
351+
352+
def update_and_commit_badge(plugin_name, passed, workflow):
353+
json_data = { "schemaVersion": 1, "label": "", "message": " ✔ ", "color": "green" }
354+
if not passed:
355+
json_data.update({"message": "✗", "color": "red"})
356+
357+
filename = os.path.join("badges", f"{plugin_name}_{workflow}.json")
358+
with open(filename, "w") as file:
359+
file.write(json.dumps(json_data))
360+
361+
output = subprocess.check_output(["git", "add", "-v", filename]).decode("utf-8")
362+
if output != "":
363+
subprocess.run(["git", "commit", "-m", f'Update {plugin_name} badge to {"passed" if passed else "failed"} ({workflow})'])
364+
return True
365+
return False
366+
367+
368+
def push_badges_data(data, workflow):
369+
print("Pushing badge data...")
370+
configure_git()
371+
subprocess.run(["git", "fetch"])
372+
subprocess.run(["git", "checkout", "badges"])
373+
374+
any_changes = False
375+
for plugin_name, passed in data.items():
376+
any_changes |= update_and_commit_badge(plugin_name, passed, workflow)
377+
378+
if any_changes:
379+
subprocess.run(["git", "push", "origin", "badges"])
380+
print("Done.")
381+
323382

324383
if __name__ == "__main__":
325-
run_all(sys.argv[1:])
384+
import argparse
385+
386+
parser = argparse.ArgumentParser(description='Plugins test script')
387+
parser.add_argument("workflow", type=str, help="Name of the GitHub workflow")
388+
parser.add_argument("--update-badges", action='store_true', help="Whether badges data should be updated")
389+
parser.add_argument("plugins", nargs="*", default=[], help="List of plugins")
390+
args = parser.parse_args()
391+
392+
run_all(args.workflow, args.update_badges, args.plugins)

.github/workflows/main.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ jobs:
9898
9999
# Run the tests: In the case of a 'pull_request' event only the plugins in `plugin_dirs`
100100
# are going to be tested; otherwise ('push' event) we test all plugins.
101-
python3 .ci/test.py $(echo "$plugin_dirs")
101+
102+
update_badges=''
103+
if [[ "${{ github.event_name }}" == 'push' && "${{ github.ref }}" == 'refs/heads/master' ]] || [[ "${{ github.event_name }}" == 'schedule' ]]
104+
then
105+
update_badges='--update-badges'
106+
fi
107+
108+
python3 .ci/test.py main $update_badges $(echo "$plugin_dirs")
102109
103110
gather:
104111
# A dummy task that depends on the full matrix of tests, and

.github/workflows/nightly.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ jobs:
9191
export CLN_PATH=${{ github.workspace }}/lightning
9292
pip3 install --upgrade pip
9393
pip3 install --user -U virtualenv pip > /dev/null
94-
python3 .ci/test.py
94+
python3 .ci/test.py nightly --update-badges
9595
9696
gather:
9797
# A dummy task that depends on the full matrix of tests, and

0 commit comments

Comments
 (0)