make tests windows friendly (#164)

This commit is contained in:
Graham Paye 2018-10-10 18:23:50 -07:00 committed by GitHub
parent 6d9d63be3e
commit 7e42656b1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 9 deletions

View File

@ -50,12 +50,16 @@ describe '#get_application_default' do
@original_env_vals = {} @original_env_vals = {}
@credential_vars.each { |var| @original_env_vals[var] = ENV[var] } @credential_vars.each { |var| @original_env_vals[var] = ENV[var] }
@home = ENV['HOME'] @home = ENV['HOME']
@app_data = ENV['APPDATA']
@program_data = ENV['ProgramData']
@scope = 'https://www.googleapis.com/auth/userinfo.profile' @scope = 'https://www.googleapis.com/auth/userinfo.profile'
end end
after(:example) do after(:example) do
@credential_vars.each { |var| ENV[var] = @original_env_vals[var] } @credential_vars.each { |var| ENV[var] = @original_env_vals[var] }
ENV['HOME'] = @home unless @home == ENV['HOME'] ENV['HOME'] = @home unless @home == ENV['HOME']
ENV['APPDATA'] = @app_data unless @app_data == ENV['APPDATA']
ENV['ProgramData'] = @program_data unless @program_data == ENV['ProgramData']
end end
shared_examples 'it cannot load misconfigured credentials' do shared_examples 'it cannot load misconfigured credentials' do
@ -90,7 +94,7 @@ describe '#get_application_default' do
FileUtils.mkdir_p(File.dirname(key_path)) FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text) File.write(key_path, cred_json_text)
ENV[@var_name] = key_path ENV[@var_name] = key_path
expect(Google::Auth.get_application_default @scope, options) expect(Google::Auth.get_application_default(@scope, options))
.to_not be_nil .to_not be_nil
end end
end end
@ -99,10 +103,12 @@ describe '#get_application_default' do
ENV.delete(@var_name) unless ENV[@var_name].nil? ENV.delete(@var_name) unless ENV[@var_name].nil?
Dir.mktmpdir do |dir| Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', WELL_KNOWN_PATH) key_path = File.join(dir, '.config', WELL_KNOWN_PATH)
key_path = File.join(dir, WELL_KNOWN_PATH) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path)) FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text) File.write(key_path, cred_json_text)
ENV['HOME'] = dir ENV['HOME'] = dir
expect(Google::Auth.get_application_default @scope, options) ENV['APPDATA'] = dir
expect(Google::Auth.get_application_default(@scope, options))
.to_not be_nil .to_not be_nil
end end
end end
@ -111,10 +117,12 @@ describe '#get_application_default' do
ENV.delete(@var_name) unless ENV[@var_name].nil? ENV.delete(@var_name) unless ENV[@var_name].nil?
Dir.mktmpdir do |dir| Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', WELL_KNOWN_PATH) key_path = File.join(dir, '.config', WELL_KNOWN_PATH)
key_path = File.join(dir, WELL_KNOWN_PATH) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path)) FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text) File.write(key_path, cred_json_text)
ENV['HOME'] = dir ENV['HOME'] = dir
expect(Google::Auth.get_application_default nil, options).to_not be_nil ENV['APPDATA'] = dir
expect(Google::Auth.get_application_default(nil, options)).to_not be_nil
end end
end end
@ -134,10 +142,12 @@ describe '#get_application_default' do
it 'succeeds with system default file' do it 'succeeds with system default file' do
ENV.delete(@var_name) unless ENV[@var_name].nil? ENV.delete(@var_name) unless ENV[@var_name].nil?
FakeFS do FakeFS do
key_path = File.join('/etc/google/auth/', CREDENTIALS_FILE_NAME) ENV['ProgramData'] = '/etc'
prefix = OS.windows? ? '/etc/Google/Auth/' : '/etc/google/auth/'
key_path = File.join(prefix, CREDENTIALS_FILE_NAME)
FileUtils.mkdir_p(File.dirname(key_path)) FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text) File.write(key_path, cred_json_text)
expect(Google::Auth.get_application_default @scope, options) expect(Google::Auth.get_application_default(@scope, options))
.to_not be_nil .to_not be_nil
File.delete(key_path) File.delete(key_path)
end end
@ -151,7 +161,7 @@ describe '#get_application_default' do
ENV[CLIENT_SECRET_VAR] = cred_json[:client_secret] ENV[CLIENT_SECRET_VAR] = cred_json[:client_secret]
ENV[REFRESH_TOKEN_VAR] = cred_json[:refresh_token] ENV[REFRESH_TOKEN_VAR] = cred_json[:refresh_token]
ENV[ACCOUNT_TYPE_VAR] = cred_json[:type] ENV[ACCOUNT_TYPE_VAR] = cred_json[:type]
expect(Google::Auth.get_application_default @scope, options) expect(Google::Auth.get_application_default(@scope, options))
.to_not be_nil .to_not be_nil
end end
@ -238,9 +248,11 @@ describe '#get_application_default' do
ENV.delete(@var_name) unless ENV[@var_name].nil? ENV.delete(@var_name) unless ENV[@var_name].nil?
Dir.mktmpdir do |dir| Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', WELL_KNOWN_PATH) key_path = File.join(dir, '.config', WELL_KNOWN_PATH)
key_path = File.join(dir, WELL_KNOWN_PATH) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path)) FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text) File.write(key_path, cred_json_text)
ENV['HOME'] = dir ENV['HOME'] = dir
ENV['APPDATA'] = dir
expect do expect do
Google::Auth.get_application_default @scope, options Google::Auth.get_application_default @scope, options
end.to raise_error RuntimeError end.to raise_error RuntimeError

