diff options
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/controllers/githubish_import_controller_shared_examples.rb | 59 | ||||
-rw-r--r-- | spec/support/drag_to_helper.rb | 4 | ||||
-rw-r--r-- | spec/support/issuables_list_metadata_shared_examples.rb | 15 | ||||
-rw-r--r-- | spec/support/matchers/gitaly_matchers.rb | 2 | ||||
-rw-r--r-- | spec/support/matchers/query_matcher.rb | 33 | ||||
-rw-r--r-- | spec/support/seed_helper.rb | 36 |
6 files changed, 128 insertions, 21 deletions
diff --git a/spec/support/controllers/githubish_import_controller_shared_examples.rb b/spec/support/controllers/githubish_import_controller_shared_examples.rb index 51f1015f43c..c59b30c772d 100644 --- a/spec/support/controllers/githubish_import_controller_shared_examples.rb +++ b/spec/support/controllers/githubish_import_controller_shared_examples.rb @@ -180,7 +180,7 @@ shared_examples 'a GitHub-ish import controller: POST create' do it "takes the new namespace" do expect(Gitlab::GithubImport::ProjectCreator). to receive(:new).with(provider_repo, provider_repo.name, an_instance_of(Group), user, access_params, type: provider). - and_return(double(execute: true)) + and_return(double(execute: true)) post :create, target_namespace: provider_repo.name, format: :js end @@ -201,7 +201,7 @@ shared_examples 'a GitHub-ish import controller: POST create' do it "takes the current user's namespace" do expect(Gitlab::GithubImport::ProjectCreator). to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider). - and_return(double(execute: true)) + and_return(double(execute: true)) post :create, format: :js end @@ -229,7 +229,7 @@ shared_examples 'a GitHub-ish import controller: POST create' do end end - context 'user has chosen a nested namespace and name for the project' do + context 'user has chosen an existing nested namespace and name for the project' do let(:parent_namespace) { create(:namespace, name: 'foo', owner: user) } let(:nested_namespace) { create(:namespace, name: 'bar', parent: parent_namespace, owner: user) } let(:test_name) { 'test_name' } @@ -242,5 +242,58 @@ shared_examples 'a GitHub-ish import controller: POST create' do post :create, { target_namespace: nested_namespace.full_path, new_name: test_name, format: :js } end end + + context 'user has chosen a non-existent nested namespaces and name for the project' do + let(:test_name) { 'test_name' } + + it 'takes the selected namespace and name' do + expect(Gitlab::GithubImport::ProjectCreator). + to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider). + and_return(double(execute: true)) + + post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js } + end + + it 'creates the namespaces' do + allow(Gitlab::GithubImport::ProjectCreator). + to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider). + and_return(double(execute: true)) + + expect { post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js } } + .to change { Namespace.count }.by(2) + end + + it 'new namespace has the right parent' do + allow(Gitlab::GithubImport::ProjectCreator). + to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider). + and_return(double(execute: true)) + + post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js } + + expect(Namespace.find_by_path_or_name('bar').parent.path).to eq('foo') + end + end + + context 'user has chosen existent and non-existent nested namespaces and name for the project' do + let(:test_name) { 'test_name' } + let!(:parent_namespace) { create(:namespace, name: 'foo', owner: user) } + + it 'takes the selected namespace and name' do + expect(Gitlab::GithubImport::ProjectCreator). + to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider). + and_return(double(execute: true)) + + post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :js } + end + + it 'creates the namespaces' do + allow(Gitlab::GithubImport::ProjectCreator). + to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider). + and_return(double(execute: true)) + + expect { post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :js } } + .to change { Namespace.count }.by(2) + end + end end end diff --git a/spec/support/drag_to_helper.rb b/spec/support/drag_to_helper.rb index 0c0659d3ecd..ae149631ed9 100644 --- a/spec/support/drag_to_helper.rb +++ b/spec/support/drag_to_helper.rb @@ -3,11 +3,11 @@ module DragTo evaluate_script("simulateDrag({scrollable: $('#{scrollable}').get(0), from: {el: $('#{selector}').eq(#{list_from_index}).get(0), index: #{from_index}}, to: {el: $('#{selector}').eq(#{list_to_index}).get(0), index: #{to_index}}});") Timeout.timeout(Capybara.default_max_wait_time) do - loop until drag_active? + loop while drag_active? end end def drag_active? - page.evaluate_script('window.SIMULATE_DRAG_ACTIVE').zero? + page.evaluate_script('window.SIMULATE_DRAG_ACTIVE').nonzero? end end diff --git a/spec/support/issuables_list_metadata_shared_examples.rb b/spec/support/issuables_list_metadata_shared_examples.rb index 7ea4073ef2b..3406e4c3161 100644 --- a/spec/support/issuables_list_metadata_shared_examples.rb +++ b/spec/support/issuables_list_metadata_shared_examples.rb @@ -33,4 +33,19 @@ shared_examples 'issuables list meta-data' do |issuable_type, action = nil| expect(meta_data[id].upvotes).to eq(id + 2) end end + + describe "when given empty collection" do + let(:project2) { create(:empty_project, :public) } + + it "doesn't execute any queries with false conditions" do + get_action = + if action + proc { get action } + else + proc { get :index, namespace_id: project2.namespace, project_id: project2 } + end + + expect(&get_action).not_to make_queries_matching(/WHERE (?:1=0|0=1)/) + end + end end diff --git a/spec/support/matchers/gitaly_matchers.rb b/spec/support/matchers/gitaly_matchers.rb index d7a53820684..65dbc01f6e4 100644 --- a/spec/support/matchers/gitaly_matchers.rb +++ b/spec/support/matchers/gitaly_matchers.rb @@ -1,3 +1,3 @@ -RSpec::Matchers.define :post_receive_request_with_repo_path do |path| +RSpec::Matchers.define :gitaly_request_with_repo_path do |path| match { |actual| actual.repository.path == path } end diff --git a/spec/support/matchers/query_matcher.rb b/spec/support/matchers/query_matcher.rb new file mode 100644 index 00000000000..ac8c4ab91d9 --- /dev/null +++ b/spec/support/matchers/query_matcher.rb @@ -0,0 +1,33 @@ +RSpec::Matchers.define :make_queries_matching do |matcher, expected_count = nil| + supports_block_expectations + + match do |block| + @counter = query_count(matcher, &block) + if expected_count + @counter.count == expected_count + else + @counter.count > 0 + end + end + + failure_message_when_negated do |_| + if expected_count + "expected #{matcher} not to match #{expected_count} queries, got #{@counter.count} matches:\n\n#{@counter.inspect}" + else + "expected #{matcher} not to match any query, got #{@counter.count} matches:\n\n#{@counter.inspect}" + end + end + + failure_message do |_| + if expected_count + "expected #{matcher} to match #{expected_count} queries, got #{@counter.count} matches:\n\n#{@counter.inspect}" + else + "expected #{matcher} to match at least one query, got #{@counter.count} matches:\n\n#{@counter.inspect}" + end + end + + def query_count(regex, &block) + @recorder = ActiveRecord::QueryRecorder.new(&block).log + @recorder.select{ |q| q.match(regex) } + end +end diff --git a/spec/support/seed_helper.rb b/spec/support/seed_helper.rb index f55fee28ff9..47b5f556e66 100644 --- a/spec/support/seed_helper.rb +++ b/spec/support/seed_helper.rb @@ -1,20 +1,22 @@ +require_relative 'test_env' + # This file is specific to specs in spec/lib/gitlab/git/ -SEED_REPOSITORY_PATH = File.expand_path('../../tmp/repositories', __dir__) -TEST_REPO_PATH = File.join(SEED_REPOSITORY_PATH, 'gitlab-git-test.git') -TEST_NORMAL_REPO_PATH = File.join(SEED_REPOSITORY_PATH, "not-bare-repo.git") -TEST_MUTABLE_REPO_PATH = File.join(SEED_REPOSITORY_PATH, "mutable-repo.git") -TEST_BROKEN_REPO_PATH = File.join(SEED_REPOSITORY_PATH, "broken-repo.git") +SEED_STORAGE_PATH = TestEnv.repos_path +TEST_REPO_PATH = 'gitlab-git-test.git'.freeze +TEST_NORMAL_REPO_PATH = 'not-bare-repo.git'.freeze +TEST_MUTABLE_REPO_PATH = 'mutable-repo.git'.freeze +TEST_BROKEN_REPO_PATH = 'broken-repo.git'.freeze module SeedHelper GITLAB_GIT_TEST_REPO_URL = ENV.fetch('GITLAB_GIT_TEST_REPO_URL', 'https://gitlab.com/gitlab-org/gitlab-git-test.git').freeze def ensure_seeds - if File.exist?(SEED_REPOSITORY_PATH) - FileUtils.rm_r(SEED_REPOSITORY_PATH) + if File.exist?(SEED_STORAGE_PATH) + FileUtils.rm_r(SEED_STORAGE_PATH) end - FileUtils.mkdir_p(SEED_REPOSITORY_PATH) + FileUtils.mkdir_p(SEED_STORAGE_PATH) create_bare_seeds create_normal_seeds @@ -26,41 +28,45 @@ module SeedHelper def create_bare_seeds system(git_env, *%W(#{Gitlab.config.git.bin_path} clone --bare #{GITLAB_GIT_TEST_REPO_URL}), - chdir: SEED_REPOSITORY_PATH, + chdir: SEED_STORAGE_PATH, out: '/dev/null', err: '/dev/null') end def create_normal_seeds system(git_env, *%W(#{Gitlab.config.git.bin_path} clone #{TEST_REPO_PATH} #{TEST_NORMAL_REPO_PATH}), + chdir: SEED_STORAGE_PATH, out: '/dev/null', err: '/dev/null') end def create_mutable_seeds system(git_env, *%W(#{Gitlab.config.git.bin_path} clone #{TEST_REPO_PATH} #{TEST_MUTABLE_REPO_PATH}), + chdir: SEED_STORAGE_PATH, out: '/dev/null', err: '/dev/null') - system(git_env, *%w(git branch -t feature origin/feature), - chdir: TEST_MUTABLE_REPO_PATH, out: '/dev/null', err: '/dev/null') + mutable_repo_full_path = File.join(SEED_STORAGE_PATH, TEST_MUTABLE_REPO_PATH) + system(git_env, *%W(#{Gitlab.config.git.bin_path} branch -t feature origin/feature), + chdir: mutable_repo_full_path, out: '/dev/null', err: '/dev/null') system(git_env, *%W(#{Gitlab.config.git.bin_path} remote add expendable #{GITLAB_GIT_TEST_REPO_URL}), - chdir: TEST_MUTABLE_REPO_PATH, out: '/dev/null', err: '/dev/null') + chdir: mutable_repo_full_path, out: '/dev/null', err: '/dev/null') end def create_broken_seeds system(git_env, *%W(#{Gitlab.config.git.bin_path} clone --bare #{TEST_REPO_PATH} #{TEST_BROKEN_REPO_PATH}), + chdir: SEED_STORAGE_PATH, out: '/dev/null', err: '/dev/null') - refs_path = File.join(TEST_BROKEN_REPO_PATH, 'refs') + refs_path = File.join(SEED_STORAGE_PATH, TEST_BROKEN_REPO_PATH, 'refs') FileUtils.rm_r(refs_path) end def create_git_attributes - dir = File.join(SEED_REPOSITORY_PATH, 'with-git-attributes.git', 'info') + dir = File.join(SEED_STORAGE_PATH, 'with-git-attributes.git', 'info') FileUtils.mkdir_p(dir) @@ -85,7 +91,7 @@ bla/bla.txt end def create_invalid_git_attributes - dir = File.join(SEED_REPOSITORY_PATH, 'with-invalid-git-attributes.git', 'info') + dir = File.join(SEED_STORAGE_PATH, 'with-invalid-git-attributes.git', 'info') FileUtils.mkdir_p(dir) |