Skip to content

Commit cd4e9e1

Browse files
committed
shared which helper (inspired by Warbler's PlatformHelper) - win support
1 parent f54152f commit cd4e9e1

File tree

6 files changed

+72
-54
lines changed

6 files changed

+72
-54
lines changed

rakelib/db.rake

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace :db do
44

55
desc "Creates the test database for MySQL"
66
task :mysql do
7+
fail "could not create test database: mysql executable not found" unless mysql = which('mysql')
78
load 'test/db/mysql_config.rb' # rescue nil
89
script = sql_script <<-SQL, 'mysql'
910
DROP DATABASE IF EXISTS `#{MYSQL_CONFIG[:database]}`;
@@ -19,12 +20,13 @@ SQL
1920
params['--password'] = password
2021
end
2122
puts "Creating MySQL (test) database: #{MYSQL_CONFIG[:database]}"
22-
sh "cat #{script.path} | mysql #{params.to_a.join(' ')}", :verbose => $VERBOSE # so password is not echoed
23+
sh "cat #{script.path} | #{mysql} #{params.to_a.join(' ')}", :verbose => $VERBOSE # so password is not echoed
2324
end
2425

2526
desc "Creates the test database for PostgreSQL"
2627
task :postgresql do
27-
fail unless PostgresHelper.have_postgres?
28+
fail 'could not create test database: psql executable not found' unless psql = which('psql')
29+
fail 'could not create test database: missing "postgres" role' unless PostgresHelper.postgres_role?
2830
load 'test/db/postgres_config.rb' # rescue nil
2931
script = sql_script <<-SQL, 'psql'
3032
DROP DATABASE IF EXISTS #{POSTGRES_CONFIG[:database]};
@@ -35,7 +37,7 @@ SQL
3537
params = { '-U' => ENV['PSQL_USER'] || 'postgres' }
3638
params['-q'] = nil unless $VERBOSE
3739
puts "Creating PostgreSQL (test) database: #{POSTGRES_CONFIG[:database]}"
38-
sh "cat #{script.path} | psql #{params.to_a.join(' ')}", :verbose => $VERBOSE
40+
sh "cat #{script.path} | #{psql} #{params.to_a.join(' ')}", :verbose => $VERBOSE
3941
end
4042
task :postgres => :postgresql
4143

test/db/mssql/rake_test.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def db_config
1111
def do_teardown
1212
drop_rake_test_database(:silence)
1313
end
14-
14+
1515
test 'rake db:create (and db:drop)' do
1616
begin
1717
Rake::Task["db:create"].invoke
@@ -35,13 +35,13 @@ def do_teardown
3535
drop_rake_test_database(:silence)
3636
Rake::Task["db:drop"].invoke
3737
end
38-
38+
3939
test 'rake db:test:purge' do
4040
# Rake::Task["db:create"].invoke
4141
create_rake_test_database do |connection|
4242
connection.create_table('users') { |t| t.string :name }
4343
end
44-
44+
4545
Rake::Task["db:test:purge"].invoke
4646

4747
ActiveRecord::Base.establish_connection db_config.merge :database => db_name
@@ -50,27 +50,27 @@ def do_teardown
5050
end
5151

5252
test 'rake db:structure:dump (and db:structure:load)' do
53-
omit('smoscript not available') unless self.class.find_executable?('smoscript')
53+
omit('smoscript not available') unless self.class.which('smoscript')
5454
# Rake::Task["db:create"].invoke
5555
create_rake_test_database do |connection|
5656
create_schema_migrations_table(connection)
5757
connection.create_table('users') { |t| t.string :name; t.timestamps }
5858
end
59-
59+
6060
structure_sql = File.join('db', structure_sql_filename)
6161
begin
6262
Dir.mkdir 'db' # db/structure.sql
6363
Rake::Task["db:structure:dump"].invoke
64-
64+
6565
assert File.exists?(structure_sql)
6666
# CREATE TABLE [dbo].[users]( ... )
6767
assert_match /CREATE TABLE .*?\[users\]/i, File.read(structure_sql)
68-
68+
6969
# db:structure:load
7070
drop_rake_test_database(:silence)
7171
create_rake_test_database
7272
Rake::Task["db:structure:load"].invoke
73-
73+
7474
ActiveRecord::Base.establish_connection db_config.merge :database => db_name
7575
assert ActiveRecord::Base.connection.table_exists?('users')
7676
ActiveRecord::Base.connection.disconnect!
@@ -79,12 +79,12 @@ def do_teardown
7979
Dir.rmdir 'db'
8080
end
8181
end
82-
82+
8383
setup { rm_r 'db' if File.exist?('db') }
84-
84+
8585
test 'rake db:charset' do
8686
create_rake_test_database
87-
# using the default character set, the character_set_name should be
87+
# using the default character set, the character_set_name should be
8888
# iso_1 (ISO 8859-1) for the char and varchar data types
8989
expect_rake_output /iso_1|UCS/i
9090
Rake::Task["db:charset"].invoke
@@ -96,7 +96,7 @@ def do_teardown
9696
expect_rake_output /SQL_.*/
9797
Rake::Task["db:collation"].invoke
9898
end
99-
99+
100100
# @override
101101
def create_rake_test_database(db_name = self.db_name)
102102
ActiveRecord::Base.establish_connection db_config
@@ -105,14 +105,14 @@ def create_rake_test_database(db_name = self.db_name)
105105
# connection.use_database('master')
106106
connection.create_database(db_name, db_config)
107107
end
108-
108+
109109
if block_given?
110110
ActiveRecord::Base.establish_connection db_config.merge :database => db_name
111111
yield ActiveRecord::Base.connection
112112
end
113113
ActiveRecord::Base.connection.disconnect!
114114
end
115-
115+
116116
# @override
117117
def drop_rake_test_database(silence = false)
118118
ActiveRecord::Base.establish_connection db_config
@@ -128,9 +128,9 @@ def drop_rake_test_database(silence = false)
128128
end
129129
ActiveRecord::Base.connection.disconnect!
130130
end
131-
131+
132132
private
133-
133+
134134
def databases
135135
if ActiveRecord::Base.connection.send(:sqlserver_2000?)
136136
select = "SELECT name FROM master..sysdatabases ORDER BY name"
@@ -139,5 +139,5 @@ def databases
139139
end
140140
ActiveRecord::Base.connection.select_rows(select).flatten
141141
end
142-
142+
143143
end

test/db/mysql/rake_test.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ def do_teardown
1212
drop_rake_test_database(:silence)
1313
end
1414

15+
MYSQL_EXE = which('mysql')
16+
1517
test 'rake db:create (and db:drop)' do
16-
# omit_unless find_executable?("mysql")
1718
Rake::Task["db:create"].invoke
1819
with_mysql do |mysql|
1920
mysql << "show databases where `Database` = '#{db_name}';"
2021
mysql.close_write
2122
output = mysql.read
2223
assert output =~ /#{db_name}/m, "db name: #{db_name.inspect} not matched in:\n#{output}"
23-
end if find_executable?("mysql")
24+
end if MYSQL_EXE
2425

2526
Rake::Task["db:drop"].invoke
2627
with_mysql do |mysql|
2728
mysql << "show databases where `Database` = '#{db_name}';"
2829
mysql.close_write
2930
output = mysql.read
3031
assert_nil output =~ /#{db_name}/m, "db name: #{db_name.inspect} matched in:\n#{output}"
31-
end if find_executable?("mysql")
32+
end if MYSQL_EXE
3233
end
3334

3435
test 'rake db:test:purge' do
@@ -116,7 +117,7 @@ def do_teardown
116117
private
117118

118119
def with_mysql(args = nil)
119-
exec = "mysql -u #{db_config[:username]} --password=#{db_config[:password]} #{args}"
120+
exec = "#{MYSQL_EXE} -u #{db_config[:username]} --password=#{db_config[:password]} #{args}"
120121
IO.popen(exec, "r+") { |mysql| yield(mysql) }
121122
end
122123

test/db/postgresql/rake_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ def do_teardown
1212
drop_rake_test_database(:silence)
1313
end
1414

15-
PSQL_EXECUTABLE = find_executable?("psql")
15+
PSQL_EXE = which('psql')
1616

1717
test 'rake db:create (and db:drop)' do
1818
Rake::Task["db:create"].invoke
19-
if PSQL_EXECUTABLE
19+
if PSQL_EXE
2020
output = psql("-d template1 -c '\\\l'")
2121
assert_match /#{db_name}/m, output
2222
end
2323

2424
Rake::Task["db:drop"].invoke
25-
if PSQL_EXECUTABLE
25+
if PSQL_EXE
2626
output = psql("-d template1 -c '\\\l'")
2727
assert_no_match /#{db_name}/m, output
2828
end
@@ -73,7 +73,7 @@ def do_teardown
7373
end
7474

7575
test 'rake db:structure:dump (and db:structure:load)' do
76-
omit('pg_dump not available') unless self.class.find_executable?('pg_dump')
76+
omit('pg_dump not available') unless self.class.which('pg_dump')
7777
# Rake::Task["db:create"].invoke
7878
create_rake_test_database do |connection|
7979
create_schema_migrations_table(connection)
@@ -137,7 +137,7 @@ def psql(args)
137137
if username = ENV['PSQL_USERNAME']
138138
args = "--username=#{username} #{args}"
139139
end
140-
`psql #{args}`
140+
`#{PSQL_EXE} #{args}`
141141
end
142142

143143
end

test/db/sqlite3/rake_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def do_teardown
3636
end
3737

3838
test 'rake db:structure:dump (and db:structure:load)' do
39-
omit('sqlite3 not available') unless self.class.find_executable?('sqlite3')
39+
omit('sqlite3 not available') unless self.class.which('sqlite3')
4040
create_rake_test_database do |connection|
4141
create_schema_migrations_table(connection)
4242
connection.create_table('users') { |t| t.string :name; t.timestamps }

test/shared_helper.rb

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,50 @@
1-
# Defining helper methods useful both in the definition of test tasks, as well as in the execution
2-
# of tests themselves. Do not require test/unit within this file, as that'll make the rake process
3-
# attempt to run tests itself. Test-specific helpers should go in test/jdbc_common.rb.
1+
# Helpers useful both in the definition of test tasks, as well as in tests.
2+
3+
require 'fileutils'
44

55
module Kernel
6-
def find_executable?(name)
7-
(ENV['PATH'] || '').split(File::PATH_SEPARATOR).
8-
detect { |p| File.executable?(File.join(p, name)) }
6+
7+
# Cross-platform way of finding an executable in the $PATH.
8+
# Thanks to @mislav
9+
#
10+
# which('ruby') #=> /usr/bin/ruby
11+
def which(cmd)
12+
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
13+
( ENV['PATH'] || '' ).split(File::PATH_SEPARATOR).each do |path|
14+
exts.each do |ext|
15+
exe = File.join(path, "#{cmd}#{ext}")
16+
return exe if File.executable? exe
17+
end
18+
end
19+
nil
920
end
21+
1022
end
1123

1224
module PostgresHelper
13-
def self.pg_cmdline_params
14-
params = ""
15-
params += "-h #{ENV['PGHOST']} " if ENV['PGHOST']
16-
params += "-p #{ENV['PGPORT']} " if ENV['PGPORT']
17-
params
18-
end
19-
20-
def self.have_postgres?(warn = nil)
21-
if find_executable?("psql")
22-
if `psql -c '\\l' -U postgres #{pg_cmdline_params}2>&1` && $?.exitstatus == 0
23-
true
24-
else
25-
if warn.nil?
26-
warn = "No \"postgres\" role? You might need to execute `createuser postgres -drs' first."
25+
class << self
26+
def postgres_role?(warn = nil)
27+
if psql = which('psql')
28+
if `#{psql} -c '\\l' -U postgres #{psql_params}2>&1` && $?.exitstatus == 0
29+
true
30+
else
31+
if warn.nil?
32+
warn = "No \"postgres\" role? You might need to execute `createuser postgres -drs' first."
33+
end
34+
send(:warn, warn) if warn # warn == false disables warnings
35+
false
2736
end
28-
send(:warn, warn) if warn # warn == false disables warnings
29-
false
3037
end
3138
end
39+
alias_method :have_postgres?, :postgres_role?
40+
41+
private
42+
43+
def psql_params
44+
params = ""
45+
params << "-h #{ENV['PGHOST']} " if ENV['PGHOST']
46+
params << "-p #{ENV['PGPORT']} " if ENV['PGPORT']
47+
params
48+
end
3249
end
3350
end
34-
35-
require 'fileutils'

0 commit comments

Comments
 (0)