<commit_before>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

begin; require File.expand_path(File.dirname(__FILE__) + "/../parallelized_specs/lib/parallelized_specs.rb"); rescue LoadError; end

if ENV['COVERAGE'] == "1"
  puts "Code Coverage enabled"
  require 'simplecov'
  require 'simplecov-rcov'
  SimpleCov.start do
    class SimpleCov::Formatter::MergedFormatter
      def format(result)
        SimpleCov::Formatter::HTMLFormatter.new.format(result)
        SimpleCov::Formatter::RcovFormatter.new.format(result)
      end
    end
    SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
    add_filter '/spec/'
    add_filter '/config/'
    add_filter 'spec_canvas'

    add_group 'Controllers', 'app/controllers'
    add_group 'Models', 'app/models'
    add_group 'App', '/app/'
    add_group 'Helpers', 'app/helpers'
    add_group 'Libraries', '/lib/'
    add_group 'Plugins', 'vendor/plugins'
    add_group "Long files" do |src_file|
      src_file.lines.count > 500
    end
    SimpleCov.at_exit do
      SimpleCov.result.format!
    end
  end
else
  puts "Code coverage not enabled"
end


ENV["RAILS_ENV"] = 'test'

require File.expand_path('../../config/environment', __FILE__) unless defined?(Rails)
if CANVAS_RAILS2
  require 'spec'
  # require 'spec/autorun'
  require 'spec/rails'
else
  require 'rspec/rails'
end
require 'webrat'
require 'mocha/api'
require File.expand_path(File.dirname(__FILE__) + '/mocha_rspec_adapter')
require File.expand_path(File.dirname(__FILE__) + '/mocha_extensions')

Dir.glob("#{File.dirname(__FILE__).gsub(/\\/, "/")}/factories/*.rb").each { |file| require file }

# rspec aliases :describe to :context in a way that it's pretty much defined
# globally on every object. :context is already heavily used in our application,
# so we remove rspec's definition. This does not prevent 'context' from being
# used within a 'describe' block.

if defined?(Spec::DSL::Main)
  module Spec::DSL::Main
    remove_method :context if respond_to? :context
  end
end

def truncate_table(model)
  case model.connection.adapter_name
    when "SQLite"
      model.delete_all
      begin
        model.connection.execute("delete from sqlite_sequence where name='#{model.connection.quote_table_name(model.table_name)}';")
        model.connection.execute("insert into sqlite_sequence (name, seq) values ('#{model.connection.quote_table_name(model.table_name)}', #{rand(100)});")
      rescue
      end
    when "PostgreSQL"
      begin
        old_proc = model.connection.raw_connection.set_notice_processor {}
        model.connection.execute("TRUNCATE TABLE #{model.connection.quote_table_name(model.table_name)} CASCADE")
      ensure
        model.connection.raw_connection.set_notice_processor(&old_proc)
      end
    else
      model.connection.execute("SET FOREIGN_KEY_CHECKS=0")
      model.connection.execute("TRUNCATE TABLE #{model.connection.quote_table_name(model.table_name)}")
      model.connection.execute("SET FOREIGN_KEY_CHECKS=1")
  end
end

def truncate_all_tables
  models_by_connection = ActiveRecord::Base.all_models.group_by { |m| m.connection }
  models_by_connection.each do |connection, models|
    if connection.adapter_name == "PostgreSQL"
      connection.execute("TRUNCATE TABLE #{models.map(&:table_name).map { |t| connection.quote_table_name(t) }.join(',')}")
    else
      models.each { |model| truncate_table(model) }
    end
  end
end

def truncate_all_cassandra_tables
  Canvas::Cassandra::Database.config_names.each do |cass_config|
    db = Canvas::Cassandra::Database.from_config(cass_config)
    db.tables.each do |table|
      db.execute("TRUNCATE #{table}")<commit_msg>fix truncate_all_tables for rails 3<commit_after>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

begin; require File.expand_path(File.dirname(__FILE__) + "/../parallelized_specs/lib/parallelized_specs.rb"); rescue LoadError; end

if ENV['COVERAGE'] == "1"
  puts "Code Coverage enabled"
  require 'simplecov'
  require 'simplecov-rcov'
  SimpleCov.start do
    class SimpleCov::Formatter::MergedFormatter
      def format(result)
        SimpleCov::Formatter::HTMLFormatter.new.format(result)
        SimpleCov::Formatter::RcovFormatter.new.format(result)
      end
    end
    SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
    add_filter '/spec/'
    add_filter '/config/'
    add_filter 'spec_canvas'

    add_group 'Controllers', 'app/controllers'
    add_group 'Models', 'app/models'
    add_group 'App', '/app/'
    add_group 'Helpers', 'app/helpers'
    add_group 'Libraries', '/lib/'
    add_group 'Plugins', 'vendor/plugins'
    add_group "Long files" do |src_file|
      src_file.lines.count > 500
    end
    SimpleCov.at_exit do
      SimpleCov.result.format!
    end
  end
else
  puts "Code coverage not enabled"
end


ENV["RAILS_ENV"] = 'test'

require File.expand_path('../../config/environment', __FILE__) unless defined?(Rails)
if CANVAS_RAILS2
  require 'spec'
  # require 'spec/autorun'
  require 'spec/rails'
else
  require 'rspec/rails'
end
require 'webrat'
require 'mocha/api'
require File.expand_path(File.dirname(__FILE__) + '/mocha_rspec_adapter')
require File.expand_path(File.dirname(__FILE__) + '/mocha_extensions')

Dir.glob("#{File.dirname(__FILE__).gsub(/\\/, "/")}/factories/*.rb").each { |file| require file }

# rspec aliases :describe to :context in a way that it's pretty much defined
# globally on every object. :context is already heavily used in our application,
# so we remove rspec's definition. This does not prevent 'context' from being
# used within a 'describe' block.

if defined?(Spec::DSL::Main)
  module Spec::DSL::Main
    remove_method :context if respond_to? :context
  end
end

def truncate_table(model)
  case model.connection.adapter_name
    when "SQLite"
      model.delete_all
      begin
        model.connection.execute("delete from sqlite_sequence where name='#{model.connection.quote_table_name(model.table_name)}';")
        model.connection.execute("insert into sqlite_sequence (name, seq) values ('#{model.connection.quote_table_name(model.table_name)}', #{rand(100)});")
      rescue
      end
    when "PostgreSQL"
      begin
        old_proc = model.connection.raw_connection.set_notice_processor {}
        model.connection.execute("TRUNCATE TABLE #{model.connection.quote_table_name(model.table_name)} CASCADE")
      ensure
        model.connection.raw_connection.set_notice_processor(&old_proc)
      end
    else
      model.connection.execute("SET FOREIGN_KEY_CHECKS=0")
      model.connection.execute("TRUNCATE TABLE #{model.connection.quote_table_name(model.table_name)}")
      model.connection.execute("SET FOREIGN_KEY_CHECKS=1")
  end
end

def truncate_all_tables
  models_by_connection = ActiveRecord::Base.all_models.group_by { |m| m.connection }
  models_by_connection.each do |connection, models|
    if connection.adapter_name == "PostgreSQL"
      table_names = connection.tables & models.map(&:table_name)
      connection.execute("TRUNCATE TABLE #{table_names.map { |t| connection.quote_table_name(t) }.join(',')}")
    else
      models.each { |model| truncate_table(model) }
    end
  end
end

def truncate_all_cassandra_tables
  Canvas::Cassandra::Database.config_names.each do |cass_config|
    db = Canvas::Cassandra::Database.from_config(cass_config)
    db.tables.each do |table|
      db.execute("TRUNCATE #{table}")
{'fraction_non_alphanumeric': 0.11189703153988868, 'fraction_numerical': 0.0035946196660482377, 'mean_word_length': 4.186410102224895, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3350996', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2665, 'n_words': 702}