|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative '../spec_helper' |
| 4 | + |
| 5 | +require OctocatalogDiff::Spec.require_path('catalog-util/git') |
| 6 | +require OctocatalogDiff::Spec.require_path('errors') |
| 7 | + |
| 8 | +require 'ostruct' |
| 9 | + |
| 10 | +describe OctocatalogDiff::CatalogUtil::Git do |
| 11 | + before(:each) do |
| 12 | + @logger, @logger_str = OctocatalogDiff::Spec.setup_logger |
| 13 | + allow(File).to receive(:'directory?').with('/tmp/foo').and_return(false) |
| 14 | + allow(File).to receive(:'directory?').with('/tmp/bar').and_return(true) |
| 15 | + end |
| 16 | + |
| 17 | + describe '#check_out_git_archive' do |
| 18 | + context 'with invalid directory' do |
| 19 | + it 'should raise OctocatalogDiff::Errors::GitCheckoutError if basedir is nil' do |
| 20 | + opts = { branch: 'foo', path: '/tmp/foo', basedir: nil, logger: @logger } |
| 21 | + expect do |
| 22 | + described_class.check_out_git_archive(opts) |
| 23 | + end.to raise_error(OctocatalogDiff::Errors::GitCheckoutError, /Source directory/) |
| 24 | + end |
| 25 | + |
| 26 | + it 'should raise OctocatalogDiff::Errors::GitCheckoutError if basedir does not exist' do |
| 27 | + opts = { branch: 'foo', path: '/tmp/foo', basedir: '/tmp/foo', logger: @logger } |
| 28 | + expect do |
| 29 | + described_class.check_out_git_archive(opts) |
| 30 | + end.to raise_error(OctocatalogDiff::Errors::GitCheckoutError, /Source directory/) |
| 31 | + end |
| 32 | + |
| 33 | + it 'should raise OctocatalogDiff::Errors::GitCheckoutError if path is nil' do |
| 34 | + opts = { branch: 'foo', path: nil, basedir: '/tmp/bar', logger: @logger } |
| 35 | + expect do |
| 36 | + described_class.check_out_git_archive(opts) |
| 37 | + end.to raise_error(OctocatalogDiff::Errors::GitCheckoutError, /Target directory/) |
| 38 | + end |
| 39 | + |
| 40 | + it 'should raise OctocatalogDiff::Errors::GitCheckoutError if path does not exist' do |
| 41 | + opts = { branch: 'foo', path: '/tmp/foo', basedir: '/tmp/bar', logger: @logger } |
| 42 | + expect do |
| 43 | + described_class.check_out_git_archive(opts) |
| 44 | + end.to raise_error(OctocatalogDiff::Errors::GitCheckoutError, /Target directory/) |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + context 'with valid directory' do |
| 49 | + context 'with successful script run' do |
| 50 | + it 'should log proper messages and not raise error' do |
| 51 | + expect(described_class).to receive(:create_git_checkout_script).and_return('/tmp/baz.sh') |
| 52 | + expect(Open3).to receive(:capture2e) |
| 53 | + .with('/tmp/baz.sh', chdir: '/tmp/bar') |
| 54 | + .and_return(['asldfkj', OpenStruct.new(exitstatus: 0)]) |
| 55 | + opts = { branch: 'foo', path: '/tmp/bar', basedir: '/tmp/bar', logger: @logger } |
| 56 | + described_class.check_out_git_archive(opts) |
| 57 | + expect(@logger_str.string).to match(%r{Success git archive /tmp/bar:foo}) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context 'with failed script run' do |
| 62 | + it 'should raise OctocatalogDiff::Errors::GitCheckoutError' do |
| 63 | + expect(described_class).to receive(:create_git_checkout_script).and_return('/tmp/baz.sh') |
| 64 | + expect(Open3).to receive(:capture2e) |
| 65 | + .with('/tmp/baz.sh', chdir: '/tmp/bar') |
| 66 | + .and_return(['errors abound', OpenStruct.new(exitstatus: 1)]) |
| 67 | + opts = { branch: 'foo', path: '/tmp/bar', basedir: '/tmp/bar', logger: @logger } |
| 68 | + expect do |
| 69 | + described_class.check_out_git_archive(opts) |
| 70 | + end.to raise_error(OctocatalogDiff::Errors::GitCheckoutError, 'Git archive foo->/tmp/bar failed: errors abound') |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + describe '#create_git_checkout_script' do |
| 77 | + it 'should create the temporary script' do |
| 78 | + result = described_class.create_git_checkout_script('foo', '/tmp/baz') |
| 79 | + expect(result).to be_a_kind_of(String) |
| 80 | + expect(File.file?(result)).to eq(true) |
| 81 | + |
| 82 | + text = File.read(result) |
| 83 | + expect(text).to match(/git archive --format=tar foo \|/) |
| 84 | + expect(text).to match(%r{\( cd /tmp/baz && tar -xf - \)}) |
| 85 | + |
| 86 | + expect(File.executable?(result)).to eq(true) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + describe '#branch_sha' do |
| 91 | + context 'with invalid directory' do |
| 92 | + it 'should raise Errno::ENOENT if basedir is nil' do |
| 93 | + opts = { branch: 'foo', basedir: nil } |
| 94 | + expect do |
| 95 | + described_class.branch_sha(opts) |
| 96 | + end.to raise_error(Errno::ENOENT, /Git directory/) |
| 97 | + end |
| 98 | + |
| 99 | + it 'should raise Errno::ENOENT if basedir does not exist' do |
| 100 | + opts = { branch: 'foo', basedir: '/tmp/foo' } |
| 101 | + expect do |
| 102 | + described_class.branch_sha(opts) |
| 103 | + end.to raise_error(Errno::ENOENT, /Git directory/) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + context 'with valid directory' do |
| 108 | + it 'should return the sha from rugged' do |
| 109 | + opts = { branch: 'foo', basedir: '/tmp/bar' } |
| 110 | + expect(Rugged::Repository).to receive(:new).with('/tmp/bar') |
| 111 | + .and_return(OpenStruct.new(branches: { 'foo' => OpenStruct.new(target_id: 'abcdef012345') })) |
| 112 | + result = described_class.branch_sha(opts) |
| 113 | + expect(result).to eq('abcdef012345') |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | +end |
0 commit comments