-
-
Notifications
You must be signed in to change notification settings - Fork 529
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:accessibility
generator
#1137
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,17 @@ | ||
module Suspenders | ||
module Generators | ||
class AccessibilityGenerator < Rails::Generators::Base | ||
include Suspenders::Generators::APIAppUnsupported | ||
|
||
desc "Installs capybara_accessibility_audit and capybara_accessible_selectors" | ||
|
||
def add_capybara_gems | ||
gem_group :test do | ||
gem "capybara_accessibility_audit" | ||
gem "capybara_accessible_selectors", github: "citizensadvice/capybara_accessible_selectors" | ||
end | ||
Bundler.with_unbundled_env { run "bundle install" } | ||
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
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,30 @@ | ||
require "active_support/concern" | ||
|
||
module Suspenders | ||
module Generators | ||
module APIAppUnsupported | ||
class Error < StandardError | ||
def message | ||
"This generator cannot be used on API only applications." | ||
end | ||
end | ||
|
||
extend ActiveSupport::Concern | ||
|
||
included do | ||
def raise_if_api_only_app | ||
if api_only_app? | ||
raise Suspenders::Generators::APIAppUnsupported::Error | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def api_only_app? | ||
File.read(Rails.root.join("config/application.rb")) | ||
.match?(/^\s*config\.api_only\s*=\s*true/i) | ||
end | ||
end | ||
end | ||
end |
83 changes: 83 additions & 0 deletions
83
test/generators/suspenders/accessibility_generator_test.rb
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,83 @@ | ||
require "test_helper" | ||
require "generators/suspenders/accessibility_generator" | ||
|
||
module Suspenders | ||
module Generators | ||
class AccessibilityGeneratorTest < Rails::Generators::TestCase | ||
include Suspenders::TestHelpers | ||
|
||
tests Suspenders::Generators::AccessibilityGenerator | ||
destination Rails.root | ||
setup :prepare_destination | ||
teardown :restore_destination | ||
|
||
test "raises if API only application" do | ||
within_api_only_app do | ||
assert_raises Suspenders::Generators::APIAppUnsupported::Error do | ||
run_generator | ||
end | ||
|
||
assert_file app_root("Gemfile") do |file| | ||
assert_no_match "capybara_accessibility_audit", file | ||
assert_no_match "capybara_accessible_selectors", file | ||
end | ||
end | ||
end | ||
|
||
test "does not raise if API configuration is commented out" do | ||
within_api_only_app do | ||
path = app_root("config/application.rb") | ||
content = File.binread(path).gsub!("config.api_only = true", "# config.api_only = true") | ||
File.binwrite(path, content) | ||
|
||
run_generator | ||
|
||
assert_file app_root("Gemfile") do |file| | ||
assert_match "capybara_accessibility_audit", file | ||
assert_match "capybara_accessible_selectors", file | ||
end | ||
end | ||
end | ||
|
||
test "adds gems to Gemfile" do | ||
expected_output = <<~RUBY | ||
group :test do | ||
gem "capybara_accessibility_audit" | ||
gem "capybara_accessible_selectors", github: "citizensadvice/capybara_accessible_selectors" | ||
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_capybara_gems | ||
end | ||
end | ||
|
||
test "generator has a description" do | ||
description = "Installs capybara_accessibility_audit and capybara_accessible_selectors" | ||
|
||
assert_equal description, Suspenders::Generators::AccessibilityGenerator.desc | ||
end | ||
|
||
private | ||
|
||
def prepare_destination | ||
touch "Gemfile" | ||
end | ||
|
||
def restore_destination | ||
remove_file_if_exists "Gemfile" | ||
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,11 @@ | ||
require "test_helper" | ||
|
||
class Suspenders::GeneratorsTest < ActiveSupport::TestCase | ||
class APIAppUnsupportedTest < Suspenders::GeneratorsTest | ||
test "message returns a custom message" do | ||
expected = "This generator cannot be used on API only applications." | ||
|
||
assert_equal expected, Suspenders::Generators::APIAppUnsupported::Error.new.message | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will run first when we call
include Suspenders::Generators::APIAppUnsupported
in a generator. This is important because we don't want to invoke any of the generator's methods.