$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'

require 'minitest/autorun'
require 'resolver'
require 'fileutils'


class Module
  def redefine_const(name, value)
    __send__(:remove_const, name) if const_defined?(name)
    const_set(name, value)
  end
end

VANILA =<<H
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.1.1
H

DIRTY =<<H
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.0.1
nameserver 127.0.1.1
H

FAKED = '/tmp/resolv.conf'

class TestResolver < MiniTest::Unit::TestCase

  def setup
    Object.redefine_const(:RESOLV,FAKED)
    File.open(FAKED, 'w') {|f| f.write(VANILA) }
    @resolver = VagrantDns::ResolvConf.new
  end

  def teardown 
    FileUtils.rm FAKED
  end

  def test_appeding
    @resolver.append()
    assert_equal File.open(FAKED,'r').read, DIRTY
  end

  def test_clearing
    @resolver.append()
    @resolver.clear()
    assert_equal File.open(FAKED,'r').read, VANILA
  end
end
