-
-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
suspenders:advisories
generator
Show security advisories during development. Uses the [bundler-audit][] gem and rake task to update the local security database and show any relevant issues with the app's dependencies. This happens on every test run and interaction with `bin/rake` and `bin/rails`. [bundler-audit]: https://github.com/rubysec/bundler-audit
- Loading branch information
1 parent
5a6b6e4
commit 9e2e4f5
Showing
6 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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 and rake task to update the local security | ||
database and show any relevant issues with the app's dependencies. This happens | ||
on every test run and interaction with `bin/rake` and `bin/rails`. | ||
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 configurea_rake_task | ||
copy_file "bundler_audit.rake", "lib/tasks/bundler_audit.rake" | ||
append_file "Rakefile", %(\ntask default: "bundle:audit"\n) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
if Rails.env.development? || Rails.env.test? | ||
require "bundler/audit/task" | ||
Bundler::Audit::Task.new | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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 | ||
Bundler.stubs(:with_unbundled_env).yields | ||
generator.expects(:run).with("bundle install").once | ||
|
||
capture(:stdout) do | ||
generator.add_bundler_audit | ||
end | ||
end | ||
|
||
test "generator has a description" do | ||
description = <<~TEXT | ||
Show security advisories during development. | ||
Uses the `bundler-audit` gem and rake task to update the local security | ||
database and show any relevant issues with the app's dependencies. This happens | ||
on every test run and interaction with `bin/rake` and `bin/rails`. | ||
TEXT | ||
|
||
assert_equal description, Suspenders::Generators::AdvisoriesGenerator.desc | ||
end | ||
|
||
test "creates custom Rake task" do | ||
expected_rake_task = <<~RUBY | ||
if Rails.env.development? || Rails.env.test? | ||
require "bundler/audit/task" | ||
Bundler::Audit::Task.new | ||
end | ||
RUBY | ||
|
||
run_generator | ||
|
||
assert_file app_root("lib/tasks/bundler_audit.rake") do |file| | ||
assert_match expected_rake_task, file | ||
end | ||
end | ||
|
||
test "modidies existing Rakefile" do | ||
run_generator | ||
|
||
assert_file app_root("Rakefile") do |file| | ||
assert_match(/task default: "bundle:audit"/, file) | ||
:w | ||
end | ||
end | ||
|
||
private | ||
|
||
def prepare_destination | ||
touch "Gemfile" | ||
backup_file "Rakefile" | ||
end | ||
|
||
def restore_destination | ||
remove_file_if_exists "Gemfile" | ||
remove_file_if_exists "lib/tasks/bundler_audit.rake" | ||
restore_file "Rakefile" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters