Skip to content
This repository was archived by the owner on Mar 13, 2024. It is now read-only.

Commit 1e92e6e

Browse files
committed
Fixes for adopting pydata-docs branch
1 parent 767528e commit 1e92e6e

File tree

2 files changed

+184
-31
lines changed

2 files changed

+184
-31
lines changed

src/python3_pip_skeleton/__main__.py

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
1818
# Extensions to change
1919
CHANGE_SUFFIXES = [".py", ".rst", ".cfg", "", ".toml"]
2020
# Files not to change
21-
IGNORE_FILES = ["CHANGELOG.rst", "test_boilerplate_removed.py"]
22-
# Ranges to ignore between
23-
IGNORE_RANGES = {
24-
"CONTRIBUTING.rst": ("\nUpdating the tools\n", None),
25-
}
21+
IGNORE_FILES = ["update-tools.rst", "test_boilerplate_removed.py"]
2622

2723
SKELETON_ROOT_COMMIT = "ededf00035e6ccfac78946213009c1ecd7c110a9"
2824

@@ -88,34 +84,15 @@ def replace_text(text: str) -> str:
8884
# will do the wrong thing
8985
shutil.rmtree(git_tmp / "src", ignore_errors=True)
9086
# Merge in the skeleton commits
91-
git_tmp("pull", SKELETON, "main")
87+
git_tmp("pull", "--rebase=false", SKELETON, "main")
9288
# Move things around
9389
git_tmp("mv", "src/python3_pip_skeleton", f"src/{package}")
94-
git_tmp("mv", "tests/test_cli.py", f"tests/test_{package}.py")
9590
# Change contents of all children known to git
9691
for relative_child in git_tmp("ls-files").splitlines():
9792
child = Path(git_tmp.name) / relative_child
9893
if child.suffix in CHANGE_SUFFIXES and child.name not in IGNORE_FILES:
99-
text = child.read_text()
100-
start_search, end_search = IGNORE_RANGES.get(child.name, (None, None))
101-
if start_search:
102-
start_ignore = text.find(start_search)
103-
assert start_ignore > 0, f"{start_search} not in {child.name}"
104-
if end_search:
105-
end_ignore = text.find(end_search, start_ignore) + len(
106-
end_search
107-
)
108-
assert end_ignore > 0, f"{end_search} not in {child.name}"
109-
else:
110-
end_ignore = len(text)
111-
else:
112-
start_ignore = 0
113-
end_ignore = 0
114-
child.write_text(
115-
replace_text(text[:start_ignore])
116-
+ text[start_ignore:end_ignore]
117-
+ replace_text(text[end_ignore:])
118-
)
94+
text = replace_text(child.read_text())
95+
child.write_text(text)
11996
# Commit what we have and push to the original repo
12097
git_tmp("commit", "-a", "-m", f"Rename python3-pip-skeleton -> {repo}")
12198
git_tmp("push", "origin", MERGE_BRANCH)
@@ -127,7 +104,7 @@ def replace_text(text: str) -> str:
127104
print(f" git branch -d {MERGE_BRANCH}")
128105
else:
129106
git("branch", "-d", MERGE_BRANCH, cwd=path)
130-
print("Instructions on how to develop this module are in CONTRIBUTING.rst")
107+
print("Developer instructions in docs/developer/tutorials/dev-install.rst")
131108

132109

133110
def validate_package(args) -> str:
@@ -154,9 +131,12 @@ def verify_not_adopted(root: Path):
154131
]
155132
)
156133

157-
assert (
158-
not_adopted
159-
), f"Package {root} has already adopted skeleton. use --force to re-adopt"
134+
assert not_adopted, (
135+
f"Package {root} has already adopted skeleton. You can type:\n"
136+
f" git pull --rebase=false {SKELETON}\n"
137+
"to update. If there were significant upstream changes a re-adopt may be "
138+
"better. use the --force flag to the command you just ran."
139+
)
160140

161141

162142
def new(args):

