diff options
author | Bob Van Landuyt <bob@gitlab.com> | 2017-04-14 19:29:22 +0200 |
---|---|---|
committer | Bob Van Landuyt <bob@gitlab.com> | 2017-05-01 11:14:24 +0200 |
commit | 27f54bebb29a1e56251ac2d669c2883aeaf1cb1c (patch) | |
tree | 44468ae50d5c43d83ae02085d3cb340b88c29351 /spec/lib/gitlab/database/rename_reserved_paths_migration | |
parent | 0369ef14525cac86b015a21fa0d01b1cad627fc1 (diff) | |
download | gitlab-ce-27f54bebb29a1e56251ac2d669c2883aeaf1cb1c.tar.gz |
Use objects for renaming namespaces and projects
Diffstat (limited to 'spec/lib/gitlab/database/rename_reserved_paths_migration')
-rw-r--r-- | spec/lib/gitlab/database/rename_reserved_paths_migration/rename_base_spec.rb | 147 | ||||
-rw-r--r-- | spec/lib/gitlab/database/rename_reserved_paths_migration/rename_namespaces_spec.rb (renamed from spec/lib/gitlab/database/rename_reserved_paths_migration/namespaces_spec.rb) | 23 | ||||
-rw-r--r-- | spec/lib/gitlab/database/rename_reserved_paths_migration/rename_projects_spec.rb (renamed from spec/lib/gitlab/database/rename_reserved_paths_migration/projects_spec.rb) | 15 |
3 files changed, 165 insertions, 20 deletions
diff --git a/spec/lib/gitlab/database/rename_reserved_paths_migration/rename_base_spec.rb b/spec/lib/gitlab/database/rename_reserved_paths_migration/rename_base_spec.rb new file mode 100644 index 00000000000..48234170d31 --- /dev/null +++ b/spec/lib/gitlab/database/rename_reserved_paths_migration/rename_base_spec.rb @@ -0,0 +1,147 @@ +require 'spec_helper' + +describe Gitlab::Database::RenameReservedPathsMigration::RenameBase do + let(:migration) { FakeRenameReservedPathMigration.new } + let(:subject) { described_class.new(['the-path'], migration) } + + before do + allow(migration).to receive(:say) + end + + def migration_namespace(namespace) + Gitlab::Database::RenameReservedPathsMigration::MigrationClasses:: + Namespace.find(namespace.id) + end + + def migration_project(project) + Gitlab::Database::RenameReservedPathsMigration::MigrationClasses:: + Project.find(project.id) + end + + describe "#remove_last_ocurrence" do + it "removes only the last occurance of a string" do + input = "this/is/a-word-to-replace/namespace/with/a-word-to-replace" + + expect(subject.remove_last_occurrence(input, "a-word-to-replace")) + .to eq("this/is/a-word-to-replace/namespace/with/") + end + end + + describe '#rename_path_for_routable' do + context 'for namespaces' do + let(:namespace) { create(:namespace, path: 'the-path') } + it "renames namespaces called the-path" do + subject.rename_path_for_routable(migration_namespace(namespace)) + + expect(namespace.reload.path).to eq("the-path0") + end + + it "renames the route to the namespace" do + subject.rename_path_for_routable(migration_namespace(namespace)) + + expect(Namespace.find(namespace.id).full_path).to eq("the-path0") + end + + it "renames the route for projects of the namespace" do + project = create(:project, path: "project-path", namespace: namespace) + + subject.rename_path_for_routable(migration_namespace(namespace)) + + expect(project.route.reload.path).to eq("the-path0/project-path") + end + + it 'returns the old & the new path' do + old_path, new_path = subject.rename_path_for_routable(migration_namespace(namespace)) + + expect(old_path).to eq('the-path') + expect(new_path).to eq('the-path0') + end + + context "the-path namespace -> subgroup -> the-path0 project" do + it "updates the route of the project correctly" do + subgroup = create(:group, path: "subgroup", parent: namespace) + project = create(:project, path: "the-path0", namespace: subgroup) + + subject.rename_path_for_routable(migration_namespace(namespace)) + + expect(project.route.reload.path).to eq("the-path0/subgroup/the-path0") + end + end + end + + context 'for projects' do + let(:parent) { create(:namespace, path: 'the-parent') } + let(:project) { create(:empty_project, path: 'the-path', namespace: parent) } + + it 'renames the project called `the-path`' do + subject.rename_path_for_routable(migration_project(project)) + + expect(project.reload.path).to eq('the-path0') + end + + it 'renames the route for the project' do + subject.rename_path_for_routable(project) + + expect(project.reload.route.path).to eq('the-parent/the-path0') + end + + it 'returns the old & new path' do + old_path, new_path = subject.rename_path_for_routable(migration_project(project)) + + expect(old_path).to eq('the-parent/the-path') + expect(new_path).to eq('the-parent/the-path0') + end + end + end + + describe '#move_pages' do + it 'moves the pages directory' do + expect(subject).to receive(:move_folders) + .with(TestEnv.pages_path, 'old-path', 'new-path') + + subject.move_pages('old-path', 'new-path') + end + end + + describe "#move_uploads" do + let(:test_dir) { File.join(Rails.root, 'tmp', 'tests', 'rename_reserved_paths') } + let(:uploads_dir) { File.join(test_dir, 'public', 'uploads') } + + it 'moves subdirectories in the uploads folder' do + expect(subject).to receive(:uploads_dir).and_return(uploads_dir) + expect(subject).to receive(:move_folders).with(uploads_dir, 'old_path', 'new_path') + + subject.move_uploads('old_path', 'new_path') + end + + it "doesn't move uploads when they are stored in object storage" do + expect(subject).to receive(:file_storage?).and_return(false) + expect(subject).not_to receive(:move_folders) + + subject.move_uploads('old_path', 'new_path') + end + end + + describe '#move_folders' do + let(:test_dir) { File.join(Rails.root, 'tmp', 'tests', 'rename_reserved_paths') } + let(:uploads_dir) { File.join(test_dir, 'public', 'uploads') } + + before do + FileUtils.remove_dir(test_dir) if File.directory?(test_dir) + FileUtils.mkdir_p(uploads_dir) + allow(subject).to receive(:uploads_dir).and_return(uploads_dir) + end + + it 'moves a folder with files' do + source = File.join(uploads_dir, 'parent-group', 'sub-group') + FileUtils.mkdir_p(source) + destination = File.join(uploads_dir, 'parent-group', 'moved-group') + FileUtils.touch(File.join(source, 'test.txt')) + expected_file = File.join(destination, 'test.txt') + + subject.move_folders(uploads_dir, File.join('parent-group', 'sub-group'), File.join('parent-group', 'moved-group')) + + expect(File.exist?(expected_file)).to be(true) + end + end +end diff --git a/spec/lib/gitlab/database/rename_reserved_paths_migration/namespaces_spec.rb b/spec/lib/gitlab/database/rename_reserved_paths_migration/rename_namespaces_spec.rb index fb24800e1f1..ee481e4610c 100644 --- a/spec/lib/gitlab/database/rename_reserved_paths_migration/namespaces_spec.rb +++ b/spec/lib/gitlab/database/rename_reserved_paths_migration/rename_namespaces_spec.rb @@ -1,14 +1,11 @@ require 'spec_helper' -describe Gitlab::Database::RenameReservedPathsMigration::Namespaces, :truncate do - let(:subject) do - ActiveRecord::Migration.new.extend( - Gitlab::Database::RenameReservedPathsMigration - ) - end +describe Gitlab::Database::RenameReservedPathsMigration::RenameNamespaces do + let(:migration) { FakeRenameReservedPathMigration.new } + let(:subject) { described_class.new(['the-path'], migration) } before do - allow(subject).to receive(:say) + allow(migration).to receive(:say) end def migration_namespace(namespace) @@ -27,7 +24,7 @@ describe Gitlab::Database::RenameReservedPathsMigration::Namespaces, :truncate d path: 'the-path', parent: create(:namespace)) - found_ids = subject.namespaces_for_paths(['the-PATH'], type: :wildcard). + found_ids = subject.namespaces_for_paths(type: :wildcard). map(&:id) expect(found_ids).to contain_exactly(namespace.id) end @@ -41,7 +38,7 @@ describe Gitlab::Database::RenameReservedPathsMigration::Namespaces, :truncate d path: 'the-path', parent: create(:namespace)) - found_ids = subject.namespaces_for_paths(['the-path'], type: :top_level). + found_ids = subject.namespaces_for_paths(type: :top_level). map(&:id) expect(found_ids).to contain_exactly(root_namespace.id) end @@ -98,12 +95,14 @@ describe Gitlab::Database::RenameReservedPathsMigration::Namespaces, :truncate d describe "#rename_namespace" do let(:namespace) { create(:namespace, path: 'the-path') } - it 'renames paths & routes for the namesapce' do + it 'renames paths & routes for the namespace' do expect(subject).to receive(:rename_path_for_routable). with(namespace). and_call_original subject.rename_namespace(namespace) + + expect(namespace.reload.path).to eq('the-path0') end it "moves the the repository for a project in the namespace" do @@ -140,14 +139,14 @@ describe Gitlab::Database::RenameReservedPathsMigration::Namespaces, :truncate d expect(subject).to receive(:rename_namespace). with(migration_namespace(top_level_namespace)) - subject.rename_namespaces(['the-path'], type: :top_level) + subject.rename_namespaces(type: :top_level) end it 'renames child namespaces' do expect(subject).to receive(:rename_namespace). with(migration_namespace(child_namespace)) - subject.rename_namespaces(['the-path'], type: :wildcard) + subject.rename_namespaces(type: :wildcard) end end end diff --git a/spec/lib/gitlab/database/rename_reserved_paths_migration/projects_spec.rb b/spec/lib/gitlab/database/rename_reserved_paths_migration/rename_projects_spec.rb index 1812e3ea7f4..4a572133b69 100644 --- a/spec/lib/gitlab/database/rename_reserved_paths_migration/projects_spec.rb +++ b/spec/lib/gitlab/database/rename_reserved_paths_migration/rename_projects_spec.rb @@ -1,14 +1,11 @@ require 'spec_helper' -describe Gitlab::Database::RenameReservedPathsMigration::Projects, :truncate do - let(:subject) do - ActiveRecord::Migration.new.extend( - Gitlab::Database::RenameReservedPathsMigration - ) - end +describe Gitlab::Database::RenameReservedPathsMigration::RenameProjects do + let(:migration) { FakeRenameReservedPathMigration.new } + let(:subject) { described_class.new(['the-path'], migration) } before do - allow(subject).to receive(:say) + allow(migration).to receive(:say) end describe '#projects_for_paths' do @@ -16,7 +13,7 @@ describe Gitlab::Database::RenameReservedPathsMigration::Projects, :truncate do project = create(:empty_project, path: 'THE-path') _other_project = create(:empty_project) - result_ids = subject.projects_for_paths(['the-PATH']).map(&:id) + result_ids = subject.projects_for_paths.map(&:id) expect(result_ids).to contain_exactly(project.id) end @@ -35,6 +32,8 @@ describe Gitlab::Database::RenameReservedPathsMigration::Projects, :truncate do and_call_original subject.rename_project(project) + + expect(project.reload.path).to eq('the-path0') end it 'moves the wiki & the repo' do |