<commit_before>require 'spec_helper'

describe Build, 'matrix' do
  include Support::ActiveRecord

  before { Build.send :public, :matrix_config, :expand_matrix_config }
  after  { Build.send :protected, :matrix_config, :expand_matrix_config }

  describe :matrix_finished? do
    context "if at least one job has not finished" do
      it 'returns false' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes(:state => :finished)
        build.matrix[1].update_attributes(:state => :started)

        build.matrix_finished?.should_not be_true
      end
    end

    context "if all jobs have finished" do
      it 'returns true' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes!(:state => :finished)
        build.matrix[1].update_attributes!(:state => :finished)

        build.matrix_finished?.should_not be_nil
      end
    end
  end

  describe :matrix_result do
    context "if any job has the result 1" do
      it 'returns 1 ' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes!(:result => 1, :state => :finished)
        build.matrix[1].update_attributes!(:result => 0, :state => :finished)
        build.matrix_result.should == 1
      end
    end

    context "if all jobs have the result 0" do
      it 'returns 0' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes!(:result => 0, :state => :finished)
        build.matrix[1].update_attributes!(:result => 0, :state => :finished)
        build.matrix_result.should == 0
      end
    end

    context "if a failed job is allowed to fail" do
      it 'returns 0' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes!(:result => 0, :state => :finished)
        build.matrix[1].update_attributes!(:result => 1, :state => :finished, :allow_failure => true)
        build.matrix_result.should == 0
      end
    end

    context "if all jobs fail and one is allowed to fail" do
      it 'returns 1' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes!(:result => 1, :state => :finished)
        build.matrix[1].update_attributes!(:result => 1, :state => :finished, :allow_failure => true)
        build.matrix_result.should == 1
      end
    end
  end

  describe :matrix_duration do
    let(:build) do
      Build.new(:matrix => [
        Job::Test.new(:started_at => 60.seconds.ago, :finished_at => 40.seconds.ago),
        Job::Test.new(:started_at => 20.seconds.ago, :finished_at => 10.seconds.ago)
       ])
    end

    context "if the matrix is finished" do
      it 'returns the sum of the matrix job durations' do
        build.stubs(:matrix_finished?).returns(true)
        build.matrix_duration.should == 30
      end
    end

    context "if the matrix is not finished" do
      it 'returns nil' do
        build.stubs(:matrix_finished?).returns(false)
        build.matrix_duration.should be_nil
      end
    end
  end



  describe "for Ruby projects" do
    let(:no_matrix_config) {
      YAML.load <<-yml
      script: "rake ci"
    yml
    }

    let(:encrypted_and_unencrypted_config) {
    YAML.load <<-yml
      script: "rake ci"
      rvm:
        - 1.8.7
      gemfile:
        - gemfiles/rails-3.0.6
      env:
        - ["TO=ENCRYPT", "FOO=BAR"]
    yml
    }

    let(:single_test_config) {
      YAML.load <<-yml
      script: "rake ci"
      rvm:
        - 1.8.7
      gemfile:
        - gemfiles/rails-3.0.6
      env:
        - USE_GIT_REPOS=true
    yml
    }

    let(:multiple_tests_config) {
      YAML.load <<-yml
      script: "rake ci"
      rvm:
        - 1.8.7
        - 1.9.1
        - 1.9.2<commit_msg>Remove unneeded config from matrix specs<commit_after>require 'spec_helper'

describe Build, 'matrix' do
  include Support::ActiveRecord

  before { Build.send :public, :matrix_config, :expand_matrix_config }
  after  { Build.send :protected, :matrix_config, :expand_matrix_config }

  describe :matrix_finished? do
    context "if at least one job has not finished" do
      it 'returns false' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes(:state => :finished)
        build.matrix[1].update_attributes(:state => :started)

        build.matrix_finished?.should_not be_true
      end
    end

    context "if all jobs have finished" do
      it 'returns true' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes!(:state => :finished)
        build.matrix[1].update_attributes!(:state => :finished)

        build.matrix_finished?.should_not be_nil
      end
    end
  end

  describe :matrix_result do
    context "if any job has the result 1" do
      it 'returns 1 ' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes!(:result => 1, :state => :finished)
        build.matrix[1].update_attributes!(:result => 0, :state => :finished)
        build.matrix_result.should == 1
      end
    end

    context "if all jobs have the result 0" do
      it 'returns 0' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes!(:result => 0, :state => :finished)
        build.matrix[1].update_attributes!(:result => 0, :state => :finished)
        build.matrix_result.should == 0
      end
    end

    context "if a failed job is allowed to fail" do
      it 'returns 0' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes!(:result => 0, :state => :finished)
        build.matrix[1].update_attributes!(:result => 1, :state => :finished, :allow_failure => true)
        build.matrix_result.should == 0
      end
    end

    context "if all jobs fail and one is allowed to fail" do
      it 'returns 1' do
        build = Factory(:build, :config => { :rvm => ['1.8.7', '1.9.2'] })
        build.matrix[0].update_attributes!(:result => 1, :state => :finished)
        build.matrix[1].update_attributes!(:result => 1, :state => :finished, :allow_failure => true)
        build.matrix_result.should == 1
      end
    end
  end

  describe :matrix_duration do
    let(:build) do
      Build.new(:matrix => [
        Job::Test.new(:started_at => 60.seconds.ago, :finished_at => 40.seconds.ago),
        Job::Test.new(:started_at => 20.seconds.ago, :finished_at => 10.seconds.ago)
       ])
    end

    context "if the matrix is finished" do
      it 'returns the sum of the matrix job durations' do
        build.stubs(:matrix_finished?).returns(true)
        build.matrix_duration.should == 30
      end
    end

    context "if the matrix is not finished" do
      it 'returns nil' do
        build.stubs(:matrix_finished?).returns(false)
        build.matrix_duration.should be_nil
      end
    end
  end



  describe "for Ruby projects" do
    let(:no_matrix_config) {
      YAML.load <<-yml
      script: "rake ci"
    yml
    }

    let(:single_test_config) {
      YAML.load <<-yml
      script: "rake ci"
      rvm:
        - 1.8.7
      gemfile:
        - gemfiles/rails-3.0.6
      env:
        - USE_GIT_REPOS=true
    yml
    }

    let(:multiple_tests_config) {
      YAML.load <<-yml
      script: "rake ci"
      rvm:
        - 1.8.7
        - 1.9.1
        - 1.9.2
{'fraction_non_alphanumeric': 0.14175190138998164, 'fraction_numerical': 0.024652504589562026, 'mean_word_length': 2.3703049049933718, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 81, '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': '5649091', 'n_tokens_mistral': 2579, 'n_tokens_neox': 2437, 'n_words': 620}