View File

@ -40,6 +40,7 @@ require 'multi_json'
require 'openssl' require 'openssl'
require 'spec_helper' require 'spec_helper'
require 'tmpdir' require 'tmpdir'
require 'os'
include Google::Auth::CredentialsLoader include Google::Auth::CredentialsLoader
@ -223,6 +224,7 @@ describe Google::Auth::ServiceAccountCredentials do
describe '#from_well_known_path' do describe '#from_well_known_path' do
before(:example) do before(:example) do
@home = ENV['HOME'] @home = ENV['HOME']
@app_data = ENV['APPDATA']
@scope = 'https://www.googleapis.com/auth/userinfo.profile' @scope = 'https://www.googleapis.com/auth/userinfo.profile'
@known_path = WELL_KNOWN_PATH @known_path = WELL_KNOWN_PATH
@clz = ServiceAccountCredentials @clz = ServiceAccountCredentials
@ -230,6 +232,7 @@ describe Google::Auth::ServiceAccountCredentials do
after(:example) do after(:example) do
ENV['HOME'] = @home unless @home == ENV['HOME'] ENV['HOME'] = @home unless @home == ENV['HOME']
ENV['APPDATA'] = @app_data unless @app_data == ENV['APPDATA']
end end
it 'is nil if no file exists' do it 'is nil if no file exists' do
@ -240,9 +243,11 @@ describe Google::Auth::ServiceAccountCredentials do
it 'successfully loads the file when it is present' do it 'successfully loads the file when it is present' do
Dir.mktmpdir do |dir| Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', @known_path) key_path = File.join(dir, '.config', @known_path)
key_path = File.join(dir, WELL_KNOWN_PATH) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path)) FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text) File.write(key_path, cred_json_text)
ENV['HOME'] = dir ENV['HOME'] = dir
ENV['APPDATA'] = dir
expect(@clz.from_well_known_path(@scope)).to_not be_nil expect(@clz.from_well_known_path(@scope)).to_not be_nil
end end
end end
@ -251,10 +256,16 @@ describe Google::Auth::ServiceAccountCredentials do
describe '#from_system_default_path' do describe '#from_system_default_path' do
before(:example) do before(:example) do
@scope = 'https://www.googleapis.com/auth/userinfo.profile' @scope = 'https://www.googleapis.com/auth/userinfo.profile'
@path = File.join('/etc/google/auth/', CREDENTIALS_FILE_NAME) @program_data = ENV['ProgramData']
@prefix = OS.windows? ? '/etc/Google/Auth/' : '/etc/google/auth/'
@path = File.join(@prefix, CREDENTIALS_FILE_NAME)
@clz = ServiceAccountCredentials @clz = ServiceAccountCredentials
end end
after(:example) do
ENV['ProgramData'] = @program_data
end
it 'is nil if no file exists' do it 'is nil if no file exists' do
FakeFS do FakeFS do
expect(ServiceAccountCredentials.from_system_default_path(@scope)) expect(ServiceAccountCredentials.from_system_default_path(@scope))
@ -264,6 +275,7 @@ describe Google::Auth::ServiceAccountCredentials do
it 'successfully loads the file when it is present' do it 'successfully loads the file when it is present' do
FakeFS do FakeFS do
ENV['ProgramData'] = '/etc'
FileUtils.mkdir_p(File.dirname(@path)) FileUtils.mkdir_p(File.dirname(@path))
File.write(@path, cred_json_text) File.write(@path, cred_json_text)
expect(@clz.from_system_default_path(@scope)).to_not be_nil expect(@clz.from_system_default_path(@scope)).to_not be_nil
@ -351,10 +363,12 @@ describe Google::Auth::ServiceAccountJwtHeaderCredentials do
describe '#from_well_known_path' do describe '#from_well_known_path' do
before(:example) do before(:example) do
@home = ENV['HOME'] @home = ENV['HOME']
@app_data = ENV['APPDATA']
end end
after(:example) do after(:example) do
ENV['HOME'] = @home unless @home == ENV['HOME'] ENV['HOME'] = @home unless @home == ENV['HOME']
ENV['APPDATA'] = @app_data unless @app_data == ENV['APPDATA']
end end
it 'is nil if no file exists' do it 'is nil if no file exists' do
@ -365,9 +379,11 @@ describe Google::Auth::ServiceAccountJwtHeaderCredentials do
it 'successfully loads the file when it is present' do it 'successfully loads the file when it is present' do
Dir.mktmpdir do |dir| Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', WELL_KNOWN_PATH) key_path = File.join(dir, '.config', WELL_KNOWN_PATH)
key_path = File.join(dir, WELL_KNOWN_PATH) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path)) FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text) File.write(key_path, cred_json_text)
ENV['HOME'] = dir ENV['HOME'] = dir
ENV['APPDATA'] = dir
expect(clz.from_well_known_path).to_not be_nil expect(clz.from_well_known_path).to_not be_nil
end end
end end

