Skip to content
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

fix(spanner): add empty string validation for emulator host #73

Merged
merged 2 commits into from Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Expand Up @@ -26,9 +26,6 @@ jobs:
strategy:
matrix:
include:
- os: ubuntu-latest
ruby: "2.6"
task: "--test"
- os: ubuntu-latest
ruby: "2.7"
task: "--test"
Expand All @@ -41,11 +38,14 @@ jobs:
- os: ubuntu-latest
ruby: "3.1"
task: "--test"
- os: ubuntu-latest
ruby: "3.2"
task: "--test"
- os: macos-latest
ruby: "3.0"
ruby: "3.2"
task: "--test"
- os: windows-latest
ruby: "3.0"
ruby: "3.2"
task: "--test"
fail-fast: false
runs-on: ${{ matrix.os }}
Expand Down
9 changes: 8 additions & 1 deletion google-cloud-spanner/lib/google/cloud/spanner.rb
Expand Up @@ -96,7 +96,7 @@ def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil,
project_id ||= project || default_project_id
scope ||= configure.scope
timeout ||= configure.timeout
emulator_host ||= configure.emulator_host
emulator_host = present_or_nil(emulator_host) || present_or_nil(configure.emulator_host)
endpoint ||= emulator_host || configure.endpoint
credentials ||= keyfile
lib_name ||= configure.lib_name
Expand Down Expand Up @@ -181,6 +181,13 @@ def self.default_credentials scope: nil
Google::Cloud.configure.credentials ||
Spanner::Credentials.default(scope: scope)
end

##
# @private checks if string is not nil or empty string
# returns the string if present else nil
def self.present_or_nil str
str.to_s.strip.empty? ? nil : str
end
end
end

Expand Down
30 changes: 30 additions & 0 deletions google-cloud-spanner/test/google/cloud/spanner_test.rb
Expand Up @@ -446,6 +446,36 @@ def quota_project_credentials.is_a? target
end
end

it 'ignores emulator_host if empty string' do
emulator_host = ""
project_id = "arbitrary-string"
ENV.stub :[], nil do
Google::Cloud::Spanner::Credentials.stub :default, default_credentials do
spanner = Google::Cloud::Spanner.new project_id: project_id, emulator_host: emulator_host
_(spanner).must_be_kind_of Google::Cloud::Spanner::Project
_(spanner.project).must_equal project_id
_(spanner.service.host).must_equal "spanner.googleapis.com"
end
end
end

it "ignores SPANNER_EMULATOR_HOST environment variable if empty" do
emulator_host = " "
emulator_check = ->(name) { (name == "SPANNER_EMULATOR_HOST") ? emulator_host : nil }
# Clear all environment variables, except SPANNER_EMULATOR_HOST
ENV.stub :[], emulator_check do
# Get project_id from Google Compute Engine
Google::Cloud.stub :env, OpenStruct.new(project_id: "project-id") do
Google::Cloud::Spanner::Credentials.stub :default, default_credentials do
spanner = Google::Cloud::Spanner.new
_(spanner).must_be_kind_of Google::Cloud::Spanner::Project
_(spanner.project).must_equal "project-id"
_(spanner.service.host).must_equal "spanner.googleapis.com"
end
end
end
end

it "uses provided lib name and lib version" do
lib_name = "spanner-ruby"
lib_version = "1.0.0"
Expand Down