diff --git a/src/python3_pip_skeleton/__main__.py b/src/python3_pip_skeleton/__main__.py index bc6be1a8..5a402675 100644 --- a/src/python3_pip_skeleton/__main__.py +++ b/src/python3_pip_skeleton/__main__.py @@ -52,6 +52,7 @@ def merge_skeleton( org: str, full_name: str, email: str, + from_branch: str, package, ): path = path.resolve() @@ -63,6 +64,7 @@ def replace_text(text: str) -> str: text = text.replace("python3_pip_skeleton", package) text = text.replace("Firstname Lastname", full_name) text = text.replace("email@address.com", email) + text = text.replace("main", from_branch) return text branches = list_branches(path) @@ -82,7 +84,7 @@ def replace_text(text: str) -> str: # will do the wrong thing shutil.rmtree(git_tmp / "src", ignore_errors=True) # Merge in the skeleton commits - git_tmp("pull", "--rebase=false", SKELETON, "main") + git_tmp("pull", "--rebase=false", SKELETON, from_branch) # Move things around if package != "python3_pip_skeleton": git_tmp("mv", "src/python3_pip_skeleton", f"src/{package}") @@ -156,6 +158,7 @@ def new(args): org=args.org, full_name=args.full_name or git("config", "--get", "user.name").strip(), email=args.email or git("config", "--get", "user.email").strip(), + from_branch=args.from_branch or "main", package=package, ) @@ -188,6 +191,7 @@ def existing(args): org=args.org, full_name=conf["metadata"]["author"], email=conf["metadata"]["author_email"], + from_branch=args.from_branch or "main", package=package, ) @@ -225,6 +229,11 @@ def main(args=None): sub.add_argument( "--email", default=None, help="Email address, defaults to git config user.email" ) + sub.add_argument( + "--from-branch", + default=None, + help="Merge from skeleton branch, defaults to main", + ) # Add a command for adopting in existing repo sub = subparsers.add_parser("existing", help="Adopt skeleton in existing repo") sub.set_defaults(func=existing) @@ -234,6 +243,11 @@ def main(args=None): sub.add_argument( "--package", default=None, help="Package name, defaults to directory name" ) + sub.add_argument( + "--from-branch", + default=None, + help="Merge from skeleton branch, defaults to main", + ) # Add a command for cleaning an existing repo of skeleton code sub = subparsers.add_parser( "clean", help="Clean up branch from failed skeleton merge" diff --git a/tests/test_adopt.py b/tests/test_adopt.py index aaece404..f785fe75 100644 --- a/tests/test_adopt.py +++ b/tests/test_adopt.py @@ -89,6 +89,45 @@ def test_new_module_existing_dir(tmp_path: Path): assert "to not exist, or be an empty dir" in str(excinfo.value) +def test_new_module_merge_from_valid_branch(tmp_path: Path): + module = tmp_path / "my-module" + check_output( + sys.executable, + "-m", + "python3_pip_skeleton", + "new", + "--org=myorg", + "--package=my_module", + "--full-name=Firstname Lastname", + "--email=me@myaddress.com", + "--from-branch=main", + str(module), + ) + # Test basic functionality + assert (module / "src" / "my_module").is_dir() + check_output("python", "-m", "venv", "venv", cwd=module) + check_output("venv/bin/pip", "install", ".[dev]", cwd=module) + + +def test_new_module_merge_from_invalid_branch(tmp_path: Path): + module = tmp_path / "my-module" + + with pytest.raises(ValueError) as excinfo: + check_output( + sys.executable, + "-m", + "python3_pip_skeleton", + "new", + "--org=myorg", + "--package=my_module", + "--full-name=Firstname Lastname", + "--email=me@myaddress.com", + "--from-branch=fail", + str(module), + ) + assert "couldn't find remote ref fail" in str(excinfo.value) + + def test_existing_module(tmp_path: Path): module = tmp_path / "scanspec" __main__.git( @@ -169,3 +208,27 @@ def test_existing_module_already_adopted(tmp_path: Path): str(module), ) assert "already adopted skeleton" in str(excinfo.value) + + +def test_existing_module_merge_from_invalid_branch(tmp_path: Path): + module = tmp_path / "scanspec" + __main__.git( + "clone", + "--depth", + "1", + "--branch", + "0.5.3", + "https://github.com/dls-controls/scanspec", + str(module), + ) + with pytest.raises(ValueError) as excinfo: + check_output( + sys.executable, + "-m", + "python3_pip_skeleton", + "existing", + "--org=epics-containers", + "--from-branch=fail", + str(module), + ) + assert "couldn't find remote ref fail" in str(excinfo.value)