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

Introduce suspenders:advisories generator #1138

Merged
merged 1 commit into from
Nov 28, 2023
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 NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Unreleased
* Introduce `suspenders:accessibility` generator
* Introduce `suspenders:inline_svg` generator
* Introduce `suspenders:factories` generator
* Introduce `suspenders:advisories` generator

20230113.0 (January, 13, 2023)

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ 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

### Advisories

Show security advisories during development.

Uses the [bundler-audit][] gem to update the local security database and
show any relevant issues with the app's dependencies. This generator is
only responsible for installing the gem and adding the Rake task.

`bin/rails g suspenders:advisories`

[bundler-audit]: https://github.com/rubysec/bundler-audit

### Factories

Build test data with clarity and ease.
Expand Down
27 changes: 27 additions & 0 deletions lib/generators/suspenders/advisories_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Suspenders
module Generators
class AdvisoriesGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates/advisories", __FILE__)
desc(<<~TEXT)
Show security advisories during development.

Uses the `bundler-audit` gem to update the local security database and
show any relevant issues with the app's dependencies via a Rake task.
TEXT

def add_bundler_audit
gem_group :development, :test do
gem "bundler-audit", ">= 0.7.0", require: false
end
Bundler.with_unbundled_env { run "bundle install" }
end

def modify_rakefile
insert_into_file "Rakefile", "\nrequire \"bundler/audit/task\"",
after: 'require_relative "config/application"'
insert_into_file "Rakefile", "\nBundler::Audit::Task.new",
after: 'require "bundler/audit/task"'
end
end
end
end
81 changes: 81 additions & 0 deletions test/generators/suspenders/advisories_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require "test_helper"
require "generators/suspenders/advisories_generator"

module Suspenders
module Generators
class AdvisoriesGeneratorTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::AdvisoriesGenerator
destination Rails.root
setup :prepare_destination
teardown :restore_destination

test "adds gems to Gemfile" do
expected_output = <<~RUBY
group :development, :test do
gem "bundler-audit", ">= 0.7.0", require: false
end
RUBY

run_generator

assert_file app_root("Gemfile") do |file|
assert_match(expected_output, file)
end
end

test "installs gems with Bundler" do
output = run_generator

assert_match(/bundle install/, output)
end

test "generator has a description" do
description = <<~TEXT
Show security advisories during development.

Uses the `bundler-audit` gem to update the local security database and
show any relevant issues with the app's dependencies via a Rake task.
TEXT

assert_equal description, Suspenders::Generators::AdvisoriesGenerator.desc
end

test "modifies Rakefile" do
touch "Rakefile"
content = <<~TEXT
require_relative "config/application"

Rails.application.load_tasks
TEXT
File.open(app_root("Rakefile"), "w") { _1.write content }
expected_rakefile = <<~TEXT
require_relative "config/application"
require "bundler/audit/task"
Bundler::Audit::Task.new

Rails.application.load_tasks
TEXT

run_generator

assert_file app_root("Rakefile") do |file|
assert_equal expected_rakefile, file
end
end

private

def prepare_destination
touch "Gemfile"
backup_file "Rakefile"
end

def restore_destination
remove_file_if_exists "Gemfile"
restore_file "Rakefile"
end
end
end
end
2 changes: 0 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ class Application < Rails::Application
restore_file "config/application.rb"
end

private

def backup_file(file)
FileUtils.mv app_root(file), app_root("#{file}.bak")
touch file
Expand Down