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

3 4
describe Gitlab::Ci::Build::Artifacts::Metadata::Entry do
  let(:entries) do
5 6
    { 'path/' => {},
      'path/dir_1/' => {},
7 8
      'path/dir_1/file_1' => { size: 10 },
      'path/dir_1/file_b' => { size: 10 },
9
      'path/dir_1/subdir/' => {},
10
      'path/dir_1/subdir/subfile' => { size: 10 },
11
      'path/second_dir' => {},
12
      'path/second_dir/dir_3/file_2' => { size: 10 },
13 14
      'path/second_dir/dir_3/file_3' => { size: 10 },
      'another_directory/' => {},
15 16
      '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 116 117 118 119

      describe '#total_size' do
        subject { |example| path(example).total_size }
        it { is_expected.to eq(30) }
      end
120
    end
121
  end
122

123
  describe 'empty path', path: '' do
124 125 126 127 128 129 130
    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
131 132 133 134 135 136 137 138

  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
139
  end
140

141 142 143 144 145 146 147 148 149 150
  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
151 152
  end

153 154 155 156 157
  describe 'another_directory/', path: 'another_directory/' do
    describe '#empty?' do
      subject { |example| path(example).empty? }
      it { is_expected.to be true }
    end
158 159
  end

160
  describe '#metadata' do
161
    let(:entries) do
162 163 164
      { 'path/' => { name: '/path/' },
        'path/file1' => { name: '/path/file1' },
        'path/file2' => { name: '/path/file2' } }
165 166 167
    end

    subject do
168
      described_class.new('path/file1', entries).metadata[:name]
169 170 171 172
    end

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