<commit_before>require 'spec_helper'

describe Vagrant::HostGateway::Middleware do
  let (:env)           { Vagrant::Action::Environment.new }
  let (:vm)            { double("Vagrant::VM").as_null_object }
  let (:app)           { double("Object").as_null_object }
  let (:ui)            { double("Vagrant::UI::Interface").as_null_object }
  let (:guest)         { double("Vagrant::HostGateway::Guest").as_null_object }
  let (:host)          { double("Vagrant::HostGateway::Host").as_null_object }
  let (:config)        { double("Object").as_null_object }
  let (:debian)        { Vagrant::Hosts::Linux.new(ui) }
  let (:debian_g)      { Vagrant::Guest::Debian.new(ui) }
  let (:windows)       { Vagrant::Hosts::Windows.new(ui) }
  let (:windows_g)     { Vagrant::Guest::Base.new(ui) }

  before :each do
    env[:vm] = vm
    env[:ui] = ui

    vm.stub(:config).and_return config

    app.stub(:call)

  end

  subject {Vagrant::HostGateway::Middleware.new(app, env) }

  describe '#call' do
    shared_examples "chained hostgateway middleware" do
      it "calls the next middleware" do
        app.should_receive(:call).with(env)

        subject.call(env)
      end
    end
    context "for supported host and guest system" do
      before :each do
        env[:host] = debian
        env[:vm].stub(:guest).and_return(debian_g)
        Vagrant::HostGateway::Enhance.new(app, env).call(env)
      end
      context "with a gateway set" do
        before :each do
          config.stub_chain(:host, :gateway).and_return('192.168.0.1')
        end
        context "and no snat on network interface" do
          before :each do
            config.stub_chain(:vm, :networks).and_return(
                                                         :hostonly => [
                                                           '10.0.0.2', { :netmask => '255.255.255.0' }
                                                           ])
          end
          it_behaves_like "chained hostgateway middleware"

          it 'should add the some methods to the guest' do
            subject.call(env)
            debian_g.methods.grep(:set_gateway).should_not be_empty
          end
          it 'should add the some methods to the host' do
            subject.call(env)
            debian.methods.grep(:setup_nat).should_not be_empty
            debian.methods.grep(:enable_forwarding).should_not be_empty
          end

          it 'should display the gateway' do
            ui.should_receive(:info).with("Setting gateway to 192.168.0.1")
            subject.call(env)
          end
          it 'should set the gateway' do
            debian_g.should_receive(:set_gateway).with('192.168.0.1')
            subject.call(env)
          end

          it 'should not try to set snat' do
            debian.should_not_receive(:enable_forwarding)
            debian.should_not_receive(:setup_nat)

            subject.call(env)
          end

        end

        context "with snat on network interface" do
          before :each do
            config.stub_chain(:vm, :networks).and_return(
                                                         :hostonly => [
                                                           '10.0.0.2', {
                                                             :netmask => '255.255.255.0',
                                                             :nat     => 'eth0'
                                                           },
                                                         ])
          end

          it 'should set snat' do
            debian.should_receive(:enable_forwarding)
            debian.should_receive(:setup_nat).with('eth0', '10.0.0.2/24')

            subject.call(env)
          end
        end
      end

      context 'with no gateway set' do
        before :each do
          config.stub_chain(:host, :gateway).and_return(nil)
        end
        it 'should not try to set the gateway' do
          debian.should_not_receive(:set_gateway)
        end
      end
    end

  end
end<commit_msg>Notify examples of the major code change.<commit_after>require 'spec_helper'

describe Vagrant::HostGateway::Middleware do
  let (:env)           { Vagrant::Action::Environment.new }
  let (:vm)            { double("Vagrant::VM").as_null_object }
  let (:app)           { double("Object").as_null_object }
  let (:ui)            { double("Vagrant::UI::Interface").as_null_object }
  let (:guest)         { double("Vagrant::HostGateway::Guest").as_null_object }
  let (:host)          { double("Vagrant::HostGateway::Host").as_null_object }
  let (:config)        { double("Object").as_null_object }
  let (:debian)        { Vagrant::Hosts::Linux.new(ui) }
  let (:debian_g)      { Vagrant::Guest::Debian.new(ui) }
  let (:windows)       { Vagrant::Hosts::Windows.new(ui) }
  let (:windows_g)     { Vagrant::Guest::Base.new(ui) }

  before :each do
    env[:vm] = vm
    env[:ui] = ui

    vm.stub(:config).and_return config

    app.stub(:call)

  end

  subject {Vagrant::HostGateway::Middleware.new(app, env) }

  describe '#call' do
    shared_examples "chained hostgateway middleware" do
      it "calls the next middleware" do
        app.should_receive(:call).with(env)

        subject.call(env)
      end
    end
    context "for supported host and guest system" do
      before :each do
        env[:host] = debian
        env[:vm].stub(:guest).and_return(debian_g)
        Vagrant::HostGateway::Enhance.new(app, env).call(env)
      end
      context "with a gateway set" do
        before :each do
          config.stub_chain(:host, :gateway).and_return('192.168.0.1')
        end
        context "and no snat on network interface" do
          before :each do
            config.stub_chain(:vm, :networks).and_return(
                                                         :hostonly => [
                                                           '10.0.0.2', { :netmask => '255.255.255.0' }
                                                           ])
          end
          it_behaves_like "chained hostgateway middleware"

          it 'should add the some methods to the guest' do
            subject.call(env)
            debian_g.methods.grep(:set_gateway).should_not be_empty
          end
          it 'should add the some methods to the host' do
            subject.call(env)
            debian.methods.grep(:setup_nat).should_not be_empty
            debian.methods.grep(:enable_forwarding).should_not be_empty
          end

          it 'should display the gateway' do
            ui.should_receive(:info).with("Setting gateway to 192.168.0.1")
            subject.call(env)
          end
          it 'should set the gateway' do
            debian_g.should_receive(:set_gateway).with('192.168.0.1')
            subject.call(env)
          end

          it 'should not try to set snat' do
            debian.should_not_receive(:enable_forwarding)
            debian.should_not_receive(:setup_nat)

            subject.call(env)
          end

        end

        context "with snat on network interface" do

          before :each do
            config.stub_chain(:vm, :networks).and_return(
                                                         :hostonly => [
                                                           '10.0.0.2', {
                                                             :netmask => '255.255.255.0',
                                                             :nat     => 'eth0'
                                                           },
                                                         ])
          end

          it 'should set snat' do
            pending("Must be refactored to enhanced.") do

              debian.should_receive(:enable_forwarding)
              debian.should_receive(:setup_nat).with('eth0', '10.0.0.2/24')

              subject.call(env)
            end
          end
        end
      end

      context 'with no gateway set' do
        before :each do
          config.stub_chain(:host, :gateway).and_return(nil)
        end
        it 'should not try to set the gateway' do
          debian.should_not_receive(:set_gateway)
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10403215982458278, 'fraction_numerical': 0.015349007187233523, 'mean_word_length': 1.473636637541428, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 16, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6683461', 'n_tokens_mistral': 2390, 'n_tokens_neox': 2234, 'n_words': 498}