entry_spec.rb 4.53 KB
Newer Older
1 2
require 'spec_helper'

3 4
describe Gitlab::Ci::Build::Artifacts::Metadata::Entry do
  let(:entries) do
5 6 7 8 9 10 11 12 13 14 15 16
    { 'path/' => {},
      'path/dir_1/' => {},
      'path/dir_1/file_1' => {},
      'path/dir_1/file_b' => {},
      'path/dir_1/subdir/' => {},
      'path/dir_1/subdir/subfile' => {},
      'path/second_dir' => {},
      'path/second_dir/dir_3/file_2' => {},
      'path/second_dir/dir_3/file_3'=> {},
      'another_directory/'=> {},
      'another_file' => {},
      '/file/with/absolute_path' => {} }
17 18
  end

19
  def path(example)
20
    entry(example.metadata[:path])
21 22
  end

23 24
  def entry(path)
    described_class.new(path, entries)
25 26 27 28
  end

  describe '/file/with/absolute_path', path: '/file/with/absolute_path' do
    subject { |example| path(example) }
29 30

    it { is_expected.to be_file }
31
    it { is_expected.to have_parent }
32 33

    describe '#basename' do
34
      subject { |example| path(example).basename }
35 36 37 38
      it { is_expected.to eq 'absolute_path' }
    end
  end

39 40
  describe 'path/dir_1/', path: 'path/dir_1/' do
    subject { |example| path(example) }
41
    it { is_expected.to have_parent }
42
    it { is_expected.to be_directory }
43

44
    describe '#basename' do
45
      subject { |example| path(example).basename }
46 47
      it { is_expected.to eq 'dir_1/' }
    end
48

49 50 51 52 53
    describe '#name' do
      subject { |example| path(example).name }
      it { is_expected.to eq 'dir_1' }
    end

54
    describe '#parent' do
55
      subject { |example| path(example).parent }
56
      it { is_expected.to eq entry('path/') }
57 58 59 60 61 62
    end

    describe '#children' do
      subject { |example| path(example).children }

      it { is_expected.to all(be_an_instance_of described_class) }
63
      it do
64 65 66
        is_expected.to contain_exactly entry('path/dir_1/file_1'),
                                       entry('path/dir_1/file_b'),
                                       entry('path/dir_1/subdir/')
67
      end
68 69 70 71 72 73 74
    end

    describe '#files' do
      subject { |example| path(example).files }

      it { is_expected.to all(be_file) }
      it { is_expected.to all(be_an_instance_of described_class) }
75
      it do
76 77
        is_expected.to contain_exactly entry('path/dir_1/file_1'),
                                       entry('path/dir_1/file_b')
78
      end
79 80 81
    end

    describe '#directories' do
82 83
      context 'without options' do
        subject { |example| path(example).directories }
84

85 86
        it { is_expected.to all(be_directory) }
        it { is_expected.to all(be_an_instance_of described_class) }
87
        it { is_expected.to contain_exactly entry('path/dir_1/subdir/') }
88
      end
89

90 91
      context 'with option parent: true' do
        subject { |example| path(example).directories(parent: true) }
92

93 94 95
        it { is_expected.to all(be_directory) }
        it { is_expected.to all(be_an_instance_of described_class) }
        it do
96 97
          is_expected.to contain_exactly entry('path/dir_1/subdir/'),
                                         entry('path/')
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        end
      end

      describe '#nodes' do
        subject { |example| path(example).nodes }
        it { is_expected.to eq 2 }
      end

      describe '#exists?' do
        subject { |example| path(example).exists? }
        it { is_expected.to be true }
      end

      describe '#empty?' do
        subject { |example| path(example).empty? }
        it { is_expected.to be false }
114
      end
115
    end
116
  end
117

118
  describe 'empty path', path: '' do
119 120 121 122 123 124 125
    subject { |example| path(example) }
    it { is_expected.to_not have_parent }

    describe '#children' do
      subject { |example| path(example).children }
      it { expect(subject.count).to eq 3 }
    end
126 127 128 129 130 131 132 133

  end

  describe 'path/dir_1/subdir/subfile', path: 'path/dir_1/subdir/subfile' do
    describe '#nodes' do
      subject { |example| path(example).nodes }
      it { is_expected.to eq 4 }
    end
134
  end
135

136 137 138 139 140 141 142 143 144 145
  describe 'non-existent/', path: 'non-existent/' do
    describe '#empty?' do
      subject { |example| path(example).empty? }
      it { is_expected.to be true }
    end

    describe '#exists?' do
      subject { |example| path(example).exists? }
      it { is_expected.to be false }
    end
146 147
  end

148 149 150 151 152
  describe 'another_directory/', path: 'another_directory/' do
    describe '#empty?' do
      subject { |example| path(example).empty? }
      it { is_expected.to be true }
    end
153 154
  end

155
  describe '#metadata' do
156
    let(:entries) do
157 158 159
      { 'path/' => { name: '/path/' },
        'path/file1' => { name: '/path/file1' },
        'path/file2' => { name: '/path/file2' } }
160 161 162
    end

    subject do
163
      described_class.new('path/file1', entries).metadata[:name]
164 165 166 167
    end

    it { is_expected.to eq '/path/file1' }
  end
168
end