From 70e5e66415968b23ef83769453d361703c4ba2b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20Mari=C3=A9?= Date: Fri, 10 Nov 2023 15:52:33 +0100 Subject: [PATCH] WIP Introduce `suspenders:lint` generator - [x] Add standard gem - [x] Add rake task - [ ] Add standard to CI - [ ] Test on new app --- Gemfile.lock | 1 + NEWS.md | 1 + README.md | 6 ++ lib/generators/suspenders/lint_generator.rb | 14 ++++ test/dummy/Rakefile | 6 -- .../suspenders/lint_generator_test.rb | 66 +++++++++++++++++++ 6 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 lib/generators/suspenders/lint_generator.rb delete mode 100644 test/dummy/Rakefile create mode 100644 test/generators/suspenders/lint_generator_test.rb diff --git a/Gemfile.lock b/Gemfile.lock index 3e7988d55..392a344f5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -231,6 +231,7 @@ GEM PLATFORMS arm64-darwin-21 + arm64-darwin-22 x86_64-linux DEPENDENCIES diff --git a/NEWS.md b/NEWS.md index 44770f5bd..6a5bcc4eb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,7 @@ Unreleased * Remove `suspenders` system executable * Introduce `suspenders:accessibility` generator * Introduce `Suspenders::Generators::APIAppUnsupported` module and concern +* Introduce `suspenders:lint` generator 20230113.0 (January, 13, 2023) diff --git a/README.md b/README.md index 27d95fa2d..62990f755 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,12 @@ Installs [capybara_accessibility_audit] and [capybara_accessible_selectors] [capybara_accessibility_audit]: https://github.com/thoughtbot/capybara_accessibility_audit [capybara_accessible_selectors]: https://github.com/citizensadvice/capybara_accessible_selectors +### Lint + +Installs [standard]. + + [standard]: https://github.com/standardrb/standard + ## Contributing See the [CONTRIBUTING] document. diff --git a/lib/generators/suspenders/lint_generator.rb b/lib/generators/suspenders/lint_generator.rb new file mode 100644 index 000000000..7ee5f8379 --- /dev/null +++ b/lib/generators/suspenders/lint_generator.rb @@ -0,0 +1,14 @@ +module Suspenders + module Generators + class LintGenerator < Rails::Generators::Base + desc "Sets up standard" + + def setup_standard + gem "standard", group: [:development, :test] + Bundler.with_unbundled_env { run "bundle install" } + prepend_to_file("Rakefile", 'require "standard/rake"') + end + end + end +end + diff --git a/test/dummy/Rakefile b/test/dummy/Rakefile deleted file mode 100644 index 9a5ea7383..000000000 --- a/test/dummy/Rakefile +++ /dev/null @@ -1,6 +0,0 @@ -# Add your own tasks in files placed in lib/tasks ending in .rake, -# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. - -require_relative "config/application" - -Rails.application.load_tasks diff --git a/test/generators/suspenders/lint_generator_test.rb b/test/generators/suspenders/lint_generator_test.rb new file mode 100644 index 000000000..85ec543d3 --- /dev/null +++ b/test/generators/suspenders/lint_generator_test.rb @@ -0,0 +1,66 @@ +require "test_helper" +require "generators/suspenders/lint_generator" + +module Suspenders + module Generators + class LintGeneratorTest < Rails::Generators::TestCase + include Suspenders::TestHelpers + + tests Suspenders::Generators::LintGenerator + destination Rails.root + setup :prepare_destination + teardown :restore_destination + + test "adds gem to Gemfile" do + expected_output = <<~RUBY + gem "standard", group: [:development, :test] + RUBY + + run_generator + + assert_file app_root("Gemfile") do |file| + assert_match(expected_output, file) + end + end + + test "adds require to Rakefile" do + expected_output = <<~RUBY + require "standard/rake" + RUBY + + run_generator + + assert_file app_root("Rakefile") do |file| + assert_match(expected_output.strip, file.strip) + end + end + + test "installs gem with Bundler" do + Bundler.stubs(:with_unbundled_env).yields + generator.expects(:run).with("bundle install").once + + capture(:stdout) do + generator.setup_standard + end + end + + test "generator has a description" do + description = "Sets up standard" + + assert_equal description, Suspenders::Generators::LintGenerator.desc + end + + private + + def prepare_destination + touch "Gemfile" + touch "Rakefile" + end + + def restore_destination + remove_file_if_exists "Gemfile" + remove_file_if_exists "Rakefile" + end + end + end +end