Rakefile 7.4 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1
require 'rake/testtask'
2
require 'rubygems/package_task'
3 4

require File.expand_path(File.dirname(__FILE__)) + "/test/config"
5
require File.expand_path(File.dirname(__FILE__)) + "/test/support/config"
6

7 8 9 10 11 12 13 14 15 16 17 18 19
def run_without_aborting(*tasks)
  errors = []

  tasks.each do |task|
    begin
      Rake::Task[task].invoke
    rescue Exception
      errors << task
    end
  end

  abort "Errors running #{errors.join(', ')}" if errors.any?
end
D
Initial  
David Heinemeier Hansson 已提交
20

B
Brian Lopez 已提交
21
desc 'Run mysql, mysql2, sqlite, and postgresql tests by default'
J
Jeremy Kemper 已提交
22
task :default => :test
D
Initial  
David Heinemeier Hansson 已提交
23

B
Brian Lopez 已提交
24
desc 'Run mysql, mysql2, sqlite, and postgresql tests'
25 26 27
task :test do
  tasks = defined?(JRUBY_VERSION) ?
    %w(test_jdbcmysql test_jdbcsqlite3 test_jdbcpostgresql) :
B
Brian Lopez 已提交
28
    %w(test_mysql test_mysql2 test_sqlite3 test_postgresql)
29 30 31
  run_without_aborting(*tasks)
end

32 33 34 35
namespace :test do
  task :isolated do
    tasks = defined?(JRUBY_VERSION) ?
      %w(isolated_test_jdbcmysql isolated_test_jdbcsqlite3 isolated_test_jdbcpostgresql) :
B
Brian Lopez 已提交
36
      %w(isolated_test_mysql isolated_test_mysql2 isolated_test_sqlite3 isolated_test_postgresql)
37 38
    run_without_aborting(*tasks)
  end
39
end
D
Initial  
David Heinemeier Hansson 已提交
40

41
desc 'Build MySQL and PostgreSQL test databases'
42
namespace :db do
43 44
  task :create => ['mysql:db:build', 'postgresql:db:build']
  task :drop => ['mysql:db:drop', 'postgresql:db:drop']
45 46
end

47
%w( mysql mysql2 postgresql sqlite3 sqlite3_mem firebird db2 oracle sybase openbase frontbase jdbcmysql jdbcpostgresql jdbcsqlite3 jdbcderby jdbch2 jdbchsqldb ).each do |adapter|
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  namespace :test do
    namespace :db do
      Rake::TestTask.new(adapter) { |t|
        adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z0-9]+/]
        t.libs << 'test'
        t.test_files = (Dir.glob( "test/cases/**/*_test.rb" ).reject {
          |x| x =~ /\/adapters\//
        } + Dir.glob("test/cases/adapters/#{adapter_short}/**/*_test.rb")).sort

        t.warning = true
        t.verbose = true
      }

      namespace :isolated do
        task adapter do
          adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z0-9]+/]
          puts [adapter, adapter_short].inspect
          (Dir["test/cases/**/*_test.rb"].reject {
            |x| x =~ /\/adapters\//
          } + Dir["test/cases/adapters/#{adapter_short}/**/*_test.rb"]).all? do |file|
            sh(Gem.ruby, '-w' ,"-Itest", file)
          end or raise "Failures"
        end
      end
    end
J
Jeremy Kemper 已提交
73 74
  end

75 76
  namespace adapter do
    task :test => "test_#{adapter}"
J
Jeremy Kemper 已提交
77
    task :isolated_test => "isolated_test_#{adapter}"
78 79

    # Set the connection environment for the adapter
80
    task(:env) { ENV['ARCONN'] = adapter }
81
  end
82 83

  # Make sure the adapter test evaluates the env setting task
84 85
  task "test_#{adapter}" => ["#{adapter}:env", "test:db:#{adapter}"]
  task "isolated_test_#{adapter}" => ["#{adapter}:env", "test:db:isolated:#{adapter}"]
86
end
87

88 89 90 91 92 93 94 95 96
rule '.sqlite3' do |t|
  sh %Q{sqlite3 "#{t.name}" "create table a (a integer); drop table a;"}
end

task :test_sqlite3 => [
  'test/fixtures/fixture_database.sqlite3',
  'test/fixtures/fixture_database_2.sqlite3'
]

