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

Added the Ability to Generate a todo File #155

Merged
merged 23 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
66e7e52
WIP: Adding todo file.
mrbiggred Dec 7, 2019
0bcb871
WIP: renamed to gen-ignore.
mrbiggred Dec 10, 2019
dd4db5b
Add test for finding gen-ignore runner.
mrbiggred Dec 10, 2019
9c91ef4
WIP: Test for merging todo is working.
mrbiggred Dec 10, 2019
9dab4dc
Remove commented out test.
mrbiggred Dec 12, 2019
22732b0
Test for Genignore runner.
mrbiggred Dec 14, 2019
facc4b4
Added generating todo file to the ReadMe.
mrbiggred Dec 14, 2019
bc74d1d
Reword using --todo flag in Readme.
mrbiggred Dec 18, 2019
6b62bda
Renamed temp exclude file to "temp_exclude".
mrbiggred Dec 18, 2019
399c014
Removed some commented out code.
mrbiggred Dec 18, 2019
ebe807c
Fixed type in test fixture.
mrbiggred Dec 18, 2019
502e296
Fixed gen ignore test name.
mrbiggred Dec 18, 2019
6197306
Change file exists test assert to be more readable.
mrbiggred Dec 18, 2019
2f7436e
Fixed issue with generating ignore file in Ruby 2.4.1.
mrbiggred Jan 2, 2020
5dba9e2
Fixed Rubocop caching the working directory during tests.
mrbiggred Jan 31, 2020
cf90f27
Fixed typo in Readme.
mrbiggred Feb 6, 2020
75f112c
Refactor the load yaml config.
mrbiggred Feb 6, 2020
d1fa3b6
Changed cli option to --generate-todo.
mrbiggred Feb 6, 2020
f6e56ad
Print Todo warning.
mrbiggred Apr 9, 2020
3f80ba3
Fixed a failing merge settings test.
mrbiggred Apr 10, 2020
d1b9976
Use temp file when generating ignore file.
mrbiggred Apr 14, 2020
241a412
Merge branch 'master' into feature-add-todo-file
searls May 6, 2020
1099958
Fixed issue with Rubocop config settings leaking between tests.
mrbiggred May 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ $ bundle exec standardrb "lib/**/*.rb" test
**Note:** by default, StandardRB will look for all `*.rb` files (and some other
files typically associated with Ruby like `*.gemspec` and `Gemfile`)

If you have an existing project but aren't ready to fix all the files yet you can
generate a todo file:

```bash
$ bundle exec standardrb --generate-todo
```

This will create a `.standard_todo.yml` that lists all the files that contain errors.
When you run Standard in the future it will ignore these files as if they lived under the
`ignore` section in the `.standard.yml` file.

As you refactor your existing project you can remove files from the list. You can
also regenerate the todo file at anytime by re-running the above command.

### Using with Rake

Standard also ships with Rake tasks. If you're using Rails, these should
Expand Down Expand Up @@ -141,6 +155,8 @@ cannot be found by ascending the current working directory (i.e. against a
temporary file buffer in your editor), you can specify the config location with
`--config path/to/.standard.yml`.

Similarly, for the `.standard_todo.yml` file, you can specify `--todo path/to/.standard_todo.yml`.

## What you might do if you're REALLY clever

Because StandardRB is essentially a wrapper on top of
Expand Down
22 changes: 19 additions & 3 deletions lib/standard/loads_yaml_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,29 @@ def initialize
end

def call(argv, search_path)
yaml_path = @parses_cli_option.call(argv, "--config") ||
FileFinder.new.call(".standard.yml", search_path)
construct_config(yaml_path, load_standard_yaml(yaml_path))
standard_yaml_path = determine_yaml_file(argv, search_path, "--config", ".standard.yml")
todo_yaml_path = determine_yaml_file(argv, search_path, "--todo", ".standard_todo.yml")

standard_yaml = load_standard_yaml(standard_yaml_path)
todo_yaml = load_standard_yaml(todo_yaml_path)

merge_ignore(standard_yaml, todo_yaml)
construct_config(standard_yaml_path, standard_yaml)
end

private

def merge_ignore(merge_into_yaml, merge_from_yaml)
merge_into_yaml["ignore"] = arrayify(merge_into_yaml["ignore"])
merge_from_yaml["ignore"] = arrayify(merge_from_yaml["ignore"])

merge_into_yaml["ignore"] = merge_into_yaml["ignore"] + merge_from_yaml["ignore"]
end

def determine_yaml_file(argv, search_path, option_name, default_file)
@parses_cli_option.call(argv, option_name) || FileFinder.new.call(default_file, search_path)
end

def load_standard_yaml(yaml_path)
if yaml_path
YAML.load_file(yaml_path) || {}
Expand Down
4 changes: 3 additions & 1 deletion lib/standard/merges_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call(argv, standard_yaml)

def separate_argv(argv)
argv.partition { |flag|
["--fix", "--no-fix", "--version", "-v", "--help", "-h"].include?(flag)
["--generate-todo", "--fix", "--no-fix", "--version", "-v", "--help", "-h"].include?(flag)
}
end

Expand All @@ -41,6 +41,8 @@ def determine_command(argv)
:help
elsif (argv & ["--version", "-v"]).any?
:version
elsif (argv & ["--generate-todo"]).any?
:genignore
else
:rubocop
end
Expand Down
43 changes: 43 additions & 0 deletions lib/standard/runners/genignore.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "yaml"
require_relative "rubocop"

module Standard
module Runners
class Genignore
def call(config)
# Run Rubocop to generate the files with errors.
config.rubocop_options[:formatters] = [["files", "temp_exclude.txt"]]
config.rubocop_options[:format] = "files"
config.rubocop_options[:out] = "temp_exclude.txt"

