<commit_before>TEST_CHANGES_SINCE = Time.now - 600

# Look up tests for recently modified sources.
def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago)
  FileList[source_pattern].map do |path|
    if File.mtime(path) > touched_since
      test = "#{test_path}/#{File.basename(path, '.rb')}_test.rb"
      test if File.exists?(test)
    end
  end.compact
end


desc 'Test all units and functionals'
task :test do
  Rake::Task["test:units"].invoke       rescue got_error = true
  Rake::Task["test:functionals"].invoke rescue got_error = true
  
  if File.exist?("test/integration")
    Rake::Task["test:integration"].invoke rescue got_error = true
  end

  raise "Test failures" if got_error
end

namespace :test do
  desc 'Test recent changes'
  Rake::TestTask.new(:recent => "db:test:prepare") do |t|
    since = TEST_CHANGES_SINCE
    touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
      recent_tests('app/models/*.rb', 'test/unit', since) +
      recent_tests('app/controllers/*.rb', 'test/functional', since)

    t.libs << 'test'
    t.verbose = true
    t.test_files = touched.uniq
  end
  
  desc 'Test changes since last checkin (only Subversion)'
  Rake::TestTask.new(:uncommitted => "db:test:prepare") do |t|
    changed_since_checkin = `svn status`.map { |path| path.chomp[7 .. -1] }
    models = changed_since_checkin.delete_if { |path| not path =~ /app\/models\/.*\.rb/ }
    tests = models.map { |model| "test/unit/#{File.basename(model, '.rb')}_test.rb" }

    t.libs << 'test'
    t.verbose = true
    t.test_files = tests.uniq
  end

  desc "Run the unit tests in test/unit"
  Rake::TestTask.new(:units => "db:test:prepare") do |t|<commit_msg>We need to silence_stderr on the call to svn in order not to get errors when svn is not available or the app is not checked in<commit_after>TEST_CHANGES_SINCE = Time.now - 600

# Look up tests for recently modified sources.
def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago)
  FileList[source_pattern].map do |path|
    if File.mtime(path) > touched_since
      test = "#{test_path}/#{File.basename(path, '.rb')}_test.rb"
      test if File.exists?(test)
    end
  end.compact
end


# Recreated here from ActiveSupport because :uncommitted needs it before Rails is available
module Kernel
  def silence_stderr
    old_stderr = STDERR.dup
    STDERR.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
    STDERR.sync = true
    yield
  ensure
    STDERR.reopen(old_stderr)
  end
end

desc 'Test all units and functionals'
task :test do
  Rake::Task["test:units"].invoke       rescue got_error = true
  Rake::Task["test:functionals"].invoke rescue got_error = true
  
  if File.exist?("test/integration")
    Rake::Task["test:integration"].invoke rescue got_error = true
  end

  raise "Test failures" if got_error
end

namespace :test do
  desc 'Test recent changes'
  Rake::TestTask.new(:recent => "db:test:prepare") do |t|
    since = TEST_CHANGES_SINCE
    touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
      recent_tests('app/models/*.rb', 'test/unit', since) +
      recent_tests('app/controllers/*.rb', 'test/functional', since)

    t.libs << 'test'
    t.verbose = true
    t.test_files = touched.uniq
  end
  
  desc 'Test changes since last checkin (only Subversion)'
  Rake::TestTask.new(:uncommitted => "db:test:prepare") do |t|
    changed_since_checkin = silence_stderr { `svn status` }.map { |path| path.chomp[7 .. -1] }
    models = changed_since_checkin.select { |path| path =~ /app\/models\/.*\.rb/ }
    tests = models.map { |model| "test/unit/#{File.basename(model, '.rb')}_test.rb" }

    t.libs << 'test'
    t.verbose = true
    t.test_files = tests.uniq
  end

  desc "Run the unit tests in test/unit"
  Rake::TestTask.new(:units => "db:test:prepare") do |t|
{'fraction_non_alphanumeric': 0.14134366925064598, 'fraction_numerical': 0.0036175710594315244, 'mean_word_length': 3.6470588235294117, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6516773', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1327, 'n_words': 331}