From 7cabd68499108e2676d497f488b6dda90bca6bd8 Mon Sep 17 00:00:00 2001 From: Michael Stubbings Date: Mon, 23 Jan 2023 11:21:02 +0000 Subject: [PATCH 1/4] Add --from-branch cli arg This will allow users to choose which skeleton branch they want to merge with their code. If they do not supply this argument, then main will be used by default. --- src/python3_pip_skeleton/__main__.py | 15 ++++++- tests/test_adopt.py | 60 ++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/python3_pip_skeleton/__main__.py b/src/python3_pip_skeleton/__main__.py index bc6be1a8..308e191f 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() @@ -82,7 +83,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 or "main") # Move things around if package != "python3_pip_skeleton": git_tmp("mv", "src/python3_pip_skeleton", f"src/{package}") @@ -156,6 +157,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, package=package, ) @@ -188,6 +190,7 @@ def existing(args): org=args.org, full_name=conf["metadata"]["author"], email=conf["metadata"]["author_email"], + from_branch=args.from_branch, package=package, ) @@ -225,6 +228,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 +242,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..b1e6151b 100644 --- a/tests/test_adopt.py +++ b/tests/test_adopt.py @@ -89,6 +89,42 @@ 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), + ) + assert check_output("git", "branch", cwd=module).strip() == "* main" + + +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 +205,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) From 7a8ca56ac802668d3c69532746db2770a96ac756 Mon Sep 17 00:00:00 2001 From: Michael Stubbings Date: Tue, 31 Jan 2023 11:31:23 +0000 Subject: [PATCH 2/4] Add from_branch to replace_text in __main__.py --- src/python3_pip_skeleton/__main__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/python3_pip_skeleton/__main__.py b/src/python3_pip_skeleton/__main__.py index 308e191f..d75a881c 100644 --- a/src/python3_pip_skeleton/__main__.py +++ b/src/python3_pip_skeleton/__main__.py @@ -64,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) From 891bd030bcaab75c7f69ad81646a5dfb360ef8c4 Mon Sep 17 00:00:00 2001 From: Michael Stubbings Date: Tue, 31 Jan 2023 11:41:07 +0000 Subject: [PATCH 3/4] Modify from_branch default value for code consistency --- src/python3_pip_skeleton/__main__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/python3_pip_skeleton/__main__.py b/src/python3_pip_skeleton/__main__.py index d75a881c..5a402675 100644 --- a/src/python3_pip_skeleton/__main__.py +++ b/src/python3_pip_skeleton/__main__.py @@ -84,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, from_branch or "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}") @@ -158,7 +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, + from_branch=args.from_branch or "main", package=package, ) @@ -191,7 +191,7 @@ def existing(args): org=args.org, full_name=conf["metadata"]["author"], email=conf["metadata"]["author_email"], - from_branch=args.from_branch, + from_branch=args.from_branch or "main", package=package, ) From af40caf9fe3d649055b2c780086670d5cb2e7aee Mon Sep 17 00:00:00 2001 From: Michael Stubbings Date: Tue, 31 Jan 2023 14:57:10 +0000 Subject: [PATCH 4/4] Add functional tests to 'from valid branch' test --- tests/test_adopt.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_adopt.py b/tests/test_adopt.py index b1e6151b..f785fe75 100644 --- a/tests/test_adopt.py +++ b/tests/test_adopt.py @@ -103,7 +103,10 @@ def test_new_module_merge_from_valid_branch(tmp_path: Path): "--from-branch=main", str(module), ) - assert check_output("git", "branch", cwd=module).strip() == "* main" + # 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):