-
Notifications
You must be signed in to change notification settings - Fork 6.2k
fix: silence git fetch stdout to prevent branch number parse error #1696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fsilvaortiz
wants to merge
3
commits into
github:main
Choose a base branch
from
fsilvaortiz:fix/silence-git-fetch-stdout-in-create-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,134 @@ | ||
| """ | ||
| Tests for scripts/bash/create-new-feature.sh. | ||
|
|
||
| Tests cover: | ||
| - JSON output validity when git fetch produces stdout noise (#1592) | ||
| - Correct stdout/stderr suppression in check_existing_branches() | ||
| """ | ||
|
|
||
| import json | ||
| import shutil | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| SCRIPT_PATH = Path(__file__).resolve().parent.parent / "scripts" / "bash" / "create-new-feature.sh" | ||
|
|
||
| requires_git = pytest.mark.skipif( | ||
| shutil.which("git") is None, | ||
| reason="git is not installed", | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def git_repo(tmp_path): | ||
| """Create a temporary git repo for testing create-new-feature.sh.""" | ||
| repo = tmp_path / "repo" | ||
| repo.mkdir() | ||
|
|
||
| # Initialize git repo | ||
| subprocess.run(["git", "init", str(repo)], capture_output=True, check=True) | ||
| subprocess.run(["git", "-C", str(repo), "config", "user.email", "test@test.com"], capture_output=True, check=True) | ||
| subprocess.run(["git", "-C", str(repo), "config", "user.name", "Test"], capture_output=True, check=True) | ||
|
|
||
| # Create an initial commit so HEAD exists | ||
| (repo / "README.md").write_text("# Test") | ||
| subprocess.run(["git", "-C", str(repo), "add", "."], capture_output=True, check=True) | ||
| subprocess.run(["git", "-C", str(repo), "config", "commit.gpgsign", "false"], capture_output=True, check=True) | ||
| subprocess.run(["git", "-C", str(repo), "commit", "-m", "init"], capture_output=True, check=True) | ||
|
|
||
|
Comment on lines
35
to
40
|
||
| # Create .specify dir to simulate an initialized project | ||
| (repo / ".specify").mkdir() | ||
| (repo / ".specify" / "templates").mkdir() | ||
|
|
||
| yield repo | ||
|
|
||
|
|
||
| @requires_git | ||
| class TestCreateNewFeatureJsonOutput: | ||
| """Test that --json output is always valid JSON (#1592).""" | ||
|
|
||
| def test_json_output_is_valid(self, git_repo): | ||
| """Script should produce valid JSON with BRANCH_NAME, SPEC_FILE, FEATURE_NUM.""" | ||
| result = subprocess.run( | ||
| ["bash", str(SCRIPT_PATH), "--json", "Test feature description"], | ||
| capture_output=True, | ||
| text=True, | ||
| cwd=str(git_repo), | ||
| ) | ||
|
|
||
| assert result.returncode == 0, f"Script failed: {result.stderr}" | ||
|
|
||
| output = result.stdout.strip() | ||
| parsed = json.loads(output) | ||
|
|
||
| assert "BRANCH_NAME" in parsed | ||
| assert "SPEC_FILE" in parsed | ||
| assert "FEATURE_NUM" in parsed | ||
|
|
||
| def test_feature_num_is_numeric(self, git_repo): | ||
| """FEATURE_NUM should be a zero-padded numeric string.""" | ||
| result = subprocess.run( | ||
| ["bash", str(SCRIPT_PATH), "--json", "Another feature"], | ||
| capture_output=True, | ||
| text=True, | ||
| cwd=str(git_repo), | ||
| ) | ||
|
|
||
| assert result.returncode == 0, f"Script failed: {result.stderr}" | ||
|
|
||
| parsed = json.loads(result.stdout.strip()) | ||
| feature_num = parsed["FEATURE_NUM"] | ||
|
|
||
| assert feature_num.isdigit(), f"FEATURE_NUM is not numeric: {feature_num}" | ||
| assert len(feature_num) == 3, f"FEATURE_NUM is not zero-padded: {feature_num}" | ||
|
|
||
|
|
||
| @requires_git | ||
| class TestGitFetchSilencing: | ||
| """Verify that git fetch stdout does not contaminate branch number detection.""" | ||
|
|
||
| def test_script_does_not_leak_git_fetch_stdout(self, git_repo): | ||
| """JSON output should contain only the JSON line, no git fetch noise.""" | ||
| result = subprocess.run( | ||
| ["bash", str(SCRIPT_PATH), "--json", "Verify no fetch noise"], | ||
| capture_output=True, | ||
| text=True, | ||
| cwd=str(git_repo), | ||
| ) | ||
|
|
||
| assert result.returncode == 0, f"Script failed: {result.stderr}" | ||
|
|
||
| stdout_lines = result.stdout.strip().splitlines() | ||
| # In JSON mode, stdout should contain exactly one line: the JSON object | ||
| json_lines = [line for line in stdout_lines if line.startswith("{")] | ||
| assert len(json_lines) == 1, f"Expected 1 JSON line, got {len(json_lines)}: {stdout_lines}" | ||
|
|
||
| def test_script_does_not_leak_git_fetch_to_json(self, git_repo): | ||
| """Repeated runs should always produce clean JSON without fetch artifacts.""" | ||
| for i in range(2): | ||
| result = subprocess.run( | ||
| ["bash", str(SCRIPT_PATH), "--json", f"Run {i} verify clean"], | ||
| capture_output=True, | ||
| text=True, | ||
| cwd=str(git_repo), | ||
| ) | ||
| assert result.returncode == 0, f"Run {i} failed: {result.stderr}" | ||
| parsed = json.loads(result.stdout.strip()) | ||
| assert parsed["FEATURE_NUM"].isdigit() | ||
|
|
||
|
|
||
| class TestScriptRedirectPattern: | ||
| """Static analysis: verify the script uses correct redirect patterns.""" | ||
|
|
||
| def test_git_fetch_redirect_pattern_in_script(self): | ||
| """The git fetch call should redirect both stdout and stderr to /dev/null.""" | ||
| script_content = SCRIPT_PATH.read_text() | ||
|
|
||
| assert "git fetch --all --prune >/dev/null 2>&1" in script_content, ( | ||
| "git fetch should redirect both stdout and stderr: >/dev/null 2>&1" | ||
| ) | ||
| assert "git fetch --all --prune 2>/dev/null" not in script_content, ( | ||
| "git fetch should NOT redirect only stderr (old pattern)" | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
git_repofixture docstring says it creates “a fake remote that produces stdout on fetch”, but the fixture never configures any remotes. Either update the docstring to match what the fixture actually does, or add a local/bare remote setup so the test genuinely exercises the fetch-output scenario described in #1592.This issue also appears in the following locations of the same file: