<commit_before># frozen_string_literal: true

require File.expand_path('../rails_test_helper', File.dirname(__FILE__))

class RailsFullStackTest < Minitest::Test
  include Capybara::DSL
  include Capybara::Minitest::Assertions

  def setup
    super
    rails_setup
    # preload first coverage hit
    Coverband::Collectors::Coverage.instance.report_coverage(true)
    require 'rainbow'
    Rainbow('this text is red').red
  end

  def teardown
    super
    Capybara.reset_sessions!
    Capybara.use_default_driver
  end

  # We have to combine everything in one test
  # because we can only initialize rails once per test
  # run. Possibly fork test runs to avoid this problem in future?
  test 'this is how we do it' do
    visit '/dummy/show'
    Coverband.report_coverage(true)
    assert_content('I am no dummy')
    visit '/coverage'
    within page.find('a', text: /dummy_controller.rb/).find(:xpath, '../..') do
      assert_selector('td', text: '100.0 %')
    end

    #Test gems are reporting coverage
    assert_content('Gems')
    assert page.html.match('rainbow/wrapper.rb')

    #Test eager load data stored separately
    dummy_controller = "./test/rails#{Rails::VERSION::MAJOR}_dummy/app/controllers/dummy_controller.rb"
    store.type = :eager_loading
    eager_expected = [1, 1, 0, nil, nil]
    results = store.coverage[dummy_controller]['data']
    assert_equal(eager_expected, results)

    store.type = nil
    runtime_expected = [0, 0, 1, nil, nil]
    results = store.coverage[dummy_controller]['data']
  end

  ###
  # Please keep this test starting on line 22
  # as we run it in single test mode via the benchmarks.
  # Add new tests below this test
  ###
  test 'memory usage' do
    return unless ENV['COVERBAND_MEMORY_TEST']
    # we don't want this to run during our standard test suite
    # as the below profiler changes the runtime
    # and shold only be included for isolated processes
    begin
      require 'memory_profiler'

      # warmup
      3.times do
        visit '/dummy/show'
        assert_content('I am no dummy')
        Coverband.report_coverage(true)
      end

      previous_out = $stdout
      capture = StringIO.new
      $stdout = capture

      MemoryProfiler.report do
        15.times do
          visit '/dummy/show'
          assert_content('I am no dummy')
          Coverband.report_coverage(true)
          # this is expected to retain memory across requests
          # clear it to remove the false positive from test
          Coverband::Collectors::Coverage.instance.send(:add_previous_results, nil)
        end
      end.pretty_print
      data = $stdout.string
      $stdout = previous_out
      if data.match(/retained objects by gem(.*)retained objects by file/m)[0]&.match(/coverband/)
        raise 'leaking memory!!!'
      end
    ensure
      $stdout = previous_out
    end
  end
end<commit_msg>Do not run memory test at all to avoid setup running twice<commit_after># frozen_string_literal: true

require File.expand_path('../rails_test_helper', File.dirname(__FILE__))

class RailsFullStackTest < Minitest::Test
  include Capybara::DSL
  include Capybara::Minitest::Assertions

  def setup
    super
    rails_setup
    # preload first coverage hit
    Coverband::Collectors::Coverage.instance.report_coverage(true)
    require 'rainbow'
    Rainbow('this text is red').red
  end

  def teardown
    super
    Capybara.reset_sessions!
    Capybara.use_default_driver
  end

  # We have to combine everything in one test
  # because we can only initialize rails once per test
  # run. Possibly fork test runs to avoid this problem in future?
  test 'this is how we do it' do
    visit '/dummy/show'
    Coverband.report_coverage(true)
    assert_content('I am no dummy')
    visit '/coverage'
    within page.find('a', text: /dummy_controller.rb/).find(:xpath, '../..') do
      assert_selector('td', text: '100.0 %')
    end

    #Test gems are reporting coverage
    assert_content('Gems')
    assert page.html.match('rainbow/wrapper.rb')

    #Test eager load data stored separately
    dummy_controller = "./test/rails#{Rails::VERSION::MAJOR}_dummy/app/controllers/dummy_controller.rb"
    store.type = :eager_loading
    eager_expected = [1, 1, 0, nil, nil]
    results = store.coverage[dummy_controller]['data']
    assert_equal(eager_expected, results)

    store.type = nil
    runtime_expected = [0, 0, 1, nil, nil]
    results = store.coverage[dummy_controller]['data']
  end

  ###
  # Please keep this test starting on line 22
  # as we run it in single test mode via the benchmarks.
  # Add new tests below this test
  ###
  if ENV['COVERBAND_MEMORY_TEST']
    test 'memory usage' do
      return unless ENV['COVERBAND_MEMORY_TEST']
      # we don't want this to run during our standard test suite
      # as the below profiler changes the runtime
      # and shold only be included for isolated processes
      begin
        require 'memory_profiler'

        # warmup
        3.times do
          visit '/dummy/show'
          assert_content('I am no dummy')
          Coverband.report_coverage(true)
        end

        previous_out = $stdout
        capture = StringIO.new
        $stdout = capture

        MemoryProfiler.report do
          15.times do
            visit '/dummy/show'
            assert_content('I am no dummy')
            Coverband.report_coverage(true)
            # this is expected to retain memory across requests
            # clear it to remove the false positive from test
            Coverband::Collectors::Coverage.instance.send(:add_previous_results, nil)
          end
        end.pretty_print
        data = $stdout.string
        $stdout = previous_out
        if data.match(/retained objects by gem(.*)retained objects by file/m)[0]&.match(/coverband/)
          raise 'leaking memory!!!'
        end
      ensure
        $stdout = previous_out
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08972405620450313, 'fraction_numerical': 0.005417301506686982, 'mean_word_length': 2.789608723540731, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3618888', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1720, 'n_words': 533}