Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rswag cop #47

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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 @@ -24,3 +24,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'
74 changes: 74 additions & 0 deletions lib/rubocop/cop/betterment/not_using_rswag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Betterment
class NotUsingRswag < Base
MSG = 'Ensure request spec conforms to the required structure.'

def on_new_investigation
return if processed_source.ast.nil?

unless valid_path_structure?(processed_source.ast)
add_offense(processed_source.ast)
end
end

private

def valid_path_structure?(node)
node.each_descendant.any? do |descendant|
next unless path_node?(descendant)

find_method_and_response_nodes(descendant)
end
end

def find_method_and_response_nodes(node)
method_found = false
response_found = false

node.each_descendant do |descendant|
method_found ||= method_node?(descendant)
response_found ||= response_node?(descendant)
break if method_found && response_found
end

method_found && response_found
end

# @!method path_with_code?(node)
def_node_matcher :path_with_code?, <<~PATTERN
(block
(send nil? :path (str _))
args
!nil?)
PATTERN

# @!method path_node?(node)
def_node_matcher :path_node?, <<~PATTERN
(block
(send nil? :path (str _))
args
_)
PATTERN

# @!method method_node?(node)
def_node_matcher :method_node?, <<~PATTERN
(block
(send nil? {:get :post :put :patch :delete} str)
args
_)
PATTERN

# @!method response_node?(node)
def_node_matcher :response_node?, <<~PATTERN
(block
(send nil? :response str str)
args
_)
PATTERN
end
end
end
end
124 changes: 124 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,124 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RuboCop::Cop::Betterment::NotUsingRswag, :config do
it 'does not register and offense on an empty syntax tree' do
expect_no_offenses("")
end

it 'registers an offense when no path is found' do
expect_offense(<<~RUBY)
RSpec.describe MyApiController do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure request spec conforms to the required structure.
describe 'A request spec' do
context 'some situation' do
it 'does something' do
expect(true).to be_truthy
end
end
end
end
RUBY
end

it 'does not register an offense for valid structure' do
expect_no_offenses(<<~RUBY)
RSpec.describe MyApiController do
path '/blogs' do
get 'Creates a blog' do
response '201', 'blog created' do
end
end
end
end
RUBY
end

it 'registers an offense if the method and response nodes are missing' do
expect_offense(<<~RUBY)
RSpec.describe MyApiController do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure request spec conforms to the required structure.
path '/blogs' do
context 'some situation' do
it 'does something else' do
expect(true).to be_truthy
end
end
end
end
RUBY
end

it 'registers an no offense if method and response nodes are present in parallel with non-rswag context' do
expect_no_offenses(<<~RUBY)
RSpec.describe MyApiController do
path '/blogs' do
context 'some situation' do
get 'Creates a blog' do
context 'another situation' do
response '201', 'blog created' do
end
end
end
end
end

it 'returns ok status with expected response' do
get "/api/widgets/1"
expect(response).to have_http_status :ok
expect(response_json).to eq accepted_response.as_json
end
end
RUBY
end

it 'does not register an offense for nested contexts with valid structure' do
expect_no_offenses(<<~RUBY)
RSpec.describe MyApiController do
path '/blogs' do
context 'some situation' do
get 'Creates a blog' do
context 'another situation' do
response '201', 'blog created' do
end
end
end
end
end
end
RUBY
end

it 'registers an offense if the response node is missing even with nested contexts' do
expect_offense(<<~RUBY)
RSpec.describe MyApiController do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure request spec conforms to the required structure.
path '/blogs' do
context 'some situation' do
get 'Creates a blog' do
context 'another situation' do
it 'does something else' do
expect(true).to be_truthy
end
end
end
end
end
end
RUBY
end

it 'registers if http method calls are happening within "it" examples' do
expect_offense(<<~RUBY)
RSpec.describe MyApiController do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure request spec conforms to the required structure.
it 'returns ok status with expected response' do
get "/api/widgets/1"
expect(response).to have_http_status :ok
expect(response_json).to eq accepted_response.as_json
end
end
RUBY
end
end
Loading