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

Add support for --stdin-filename #1343

Merged
merged 2 commits into from
Jun 25, 2018
Merged
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
1 change: 1 addition & 0 deletions features/command_line_interface/options.feature
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Feature: Reek can be controlled using command-line options
--smell SMELL Only look for a specific smell.
Call it like this: reek --smell MissingSafeMethod source.rb
Check out https://github.com/troessner/reek/blob/v4.8.1/docs/Code-Smells.md for a list of smells
--stdin-filename FILE When passing code in via pipe, assume this filename when checking file or directory rules in the config.

Generate a todo list:
-t, --todo Generate a todo list
Expand Down
30 changes: 30 additions & 0 deletions features/command_line_interface/stdin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,33 @@ Feature: Reek reads from $stdin when no files are given
[2]:Syntax: This file has unexpected token $end
[1]:Syntax: This file has unexpected token tEQL
"""

Scenario: providing a filename to use for the config to match against
Given a file named "web_app/config.reek" with:
"""
---
directories:
"web_app/app/controllers":
IrresponsibleModule:
enabled: false
NestedIterators:
enabled: false
InstanceVariableAssumption:
enabled: false
"""
When I pass a stdin to reek --config web_app/config.reek --stdin-filename web_app/app/controllers/users_controller with:
"""
class UsersController < ApplicationController
def show
respond_with do |format|
format.json { |json| @user.to_custom_json }
format.xml { |xml| @user.to_fancy_xml }
end
end
end
"""
Then it succeeds
And it reports nothing



4 changes: 4 additions & 0 deletions features/step_definitions/reek_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
reek_with_pipe(stdin, args)
end

When /^I pass a stdin to reek *(.*) with:$/ do |args, stdin|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

reek_with_pipe(stdin, args)
end

Then /^it reports nothing$/ do
expect(last_command_started).to have_output_on_stdout('')
end
Expand Down
3 changes: 2 additions & 1 deletion lib/reek/cli/command/report_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def execute

def populate_reporter_with_smells
sources.each do |source|
reporter.add_examiner Examiner.new(source,
reporter.add_examiner Examiner.new(Source::SourceCode.from(source),
origin: options.stdin_filename,
filter_by_smells: smell_names,
configuration: configuration,
error_handler: LoggingErrorHandler.new)
Expand Down
8 changes: 7 additions & 1 deletion lib/reek/cli/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Options
:show_empty,
:show_links,
:sorting,
:stdin_filename,
:success_exit_code,
:failure_exit_code,
:generate_todo_list,
Expand Down Expand Up @@ -98,7 +99,7 @@ def set_banner
BANNER
end

# :reek:TooManyStatements { max_statements: 6 }
# :reek:TooManyStatements { max_statements: 7 }
def set_configuration_options
parser.separator 'Configuration:'
parser.on('-c', '--config FILE', 'Read configuration options from FILE') do |file|
Expand All @@ -111,6 +112,11 @@ def set_configuration_options
'for a list of smells') do |smell|
smells_to_detect << smell
end
parser.on('--stdin-filename FILE',
'When passing code in via pipe, assume this filename when '\
'checking file or directory rules in the config.') do |file|
self.stdin_filename = Pathname.new(file)
end
end

def set_generate_todo_list_options
Expand Down
13 changes: 7 additions & 6 deletions lib/reek/examiner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Reek
# Applies all available smell detectors to a source.
#
# @public
# :reek:TooManyInstanceVariables { max_instance_variables: 5 }
class Examiner
# Handles no errors
class NullHandler
Expand All @@ -33,12 +34,16 @@ def handle(_exception)
# The configuration for this Examiner.
#
# @public
# :reek:ControlParameter
# :reek:LongParameterList { max_params: 6 }
def initialize(source,
origin: nil,
filter_by_smells: [],
configuration: Configuration::AppConfiguration.default,
detector_repository_class: DetectorRepository,
error_handler: NullHandler.new)
@source = Source::SourceCode.from(source)
@origin = origin || @source.origin
@smell_types = detector_repository_class.eligible_smell_types(filter_by_smells)
@detector_repository = detector_repository_class.new(smell_types: @smell_types,
configuration: configuration.directive_for(description))
Expand All @@ -48,17 +53,13 @@ def initialize(source,
# @return [String] origin of the source being analysed
#
# @public
def origin
@origin ||= source.origin
end
attr_reader :origin

# @return [String] description of the source being analysed
#
# @public
# @deprecated Use origin
def description
origin
end
alias description origin

#
# @return [Array<SmellWarning>] the smells found in the source
Expand Down
1 change: 1 addition & 0 deletions lib/reek/source/source_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def initialize(code:, origin:, parser: self.class.default_parser)
# :reek:DuplicateMethodCall { max_calls: 2 }
def self.from(source)
case source
when self then source
when File then new(code: source.read, origin: source.path)
when IO then new(code: source.readlines.join, origin: IO_IDENTIFIER)
when Pathname then new(code: source.read, origin: source.to_s)
Expand Down
12 changes: 12 additions & 0 deletions spec/reek/cli/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@
configuration: Reek::Configuration::AppConfiguration,
options: Reek::CLI::Options)
end

context 'when a stdin filename is provided' do
let(:app) { described_class.new ['--stdin-filename', 'foo.rb'] }

it 'assumes that filename' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the specific filename being checked in the implementation of this spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably just remove this spec. I wanted somewhere in the regular specs (not just features) that would test the --stdin-filename option, and this seemed like the most logical place to do so. However, once I dug in, everything in here is so mocked out I wasn't sure how to actually test it, or even what I should be testing. I'm also surprised it passed, had it failed I probably would have just removed it.

app.execute
expect(Reek::CLI::Command::ReportCommand).to have_received(:new).
with(sources: [$stdin],
configuration: Reek::Configuration::AppConfiguration,
options: Reek::CLI::Options)
end
end
end

context 'when no source files given and no input was piped' do
Expand Down