<commit_before>require File.dirname(__FILE__) + "/test_helper"
require "fileutils"

describe "ProjectBuilder" do
  ProjectBuilder = Integrity::ProjectBuilder  unless defined?(ProjectBuilder)
  Build   = Integrity::Build                  unless defined?(Build)
  Git     = Integrity::SCM::Git               unless defined?(Git)

  before(:all) do
    Integrity.config[:export_directory] = "./test"
    @directory = Integrity.config[:export_directory] + "/foca-integrity-master"
    FileUtils.mkdir(@directory)
  end

  after(:all) do
    FileUtils.rm_rf(@directory)
  end

  before(:each) do
    setup_and_reset_database!
    @project = Integrity::Project.generate(:integrity, :command => "echo -n 'output!'")
    ignore_logs!
  end

  it "creates a new SCM with given project's uri, branch and export_directory" do
    Git.expects(:new).with(@project.uri, @project.branch, "./test/foca-integrity-master")
    ProjectBuilder.new(@project)
  end

  describe "When building" do
    it "creates a new build" do
      lambda do
        Git.any_instance.expects(:with_revision).with("commit").yields
        Git.any_instance.expects(:commit_identifier).with("commit").returns("commit identifier")
        Git.any_instance.expects(:commit_metadata).with("commit").returns(:meta => "data")

        build = ProjectBuilder.new(@project).build("commit")
        build.commit_identifier.should  == "commit identifier"
        build.commit_metadata.should    == {:meta => "data"}
        build.output.should == "output!"
        build.should be_successful
      end.should change(@project.builds, :count).by(1)
    end

    it "creates a new build even if something horrible happens" do
      lambda do
        lambda do
          Git.any_instance.expects(:with_revision).with("commit").raises
          Git.any_instance.expects(:commit_identifier).with("commit").returns("commit identifier")
          Git.any_instance.expects(:commit_metadata).with("commit").returns(:meta => "data")

          build = ProjectBuilder.new(@project).build("commit")
          build.commit_identifier.should  == "commit identifier"
          build.commit_metadata.should    == {:meta => "data"}
        end.should change(@project.builds, :count).by(1)
      end.should raise_error
    end

    it "sets the build status to failure when the build command exits with a non-zero status" do
      @project.update_attributes(:command => "exit 1")
      Git.any_instance.expects(:with_revision).with("HEAD").yields
      build = ProjectBuilder.new(@project).build("HEAD")
      build.should be_failed
    end

    it "sets the build status to failure when the build command exits with a zero status" do
      @project.update_attributes(:command => "exit 0")
      Git.any_instance.expects(:with_revision).with("HEAD").yields
      build = ProjectBuilder.new(@project).build("HEAD")
      build.should be_successful
    end

    it "runs the command in the export directory" do
      @project.update_attributes(:command => "cat foo.txt")
      File.open(@directory + "/foo.txt", "w") { |file| file << "bar!" }
      Git.any_instance.expects(:with_revision).with("HEAD").yields

      build = ProjectBuilder.new(@project).build("HEAD")
      build.output.should == "bar!"
    end
  end
end
<commit_msg>test that we capture both stdout and stderr<commit_after>require File.dirname(__FILE__) + "/test_helper"
require "fileutils"

describe "ProjectBuilder" do
  ProjectBuilder = Integrity::ProjectBuilder  unless defined?(ProjectBuilder)
  Build   = Integrity::Build                  unless defined?(Build)
  Git     = Integrity::SCM::Git               unless defined?(Git)

  before(:all) do
    Integrity.config[:export_directory] = "./test"
    @directory = Integrity.config[:export_directory] + "/foca-integrity-master"
    FileUtils.mkdir(@directory)
  end

  after(:all) do
    FileUtils.rm_rf(@directory)
  end

  before(:each) do
    setup_and_reset_database!
    @project = Integrity::Project.generate(:integrity, :command => "echo -n 'output!'")
    ignore_logs!
  end

  it "creates a new SCM with given project's uri, branch and export_directory" do
    Git.expects(:new).with(@project.uri, @project.branch, "./test/foca-integrity-master")
    ProjectBuilder.new(@project)
  end

  describe "When building" do
    it "creates a new build" do
      lambda do
        Git.any_instance.expects(:with_revision).with("commit").yields
        Git.any_instance.expects(:commit_identifier).with("commit").returns("commit identifier")
        Git.any_instance.expects(:commit_metadata).with("commit").returns(:meta => "data")

        build = ProjectBuilder.new(@project).build("commit")
        build.commit_identifier.should  == "commit identifier"
        build.commit_metadata.should    == {:meta => "data"}
        build.output.should == "output!"
        build.should be_successful
      end.should change(@project.builds, :count).by(1)
    end

    it "creates a new build even if something horrible happens" do
      lambda do
        lambda do
          Git.any_instance.expects(:with_revision).with("commit").raises
          Git.any_instance.expects(:commit_identifier).with("commit").returns("commit identifier")
          Git.any_instance.expects(:commit_metadata).with("commit").returns(:meta => "data")

          build = ProjectBuilder.new(@project).build("commit")
          build.commit_identifier.should  == "commit identifier"
          build.commit_metadata.should    == {:meta => "data"}
        end.should change(@project.builds, :count).by(1)
      end.should raise_error
    end

    it "sets the build status to failure when the build command exits with a non-zero status" do
      @project.update_attributes(:command => "exit 1")
      Git.any_instance.expects(:with_revision).with("HEAD").yields
      build = ProjectBuilder.new(@project).build("HEAD")
      build.should be_failed
    end

    it "sets the build status to failure when the build command exits with a zero status" do
      @project.update_attributes(:command => "exit 0")
      Git.any_instance.expects(:with_revision).with("HEAD").yields
      build = ProjectBuilder.new(@project).build("HEAD")
      build.should be_successful
    end

    it "runs the command in the export directory" do
      @project.update_attributes(:command => "cat foo.txt")
      File.open(@directory + "/foo.txt", "w") { |file| file << "bar!" }
      Git.any_instance.expects(:with_revision).with("HEAD").yields

      build = ProjectBuilder.new(@project).build("HEAD")
      build.output.should == "bar!"
    end

    it "captures both stdout and stderr" do
      @project.update_attributes(:command => "cat /no/such/file.txt")
      Git.any_instance.expects(:with_revision).with("HEAD").yields

      build = ProjectBuilder.new(@project).build("HEAD")
      build.output.should == "cat: /no/such/file.txt: No such file or directory\n"
    end
  end
end

{'fraction_non_alphanumeric': 0.13046002031635467, 'fraction_numerical': 0.0011609345523146133, 'mean_word_length': 3.4038338658146965, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 20, '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': '5804591', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1967, 'n_words': 443}