From 61fb47a43202332fe9ac57847996da929ba42d3f Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Sat, 9 Jan 2016 14:41:43 +0100 Subject: Simplify implementation of build artifacts browser (refactoring) --- .../ci/build/artifacts/metadata/path_spec.rb | 148 ++++++++++++++++ .../lib/gitlab/ci/build/artifacts/metadata_spec.rb | 22 +-- spec/lib/gitlab/string_path_spec.rb | 186 --------------------- 3 files changed, 154 insertions(+), 202 deletions(-) create mode 100644 spec/lib/gitlab/ci/build/artifacts/metadata/path_spec.rb delete mode 100644 spec/lib/gitlab/string_path_spec.rb (limited to 'spec/lib') diff --git a/spec/lib/gitlab/ci/build/artifacts/metadata/path_spec.rb b/spec/lib/gitlab/ci/build/artifacts/metadata/path_spec.rb new file mode 100644 index 00000000000..148d05b5902 --- /dev/null +++ b/spec/lib/gitlab/ci/build/artifacts/metadata/path_spec.rb @@ -0,0 +1,148 @@ +require 'spec_helper' + +describe Gitlab::Ci::Build::Artifacts::Metadata::Path do + let(:universe) do + ['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'] + end + + def path(example) + string_path(example.metadata[:path]) + end + + def string_path(string_path) + described_class.new(string_path, universe) + 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 string_path('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 string_path('path/dir_1/file_1'), + string_path('path/dir_1/file_b'), + string_path('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 string_path('path/dir_1/file_1'), + string_path('path/dir_1/file_b') + end + end + + describe '#directories' 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 string_path('path/dir_1/subdir/') } + end + + describe '#directories!' 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 do + is_expected.to contain_exactly string_path('path/dir_1/subdir/'), + string_path('path/') + 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 '#nodes', path: './test' do + subject { |example| path(example).nodes } + it { is_expected.to eq 2 } + end + + describe '#nodes', path: './test/' do + subject { |example| path(example).nodes } + it { is_expected.to eq 2 } + end + + describe '#metadata' do + let(:universe) do + ['path/', 'path/file1', 'path/file2'] + end + + let(:metadata) do + [{ name: '/path/' }, { name: '/path/file1' }, { name: '/path/file2' }] + end + + subject do + described_class.new('path/file1', universe, metadata).metadata[:name] + end + + it { is_expected.to eq '/path/file1' } + end + + describe '#exists?', path: 'another_file' do + subject { |example| path(example).exists? } + it { is_expected.to be true } + end + + describe '#exists?', path: './non_existent/' do + let(:universe) { ['./something'] } + subject { |example| path(example).exists? } + + it { is_expected.to be false } + end +end diff --git a/spec/lib/gitlab/ci/build/artifacts/metadata_spec.rb b/spec/lib/gitlab/ci/build/artifacts/metadata_spec.rb index 0c8a41cfab7..36c4851126c 100644 --- a/spec/lib/gitlab/ci/build/artifacts/metadata_spec.rb +++ b/spec/lib/gitlab/ci/build/artifacts/metadata_spec.rb @@ -10,13 +10,8 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do end context 'metadata file exists' do - describe '#exists?' do - subject { metadata.exists? } - it { is_expected.to be true } - end - - describe '#match! ./' do - subject { metadata('./').match! } + describe '#match! empty string' do + subject { metadata('').match! } it 'matches correct paths' do expect(subject.first).to contain_exactly 'ci_artifacts.txt', @@ -55,9 +50,9 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do end end - describe '#to_string_path' do - subject { metadata('').to_string_path } - it { is_expected.to be_an_instance_of(Gitlab::StringPath) } + describe '#to_path' do + subject { metadata('').to_path } + it { is_expected.to be_an_instance_of(Gitlab::Ci::Build::Artifacts::Metdata::Path) } end describe '#full_version' do @@ -79,14 +74,9 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do context 'metadata file does not exist' do let(:metadata_file_path) { '' } - describe '#exists?' do - subject { metadata.exists? } - it { is_expected.to be false } - end - describe '#match!' do it 'raises error' do - expect { metadata.match! }.to raise_error(StandardError, /Metadata file not found/) + expect { metadata.match! }.to raise_error(Errno::ENOENT) end end end diff --git a/spec/lib/gitlab/string_path_spec.rb b/spec/lib/gitlab/string_path_spec.rb deleted file mode 100644 index 7f1d111478b..00000000000 --- a/spec/lib/gitlab/string_path_spec.rb +++ /dev/null @@ -1,186 +0,0 @@ -require 'spec_helper' - -describe Gitlab::StringPath do - let(:universe) do - ['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'] - end - - def path(example) - string_path(example.metadata[:path]) - end - - def string_path(string_path) - described_class.new(string_path, universe) - end - - describe '/file/with/absolute_path', path: '/file/with/absolute_path' do - subject { |example| path(example) } - - it { is_expected.to be_absolute } - it { is_expected.to_not be_relative } - it { is_expected.to be_file } - it { is_expected.to have_parent } - it { is_expected.to_not have_descendants } - it { is_expected.to exist } - - describe '#basename' do - subject { |example| path(example).basename } - - it { is_expected.to eq 'absolute_path' } - end - end - - describe 'path/', path: 'path/' do - subject { |example| path(example) } - - it { is_expected.to be_directory } - it { is_expected.to be_relative } - end - - describe 'path/dir_1/', path: 'path/dir_1/' do - subject { |example| path(example) } - it { is_expected.to have_parent } - - 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 string_path('path/') } - end - - describe '#descendants' do - subject { |example| path(example).descendants } - - it { is_expected.to be_an_instance_of Array } - it { is_expected.to all(be_an_instance_of described_class) } - it do - is_expected.to contain_exactly string_path('path/dir_1/file_1'), - string_path('path/dir_1/file_b'), - string_path('path/dir_1/subdir/'), - string_path('path/dir_1/subdir/subfile') - end - 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 string_path('path/dir_1/file_1'), - string_path('path/dir_1/file_b'), - string_path('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 string_path('path/dir_1/file_1'), - string_path('path/dir_1/file_b') - end - end - - describe '#directories' 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 string_path('path/dir_1/subdir/') } - end - - describe '#directories!' 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 do - is_expected.to contain_exactly string_path('path/dir_1/subdir/'), - string_path('path/dir_1/../') - end - end - end - - describe './', path: './' do - subject { |example| path(example) } - - it { is_expected.to_not have_parent } - it { is_expected.to have_descendants } - - describe '#descendants' do - subject { |example| path(example).descendants } - - it { expect(subject.count).to eq universe.count - 1 } - it { is_expected.to_not include string_path('./') } - end - - describe '#children' do - subject { |example| path(example).children } - it { expect(subject.count).to eq 3 } - end - end - - describe '#nodes', path: './' do - subject { |example| path(example).nodes } - it { is_expected.to eq 1 } - end - - describe '#nodes', path: './test' do - subject { |example| path(example).nodes } - it { is_expected.to eq 2 } - end - - describe '#nodes', path: './test/' do - subject { |example| path(example).nodes } - it { is_expected.to eq 2 } - end - - describe '#metadata' do - let(:universe) do - ['path/', 'path/file1', 'path/file2'] - end - - let(:metadata) do - [{ name: '/path/' }, { name: '/path/file1' }, { name: '/path/file2' }] - end - - subject do - described_class.new('path/file1', universe, metadata).metadata[:name] - end - - it { is_expected.to eq '/path/file1' } - end - - describe '#exists?', path: 'another_file' do - subject { |example| path(example).exists? } - it { is_expected.to be true } - end - - describe '#exists?', path: './non_existent/' do - let(:universe) { ['./something'] } - subject { |example| path(example).exists? } - - it { is_expected.to be false } - end -end -- cgit v1.2.1