Skip to content

Commit

Permalink
feat: add rswag cop
Browse files Browse the repository at this point in the history
  • Loading branch information
andimrob committed Oct 4, 2024
1 parent 98c52dd commit 6820c4e
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ Betterment/UnscopedFind:
FactoryBot/AssociationStyle:
Enabled: false

Betterment/NotUsingRswag:
Enabled: false
SafeAutoCorrect: false
Description: Detect API specs missing OpenAPI documentation using rswag
Include:
- spec/requests/**/*_spec.rb

FactoryBot/ConsistentParenthesesStyle:
Enabled: false

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/betterment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
require 'rubocop/cop/betterment/fetch_boolean'
require 'rubocop/cop/betterment/render_status'
require 'rubocop/cop/betterment/redirect_status'
require 'rubocop/cop/betterment/not_using_rswag'
34 changes: 34 additions & 0 deletions lib/rubocop/cop/betterment/not_using_rswag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Betterment
class NotUsingRswag < Base
MSG = 'API tests should use documented using rswag and not the built in `get`, `post`, `put`, `patch`, `delete` methods'

# @!method test?(node)
def_node_matcher :test?, <<-PATTERN
(block (send nil? :it _) ...)
PATTERN

# @!method shared_method?(node)
def_node_matcher :shared_method?, <<-PATTERN
(def ...)
PATTERN

# @!method before_block?(node)
def_node_matcher :before_block?, <<-PATTERN
(block (send nil? :before) ...)
PATTERN

RESTRICT_ON_SEND = %i(get put patch post delete).freeze

def on_send(node)
return unless node.ancestors.any? { |a| test?(a) || shared_method?(a) || before_block?(a) }

add_offense(node)
end
end
end
end
end
74 changes: 74 additions & 0 deletions spec/rubocop/cop/betterment/not_using_rswag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

require 'spec_helper'

describe RuboCop::Cop::Betterment::NotUsingRswag, :config do
let(:error_message) do
'API tests should use documented using rswag and not the built in `get`, `post`, `put`, `patch`, `delete` methods'
end

%w(get put patch post delete).each do |method_name|
it "rejects using the built in #{method_name} method in a test" do
expect_offense(<<~RUBY)
RSpec.describe MyApiController do
it 'returns ok status with expected response' do
get "/api/widgets/1"
^^^^^^^^^^^^^^^^^^^^ #{error_message}
expect(response).to have_http_status :ok
expect(response_json).to eq accepted_response.as_json
end
end
RUBY
end

it "rejects using the built in #{method_name} method in a shared method" do
expect_offense(<<~RUBY)
RSpec.describe MyApiController do
def make_get_request
get "/api/widgets/1"
^^^^^^^^^^^^^^^^^^^^ #{error_message}
end
it 'returns ok status with expected response' do
make_get_request
expect(response).to have_http_status :ok
expect(response_json).to eq accepted_response.as_json
end
end
RUBY
end

it "rejects using the built in #{method_name} method in a before block" do
expect_offense(<<~RUBY)
RSpec.describe MyApiController do
before do
get "/api/widgets/1"
^^^^^^^^^^^^^^^^^^^^ #{error_message}
end
it 'returns ok status with expected response' do
expect(response).to have_http_status :ok
expect(response_json).to eq accepted_response.as_json
end
end
RUBY
end

it "accepts using the rswag #{method_name} method in the example setup" do
expect_no_offenses(<<~RUBY)
RSpec.describe MyApiController do
path "/api/widgets/1" do
get 'shows a foo widget' do
it 'returns ok status with expected response' do
expect(response).to have_http_status :ok
expect(response_json).to eq accepted_response.as_json
end
end
end
end
RUBY
end
end
end

0 comments on commit 6820c4e

Please sign in to comment.