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

Commit e1070b6

Browse files
authored
Merge pull request #66 from DiamondLightSource/add-from-branch-arg
Add --from-branch cli arg
2 parents 282fc92 + af40caf commit e1070b6

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/python3_pip_skeleton/__main__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def merge_skeleton(
5252
org: str,
5353
full_name: str,
5454
email: str,
55+
from_branch: str,
5556
package,
5657
):
5758
path = path.resolve()
@@ -63,6 +64,7 @@ def replace_text(text: str) -> str:
6364
text = text.replace("python3_pip_skeleton", package)
6465
text = text.replace("Firstname Lastname", full_name)
6566
text = text.replace("[email protected]", email)
67+
text = text.replace("main", from_branch)
6668
return text
6769

6870
branches = list_branches(path)
@@ -82,7 +84,7 @@ def replace_text(text: str) -> str:
8284
# will do the wrong thing
8385
shutil.rmtree(git_tmp / "src", ignore_errors=True)
8486
# Merge in the skeleton commits
85-
git_tmp("pull", "--rebase=false", SKELETON, "main")
87+
git_tmp("pull", "--rebase=false", SKELETON, from_branch)
8688
# Move things around
8789
if package != "python3_pip_skeleton":
8890
git_tmp("mv", "src/python3_pip_skeleton", f"src/{package}")
@@ -156,6 +158,7 @@ def new(args):
156158
org=args.org,
157159
full_name=args.full_name or git("config", "--get", "user.name").strip(),
158160
email=args.email or git("config", "--get", "user.email").strip(),
161+
from_branch=args.from_branch or "main",
159162
package=package,
160163
)
161164

@@ -188,6 +191,7 @@ def existing(args):
188191
org=args.org,
189192
full_name=conf["metadata"]["author"],
190193
email=conf["metadata"]["author_email"],
194+
from_branch=args.from_branch or "main",
191195
package=package,
192196
)
193197

@@ -225,6 +229,11 @@ def main(args=None):
225229
sub.add_argument(
226230
"--email", default=None, help="Email address, defaults to git config user.email"
227231
)
232+
sub.add_argument(
233+
"--from-branch",
234+
default=None,
235+
help="Merge from skeleton branch, defaults to main",
236+
)
228237
# Add a command for adopting in existing repo
229238
sub = subparsers.add_parser("existing", help="Adopt skeleton in existing repo")
230239
sub.set_defaults(func=existing)
@@ -234,6 +243,11 @@ def main(args=None):
234243
sub.add_argument(
235244
"--package", default=None, help="Package name, defaults to directory name"
236245
)
246+
sub.add_argument(
247+
"--from-branch",
248+
default=None,
249+
help="Merge from skeleton branch, defaults to main",
250+
)
237251
# Add a command for cleaning an existing repo of skeleton code
238252
sub = subparsers.add_parser(
239253
"clean", help="Clean up branch from failed skeleton merge"

tests/test_adopt.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,45 @@ def test_new_module_existing_dir(tmp_path: Path):
9090
assert "to not exist, or be an empty dir" in str(excinfo.value)
9191

9292

93+
def test_new_module_merge_from_valid_branch(tmp_path: Path):
94+
module = tmp_path / "my-module"
95+
check_output(
96+
sys.executable,
97+
"-m",
98+
"python3_pip_skeleton",
99+
"new",
100+
"--org=myorg",
101+
"--package=my_module",
102+
"--full-name=Firstname Lastname",
103+
104+
"--from-branch=main",
105+
str(module),
106+
)
107+
# Test basic functionality
108+
assert (module / "src" / "my_module").is_dir()
109+
check_output("python", "-m", "venv", "venv", cwd=module)
110+
check_output("venv/bin/pip", "install", ".[dev]", cwd=module)
111+
112+
113+
def test_new_module_merge_from_invalid_branch(tmp_path: Path):
114+
module = tmp_path / "my-module"
115+
116+
with pytest.raises(ValueError) as excinfo:
117+
check_output(
118+
sys.executable,
119+
"-m",
120+
"python3_pip_skeleton",
121+
"new",
122+
"--org=myorg",
123+
"--package=my_module",
124+
"--full-name=Firstname Lastname",
125+
126+
"--from-branch=fail",
127+
str(module),
128+
)
129+
assert "couldn't find remote ref fail" in str(excinfo.value)
130+
131+
93132
def test_existing_module(tmp_path: Path):
94133
module = tmp_path / "scanspec"
95134
__main__.git(
@@ -170,3 +209,27 @@ def test_existing_module_already_adopted(tmp_path: Path):
170209
str(module),
171210
)
172211
assert "already adopted skeleton" in str(excinfo.value)
212+
213+
214+
def test_existing_module_merge_from_invalid_branch(tmp_path: Path):
215+
module = tmp_path / "scanspec"
216+
__main__.git(
217+
"clone",
218+
"--depth",
219+
"1",
220+
"--branch",
221+
"0.5.3",
222+
"https://github.com/dls-controls/scanspec",
223+
str(module),
224+
)
225+
with pytest.raises(ValueError) as excinfo:
226+
check_output(
227+
sys.executable,
228+
"-m",
229+
"python3_pip_skeleton",
230+
"existing",
231+
"--org=epics-containers",
232+
"--from-branch=fail",
233+
str(module),
234+
)
235+
assert "couldn't find remote ref fail" in str(excinfo.value)

0 commit comments

Comments
 (0)