require 'spec_helper'
require 'ec2ssh/ssh_config'

describe Ec2ssh::SshConfig do
  include FakeFS::SpecHelpers

  let(:path) { Pathname('/ssh_config') }

  subject(:ssh_config) do
    described_class.new(path).tap(&:parse!)
  end

  before do
    File.open(path, 'w') {|f| f.write config_str }
  end

  context 'expect be false' do
    let(:config_str) { <<-END }
Host host1
HostName 0.0.0.0
END

    it { expect(ssh_config.mark_exist?).to be_falsey }
  end

  describe '#mark_exist?' do
    context 'expect be true' do
      let(:config_str) { <<-END }
### EC2SSH BEGIN ###
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
# DO NOT edit this block!
# Updated 2013-01-01T00:00:00+00:00
Host db-01.ap-northeast-1
  HostName ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com

### EC2SSH END ###
END

      it { expect(ssh_config.mark_exist?).to be_truthy }
    end
  end

  describe '#parse!' do
    context 'when no section exists' do
      let(:config_str) { ; <<-END }
### EC2SSH BEGIN ###
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
# DO NOT edit this block!
# Updated 2013-01-01T00:00:00+00:00
Host db-01.ap-northeast-1
  HostName ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com

### EC2SSH END ###
END

      it { expect(ssh_config.sections.size).to be == 1 }
      it { expect(ssh_config.sections['default']).to be_an_instance_of Ec2ssh::SshConfig::Section }
    end

    context 'when a section exists' do
      let(:config_str) { <<-END }
Host foo.bar.com
  HostName 1.2.3.4
### EC2SSH BEGIN ###
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
# DO NOT edit this block!
# Updated 2013-01-01T00:00:00+00:00
# section: foo
Host db-01.ap-northeast-1
  HostName ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com

### EC2SSH END ###
END

      it { expect(subject.sections.size).to be == 2 }
      it { expect(subject.sections['foo']).to be_an_instance_of Ec2ssh::SshConfig::Section }
    end

    context 'when multiple sections exist' do
      let(:config_str) { <<-END }
Host foo.bar.com
  HostName 1.2.3.4
### EC2SSH BEGIN ###
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
# DO NOT edit this block!
# Updated 2013-01-01T00:00:00+00:00
# section: foo
Host db-01.ap-northeast-1
  HostName ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com
# section: bar
Host db-02.ap-northeast-1
  HostName ec2-1-1-1-2.ap-northeast-1.ec2.amazonaws.com

### EC2SSH END ###
END

      it { expect(ssh_config.sections.size).to be == 3 }
      it { expect(ssh_config.sections['foo']).to be_an_instance_of Ec2ssh::SshConfig::Section }
      it { expect(ssh_config.sections['bar']).to be_an_instance_of Ec2ssh::SshConfig::Section }
    end
  end
end

describe Ec2ssh::SshConfig::Section do
  describe '#append' do
    let(:section) { Ec2ssh::SshConfig::Section.new('test') }
    before { section.append('foo') }

    it { expect(section.text).to eq 'foo' }
  end

  describe '#replace' do
    let(:section) { Ec2ssh::SshConfig::Section.new('test', 'foo') }
    before { section.replace!('bar') }

    it { expect(section.text).to eq 'bar' }
  end

  describe '#to_s' do
    context 'when no text given' do
      let(:section) { Ec2ssh::SshConfig::Section.new('test') }

      it { expect(section.to_s).to eq '' }
    end

    context 'when empty text given' do
      let(:section) { Ec2ssh::SshConfig::Section.new('test', '') }

      it { expect(section.to_s).to eq '' }
    end

    context 'when some text given' do
      let(:section) { Ec2ssh::SshConfig::Section.new('test', 'foo') }

      it {
        expect(section.to_s).to eq <<EOS
# section: test
foo
EOS
      }
    end
  end
end