View File

@ -40,6 +40,7 @@ require 'multi_json'
require 'openssl' require 'openssl'
require 'spec_helper' require 'spec_helper'
require 'tmpdir' require 'tmpdir'
require 'os'
include Google::Auth::CredentialsLoader include Google::Auth::CredentialsLoader
@ -154,6 +155,7 @@ describe Google::Auth::UserRefreshCredentials do
describe '#from_well_known_path' do describe '#from_well_known_path' do
before(:example) do before(:example) do
@home = ENV['HOME'] @home = ENV['HOME']
@app_data = ENV['APPDATA']
@scope = 'https://www.googleapis.com/auth/userinfo.profile' @scope = 'https://www.googleapis.com/auth/userinfo.profile'
@known_path = WELL_KNOWN_PATH @known_path = WELL_KNOWN_PATH
@clz = UserRefreshCredentials @clz = UserRefreshCredentials
@ -161,6 +163,7 @@ describe Google::Auth::UserRefreshCredentials do
after(:example) do after(:example) do
ENV['HOME'] = @home unless @home == ENV['HOME'] ENV['HOME'] = @home unless @home == ENV['HOME']
ENV['APPDATA'] = @app_data unless @app_data == ENV['APPDATA']
end end
it 'is nil if no file exists' do it 'is nil if no file exists' do
@ -173,9 +176,11 @@ describe Google::Auth::UserRefreshCredentials do
needed.each do |missing| needed.each do |missing|
Dir.mktmpdir do |dir| Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', @known_path) key_path = File.join(dir, '.config', @known_path)
key_path = File.join(dir, @known_path) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path)) FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text(missing)) File.write(key_path, cred_json_text(missing))
ENV['HOME'] = dir ENV['HOME'] = dir
ENV['APPDATA'] = dir
expect { @clz.from_well_known_path(@scope) } expect { @clz.from_well_known_path(@scope) }
.to raise_error RuntimeError .to raise_error RuntimeError
end end
@ -185,9 +190,11 @@ describe Google::Auth::UserRefreshCredentials do
it 'successfully loads the file when it is present' do it 'successfully loads the file when it is present' do
Dir.mktmpdir do |dir| Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', @known_path) key_path = File.join(dir, '.config', @known_path)
key_path = File.join(dir, @known_path) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path)) FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text) File.write(key_path, cred_json_text)
ENV['HOME'] = dir ENV['HOME'] = dir
ENV['APPDATA'] = dir
expect(@clz.from_well_known_path(@scope)).to_not be_nil expect(@clz.from_well_known_path(@scope)).to_not be_nil
end end
end end
@ -196,10 +203,16 @@ describe Google::Auth::UserRefreshCredentials do
describe '#from_system_default_path' do describe '#from_system_default_path' do
before(:example) do before(:example) do
@scope = 'https://www.googleapis.com/auth/userinfo.profile' @scope = 'https://www.googleapis.com/auth/userinfo.profile'
@path = File.join('/etc/google/auth/', CREDENTIALS_FILE_NAME) @prefix = OS.windows? ? '/etc/Google/Auth/' : '/etc/google/auth/'
@path = File.join(@prefix, CREDENTIALS_FILE_NAME)
@program_data = ENV['ProgramData']
@clz = UserRefreshCredentials @clz = UserRefreshCredentials
end end
after(:example) do
ENV['ProgramData'] = @program_data
end
it 'is nil if no file exists' do it 'is nil if no file exists' do
FakeFS do FakeFS do
expect(UserRefreshCredentials.from_system_default_path(@scope)) expect(UserRefreshCredentials.from_system_default_path(@scope))
@ -211,6 +224,7 @@ describe Google::Auth::UserRefreshCredentials do
needed = %w(client_id client_secret refresh_token) needed = %w(client_id client_secret refresh_token)
needed.each do |missing| needed.each do |missing|
FakeFS do FakeFS do
ENV['ProgramData'] = '/etc'
FileUtils.mkdir_p(File.dirname(@path)) FileUtils.mkdir_p(File.dirname(@path))
File.write(@path, cred_json_text(missing)) File.write(@path, cred_json_text(missing))
expect { @clz.from_system_default_path(@scope) } expect { @clz.from_system_default_path(@scope) }
@ -222,6 +236,7 @@ describe Google::Auth::UserRefreshCredentials do
it 'successfully loads the file when it is present' do it 'successfully loads the file when it is present' do
FakeFS do FakeFS do
ENV['ProgramData'] = '/etc'
FileUtils.mkdir_p(File.dirname(@path)) FileUtils.mkdir_p(File.dirname(@path))
File.write(@path, cred_json_text) File.write(@path, cred_json_text)
expect(@clz.from_system_default_path(@scope)).to_not be_nil expect(@clz.from_system_default_path(@scope)).to_not be_nil