Skip to content

Commit 1815c45

Browse files
committed
Rework a bit make_readme.py and test_make_readme.py
* Use pathlib.Path * Use difflib to show actual diffs between new and old readmes
1 parent f3c2b77 commit 1815c45

File tree

2 files changed

+46
-41
lines changed

2 files changed

+46
-41
lines changed

tools/readme_generator/make_readme.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import argparse
44
import json
5-
import os
65
from pathlib import Path
76
from copy import deepcopy
87

@@ -11,6 +10,9 @@
1110
import toml
1211
from jinja2 import Environment, FileSystemLoader
1312

13+
README_GEN_DIR = Path(__file__).resolve().parent
14+
APPS_REPO_ROOT = README_GEN_DIR.parent.parent
15+
1416

1517
def value_for_lang(values: Dict, lang: str):
1618
if not isinstance(values, dict):
@@ -27,20 +29,18 @@ def generate_READMEs(app_path: Path):
2729
if not app_path.exists():
2830
raise Exception("App path provided doesn't exists ?!")
2931

30-
if os.path.exists(app_path / "manifest.json"):
32+
if (app_path / "manifest.json").exists():
3133
manifest = json.load(open(app_path / "manifest.json"))
3234
else:
3335
manifest = toml.load(open(app_path / "manifest.toml"))
3436

3537
upstream = manifest.get("upstream", {})
3638

37-
catalog = toml.load(
38-
open(Path(os.path.abspath(__file__)).parent.parent.parent / "apps.toml")
39-
)
39+
catalog = toml.load((APPS_REPO_ROOT / "apps.toml").open(encoding="utf-8"))
4040
from_catalog = catalog.get(manifest["id"], {})
4141

4242
antifeatures_list = toml.load(
43-
open(Path(os.path.abspath(__file__)).parent.parent.parent / "antifeatures.toml")
43+
(APPS_REPO_ROOT / "antifeatures.toml").open(encoding="utf-8")
4444
)
4545

4646
if not upstream and not (app_path / "doc" / "DISCLAIMER.md").exists():
@@ -49,17 +49,19 @@ def generate_READMEs(app_path: Path):
4949
)
5050
return
5151

52-
env = Environment(loader=FileSystemLoader(Path(__file__).parent / "templates"))
52+
env = Environment(loader=FileSystemLoader(README_GEN_DIR / "templates"))
53+
54+
screenshots: List[str] = []
5355

54-
screenshots: List[str]
55-
screenshots = []
56-
if (app_path / "doc" / "screenshots").exists():
56+
screenshots_dir = app_path / "doc" / "screenshots"
57+
if screenshots_dir.exists():
5758
# only pick files (no folder) on the root of 'screenshots'
58-
for entry in os.scandir(os.path.join(app_path, "doc", "screenshots")):
59-
if os.DirEntry.is_file(entry):
60-
# ignore '.gitkeep' or any file whose name begins with a dot
61-
if not entry.name.startswith("."):
62-
screenshots.append(os.path.relpath(entry.path, app_path))
59+
for entry in screenshots_dir.iterdir():
60+
if not entry.is_file():
61+
continue
62+
if entry.name.startswith("."):
63+
continue
64+
screenshots.append(str(entry.relative_to(app_path)))
6365

6466
# parse available README template and generate a list in the form of:
6567
# > [("en", ""), ("fr", "_fr"), ...]
@@ -133,7 +135,7 @@ def generate_READMEs(app_path: Path):
133135
description="Automatically (re)generate README for apps"
134136
)
135137
parser.add_argument(
136-
"app_path", help="Path to the app to generate/update READMEs for"
138+
"app_path", type=Path, help="Path to the app to generate/update READMEs for"
137139
)
138140

139141
args = parser.parse_args()

tools/readme_generator/tests/test_make_readme.py

100644100755
Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
1-
import os
1+
#!/usr/bin/env python3
2+
3+
import difflib
24
import tempfile
35
import subprocess
46

57
from pathlib import Path
68

7-
CWD = Path(os.path.split(os.path.realpath(__file__))[0])
8-
COMMIT_ID = "8f788213b363a46a5b6faa8f844d86d4adac9446"
9+
CWD = Path(__file__).resolve().parent
10+
11+
TEST_APP_NAME = "gotosocial_ynh"
12+
TEST_APP_REPO = "https://github.com/yunohost-apps/gotosocial_ynh"
13+
TEST_APP_COMMIT_ID = "8f788213b363a46a5b6faa8f844d86d4adac9446"
14+
15+
def diff_files(file_a: Path, file_b: Path) -> bool:
16+
lines_a = file_a.open(encoding="utf-8").readlines()
17+
lines_b = file_b.open(encoding="utf-8").readlines()
18+
19+
diffs = list(difflib.unified_diff(lines_a, lines_b, fromfile='README.before.md', tofile='README.after.md'))
20+
print("".join(diffs))
21+
return len(diffs) == 0
922

1023

1124
def test_running_make_readme():
12-
with tempfile.TemporaryDirectory() as name:
13-
name = Path(name)
14-
DIRECTORY = name / "gotosocial_ynh"
15-
16-
subprocess.check_call(
17-
[
18-
"git",
19-
"clone",
20-
"https://github.com/yunohost-apps/gotosocial_ynh",
21-
DIRECTORY,
22-
"-q",
23-
]
24-
)
25-
subprocess.check_call(["git", "checkout", COMMIT_ID, "-q"], cwd=DIRECTORY)
26-
27-
print(CWD)
28-
subprocess.check_call([CWD / "../make_readme.py", DIRECTORY])
29-
30-
assert open(CWD / "README.md").read() == open(DIRECTORY / "README.md").read()
31-
assert (
32-
open(CWD / "README_fr.md").read() == open(DIRECTORY / "README_fr.md").read()
33-
)
25+
with tempfile.TemporaryDirectory() as tempdir:
26+
tempdir = Path(tempdir)
27+
DIRECTORY = tempdir / TEST_APP_NAME
28+
29+
subprocess.check_call(["git", "clone", "-q", TEST_APP_REPO, DIRECTORY])
30+
subprocess.check_call(["git", "checkout", "-q", TEST_APP_COMMIT_ID], cwd=DIRECTORY)
31+
32+
# Now run test...
33+
subprocess.check_call([CWD.parent / "make_readme.py", DIRECTORY])
34+
35+
assert diff_files(CWD / "README.md", DIRECTORY / "README.md")
36+
assert diff_files(CWD / "README_fr.md", DIRECTORY / "README_fr.md")
3437

3538

3639
if __name__ == "__main__":

0 commit comments

Comments
 (0)