diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-17 12:09:20 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-17 12:09:20 +0300 |
commit | fc1df8c8307fc5022f9e8aae04164c089d8fdf2e (patch) | |
tree | a759f58abf9e41200c48a60de73c84cab47a250d /spec/lib/gitlab | |
parent | c8df22c555ab707a705e57c4257fd3ed1ce7c3b0 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab')
4 files changed, 168 insertions, 60 deletions
diff --git a/spec/lib/gitlab/import_export/json/legacy_reader_spec.rb b/spec/lib/gitlab/import_export/json/legacy_reader_spec.rb new file mode 100644 index 00000000000..0009a5f81de --- /dev/null +++ b/spec/lib/gitlab/import_export/json/legacy_reader_spec.rb @@ -0,0 +1,149 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::ImportExport::JSON::LegacyReader::User do + let(:relation_names) { [] } + let(:legacy_reader) { described_class.new(tree_hash, relation_names) } + + describe '#valid?' do + subject { legacy_reader.valid? } + + context 'tree_hash not present' do + let(:tree_hash) { nil } + + it { is_expected.to be false } + end + + context 'tree_hash presents' do + let(:tree_hash) { { "issues": [] } } + + it { is_expected.to be true } + end + end +end + +describe Gitlab::ImportExport::JSON::LegacyReader::File do + let(:fixture) { 'spec/fixtures/lib/gitlab/import_export/light/project.json' } + let(:project_tree) { JSON.parse(File.read(fixture)) } + let(:relation_names) { [] } + let(:legacy_reader) { described_class.new(path, relation_names) } + + describe '#valid?' do + subject { legacy_reader.valid? } + + context 'given valid path' do + let(:path) { fixture } + + it { is_expected.to be true } + end + + context 'given invalid path' do + let(:path) { 'spec/non-existing-folder/do-not-create-this-file.json' } + + it { is_expected.to be false } + end + end + + describe '#root_attributes' do + let(:path) { fixture } + + subject { legacy_reader.root_attributes(excluded_attributes) } + + context 'No excluded attributes' do + let(:excluded_attributes) { [] } + let(:relation_names) { [] } + + it 'returns the whole tree from parsed JSON' do + expect(subject).to eq(project_tree) + end + end + + context 'Some attributes are excluded' do + let(:excluded_attributes) { %w[milestones labels issues services snippets] } + let(:relation_names) { %w[import_type archived] } + + it 'returns hash without excluded attributes and relations' do + expect(subject).not_to include('milestones', 'labels', 'issues', 'services', 'snippets', 'import_type', 'archived') + end + end + end + + describe '#consume_relation' do + let(:path) { fixture } + let(:key) { 'description' } + + context 'block not given' do + it 'returns value of the key' do + expect(legacy_reader).to receive(:relations).and_return({ key => 'test value' }) + expect(legacy_reader.consume_relation(key)).to eq('test value') + end + end + + context 'key has been consumed' do + before do + legacy_reader.consume_relation(key) + end + + it 'does not yield' do + expect do |blk| + legacy_reader.consume_relation(key, &blk) + end.not_to yield_control + end + end + + context 'value is nil' do + before do + expect(legacy_reader).to receive(:relations).and_return({ key => nil }) + end + + it 'does not yield' do + expect do |blk| + legacy_reader.consume_relation(key, &blk) + end.not_to yield_control + end + end + + context 'value is not array' do + before do + expect(legacy_reader).to receive(:relations).and_return({ key => 'value' }) + end + + it 'yield the value with index 0' do + expect do |blk| + legacy_reader.consume_relation(key, &blk) + end.to yield_with_args('value', 0) + end + end + + context 'value is an array' do + before do + expect(legacy_reader).to receive(:relations).and_return({ key => %w[item1 item2 item3] }) + end + + it 'yield each array element with index' do + expect do |blk| + legacy_reader.consume_relation(key, &blk) + end.to yield_successive_args(['item1', 0], ['item2', 1], ['item3', 2]) + end + end + end + + describe '#tree_hash' do + let(:path) { fixture } + + subject { legacy_reader.send(:tree_hash) } + + it 'parses the JSON into the expected tree' do + expect(subject).to eq(project_tree) + end + + context 'invalid JSON' do + let(:path) { 'spec/fixtures/lib/gitlab/import_export/invalid_json/project.json' } + + it 'raise Exception' do + expect { subject }.to raise_exception(Gitlab::ImportExport::Error, 'Incorrect JSON format') + end + end + end +end diff --git a/spec/lib/gitlab/import_export/project/tree_loader_spec.rb b/spec/lib/gitlab/import_export/project/tree_loader_spec.rb deleted file mode 100644 index e683eefa7c0..00000000000 --- a/spec/lib/gitlab/import_export/project/tree_loader_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe Gitlab::ImportExport::Project::TreeLoader do - let(:fixture) { 'spec/fixtures/lib/gitlab/import_export/with_duplicates.json' } - let(:project_tree) { JSON.parse(File.read(fixture)) } - - context 'without de-duplicating entries' do - let(:parsed_tree) do - subject.load(fixture) - end - - it 'parses the JSON into the expected tree' do - expect(parsed_tree).to eq(project_tree) - end - - it 'does not de-duplicate entries' do - expect(parsed_tree['duped_hash_with_id']).not_to be(parsed_tree['array'][0]['duped_hash_with_id']) - end - end - - context 'with de-duplicating entries' do - let(:parsed_tree) do - subject.load(fixture, dedup_entries: true) - end - - it 'parses the JSON into the expected tree' do - expect(parsed_tree).to eq(project_tree) - end - - it 'de-duplicates equal values' do - expect(parsed_tree['duped_hash_with_id']).to be(parsed_tree['array'][0]['duped_hash_with_id']) - expect(parsed_tree['duped_hash_with_id']).to be(parsed_tree['nested']['duped_hash_with_id']) - expect(parsed_tree['duped_array']).to be(parsed_tree['array'][1]['duped_array']) - expect(parsed_tree['duped_array']).to be(parsed_tree['nested']['duped_array']) - end - - it 'does not de-duplicate hashes without IDs' do - expect(parsed_tree['duped_hash_no_id']).to eq(parsed_tree['array'][2]['duped_hash_no_id']) - expect(parsed_tree['duped_hash_no_id']).not_to be(parsed_tree['array'][2]['duped_hash_no_id']) - end - - it 'keeps single entries intact' do - expect(parsed_tree['simple']).to eq(42) - expect(parsed_tree['nested']['array']).to eq(["don't touch"]) - end - end -end diff --git a/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb b/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb index 9c2b202d5bb..e38ef75d085 100644 --- a/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb @@ -783,7 +783,8 @@ describe Gitlab::ImportExport::Project::TreeRestorer do end before do - expect(restorer).to receive(:read_tree_hash) { tree_hash } + allow_any_instance_of(Gitlab::ImportExport::JSON::LegacyReader::File).to receive(:valid?).and_return(true) + allow_any_instance_of(Gitlab::ImportExport::JSON::LegacyReader::File).to receive(:tree_hash) { tree_hash } end context 'no group visibility' do diff --git a/spec/lib/gitlab/import_export/relation_tree_restorer_spec.rb b/spec/lib/gitlab/import_export/relation_tree_restorer_spec.rb index 80901feb893..578418998c0 100644 --- a/spec/lib/gitlab/import_export/relation_tree_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/relation_tree_restorer_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # This spec is a lightweight version of: -# * project_tree_restorer_spec.rb +# * project/tree_restorer_spec.rb # # In depth testing is being done in the above specs. # This spec tests that restore project works @@ -25,7 +25,7 @@ describe Gitlab::ImportExport::RelationTreeRestorer do described_class.new( user: user, shared: shared, - tree_hash: tree_hash, + relation_reader: relation_reader, importable: importable, object_builder: object_builder, members_mapper: members_mapper, @@ -36,14 +36,7 @@ describe Gitlab::ImportExport::RelationTreeRestorer do subject { relation_tree_restorer.restore } - context 'when restoring a project' do - let(:path) { 'spec/fixtures/lib/gitlab/import_export/complex/project.json' } - let(:importable) { create(:project, :builds_enabled, :issues_disabled, name: 'project', path: 'project') } - let(:object_builder) { Gitlab::ImportExport::Project::ObjectBuilder } - let(:relation_factory) { Gitlab::ImportExport::Project::RelationFactory } - let(:reader) { Gitlab::ImportExport::Reader.new(shared: shared) } - let(:tree_hash) { importable_hash } - + shared_examples 'import project successfully' do it 'restores project tree' do expect(subject).to eq(true) end @@ -66,4 +59,18 @@ describe Gitlab::ImportExport::RelationTreeRestorer do end end end + + context 'when restoring a project' do + let(:path) { 'spec/fixtures/lib/gitlab/import_export/complex/project.json' } + let(:importable) { create(:project, :builds_enabled, :issues_disabled, name: 'project', path: 'project') } + let(:object_builder) { Gitlab::ImportExport::Project::ObjectBuilder } + let(:relation_factory) { Gitlab::ImportExport::Project::RelationFactory } + let(:reader) { Gitlab::ImportExport::Reader.new(shared: shared) } + + context 'using legacy reader' do + let(:relation_reader) { Gitlab::ImportExport::JSON::LegacyReader::File.new(path, reader.project_relation_names) } + + it_behaves_like 'import project successfully' + end + end end |