Commit 0ce67858 authored by Jacopo's avatar Jacopo

Replaces `tag: true` into `:tag` in the specs

Replaces all the explicit include metadata syntax in the specs (tag:
true) into the implicit one (:tag).
Added a cop to prevent future errors and handle autocorrection.
parent 2ef28db9
---
title: 'Replace `tag: true` into `:tag` in the specs'
merge_request: 14653
author: Jacopo Beschi @jacopo-beschi
type: added
# frozen_string_literal: true
require 'rubocop-rspec'
module RuboCop
module Cop
module RSpec
# Checks for verbose include metadata used in the specs.
#
# @example
# # bad
# describe MyClass, js: true do
# end
#
# # good
# describe MyClass, :js do
# end
class VerboseIncludeMetadata < Cop
MSG = 'Use `%s` instead of `%s`.'
SELECTORS = %i[describe context feature example_group it specify example scenario its].freeze
def_node_matcher :include_metadata, <<-PATTERN
(send {(const nil :RSpec) nil} {#{SELECTORS.map(&:inspect).join(' ')}}
!const
...
(hash $...))
PATTERN
def_node_matcher :invalid_metadata?, <<-PATTERN
(pair
(sym $...)
(true))
PATTERN
def on_send(node)
invalid_metadata_matches(node) do |match|
add_offense(node, :expression, format(MSG, good(match), bad(match)))
end
end
def autocorrect(node)
lambda do |corrector|
invalid_metadata_matches(node) do |match|
corrector.replace(match.loc.expression, good(match))
end
end
end
private
def invalid_metadata_matches(node)
include_metadata(node) do |matches|
matches.select(&method(:invalid_metadata?)).each do |match|
yield match
end
end
end
def bad(match)
"#{metadata_key(match)}: true"
end
def good(match)
":#{metadata_key(match)}"
end
def metadata_key(match)
match.children[0].source
end
end
end
end
end
...@@ -21,3 +21,4 @@ require_relative 'cop/migration/reversible_add_column_with_default' ...@@ -21,3 +21,4 @@ require_relative 'cop/migration/reversible_add_column_with_default'
require_relative 'cop/migration/timestamps' require_relative 'cop/migration/timestamps'
require_relative 'cop/migration/update_column_in_batches' require_relative 'cop/migration/update_column_in_batches'
require_relative 'cop/rspec/single_line_hook' require_relative 'cop/rspec/single_line_hook'
require_relative 'cop/rspec/verbose_include_metadata'
...@@ -141,7 +141,7 @@ describe ProjectsController do ...@@ -141,7 +141,7 @@ describe ProjectsController do
end end
end end
context 'when the storage is not available', broken_storage: true do context 'when the storage is not available', :broken_storage do
set(:project) { create(:project, :broken_storage) } set(:project) { create(:project, :broken_storage) }
before do before do
......
require 'spec_helper' require 'spec_helper'
describe "Admin::AbuseReports", js: true do describe "Admin::AbuseReports", :js do
let(:user) { create(:user) } let(:user) { create(:user) }
context 'as an admin' do context 'as an admin' do
......
...@@ -40,7 +40,7 @@ feature 'Admin Broadcast Messages' do ...@@ -40,7 +40,7 @@ feature 'Admin Broadcast Messages' do
expect(page).not_to have_content 'Migration to new server' expect(page).not_to have_content 'Migration to new server'
end end
scenario 'Live preview a customized broadcast message', js: true do scenario 'Live preview a customized broadcast message', :js do
fill_in 'broadcast_message_message', with: "Live **Markdown** previews. :tada:" fill_in 'broadcast_message_message', with: "Live **Markdown** previews. :tada:"
page.within('.broadcast-message-preview') do page.within('.broadcast-message-preview') do
......
require 'rails_helper' require 'rails_helper'
feature 'Admin disables 2FA for a user' do feature 'Admin disables 2FA for a user' do
scenario 'successfully', js: true do scenario 'successfully', :js do
sign_in(create(:admin)) sign_in(create(:admin))
user = create(:user, :two_factor) user = create(:user, :two_factor)
......
...@@ -52,7 +52,7 @@ feature 'Admin Groups' do ...@@ -52,7 +52,7 @@ feature 'Admin Groups' do
expect_selected_visibility(internal) expect_selected_visibility(internal)
end end
scenario 'when entered in group path, it auto filled the group name', js: true do scenario 'when entered in group path, it auto filled the group name', :js do
visit admin_groups_path visit admin_groups_path
click_link "New group" click_link "New group"
group_path = 'gitlab' group_path = 'gitlab'
...@@ -81,7 +81,7 @@ feature 'Admin Groups' do ...@@ -81,7 +81,7 @@ feature 'Admin Groups' do
expect_selected_visibility(group.visibility_level) expect_selected_visibility(group.visibility_level)
end end
scenario 'edit group path does not change group name', js: true do scenario 'edit group path does not change group name', :js do
group = create(:group, :private) group = create(:group, :private)
visit admin_group_edit_path(group) visit admin_group_edit_path(group)
...@@ -93,7 +93,7 @@ feature 'Admin Groups' do ...@@ -93,7 +93,7 @@ feature 'Admin Groups' do
end end
end end
describe 'add user into a group', js: true do describe 'add user into a group', :js do
shared_context 'adds user into a group' do shared_context 'adds user into a group' do
it do it do
visit admin_group_path(group) visit admin_group_path(group)
...@@ -124,7 +124,7 @@ feature 'Admin Groups' do ...@@ -124,7 +124,7 @@ feature 'Admin Groups' do
group.add_user(:user, Gitlab::Access::OWNER) group.add_user(:user, Gitlab::Access::OWNER)
end end
it 'adds admin a to a group as developer', js: true do it 'adds admin a to a group as developer', :js do
visit group_group_members_path(group) visit group_group_members_path(group)
page.within '.users-group-form' do page.within '.users-group-form' do
...@@ -141,7 +141,7 @@ feature 'Admin Groups' do ...@@ -141,7 +141,7 @@ feature 'Admin Groups' do
end end
end end
describe 'admin remove himself from a group', js: true do describe 'admin remove himself from a group', :js do
it 'removes admin from the group' do it 'removes admin from the group' do
group.add_user(current_user, Gitlab::Access::DEVELOPER) group.add_user(current_user, Gitlab::Access::DEVELOPER)
......
require 'spec_helper' require 'spec_helper'
feature "Admin Health Check", feature: true, broken_storage: true do feature "Admin Health Check", :feature, :broken_storage do
include StubENV include StubENV
before do before do
......
...@@ -74,7 +74,7 @@ describe 'Admin::Hooks', :js do ...@@ -74,7 +74,7 @@ describe 'Admin::Hooks', :js do
end end
end end
describe 'Test', js: true do describe 'Test', :js do
before do before do
WebMock.stub_request(:post, @system_hook.url) WebMock.stub_request(:post, @system_hook.url)
visit admin_hooks_path visit admin_hooks_path
......
...@@ -30,7 +30,7 @@ RSpec.describe 'admin issues labels' do ...@@ -30,7 +30,7 @@ RSpec.describe 'admin issues labels' do
end end
end end
it 'deletes all labels', js: true do it 'deletes all labels', :js do
page.within '.labels' do page.within '.labels' do
page.all('.btn-remove').each do |remove| page.all('.btn-remove').each do |remove|
remove.click remove.click
......
...@@ -28,7 +28,7 @@ describe "Admin::Projects" do ...@@ -28,7 +28,7 @@ describe "Admin::Projects" do
expect(page).not_to have_content(archived_project.name) expect(page).not_to have_content(archived_project.name)
end end
it 'renders all projects', js: true do it 'renders all projects', :js do
find(:css, '#sort-projects-dropdown').click find(:css, '#sort-projects-dropdown').click
click_link 'Show archived projects' click_link 'Show archived projects'
...@@ -37,7 +37,7 @@ describe "Admin::Projects" do ...@@ -37,7 +37,7 @@ describe "Admin::Projects" do
expect(page).to have_xpath("//span[@class='label label-warning']", text: 'archived') expect(page).to have_xpath("//span[@class='label label-warning']", text: 'archived')
end end
it 'renders only archived projects', js: true do it 'renders only archived projects', :js do
find(:css, '#sort-projects-dropdown').click find(:css, '#sort-projects-dropdown').click
click_link 'Show archived projects only' click_link 'Show archived projects only'
...@@ -74,7 +74,7 @@ describe "Admin::Projects" do ...@@ -74,7 +74,7 @@ describe "Admin::Projects" do
.to receive(:move_uploads_to_new_namespace).and_return(true) .to receive(:move_uploads_to_new_namespace).and_return(true)
end end
it 'transfers project to group web', js: true do it 'transfers project to group web', :js do
visit admin_project_path(project) visit admin_project_path(project)
click_button 'Search for Namespace' click_button 'Search for Namespace'
...@@ -91,7 +91,7 @@ describe "Admin::Projects" do ...@@ -91,7 +91,7 @@ describe "Admin::Projects" do
project.team << [user, :master] project.team << [user, :master]
end end
it 'adds admin a to a project as developer', js: true do it 'adds admin a to a project as developer', :js do
visit project_project_members_path(project) visit project_project_members_path(project)
page.within '.users-project-form' do page.within '.users-project-form' do
......
require 'spec_helper' require 'spec_helper'
describe 'Admin > Users > Impersonation Tokens', js: true do describe 'Admin > Users > Impersonation Tokens', :js do
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
let!(:user) { create(:user) } let!(:user) { create(:user) }
......
...@@ -288,7 +288,7 @@ describe "Admin::Users" do ...@@ -288,7 +288,7 @@ describe "Admin::Users" do
end end
end end
it 'allows group membership to be revoked', js: true do it 'allows group membership to be revoked', :js do
page.within(first('.group_member')) do page.within(first('.group_member')) do
find('.btn-remove').click find('.btn-remove').click
end end
...@@ -309,7 +309,7 @@ describe "Admin::Users" do ...@@ -309,7 +309,7 @@ describe "Admin::Users" do
end end
end end
describe 'remove users secondary email', js: true do describe 'remove users secondary email', :js do
let!(:secondary_email) do let!(:secondary_email) do
create :email, email: 'secondary@example.com', user: user create :email, email: 'secondary@example.com', user: user
end end
......
...@@ -32,7 +32,7 @@ feature 'Admin uses repository checks' do ...@@ -32,7 +32,7 @@ feature 'Admin uses repository checks' do
end end
end end
scenario 'to clear all repository checks', js: true do scenario 'to clear all repository checks', :js do
visit admin_application_settings_path visit admin_application_settings_path
expect(RepositoryCheck::ClearWorker).to receive(:perform_async) expect(RepositoryCheck::ClearWorker).to receive(:perform_async)
......
...@@ -31,7 +31,7 @@ describe 'Auto deploy' do ...@@ -31,7 +31,7 @@ describe 'Auto deploy' do
expect(page).to have_link('Set up auto deploy') expect(page).to have_link('Set up auto deploy')
end end
it 'includes OpenShift as an available template', js: true do it 'includes OpenShift as an available template', :js do
click_link 'Set up auto deploy' click_link 'Set up auto deploy'
click_button 'Apply a GitLab CI Yaml template' click_button 'Apply a GitLab CI Yaml template'
...@@ -40,7 +40,7 @@ describe 'Auto deploy' do ...@@ -40,7 +40,7 @@ describe 'Auto deploy' do
end end
end end
it 'creates a merge request using "auto-deploy" branch', js: true do it 'creates a merge request using "auto-deploy" branch', :js do
click_link 'Set up auto deploy' click_link 'Set up auto deploy'
click_button 'Apply a GitLab CI Yaml template' click_button 'Apply a GitLab CI Yaml template'
within '.gitlab-ci-yml-selector' do within '.gitlab-ci-yml-selector' do
......
require 'rails_helper' require 'rails_helper'
describe 'Issue Boards', js: true do describe 'Issue Boards', :js do
include DragTo include DragTo
let(:group) { create(:group, :nested) } let(:group) { create(:group, :nested) }
......
require 'rails_helper' require 'rails_helper'
describe 'Issue Boards shortcut', js: true do describe 'Issue Boards shortcut', :js do
let(:project) { create(:project) } let(:project) { create(:project) }
before do before do
......
require 'rails_helper' require 'rails_helper'
describe 'Issue Boards new issue', js: true do describe 'Issue Boards new issue', :js do
let(:project) { create(:project, :public) } let(:project) { create(:project, :public) }
let(:board) { create(:board, project: project) } let(:board) { create(:board, project: project) }
let!(:list) { create(:list, board: board, position: 0) } let!(:list) { create(:list, board: board, position: 0) }
......
require 'rails_helper' require 'rails_helper'
describe 'Issue Boards', js: true do describe 'Issue Boards', :js do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
let(:project) { create(:project, :public) } let(:project) { create(:project, :public) }
......
require 'spec_helper' require 'spec_helper'
describe 'CI Lint', js: true do describe 'CI Lint', :js do
before do before do
sign_in(create(:user)) sign_in(create(:user))
end end
......
require 'spec_helper' require 'spec_helper'
describe "Container Registry", js: true do describe "Container Registry", :js do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project) } let(:project) { create(:project) }
......
require 'spec_helper' require 'spec_helper'
describe 'Copy as GFM', js: true do describe 'Copy as GFM', :js do
include MarkupHelper include MarkupHelper
include RepoHelpers include RepoHelpers
include ActionView::Helpers::JavaScriptHelper include ActionView::Helpers::JavaScriptHelper
......
require 'spec_helper' require 'spec_helper'
feature 'Cycle Analytics', js: true do feature 'Cycle Analytics', :js do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:guest) { create(:user) } let(:guest) { create(:user) }
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
......
require 'spec_helper' require 'spec_helper'
RSpec.describe 'Dashboard Active Tab', js: true do RSpec.describe 'Dashboard Active Tab', :js do
before do before do
sign_in(create(:user)) sign_in(create(:user))
end end
......
require 'spec_helper' require 'spec_helper'
feature 'Tooltips on .timeago dates', js: true do feature 'Tooltips on .timeago dates', :js do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, name: 'test', namespace: user.namespace) } let(:project) { create(:project, name: 'test', namespace: user.namespace) }
let(:created_date) { Date.yesterday.to_time } let(:created_date) { Date.yesterday.to_time }
......
...@@ -5,7 +5,7 @@ RSpec.describe 'Dashboard Group' do ...@@ -5,7 +5,7 @@ RSpec.describe 'Dashboard Group' do
sign_in(create(:user)) sign_in(create(:user))
end end
it 'creates new group', js: true do it 'creates new group', :js do
visit dashboard_groups_path visit dashboard_groups_path
find('.btn-new').trigger('click') find('.btn-new').trigger('click')
new_path = 'Samurai' new_path = 'Samurai'
......
...@@ -24,7 +24,7 @@ RSpec.describe 'Dashboard Issues' do ...@@ -24,7 +24,7 @@ RSpec.describe 'Dashboard Issues' do
expect(page).not_to have_content(other_issue.title) expect(page).not_to have_content(other_issue.title)
end end
it 'shows checkmark when unassigned is selected for assignee', js: true do it 'shows checkmark when unassigned is selected for assignee', :js do
find('.js-assignee-search').click find('.js-assignee-search').click
find('li', text: 'Unassigned').click find('li', text: 'Unassigned').click
find('.js-assignee-search').click find('.js-assignee-search').click
...@@ -32,7 +32,7 @@ RSpec.describe 'Dashboard Issues' do ...@@ -32,7 +32,7 @@ RSpec.describe 'Dashboard Issues' do
expect(find('li[data-user-id="0"] a.is-active')).to be_visible expect(find('li[data-user-id="0"] a.is-active')).to be_visible
end end
it 'shows issues when current user is author', js: true do it 'shows issues when current user is author', :js do
find('#assignee_id', visible: false).set('') find('#assignee_id', visible: false).set('')
find('.js-author-search', match: :first).click find('.js-author-search', match: :first).click
...@@ -70,7 +70,7 @@ RSpec.describe 'Dashboard Issues' do ...@@ -70,7 +70,7 @@ RSpec.describe 'Dashboard Issues' do
end end
describe 'new issue dropdown' do describe 'new issue dropdown' do
it 'shows projects only with issues feature enabled', js: true do it 'shows projects only with issues feature enabled', :js do
find('.new-project-item-select-button').trigger('click') find('.new-project-item-select-button').trigger('click')
page.within('.select2-results') do page.within('.select2-results') do
...@@ -79,7 +79,7 @@ RSpec.describe 'Dashboard Issues' do ...@@ -79,7 +79,7 @@ RSpec.describe 'Dashboard Issues' do
end end
end end
it 'shows the new issue page', js: true do it 'shows the new issue page', :js do
find('.new-project-item-select-button').trigger('click') find('.new-project-item-select-button').trigger('click')
wait_for_requests wait_for_requests
......
require 'spec_helper' require 'spec_helper'
describe 'Dashboard > label filter', js: true do describe 'Dashboard > label filter', :js do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, name: 'test', namespace: user.namespace) } let(:project) { create(:project, name: 'test', namespace: user.namespace) }
let(:project2) { create(:project, name: 'test2', path: 'test2', namespace: user.namespace) } let(:project2) { create(:project, name: 'test2', path: 'test2', namespace: user.namespace) }
......
...@@ -24,7 +24,7 @@ feature 'Dashboard Merge Requests' do ...@@ -24,7 +24,7 @@ feature 'Dashboard Merge Requests' do
visit merge_requests_dashboard_path visit merge_requests_dashboard_path
end end
it 'shows projects only with merge requests feature enabled', js: true do it 'shows projects only with merge requests feature enabled', :js do
find('.new-project-item-select-button').trigger('click') find('.new-project-item-select-button').trigger('click')
page.within('.select2-results') do page.within('.select2-results') do
...@@ -89,7 +89,7 @@ feature 'Dashboard Merge Requests' do ...@@ -89,7 +89,7 @@ feature 'Dashboard Merge Requests' do
expect(page).not_to have_content(other_merge_request.title) expect(page).not_to have_content(other_merge_request.title)
end end
it 'shows authored merge requests', js: true do it 'shows authored merge requests', :js do
filter_item_select('Any Assignee', '.js-assignee-search') filter_item_select('Any Assignee', '.js-assignee-search')
filter_item_select(current_user.to_reference, '.js-author-search') filter_item_select(current_user.to_reference, '.js-author-search')
...@@ -101,7 +101,7 @@ feature 'Dashboard Merge Requests' do ...@@ -101,7 +101,7 @@ feature 'Dashboard Merge Requests' do
expect(page).not_to have_content(other_merge_request.title) expect(page).not_to have_content(other_merge_request.title)
end end