tests/test_adopt.py

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import subprocess
2+
import sys
3+
from configparser import ConfigParser
4+
from os import makedirs
5+
from pathlib import Path
6+
7+
import pytest
8+
9+
from python3_pip_skeleton import __main__, __version__
10+
11+
12+
def check_output(*args, cwd=None) -> str:
13+
try:
14+
return subprocess.check_output(
15+
args, stderr=subprocess.STDOUT, text=True, cwd=cwd
16+
)
17+
except subprocess.CalledProcessError as e:
18+
raise ValueError(e.output)
19+
20+
21+
def test_cli_version():
22+
output = check_output(sys.executable, "-m", "python3_pip_skeleton", "--version")
23+
assert output.strip() == __version__
24+
25+
26+
def test_new_module(tmp_path: Path):
27+
module = tmp_path / "my-module"
28+
output = check_output(
29+
sys.executable,
30+
"-m",
31+
"python3_pip_skeleton",
32+
"new",
33+
"--org=myorg",
34+
"--package=my_module",
35+
"--full-name=Firstname Lastname",
36+
37+
str(module),
38+
)
39+
assert output.strip().endswith(
40+
"Developer instructions in docs/developer/tutorials/dev-install.rst"
41+
)
42+
43+
conf = ConfigParser()
44+
conf.read(module / "setup.cfg")
45+
assert conf["metadata"]["author"] == "Firstname Lastname"
46+
assert conf["metadata"]["author_email"] == "[email protected]"
47+
api_rst = module / "docs" / "user" / "reference" / "api.rst"
48+
assert (
49+
"Version number as calculated by https://github.com/pypa/setuptools_scm"
50+
in api_rst.read_text()
51+
)
52+
assert (module / "src" / "my_module").is_dir()
53+
assert check_output("git", "branch", cwd=module).strip() == "* main"
54+
check_output("python", "-m", "venv", "venv", cwd=module)
55+
check_output("venv/bin/pip", "install", ".[dev]", cwd=module)
56+
check_output(
57+
"venv/bin/python",
58+
"-m",
59+
"sphinx",
60+
"-EWT",
61+
"--keep-going",
62+
"docs",
63+
"build/html",
64+
cwd=module,
65+
)
66+
with pytest.raises(ValueError) as ctx:
67+
check_output("venv/bin/python", "-m", "pytest", module / "tests", cwd=module)
68+
out = ctx.value.args[0]
69+
print(out)
70+
assert "4 failed, 1 passed" in out
71+
assert "Please change description in ./setup.cfg" in out
72+
assert "Please change ./README.rst" in out
73+
74+
75+
def test_new_module_existing_dir(tmp_path: Path):
76+
module = tmp_path / "my-module"
77+
makedirs(module / "existing_dir")
78+
79+
with pytest.raises(Exception) as excinfo:
80+
check_output(
81+
sys.executable,
82+
"-m",
83+
"python3_pip_skeleton",
84+
"new",
85+
"--org=myorg",
86+
"--package=my_module",
87+
"--full-name=Firstname Lastname",
88+
89+
str(module),
90+
)
91+
assert "to not exist, or be an empty dir" in str(excinfo.value)
92+
93+
94+
def test_existing_module(tmp_path: Path):
95+
module = tmp_path / "scanspec"
96+
__main__.git(
97+
"clone",
98+
"--depth",
99+
"1",
100+
"--branch",
101+
"0.5.3",
102+
"https://github.com/dls-controls/scanspec",
103+
str(module),
104+
)
105+
output = check_output(
106+
sys.executable,
107+
"-m",
108+
"python3_pip_skeleton",
109+
"existing",
110+
"--org=epics-containers",
111+
str(module),
112+
)
113+
assert output.endswith(
114+
"""
115+
Automatic merge failed; fix conflicts and then commit the result.
116+
117+
Please fix the conflicts above, then you can run:
118+
git branch -d skeleton-merge-branch
119+
Developer instructions in docs/developer/tutorials/dev-install.rst
120+
"""
121+
)
122+
__main__.git("merge", "--abort", cwd=str(module))
123+
MERGE_BRANCH = "skeleton-merge-branch"
124+
125+
with pytest.raises(Exception) as excinfo:
126+
output = check_output(
127+
sys.executable,
128+
"-m",
129+
"python3_pip_skeleton",
130+
"existing",
131+
"--org=epics-containers",
132+
str(module),
133+
)
134+
assert (
135+
f"{MERGE_BRANCH} already exists. \
136+
Please run 'python3-pip-skeleton clean' to remove it."
137+
in str(excinfo.value)
138+
)
139+
140+
branches = __main__.list_branches(module)
141+
assert MERGE_BRANCH in branches
142+
output = check_output(
143+
sys.executable,
144+
"-m",
145+
"python3_pip_skeleton",
146+
"clean",
147+
".",
148+
cwd=str(module),
149+
)
150+
assert output.strip("\n") == f"{MERGE_BRANCH} deleted from existing repo"
151+
branches = __main__.list_branches(module)
152+
assert MERGE_BRANCH not in branches
153+
154+
155+
def test_existing_module_already_adopted(tmp_path: Path):
156+
module = tmp_path / "scanspec"
157+
__main__.git(
158+
"clone",
159+
"--branch",
160+
"0.5.4", # dls-python3-skeleton was adopted in this release
161+
"https://github.com/dls-controls/scanspec",
162+
str(module),
163+
)
164+
with pytest.raises(Exception) as excinfo:
165+
check_output(
166+
sys.executable,
167+
"-m",
168+
"python3_pip_skeleton",
169+
"existing",
170+
"--org=epics-containers",
171+
str(module),
172+
)
173+
assert "already adopted skeleton" in str(excinfo.value)

0 commit comments

Comments
 (0)