<commit_before>require "octokit"

RSpec.describe Danger::LocalSetup do
  describe "#setup" do
    it "informs the user and runs the block" do
      github = double(:host => "github.com", :support_tokenless_auth= => nil, :fetch_details => nil)
      env = FakeEnv.new(FakeCiSource.new("danger/danger", "1337"), github)
      dangerfile = FakeDangerfile.new(env, false)
      ui = testing_ui
      subject = described_class.new(dangerfile, ui)

      subject.setup(verbose: false) { ui.puts "evaluated" }
      expect(ui.string).to include("Running your Dangerfile against this PR - https://github.com/danger/danger/pull/1337")
      expect(ui.string).to include("evaluated")
    end

    it "exits when no applicable ci source was identified" do
      dangerfile = FakeDangerfile.new(FakeEnv.new(nil, nil), false)
      ui = testing_ui
      subject = described_class.new(dangerfile, ui)

      expect do
        subject.setup(verbose: false) { ui.puts "evaluated" }
      end.to raise_error(SystemExit)
      expect(ui.string).to include("only works with GitHub")
      expect(ui.string).not_to include("evaluated")
    end

    it "turns on verbose if arguments wasn't passed" do
      github = double(:host => "", :support_tokenless_auth= => nil, :fetch_details => nil)
      env = FakeEnv.new(FakeCiSource.new("danger/danger", "123"), github)
      dangerfile = FakeDangerfile.new(env, false)
      ui = testing_ui
      subject = described_class.new(dangerfile, ui)

      subject.setup(verbose: false) {}
      expect(ui.string).to include("Turning on --verbose")
    end

    it "does not evaluate Dangerfile if local repo was not found on github" do
      github = double(:host => "", :support_tokenless_auth= => nil)
      allow(github).to receive(:fetch_details).and_raise(Octokit::NotFound.new)
      env = FakeEnv.new(FakeCiSource.new("danger/danger", "123"), github)
      dangerfile = FakeDangerfile.new(env, false)
      ui = testing_ui
      subject = described_class.new(dangerfile, ui)

      subject.setup(verbose: true) { ui.puts "evaluated" }
      expect(ui.string).to include("was not found on GitHub")
      expect(ui.string).not_to include("evaluated")
    end
  end

  class FakeDangerfile
    attr_reader :env<commit_msg>Extract FakeCiSource instantiation into rspec let<commit_after>require "octokit"

RSpec.describe Danger::LocalSetup do
  let(:ci_source) { FakeCiSource.new("danger/danger", "1337") }

  describe "#setup" do
    it "informs the user and runs the block" do
      github = double(:host => "github.com", :support_tokenless_auth= => nil, :fetch_details => nil)
      env = FakeEnv.new(ci_source, github)
      dangerfile = FakeDangerfile.new(env, false)
      ui = testing_ui
      subject = described_class.new(dangerfile, ui)

      subject.setup(verbose: false) { ui.puts "evaluated" }
      expect(ui.string).to include("Running your Dangerfile against this PR - https://github.com/danger/danger/pull/1337")
      expect(ui.string).to include("evaluated")
    end

    it "exits when no applicable ci source was identified" do
      dangerfile = FakeDangerfile.new(FakeEnv.new(nil, nil), false)
      ui = testing_ui
      subject = described_class.new(dangerfile, ui)

      expect do
        subject.setup(verbose: false) { ui.puts "evaluated" }
      end.to raise_error(SystemExit)
      expect(ui.string).to include("only works with GitHub")
      expect(ui.string).not_to include("evaluated")
    end

    it "turns on verbose if arguments wasn't passed" do
      github = double(:host => "", :support_tokenless_auth= => nil, :fetch_details => nil)
      env = FakeEnv.new(ci_source, github)
      dangerfile = FakeDangerfile.new(env, false)
      ui = testing_ui
      subject = described_class.new(dangerfile, ui)

      subject.setup(verbose: false) {}
      expect(ui.string).to include("Turning on --verbose")
    end

    it "does not evaluate Dangerfile if local repo wasn't found on github" do
      github = double(:host => "", :support_tokenless_auth= => nil)
      allow(github).to receive(:fetch_details).and_raise(Octokit::NotFound.new)
      env = FakeEnv.new(ci_source, github)
      dangerfile = FakeDangerfile.new(env, false)
      ui = testing_ui
      subject = described_class.new(dangerfile, ui)

      subject.setup(verbose: true) { ui.puts "evaluated" }
      expect(ui.string).to include("was not found on GitHub")
      expect(ui.string).not_to include("evaluated")
    end
  end

  class FakeDangerfile
    attr_reader :env
{'fraction_non_alphanumeric': 0.11911111111111111, 'fraction_numerical': 0.004888888888888889, 'mean_word_length': 3.4301181102362204, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '3312747', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1376, 'n_words': 343}