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

Ignores .rubycritic.yml file #303

Open
cvoltz opened this issue Mar 21, 2019 · 2 comments
Open

Ignores .rubycritic.yml file #303

cvoltz opened this issue Mar 21, 2019 · 2 comments
Labels

Comments

@cvoltz
Copy link
Contributor

cvoltz commented Mar 21, 2019

I tried testing with the example .rubycritic.yml configuration file:

mode_ci:
  enabled: true # default is false
  branch: 'production' # default is master
branch: 'production' # default is master
path: '/tmp/mycustompath' # Set path where report will be saved (tmp/rubycritic by default)
threshhold_score: 10 # default is 0
deduplicate_symlinks: true # default is false
suppress_ratings: true # default is false
no_browser: true # default is false
format: console # Available values are: html, json, console, lint. Default value is html.
minimum_score: 95 # default is 0
paths: # Files to analyse.
  - 'app/controllers/'
  - 'app/models/'

Changes in the configuration file (e.g., no_browser, path, and paths), don't affect the settings rubycritic actually uses when it runs. It appears to always use the default settings (i.e., as if the .rubycritic.yml wasn't present) unless command line arguments are specified (then the CLI setting is used). When rubycritic runs, it does read the configuration file. It just appears to ignore the settings.

Also, it looks like the documented format option for the config file has changed to formats but the documentation hasn't been updated to match (see RubyCritic::Cli::Options::File::formats).

@Adre
Copy link
Contributor

Adre commented Aug 2, 2019

I think this is just a simple typo in your config caused by a typo in the example config from the README. I had some issues until I updated the threshold_score key.

I've submitted a pull request to address this: #315

cvoltz added a commit to cvoltz/rubycritic that referenced this issue Aug 2, 2019
Fixes: Ignores .rubycritic.yml file whitesmith#303
whitesmith#303.

Update the documentation to indicate that the key in the
.rubycritic.yml configuration file which controls the output
format is 'formats' rather than 'format'.

Fix a bug in RubyCritic::Cli::Options#to_h which prevents a
setting in the YAML configuration file from being used when the
setting is an Array (as is the case for the output formats).

Modify RubyCritic::CLI::Options::File#formats to return an array
of symbols instead of an array of strings, to be consistent with
how RubyCritic::CLI::Options::Argv#parse returns the array. Also
modify it so it can accept a single output format or a list of
formats. The documentation indicates that either is acceptable
but the code assumed the input was always an array.

Signed-off-by: Christopher Voltz <[email protected]>
@cvoltz
Copy link
Contributor Author

cvoltz commented Aug 2, 2019

It would be nice if RubyCritic would give an error message if the configuration file was incorrect, rather than just silently ignoring mistakes.

Fixing or removing the line doesn't get RubyCritic to use the other settings so the problem is more then just a single key being wrong.

I looked into the issue some more and it looks like format should be formats as well:

formats = options['formats'] || []

However, fixing the entry in the rubycritic.yml file then results in:

Traceback (most recent call last):
        8: from /home/cvoltz/.rvm/gems/ruby-2.6.3/bin/ruby_executable_hooks:24:in `<main>'
        7: from /home/cvoltz/.rvm/gems/ruby-2.6.3/bin/ruby_executable_hooks:24:in `eval'
        6: from /home/cvoltz/.rvm/gems/ruby-2.6.3/bin/rubycritic:23:in `<main>'
        5: from /home/cvoltz/.rvm/gems/ruby-2.6.3/bin/rubycritic:23:in `load'
        4: from /home/cvoltz/.rvm/gems/ruby-2.6.3/gems/rubycritic-4.1.0/bin/rubycritic:10:in `<top (required)>'
        3: from /home/cvoltz/.rvm/gems/ruby-2.6.3/gems/rubycritic-4.1.0/lib/rubycritic/cli/application.rb:19:in `execute'
        2: from /home/cvoltz/.rvm/gems/ruby-2.6.3/gems/rubycritic-4.1.0/lib/rubycritic/cli/options.rb:24:in `to_h'
        1: from /home/cvoltz/.rvm/gems/ruby-2.6.3/gems/rubycritic-4.1.0/lib/rubycritic/cli/options/file.rb:25:in `to_h'
/home/cvoltz/.rvm/gems/ruby-2.6.3/gems/rubycritic-4.1.0/lib/rubycritic/cli/options/file.rb:80:in `formats': private method `select' called for "console":String (NoMethodError)

That problem is caused by the code assuming formats will always be an array:

formats.select do |format|
%w[html json console lint].include?(format)
end

So either the sample configuration file needs to show formats as an array:

formats:
  - console

or RubyCritic::Cli::Options::File#formats needs to be modified to ensure formats is always an array:

        def formats
-         formats = options['formats'] || []
+         formats = Array(options['formats'])
          formats.select do |format|
            %w[html json console lint].include?(format)
          end
        end

Unfortunately, even that is not enough to fix this problem because there is also a bug in RubyCritic::Cli::Options#to_h when it merges the hash of options it has read from the configuration file with those specified on the command line:

def to_h
file_hash = file_options.to_h
argv_hash = argv_options.to_h
file_hash.merge(argv_hash) do |_, file_option, argv_option|
argv_option.nil? ? file_option : argv_option
end
end

argv_option[:formats] is an empty array when it isn't specified on the CLI but any empty array isn't nil so the empty array from the CLI options (argv_option) overwrites the setting in the configuration file (file_hash) even when it is specified. This can be fixed with an extra check to see if the argument is an empty array:

      def to_h
        file_hash = file_options.to_h
        argv_hash = argv_options.to_h

        file_hash.merge(argv_hash) do |_, file_option, argv_option|
-         argv_option.nil? ? file_option : argv_option
+         Array(argv_option).empty? ? file_option : argv_option
        end
      end

Note that argv_option can be an Array, a Float, an Integer, or a String. Casting it to an Array allows a simple check for whether the argument is nil or an empty array without having to check its type.

Finally, it appears that the formats must be symbols in the array, rather then strings. If we look at RubyCritic::CLI::Options::Argv#parse we see that it always returns a symbol:

opts.on(
'-f', '--format [FORMAT]',
%i[html json console lint],
'Report smells in the given format:',
' html (default; will open in a browser)',
' json',
' console',
' lint',
'Multiple formats are supported.'
) do |format|
formats << format
end

So, we need to make another change to RubyCritic::Cli::Options::File#formats to get it to also return a symbol:

        def formats
          formats = Array(options['formats'])
          formats.select do |format|
            %w[html json console lint].include?(format)
-         end
+         end.map(&:to_sym)
        end

Created pull request #316 to fix the problems.

nunosilva800 pushed a commit that referenced this issue Oct 13, 2019
Fixes: Ignores .rubycritic.yml file #303
#303.

Update the documentation to indicate that the key in the
.rubycritic.yml configuration file which controls the output
format is 'formats' rather than 'format'.

Fix a bug in RubyCritic::Cli::Options#to_h which prevents a
setting in the YAML configuration file from being used when the
setting is an Array (as is the case for the output formats).

Modify RubyCritic::CLI::Options::File#formats to return an array
of symbols instead of an array of strings, to be consistent with
how RubyCritic::CLI::Options::Argv#parse returns the array. Also
modify it so it can accept a single output format or a list of
formats. The documentation indicates that either is acceptable
but the code assumed the input was always an array.

Signed-off-by: Christopher Voltz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants