Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .toys/kokoro-ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def list_products
# Currently, we include the rails tests for the run/rails tutorial.
omit_list = ["run/rails/test"]
@products = []
(Dir.glob("*/Gemfile") + Dir.glob("*/*/Gemfile")).each do |gemfile|
Dir.glob("*/**/Gemfile").each do |gemfile|

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using a recursive glob like */**/Gemfile can inadvertently match Gemfiles located inside dependency directories (such as vendor/ or .bundle/) or test fixtures (such as spec/ or test/ subdirectories). This can lead to executing tests on third-party dependencies or fixture code, which may cause unexpected failures or slow down the test suite. Filtering out these directories from the matched paths prevents this issue.

  Dir.glob("*/**/Gemfile").each do |gemfile|
    next if gemfile.split("/").any? { |part| ["vendor", ".bundle", "node_modules", "test", "spec"].include? part }

dir = File.dirname gemfile
if (File.directory?("#{dir}/test") && !omit_list.include?("#{dir}/test")) ||
(File.directory?("#{dir}/spec") && !omit_list.include?("#{dir}/spec")) ||
Expand Down Expand Up @@ -208,7 +208,7 @@ def start_app_engine_services
# Launch cloud sql proxy instances for cloud-sql tests
#
def start_cloud_sql_services
if @products.include? "cloud-sql/mysql"
if tests_product? "cloud-sql/mysql"
puts "Starting Cloud SQL Proxy for MySQL", :bold
connection_name = assert_env "MYSQL_INSTANCE_CONNECTION_NAME"
process = exec ["/bin/cloud_sql_proxy",
Expand All @@ -218,7 +218,7 @@ def start_cloud_sql_services
ENV["MYSQL_CLOUD_SQL_PROXY_PROCESS_ID"] = process.pid.to_s
@kill_on_cleanup << process
end
if @products.include? "cloud-sql/postgres"
if tests_product? "cloud-sql/postgres"
puts "Starting Cloud SQL Proxy for Postgres", :bold
connection_name = assert_env "POSTGRES_INSTANCE_CONNECTION_NAME"
process = exec ["/bin/cloud_sql_proxy",
Expand All @@ -228,7 +228,7 @@ def start_cloud_sql_services
ENV["POSTGRES_CLOUD_SQL_PROXY_PROCESS_ID"] = process.pid.to_s
@kill_on_cleanup << process
end
if @products.include? "cloud-sql/postgres"
if tests_product? "cloud-sql/sqlserver"
puts "Starting Cloud SQL Proxy for SQL Server", :bold
connection_name = assert_env "SQLSERVER_INSTANCE_CONNECTION_NAME"
process = exec ["/bin/cloud_sql_proxy",
Expand Down Expand Up @@ -369,6 +369,13 @@ def gac_path
@gac_path ||= ENV["GOOGLE_APPLICATION_CREDENTIALS"]
end

# Products are the sample directories themselves, but samples can also be
# nested underneath a product (for example cloud-sql/mysql/activerecord under
# cloud-sql/mysql), so match on the directory prefix rather than the exact name.
def tests_product? name
@products.any? { |dir| dir == name || dir.start_with?("#{name}/") }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If @products is not initialized or is set to nil, calling .any? on it will raise a NoMethodError. Wrapping @products in Array() ensures it is safely coerced to an empty array if it is nil, preventing potential runtime crashes.

  Array(@products).any? { |dir| dir == name || dir.start_with?("#{name}/") }

end

def presubmit?
unless defined? @is_presubmit
@is_presubmit = (/system-tests/ =~ assert_env("KOKORO_BUILD_ARTIFACTS_SUBDIR")).nil?
Expand Down