-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(cli): respect ignore files in adk deploy commands #4187
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
kotaitos
wants to merge
5
commits into
google:main
Choose a base branch
from
kotaitos:fix/issue-4183-ignore-files
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0de8136
fix(cli): respect ignore files in adk deploy commands
kotaitos 6170db4
Merge remote-tracking branch 'upstream/main' into fix/issue-4183-igno…
kotaitos 04d56e2
chore(cli): fix syntax errors and reformat after merge
kotaitos b871d21
test(cli): update unit tests for ignore files after upstream merge
kotaitos fc259c3
chore(cli): revert accidental docstring changes
kotaitos 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,199 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Tests for ignore file support in cli_deploy.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from pathlib import Path | ||
| import shutil | ||
| import subprocess | ||
| from unittest import mock | ||
|
|
||
| import click | ||
| import pytest | ||
|
|
||
| import src.google.adk.cli.cli_deploy as cli_deploy | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _mute_click(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Suppress click.echo to keep test output clean.""" | ||
| monkeypatch.setattr(click, "echo", lambda *_a, **_k: None) | ||
| monkeypatch.setattr(click, "secho", lambda *_a, **_k: None) | ||
|
|
||
|
|
||
| def test_to_cloud_run_respects_ignore_files( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| tmp_path: Path, | ||
| ) -> None: | ||
| """Test that to_cloud_run respects .gitignore and .gcloudignore.""" | ||
| agent_dir = tmp_path / "agent" | ||
| agent_dir.mkdir() | ||
| (agent_dir / "agent.py").write_text("# agent") | ||
| (agent_dir / "__init__.py").write_text("") | ||
| (agent_dir / "ignored_by_git.txt").write_text("ignored") | ||
| (agent_dir / "ignored_by_gcloud.txt").write_text("ignored") | ||
| (agent_dir / "not_ignored.txt").write_text("keep") | ||
|
|
||
| (agent_dir / ".gitignore").write_text("ignored_by_git.txt\n") | ||
| (agent_dir / ".gcloudignore").write_text("ignored_by_gcloud.txt\n") | ||
|
|
||
| temp_deploy_dir = tmp_path / "temp_deploy" | ||
|
|
||
| # Mock subprocess.run to avoid actual gcloud call | ||
| monkeypatch.setattr(subprocess, "run", mock.Mock()) | ||
| # Mock shutil.rmtree to keep the temp folder for verification | ||
| monkeypatch.setattr( | ||
| shutil, | ||
| "rmtree", | ||
| lambda path, **kwargs: None | ||
| if "temp_deploy" in str(path) | ||
| else shutil.rmtree(path, **kwargs), | ||
| ) | ||
|
|
||
| cli_deploy.to_cloud_run( | ||
| agent_folder=str(agent_dir), | ||
| project="proj", | ||
| region="us-central1", | ||
| service_name="svc", | ||
| app_name="app", | ||
| temp_folder=str(temp_deploy_dir), | ||
| port=8080, | ||
| trace_to_cloud=False, | ||
| otel_to_cloud=False, | ||
| with_ui=False, | ||
| log_level="info", | ||
| verbosity="info", | ||
| adk_version="1.0.0", | ||
| ) | ||
|
|
||
| agent_src_path = temp_deploy_dir / "agents" / "app" | ||
|
|
||
| assert (agent_src_path / "agent.py").exists() | ||
| assert (agent_src_path / "not_ignored.txt").exists() | ||
|
|
||
| # These should be ignored | ||
| assert not ( | ||
| agent_src_path / "ignored_by_git.txt" | ||
| ).exists(), "Should respect .gitignore" | ||
| assert not ( | ||
| agent_src_path / "ignored_by_gcloud.txt" | ||
| ).exists(), "Should respect .gcloudignore" | ||
|
|
||
|
|
||
| def test_to_agent_engine_respects_multiple_ignore_files( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| tmp_path: Path, | ||
| ) -> None: | ||
| """Test that to_agent_engine respects .gitignore, .gcloudignore and .ae_ignore.""" | ||
| # We need to be in the project dir for to_agent_engine | ||
| project_dir = tmp_path / "project" | ||
| project_dir.mkdir() | ||
| monkeypatch.chdir(project_dir) | ||
|
|
||
| agent_dir = project_dir / "my_agent" | ||
| agent_dir.mkdir() | ||
| (agent_dir / "agent.py").write_text("root_agent = None") | ||
| (agent_dir / "__init__.py").write_text("from . import agent") | ||
| (agent_dir / "ignored_by_git.txt").write_text("ignored") | ||
| (agent_dir / "ignored_by_ae.txt").write_text("ignored") | ||
|
|
||
| (agent_dir / ".gitignore").write_text("ignored_by_git.txt\n") | ||
| (agent_dir / ".ae_ignore").write_text("ignored_by_ae.txt\n") | ||
|
|
||
| # Mock vertexai.Client and other things to avoid network/complex setup | ||
| monkeypatch.setattr("vertexai.Client", mock.Mock()) | ||
| # Mock shutil.rmtree to keep the temp folder for verification | ||
| original_rmtree = shutil.rmtree | ||
|
|
||
| def mock_rmtree(path, **kwargs): | ||
| if "_tmp" in str(path): | ||
| return None | ||
| return original_rmtree(path, **kwargs) | ||
|
|
||
| monkeypatch.setattr(shutil, "rmtree", mock_rmtree) | ||
|
|
||
| cli_deploy.to_agent_engine( | ||
| agent_folder=str(agent_dir), | ||
| staging_bucket="gs://test", | ||
| adk_app="adk_app", | ||
| ) | ||
|
|
||
| # Find the temp folder created by to_agent_engine | ||
| temp_folders = [ | ||
| d for d in project_dir.iterdir() if d.is_dir() and "_tmp" in d.name | ||
| ] | ||
| assert len(temp_folders) == 1 | ||
| agent_src_path = temp_folders[0] | ||
|
|
||
| assert (agent_src_path / "agent.py").exists() | ||
| assert not ( | ||
| agent_src_path / "ignored_by_git.txt" | ||
| ).exists(), "Should respect .gitignore" | ||
| assert not ( | ||
| agent_src_path / "ignored_by_ae.txt" | ||
| ).exists(), "Should respect .ae_ignore" | ||
|
|
||
|
|
||
| def test_to_gke_respects_ignore_files( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| tmp_path: Path, | ||
| ) -> None: | ||
| """Test that to_gke respects ignore files.""" | ||
| agent_dir = tmp_path / "agent" | ||
| agent_dir.mkdir() | ||
| (agent_dir / "agent.py").write_text("# agent") | ||
| (agent_dir / "__init__.py").write_text("") | ||
| (agent_dir / "ignored.txt").write_text("ignored") | ||
| (agent_dir / ".gitignore").write_text("ignored.txt\n") | ||
|
|
||
| temp_deploy_dir = tmp_path / "temp_deploy" | ||
|
|
||
| # Mock subprocess.run to avoid actual gcloud call | ||
| mock_run = mock.Mock() | ||
| mock_run.return_value.stdout = "deployment created" | ||
| monkeypatch.setattr(subprocess, "run", mock_run) | ||
| # Mock shutil.rmtree to keep the temp folder for verification | ||
| monkeypatch.setattr( | ||
| shutil, | ||
| "rmtree", | ||
| lambda path, **kwargs: None | ||
| if "temp_deploy" in str(path) | ||
| else shutil.rmtree(path, **kwargs), | ||
| ) | ||
|
|
||
| cli_deploy.to_gke( | ||
| agent_folder=str(agent_dir), | ||
| project="proj", | ||
| region="us-central1", | ||
| cluster_name="cluster", | ||
| service_name="svc", | ||
| app_name="app", | ||
| temp_folder=str(temp_deploy_dir), | ||
| port=8080, | ||
| trace_to_cloud=False, | ||
| otel_to_cloud=False, | ||
| with_ui=False, | ||
| log_level="info", | ||
| adk_version="1.0.0", | ||
| ) | ||
|
|
||
| agent_src_path = temp_deploy_dir / "agents" / "app" | ||
|
|
||
| assert (agent_src_path / "agent.py").exists() | ||
| assert not ( | ||
| agent_src_path / "ignored.txt" | ||
| ).exists(), "Should respect .gitignore" |
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.
This
try...exceptblock can be made more robust.utf-8is a safe default for ignore files.Exceptionis too broad and can hide unexpected bugs. It's better to catch more specific exceptions, such asOSErrorfor file I/O problems andUnicodeDecodeErrorwhen an encoding is specified.