97
namespace :mysql do
98 99 100 101 102 103 104
  namespace :db do
    desc 'Build the MySQL test databases'
    task :build do
      config = ARTest.config['connections']['mysql']
      %x( mysql --user=#{config['arunit']['username']} -e "create DATABASE #{config['arunit']['database']} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci ")
      %x( mysql --user=#{config['arunit2']['username']} -e "create DATABASE #{config['arunit2']['database']} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci ")
    end
105

106 107 108 109 110 111
    desc 'Drop the MySQL test databases'
    task :drop do
      config = ARTest.config['connections']['mysql']
      %x( mysqladmin --user=#{config['arunit']['username']} -f drop #{config['arunit']['database']} )
      %x( mysqladmin --user=#{config['arunit2']['username']} -f drop #{config['arunit2']['database']} )
    end
112

113 114 115
    desc 'Rebuild the MySQL test databases'
    task :rebuild => [:drop, :build]
  end
116 117
end

118 119 120
task :build_mysql_databases => 'mysql:db:build'
task :drop_mysql_databases => 'mysql:db:drop'
task :rebuild_mysql_databases => 'mysql:db:rebuild'
121 122


123
namespace :postgresql do
124
    # notify about preparing hstore
125 126 127 128 129 130 131 132 133 134 135
  namespace :db do
    desc 'Build the PostgreSQL test databases'
    task :build do
      config = ARTest.config['connections']['postgresql']
      %x( createdb -E UTF8 -T template0 #{config['arunit']['database']} )
      %x( createdb -E UTF8 -T template0 #{config['arunit2']['database']} )

      # prepare hstore
      if %x( createdb --version ).strip.gsub(/(.*)(\d\.\d\.\d)$/, "\\2") < "9.1.0"
        puts "Please prepare hstore data type. See http://www.postgresql.org/docs/9.0/static/hstore.html"
      end
136
    end
137

138 139 140 141 142 143
    desc 'Drop the PostgreSQL test databases'
    task :drop do
      config = ARTest.config['connections']['postgresql']
      %x( dropdb #{config['arunit']['database']} )
      %x( dropdb #{config['arunit2']['database']} )
    end
144

145 146 147
    desc 'Rebuild the PostgreSQL test databases'
    task :rebuild => [:drop, :build]
  end
148 149
end

150 151 152
task :build_postgresql_databases => 'postgresql:db:build'
task :drop_postgresql_databases => 'postgresql:db:drop'
task :rebuild_postgresql_databases => 'postgresql:db:rebuild'
153

154

155
namespace :frontbase do
156 157 158
  namespace :db do
    desc 'Build the FrontBase test databases'
    task :build => :rebuild_frontbase_databases
159

160 161 162 163 164 165 166
    desc 'Rebuild the FrontBase test databases'
    task :rebuild do
      build_frontbase_database = Proc.new do |db_name, sql_definition_file|
        %(
          STOP DATABASE #{db_name};
          DELETE DATABASE #{db_name};
          CREATE DATABASE #{db_name};
167

168 169
          CONNECT TO #{db_name} AS SESSION_NAME USER _SYSTEM;
          SET COMMIT FALSE;
170

171 172 173
          CREATE USER RAILS;
          CREATE SCHEMA RAILS AUTHORIZATION RAILS;
          COMMIT;
174

175 176
          SET SESSION AUTHORIZATION RAILS;
          SCRIPT '#{sql_definition_file}';
177

178
          COMMIT;
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
          DISCONNECT ALL;
        )
      end
      config = ARTest.config['connections']['frontbase']
      create_activerecord_unittest  = build_frontbase_database[config['arunit']['database'],  File.join(SCHEMA_ROOT, 'frontbase.sql')]
      create_activerecord_unittest2 = build_frontbase_database[config['arunit2']['database'], File.join(SCHEMA_ROOT, 'frontbase2.sql')]
      execute_frontbase_sql = Proc.new do |sql|
        system(<<-SHELL)
        /Library/FrontBase/bin/sql92 <<-SQL
        #{sql}
        SQL
        SHELL
      end
      execute_frontbase_sql[create_activerecord_unittest]
      execute_frontbase_sql[create_activerecord_unittest2]
195
    end
196 197 198
  end
end

199 200
task :build_frontbase_databases => 'frontbase:db:build'
task :rebuild_frontbase_databases => 'frontbase:db:rebuild'
201

202
spec = eval(File.read('activerecord.gemspec'))
203

204
Gem::PackageTask.new(spec) do |p|
D
Initial  
David Heinemeier Hansson 已提交
205 206 207
  p.gem_spec = spec
end

208 209 210
task :lines do
  lines, codelines, total_lines, total_codelines = 0, 0, 0, 0

211
  FileList["lib/active_record/**/*.rb"].each do |file_name|
212
    next if file_name =~ /vendor/
213 214 215 216 217 218 219
    File.open(file_name, 'r') do |f|
      while line = f.gets
        lines += 1
        next if line =~ /^\s*$/
        next if line =~ /^\s*#/
        codelines += 1
      end
220 221
    end
    puts "L: #{sprintf("%4d", lines)}, LOC #{sprintf("%4d", codelines)} | #{file_name}"
222

223 224
    total_lines     += lines
    total_codelines += codelines
225

226 227 228 229 230 231
    lines, codelines = 0, 0
  end

  puts "Total: Lines #{total_lines}, LOC #{total_codelines}"
end

D
Initial  
David Heinemeier Hansson 已提交
232

233
# Publishing ------------------------------------------------------
D
Initial  
David Heinemeier Hansson 已提交
234

235
desc "Release to rubygems"
236 237 238 239 240
task :release => :package do
  require 'rake/gemcutter'
  Rake::Gemcutter::Tasks.new(spec).define
  Rake::Task['gem:push'].invoke
end