File.delete("exclude.txt") if File.exist?("temp_exclude.txt")
Copy link
Contributor

Choose a reason for hiding this comment

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

What's exclude.txt? It's only mentioned here.

As for temp_exclude.txt, I don't love spitting out a temporary file into a person's project directory, because if there's a failure it'll sit there and they won't know what it is. Maybe it'd be better to use Ruby's tempfile instead?

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've updated to the code to use Ruby's tempfile. Thanks for the tip.

Runners::Rubocop.new.call(config)

# Read in the files with errors. It will have the absolute paths
# of the files but we only want the relative path.
files_with_errors = File.open("temp_exclude.txt").readlines.map(&:chomp)
files_with_errors.map! do |file|
# Get the relative file path. Don't use the
# relative_path_from method as it will raise an
# error in Ruby 2.4.1 and possibly other versions.
#
# https://bugs.ruby-lang.org/issues/10011
#
file.sub(Dir.pwd + File::SEPARATOR, "")
end

yaml_format_errors = {"ignore" => files_with_errors}

# Regenerate the todo file.
File.open(".standard_todo.yml", "w") do |file|
file.puts "# Auto generated files with errors to ignore."
file.puts "# Remove from this list as you refactor files."
file.write(yaml_format_errors.to_yaml)
end

# Clean up
File.delete("temp_exclude.txt") if File.exist?("temp_exclude.txt")
end
end
end
end
3 changes: 3 additions & 0 deletions test/fixture/config/u/.standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ignore:
- none_todo_path/**/*
- none_todo_file.rb
3 changes: 3 additions & 0 deletions test/fixture/config/u/.standard_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ignore:
- todo_file_one.rb
- todo_file_two.rb
9 changes: 9 additions & 0 deletions test/fixture/genignore/errors_one.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ErrorsOne
def stuff(id)
if bar = Bar.find(id)
bar.nice!
else
false
end
end
end
1 change: 1 addition & 0 deletions test/fixture/genignore/errors_two.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
useless_assignment = "LOL"
8 changes: 8 additions & 0 deletions test/fixture/genignore/no_errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

# Has no errors
class NoErrors
def add(aaaa, bbbb)
aaaa + bbbb
end
end
13 changes: 13 additions & 0 deletions test/standard/builds_config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ def test_specified_standard_yaml_raises
assert_match(/Configuration file ".*fake\.file" not found/, err.message)
end

def test_todo_merged
result = @subject.call([], path("test/fixture/config/u"))

assert_equal DEFAULT_OPTIONS, result.rubocop_options

assert_equal config_store("test/fixture/config/u").dup.tap { |config_store|
config_store["AllCops"]["Exclude"] |= [path("test/fixture/config/u/none_todo_path/**/*")]
config_store["AllCops"]["Exclude"] |= [path("test/fixture/config/u/none_todo_file.rb")]
config_store["AllCops"]["Exclude"] |= [path("test/fixture/config/u/todo_file_one.rb")]
config_store["AllCops"]["Exclude"] |= [path("test/fixture/config/u/todo_file_two.rb")]
}, result.rubocop_config_store.for("").to_h
end

private

def config_store(config_root = nil, rubocop_yml = "config/base.yml", ruby_version = RUBY_VERSION)
Expand Down
4 changes: 4 additions & 0 deletions test/standard/merges_settings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def test_version_sets_command_to_version
assert_equal :version, @subject.call(["-v"], {}).runner
end

def test_gen_ignore_sets_command_to_gen_ignore
assert_equal :genignore, @subject.call(["--generate-todo"], {}).runner
end

def test_fix_flag_sets_auto_correct_options
options = @subject.call(["--fix"], {}).options

Expand Down
33 changes: 33 additions & 0 deletions test/standard/runners/genignore_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "test_helper"

require "standard/runners/genignore"
require "standard/runners/rubocop"

class Standard::Runners::GenignoreTest < UnitTest
def setup
super

@subject = Standard::Runners::Genignore.new
end

def test_todo_generated
FileUtils.rm_rf("tmp/genignore_test")
FileUtils.mkdir_p("tmp/genignore_test")
FileUtils.cp_r("test/fixture/genignore/.", "tmp/genignore_test")

Dir.chdir("tmp/genignore_test") do
@subject.call(create_config)
end

assert File.exist?("tmp/genignore_test/.standard_todo.yml")

expected_yaml = {"ignore" => %w[errors_one.rb errors_two.rb]}
assert_equal expected_yaml, YAML.load_file("tmp/genignore_test/.standard_todo.yml")
end

private

def create_config
Standard::Config.new(nil, ["."], {}, RuboCop::ConfigStore.new)
end
end
2 changes: 2 additions & 0 deletions test/standard/runners/rubocop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Standard::Runners::RubocopTest < UnitTest
}.freeze

def setup
super

@subject = Standard::Runners::Rubocop.new
end

Expand Down
13 changes: 13 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ def self.path(relative)
Pathname.new(Dir.pwd).join(relative).to_s
end

def setup
# Rubocop will cache and reuse the current working directory, which can
# be different the actual working directory. This can mess up tests that
# use the quite/simple formatter as they can write out either relative or
# absolute paths. This is not in the Rubocop documentation but can be
# found in this PR:
#
# https://github.com/rubocop-hq/rubocop/pull/262#issuecomment-19265766
#
# This forces RuboCop to use current working directory.
RuboCop::PathUtil.reset_pwd
end

def teardown
Gimme.reset
end
Expand Down