summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/build/artifacts/metadata/entry_spec.rb
blob: acca0b08bab3497622a089a26cd9ee614f59a682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require 'spec_helper'

describe Gitlab::Ci::Build::Artifacts::Metadata::Entry do
  let(:entries) do
    { 'path/' => {},
      'path/dir_1/' => {},
      'path/dir_1/file_1' => { size: 10 },
      'path/dir_1/file_b' => { size: 10 },
      'path/dir_1/subdir/' => {},
      'path/dir_1/subdir/subfile' => { size: 10 },
      'path/second_dir' => {},
      'path/second_dir/dir_3/file_2' => { size: 10 },
      'path/second_dir/dir_3/file_3'=> { size: 10 },
      'another_directory/'=> {},
      'another_file' => {},
      '/file/with/absolute_path' => {} }
  end

  def path(example)
    entry(example.metadata[:path])
  end

  def entry(path)
    described_class.new(path, entries)
  end

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

    it { is_expected.to be_file }
    it { is_expected.to have_parent }

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

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

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

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

    describe '#parent' do
      subject { |example| path(example).parent }
      it { is_expected.to eq entry('path/') }
    end

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

      it { is_expected.to all(be_an_instance_of described_class) }
      it do
        is_expected.to contain_exactly entry('path/dir_1/file_1'),
                                       entry('path/dir_1/file_b'),
                                       entry('path/dir_1/subdir/')
      end
    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) }
      it do
        is_expected.to contain_exactly entry('path/dir_1/file_1'),
                                       entry('path/dir_1/file_b')
      end
    end

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

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

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

        it { is_expected.to all(be_directory) }
        it { is_expected.to all(be_an_instance_of described_class) }
        it do
          is_expected.to contain_exactly entry('path/dir_1/subdir/'),
                                         entry('path/')
        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 }
      end

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

  describe 'empty path', path: '' do
    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

  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
  end

  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
  end

  describe 'another_directory/', path: 'another_directory/' do
    describe '#empty?' do
      subject { |example| path(example).empty? }
      it { is_expected.to be true }
    end
  end

  describe '#metadata' do
    let(:entries) do
      { 'path/' => { name: '/path/' },
        'path/file1' => { name: '/path/file1' },
        'path/file2' => { name: '/path/file2' } }
    end

    subject do
      described_class.new('path/file1', entries).metadata[:name]
    end

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