-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/standalone branch name validation (#4)
* Rehome acquire_repo with other repo functions * Build ALL_NAME_PARTS outside prediction call * Add standalone branch name validation command * Bump versions
- Loading branch information
1 parent
23ac26c
commit a32b3e1
Showing
12 changed files
with
198 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import click | ||
|
||
from .commands import branch_name | ||
|
||
|
||
@click.group(name="validate") | ||
def validate_group(): | ||
"""Command family for validation-related tasks.""" | ||
|
||
|
||
validate_group.add_command(branch_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
import sys | ||
from pathlib import Path | ||
|
||
import click | ||
|
||
from launch.local_repo.predict import validate_name | ||
from launch.local_repo.repo import acquire_repo | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"-b", | ||
"--branch-name", | ||
default="", | ||
help="Provide the exact name of a branch to be validated", | ||
) | ||
def branch_name(branch_name: str): | ||
"""Validates that a branch name will be compatible with our semver handling. If this command is launched from within a Git repository, the current branch will be evaluated. This behavior can be overridden by the --branch-name flag.""" | ||
|
||
cwd = Path.cwd() | ||
git_path = cwd.joinpath(".git") | ||
if git_path.exists() and git_path.is_dir(): | ||
# We're in a Git repo | ||
if not branch_name: | ||
this_repo = acquire_repo(cwd) | ||
try: | ||
branch_name = this_repo.active_branch.name | ||
except TypeError as te: | ||
if "HEAD is a detached symbolic reference" in str(te): | ||
click.secho( | ||
"Current directory contains a git repo that has a detached HEAD. Check out a branch or supply the --branch-name parameter.", | ||
fg="red", | ||
) | ||
sys.exit(-1) | ||
else: | ||
raise | ||
if not branch_name: | ||
click.secho( | ||
"Current directory doesn't contain a git repo, you must provide a branch name with the --branch-name parameter." | ||
) | ||
sys.exit(-2) | ||
try: | ||
if not validate_name(branch_name=branch_name): | ||
raise Exception(f"Branch {branch_name} isn't valid!") | ||
click.secho(f"Branch {branch_name} is valid.", fg="green") | ||
sys.exit(0) | ||
except Exception as e: | ||
click.secho(e, fg="red") | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import os | ||
|
||
import pytest | ||
from faker import Faker | ||
from git.repo import Repo | ||
|
||
fake = Faker() | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def working_dir(tmp_path): | ||
old_cwd = os.getcwd() | ||
os.chdir(tmp_path) | ||
yield tmp_path | ||
os.chdir(old_cwd) | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def example_repo(working_dir): | ||
repo = Repo.init(path=working_dir) | ||
yield repo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import pytest | ||
|
||
from launch.cli.validate.commands import branch_name | ||
|
||
ACCEPTABLE_BRANCH_NAMES = [ | ||
"fix/ok", | ||
"fix!/ok", | ||
"patch/ok", | ||
"patch!/ok", | ||
"!patch/ok", | ||
"BUG/ok", | ||
"BUG!/ok", | ||
"!buG/ok", | ||
"feature/ok", | ||
"feature!/ok", | ||
"Feature/ok", | ||
"!Feature/ok", | ||
"feature/ok", | ||
"FEATURE/ok", | ||
] | ||
|
||
UNACCEPTABLE_BRANCH_NAMES = [ | ||
"main", | ||
"foo/bar", | ||
"foo.bar", | ||
"aab20a1f33aa86ffae87b1786e6736f1c7e10d1d", # pragma: allowlist secret | ||
] | ||
|
||
|
||
class TestBranchName: | ||
@pytest.mark.parametrize("create_branch_name", ACCEPTABLE_BRANCH_NAMES) | ||
def test_branch_name_valid_in_git_repo( | ||
self, create_branch_name, cli_runner, example_repo | ||
): | ||
example_repo.git.checkout(["-b", create_branch_name]) | ||
result = cli_runner.invoke(branch_name, []) | ||
assert not result.exception | ||
assert f"Branch {create_branch_name} is valid." in result.output | ||
|
||
@pytest.mark.parametrize("create_branch_name", UNACCEPTABLE_BRANCH_NAMES) | ||
def test_branch_name_valid_in_git_repo( | ||
self, create_branch_name, cli_runner, example_repo | ||
): | ||
example_repo.git.checkout(["-b", create_branch_name]) | ||
result = cli_runner.invoke(branch_name, []) | ||
assert result.exception | ||
assert f"Branch {create_branch_name} isn't valid!" in result.output | ||
|
||
def test_branch_name_invalid_outside_git_repo(self, cli_runner, working_dir): | ||
result = cli_runner.invoke(branch_name, []) | ||
assert result.exception | ||
assert "Current directory doesn't contain a git repo" in result.output | ||
|
||
@pytest.mark.parametrize("use_branch_name", ACCEPTABLE_BRANCH_NAMES) | ||
def test_branch_name_valid_outside_git_repo( | ||
self, use_branch_name, cli_runner, working_dir | ||
): | ||
result = cli_runner.invoke(branch_name, ["--branch-name", use_branch_name]) | ||
assert not result.exception | ||
assert f"Branch {use_branch_name} is valid." in result.output | ||
|
||
@pytest.mark.parametrize("use_branch_name", UNACCEPTABLE_BRANCH_NAMES) | ||
def test_branch_name_invalid_outside_git_repo( | ||
self, use_branch_name, cli_runner, working_dir | ||
): | ||
result = cli_runner.invoke(branch_name, ["--branch-name", use_branch_name]) | ||
assert result.exception | ||
assert f"Branch {use_branch_name} isn't valid!" in result.output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters