-
Notifications
You must be signed in to change notification settings - Fork 50
Fix detection of Golang project layout #435
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
mmorhun
wants to merge
2
commits into
ambient-code:main
Choose a base branch
from
mmorhun:fix-golang-dir-layout
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.
+358
−8
Open
Changes from all commits
Commits
Show all changes
2 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,282 @@ | ||
| """Unit tests for BaseAssessor helper methods.""" | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| from agentready.assessors.base import BaseAssessor | ||
| from agentready.models.finding import Finding | ||
| from agentready.models.repository import Repository | ||
|
|
||
|
|
||
| class ConcreteAssessor(BaseAssessor): | ||
| """Concrete implementation for testing BaseAssessor methods.""" | ||
|
|
||
| @property | ||
| def attribute_id(self) -> str: | ||
| return "test_attribute" | ||
|
|
||
| @property | ||
| def tier(self) -> int: | ||
| return 1 | ||
|
|
||
| def assess(self, repository: Repository) -> Finding: | ||
| return Finding.create_pass(self.attribute_id, evidence="test", details="test") | ||
|
|
||
|
mmorhun marked this conversation as resolved.
|
||
|
|
||
| class TestPrimaryLanguage: | ||
| """Tests for _primary_language() method.""" | ||
|
|
||
| def test_go_manifest_detection(self, tmp_path): | ||
| """Single language detected by manifest returns immediately.""" | ||
| (tmp_path / "go.mod").write_text("module test\n") | ||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={"Go": 10, "Python": 100}, # Python has more files | ||
| total_files=110, | ||
| total_lines=1000, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| result = assessor._primary_language(repo, {"Go", "Python"}) | ||
|
|
||
| # Go should win despite having fewer files, because go.mod exists | ||
| assert result == "Go" | ||
|
|
||
| def test_python_manifest_detection(self, tmp_path): | ||
| """Python project detected by pyproject.toml.""" | ||
| (tmp_path / "pyproject.toml").write_text("[project]\nname = 'test'\n") | ||
|
|
||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={"Python": 50, "JavaScript": 100}, # JS has more files | ||
| total_files=150, | ||
| total_lines=1000, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| result = assessor._primary_language(repo, {"Python", "JavaScript"}) | ||
|
|
||
| # Python should win because pyproject.toml exists | ||
| assert result == "Python" | ||
|
|
||
| def test_typescript_manifest_tsconfig_priority(self, tmp_path): | ||
| """TypeScript detected when tsconfig.json exists.""" | ||
| (tmp_path / "package.json").write_text("{}") | ||
| (tmp_path / "tsconfig.json").write_text("{}") | ||
|
|
||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={"JavaScript": 200, "TypeScript": 50}, # More JS files | ||
| total_files=250, | ||
| total_lines=5000, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| result = assessor._primary_language(repo, {"JavaScript", "TypeScript"}) | ||
|
|
||
| # TypeScript should win because tsconfig.json exists | ||
| assert result == "TypeScript" | ||
|
|
||
| def test_javascript_manifest_detection_without_tsconfig(self, tmp_path): | ||
| """JavaScript wins when no tsconfig.json and more JS files.""" | ||
| (tmp_path / "package.json").write_text("{}") | ||
|
|
||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={"JavaScript": 200, "TypeScript": 50}, | ||
| total_files=250, | ||
| total_lines=5000, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| result = assessor._primary_language(repo, {"JavaScript", "TypeScript"}) | ||
|
|
||
| # JavaScript should win by file count | ||
| assert result == "JavaScript" | ||
|
|
||
| def test_typescript_by_file_count(self, tmp_path): | ||
| """TypeScript wins by file count when no tsconfig.json.""" | ||
| (tmp_path / "package.json").write_text("{}") | ||
|
|
||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={"JavaScript": 50, "TypeScript": 200}, | ||
| total_files=250, | ||
| total_lines=5000, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| result = assessor._primary_language(repo, {"JavaScript", "TypeScript"}) | ||
|
|
||
| assert result == "TypeScript" | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| def test_java_maven_detection(self, tmp_path): | ||
| """Java project detected by pom.xml.""" | ||
| (tmp_path / "pom.xml").write_text("<project></project>") | ||
|
|
||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={"Java": 100}, | ||
| total_files=100, | ||
| total_lines=2000, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| result = assessor._primary_language(repo, {"Java", "Python"}) | ||
|
|
||
| assert result == "Java" | ||
|
|
||
| def test_java_gradle_detection(self, tmp_path): | ||
| """Java project detected by build.gradle.""" | ||
| (tmp_path / "build.gradle").write_text("plugins {}") | ||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={"Java": 100}, | ||
| total_files=100, | ||
| total_lines=2000, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| result = assessor._primary_language(repo, {"Java", "Python"}) | ||
|
|
||
| assert result == "Java" | ||
|
|
||
| def test_rust_cargo_detection(self, tmp_path): | ||
| """Rust project detected by Cargo.toml.""" | ||
| (tmp_path / "Cargo.toml").write_text("[package]\nname = 'test'\n") | ||
|
|
||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={"Rust": 100}, | ||
| total_files=100, | ||
| total_lines=2000, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| result = assessor._primary_language(repo, {"Rust"}) | ||
|
|
||
| assert result == "Rust" | ||
|
|
||
| def test_multiple_manifests_use_file_count(self, tmp_path): | ||
| """When multiple languages have manifests, use file count.""" | ||
| (tmp_path / "go.mod").write_text("module test\n") | ||
| (tmp_path / "pyproject.toml").write_text("[project]\n") | ||
|
|
||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={"Go": 50, "Python": 150}, | ||
| total_files=200, | ||
| total_lines=4000, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| result = assessor._primary_language(repo, {"Go", "Python"}) | ||
|
|
||
| # Python has more files, so it should win | ||
| assert result == "Python" | ||
|
|
||
| def test_no_manifests_use_file_count(self, tmp_path): | ||
| """Falls back to file count when no manifests.""" | ||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={"Python": 100, "JavaScript": 50}, | ||
| total_files=150, | ||
| total_lines=3000, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| result = assessor._primary_language(repo, {"Python", "JavaScript"}) | ||
|
|
||
| # Python has more files | ||
| assert result == "Python" | ||
|
|
||
| def test_language_not_in_candidates(self, tmp_path): | ||
| """Manifest for language not in candidates is ignored.""" | ||
| (tmp_path / "go.mod").write_text("module test\n") | ||
|
|
||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={"Python": 100}, | ||
| total_files=100, | ||
| total_lines=2000, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| # Only ask about Python, not Go | ||
| result = assessor._primary_language(repo, {"Python"}) | ||
|
|
||
| assert result == "Python" | ||
|
|
||
| def test_no_languages_returns_none(self, tmp_path): | ||
| """Returns None when no languages present.""" | ||
|
|
||
| with patch.object(Repository, "__post_init__", lambda self: None): | ||
| repo = Repository( | ||
| path=tmp_path, | ||
| name="test", | ||
| url=None, | ||
| branch="main", | ||
| commit_hash="abc", | ||
| languages={}, | ||
| total_files=0, | ||
| total_lines=0, | ||
| ) | ||
|
|
||
| assessor = ConcreteAssessor() | ||
| result = assessor._primary_language(repo, {"Go", "Python"}) | ||
|
|
||
| assert result is None | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.