ammeter-1.1.5/ 0000755 0000041 0000041 00000000000 14022221032 013164 5 ustar www-data www-data ammeter-1.1.5/README.md 0000644 0000041 0000041 00000010512 14022221032 014442 0 ustar www-data www-data # Ammeter [](https://github.com/alexrothenberg/ammeter/actions/workflows/ci.yml) [](https://codeclimate.com/github/alexrothenberg/ammeter) [](http://badge.fury.io/rb/ammeter)
A gem that makes it easy to write specs for your Rails 3 Generators.
RSpec is using ammeter to
[spec](https://github.com/rspec/rspec-rails/blob/master/spec/generators/rspec/model/model_generator_spec.rb)
[its](https://github.com/rspec/rspec-rails/blob/master/spec/generators/rspec/controller/controller_generator_spec.rb)
[own](https://github.com/rspec/rspec-rails/blob/master/spec/generators/rspec/helper/helper_generator_spec.rb)
[generators](https://github.com/rspec/rspec-rails/blob/master/spec/generators/rspec/scaffold/scaffold_generator_spec.rb)
and we think you may find it useful too.
An [ammeter](http://en.wikipedia.org/wiki/Ammeter) is used to measure electrical current and
electricity can be produced by a generator.
# Installation
Add this line to your Gemfile (or gemspec):
```ruby
gem 'ammeter'
```
And then execute:
```bash
$ bundle
```
Add:
```ruby
require 'ammeter/init'
```
To your `spec/spec_helper.rb`.
# Example
```ruby
require 'spec_helper'
# Generators are not automatically loaded by Rails
require 'generators/rspec/model/model_generator'
describe Rspec::Generators::ModelGenerator, :type => :generator do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path("../../../../../tmp", __FILE__)
before do
prepare_destination
end
# using mocks to ensure proper methods are called
# invoke_all - will call all the tasks in the generator
it 'should run all tasks in the generator' do
gen = generator %w(posts)
gen.should_receive :create_model_spec
gen.should_receive :create_fixture_file
capture(:stdout) { gen.invoke_all }
end
# invoke_task - will call just the named task in the generator
it 'should run a specific tasks in the generator' do
gen = generator %w(posts)
gen.should_receive :create_model_spec
gen.should_not_receive :create_fixture_file
capture(:stdout) { gen.invoke_task :create_model_spec }
end
# custom matchers make it easy to verify what the generator creates
describe 'the generated files' do
before do
run_generator %w(posts)
end
describe 'the spec' do
# file - gives you the absolute path where the generator will create the file
subject { file('spec/models/posts_spec.rb') }
# is_expected_to exist - verifies the file exists
it { is_expected_to exist }
# is_expected_to contain - verifies the file's contents
it { is_expected_to contain /require 'spec_helper'/ }
it { is_expected_to contain /describe Posts/ }
end
describe 'the migration' do
subject { migration_file('db/migrate/create_posts.rb') }
# is_expected_to be_a_migration - verifies the file exists with a migration timestamp as part of the filename
it { is_expected_to exist }
it { is_expected_to contain /create_table/ }
end
end
end
```
# Available matchers
- `exist` - verifies the file exists
- `contain` - verifies the file's contents
- `be_a_migration` - verifies the file exists with a migration timestamp as part of the filename
- `have_method` - verifies the file (or a class withing it) implements a method
- `have_correct_syntax` - verifies the file has correct syntax and is not broken (works for .rb, .erb and .haml files)
# Contributing
* Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
* Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
* Fork the project
* Start a feature/bugfix branch
* Commit and push until you are happy with your contribution
* Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
# Copyright
Copyright © 2011 Alex Rothenberg. See LICENSE.txt for further details.
ammeter-1.1.5/spec/ 0000755 0000041 0000041 00000000000 14022221032 014116 5 ustar www-data www-data ammeter-1.1.5/spec/spec_helper.rb 0000644 0000041 0000041 00000001127 14022221032 016735 0 ustar www-data www-data require 'rails/all'
require 'rspec/rails'
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
module TestApp
class Application < Rails::Application
config.root = File.dirname(__FILE__)
end
end
require 'ammeter/init'
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
RSpec.configure do |c|
c.include MatchesForRSpecRailsSpecs
if RSpec::Core::Version::STRING < '3'
c.include RSpec2MemoizedHelpersCompatibility
end
end
def stub_file(filename, content)
allow(File).to receive(:read).with(filename).and_return(content)
end
ammeter-1.1.5/spec/support/ 0000755 0000041 0000041 00000000000 14022221032 015632 5 ustar www-data www-data ammeter-1.1.5/spec/support/matchers.rb 0000644 0000041 0000041 00000001042 14022221032 017762 0 ustar www-data www-data # Copied from rspec-rails spec/support/matchers.rb
module MatchesForRSpecRailsSpecs
extend RSpec::Matchers::DSL
matcher :be_included_in_files_in do |path|
match do |mod|
if RSpec::Core::Version::STRING >= '3'
stub_metadata(
:file_path => "#{path}whatever_spec.rb:15"
)
else
stub_metadata(
:example_group => {:file_path => "#{path}whatever_spec.rb:15"}
)
end
group = RSpec::Core::ExampleGroup.describe
group.included_modules.include?(mod)
end
end
end ammeter-1.1.5/spec/support/memoized_helpers_rspec2compatibility.rb 0000644 0000041 0000041 00000000357 14022221032 025567 0 ustar www-data www-data # rspec3 defines this method in RSpec::Core::MemoizedHelpers
# see https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/memoized_helpers.rb
module RSpec2MemoizedHelpersCompatibility
def is_expected
expect(subject)
end
end ammeter-1.1.5/spec/support/helpers.rb 0000644 0000041 0000041 00000000550 14022221032 017621 0 ustar www-data www-data module Helpers
def stub_metadata(additional_metadata)
stub_metadata = metadata_with(additional_metadata)
allow(RSpec::Core::ExampleGroup).to receive(:metadata) { stub_metadata }
end
def metadata_with(additional_metadata)
::RSpec.describe("example group").metadata.merge(additional_metadata)
end
RSpec.configure {|c| c.include self}
end
ammeter-1.1.5/spec/support/mock_generator.rb 0000644 0000041 0000041 00000000410 14022221032 021151 0 ustar www-data www-data # Used in generator_example_helpers_spec.rb
# set_shell_prompt_responses requires a generator w/ a shell
# as an argument. This class defines that generator w/ a shell.
class MockGenerator
attr_accessor :shell
def initialize
@shell = Object.new
end
end
ammeter-1.1.5/spec/ammeter/ 0000755 0000041 0000041 00000000000 14022221032 015550 5 ustar www-data www-data ammeter-1.1.5/spec/ammeter/rspec/ 0000755 0000041 0000041 00000000000 14022221032 016664 5 ustar www-data www-data ammeter-1.1.5/spec/ammeter/rspec/generator/ 0000755 0000041 0000041 00000000000 14022221032 020652 5 ustar www-data www-data ammeter-1.1.5/spec/ammeter/rspec/generator/matchers/ 0000755 0000041 0000041 00000000000 14022221032 022460 5 ustar www-data www-data ammeter-1.1.5/spec/ammeter/rspec/generator/matchers/have_method_spec.rb 0000644 0000041 0000041 00000001061 14022221032 026300 0 ustar www-data www-data require 'spec_helper'
describe 'have_method' do
let(:contents) { "class SomeClass\n def some_method\n puts 'Hello world!'\n end\nend" }
subject { '/some/file/path' }
before { allow(File).to receive(:read).with('/some/file/path').and_return(contents) }
it { is_expected.to have_method(:some_method) }
it { is_expected.to have_method(:some_method).containing("puts 'Hello world!'")}
it { is_expected.to_not have_method(:some_method).containing("puts 'Good bye cruel world!' ") }
it { is_expected.to_not have_method(:some_other_method) }
end
ammeter-1.1.5/spec/ammeter/rspec/generator/matchers/be_a_migration_spec.rb 0000644 0000041 0000041 00000001643 14022221032 026762 0 ustar www-data www-data require 'spec_helper'
describe "be_a_migration" do
let(:migration_files) { ['db/migrate/20110504132601_create_posts.rb', 'db/migrate/20110520132601_create_users.rb'] }
before do
migration_files.each do |migration_file|
allow(File).to receive(:exist?).with(migration_file).and_return(true)
end
end
it 'should find for the migration file adding the filename timestamp for us' do
expect(Dir).to receive(:glob).with('db/migrate/[0-9]*_*.rb').and_return(migration_files)
expect('db/migrate/create_users.rb').to be_a_migration
end
it 'should find for the migration file when we know the filename timestamp' do
expect('db/migrate/20110504132601_create_posts.rb').to be_a_migration
end
it 'should know when a migration does not exist' do
expect(Dir).to receive(:glob).with('db/migrate/[0-9]*_*.rb').and_return([])
expect('db/migrate/create_comments.rb').to_not be_a_migration
end
end
ammeter-1.1.5/spec/ammeter/rspec/generator/matchers/have_correct_syntax_spec.rb 0000644 0000041 0000041 00000005365 14022221032 030102 0 ustar www-data www-data require 'spec_helper'
describe 'have_correct_syntax' do
describe 'ruby files' do
subject { '/test.rb' }
it 'checks if Ruby file has a proper syntax' do
stub_file subject, "class SomeClass\n def some_method\n puts 'Hello world!'\n end\nend"
expect(subject).to have_correct_syntax
end
it 'checks if Ruby file has an incorrect syntax' do
stub_file subject, "class SomeClass\ndef some_method\nputs 'Hello world!'"
expect(subject).to_not have_correct_syntax
end
end
describe 'erb files' do
subject { '/test.erb' }
it 'checks if simple ERB file has a proper syntax' do
stub_file subject, '<% if nonexistent_var == 1 %><% end %>'
expect(subject).to have_correct_syntax
end
it 'checks if simple ERB file has an incorrect syntax' do
stub_file subject, '<% if 1 + 1 %> abc'
expect(subject).to_not have_correct_syntax
end
it 'checks if complex ERB file has a proper syntax' do
stub_file subject, <<-EOF
<% @elements.each do |element| %>
<%= element.id %> |
<%= element.title %> |
<%= link_to 'Show', element_path(element) %> |
<% end %>
EOF
expect(subject).to have_correct_syntax
end
it 'checks if complex ERB file has an incorrect syntax' do
stub_file subject, <<-EOF
This test should fail because I didn't close an each loop.
<% @elements.each do |element| %>
<%= element.id %> |
EOF
expect(subject).to_not have_correct_syntax
end
end
describe 'haml files' do
subject { '/test.haml' }
it 'checks if simple Haml file has a proper syntax' do
stub_file subject, <<-EOF
%a{:href => "#"}= @order.id
EOF
expect(subject).to have_correct_syntax
end
it 'checks if simple Haml file has an incorrect syntax' do
stub_file subject, <<-EOF
%a{
EOF
expect(subject).to_not have_correct_syntax
end
it 'checks if complex Haml file has a proper syntax' do
stub_file subject, <<-EOF
%header
%h1= application_title
#body
%table
- @elements.each do |element|
%tr
%td= element.id
%td= element.title
%td= link_to 'Show', element_path(element)
EOF
expect(subject).to have_correct_syntax
end
it 'checks if complex Haml file has an incorrect syntax' do
stub_file subject, <<-EOF
#broken-example
%p This test should fail because of broken indentation
- @elements.each do |element|
%tr
$td= element.id
EOF
expect(subject).to_not have_correct_syntax
end
end
end
ammeter-1.1.5/spec/ammeter/rspec/generator/matchers/exist_spec.rb 0000644 0000041 0000041 00000000574 14022221032 025161 0 ustar www-data www-data require 'spec_helper'
describe "exist" do
it 'passes when the file exists' do
allow(File).to receive(:exist?).with('/some/file/path').and_return(true)
expect('/some/file/path').to exist
end
it 'fails when the file does not exist' do
allow(File).to receive(:exist?).with('/some/file/path').and_return(false)
expect('/some/file/path').to_not exist
end
end
ammeter-1.1.5/spec/ammeter/rspec/generator/matchers/contain_spec.rb 0000644 0000041 0000041 00000001743 14022221032 025457 0 ustar www-data www-data require 'spec_helper'
describe "contain" do
context "when the file exists" do
let(:contents) { "This file\ncontains\nthis text" }
subject { '/some/file/path' }
before do
allow(File).to receive(:read).with('/some/file/path').and_return(contents)
end
it { is_expected.to contain "This file\ncontains\nthis text" }
it { is_expected.to contain "This file" }
it { is_expected.to contain "this text" }
it { is_expected.to contain /This file/ }
it { is_expected.to contain /this text/ }
it { is_expected.to contain "contains", /this text/ }
it { is_expected.to_not contain /something not there/ }
it { is_expected.to_not contain /this isn't at the contents/, /neither is this/ }
end
context "when the file is not there" do
it 'raises an error when the file does not exist' do
expect do
expect('some/file/that/does/not/exist').to contain 'something'
end.to raise_error /No such file or directory/
end
end
end
ammeter-1.1.5/spec/ammeter/rspec/generator/example/ 0000755 0000041 0000041 00000000000 14022221032 022305 5 ustar www-data www-data ammeter-1.1.5/spec/ammeter/rspec/generator/example/generator_example_helpers_spec.rb 0000644 0000041 0000041 00000002007 14022221032 031066 0 ustar www-data www-data require "spec_helper"
module Ammeter::RSpec::Rails
describe GeneratorExampleHelpers do
include GeneratorExampleHelpers
let(:generator) { MockGenerator.new }
it "mocks return value of ask to response" do
set_shell_prompt_responses(generator, { :ask => "response" })
expect(generator.shell.ask).to eq("response")
end
context "arguments contain multiple shell commands" do
it "mocks return values response for ask and true for yes?" do
set_shell_prompt_responses(generator, { :ask => "response", :yes? => true })
expect(generator.shell.ask).to eq("response")
expect(generator.shell.yes?).to be true
end
end
context "argument shell command contains an array" do
it "mocks sequential return values response1 and response2" do
set_shell_prompt_responses(generator, { :ask => ["response1", "response2"] })
expect(generator.shell.ask).to eq("response1")
expect(generator.shell.ask).to eq("response2")
end
end
end
end
ammeter-1.1.5/spec/ammeter/rspec/generator/example/generator_example_group_spec.rb 0000644 0000041 0000041 00000005020 14022221032 030556 0 ustar www-data www-data require "spec_helper"
module Ammeter::RSpec::Rails
describe GeneratorExampleGroup do
it { is_expected.to be_included_in_files_in('./spec/generators/') }
it { is_expected.to be_included_in_files_in('.\\spec\\generators\\') }
let(:group_class) do
::RSpec::Core::ExampleGroup.describe do
include GeneratorExampleGroup
end
end
it "adds :type => :generator to the metadata" do
expect(group_class.metadata[:type]).to eq(:generator)
end
describe 'an instance of the group' do
let(:group) { group_class.new }
describe 'uses the generator as the implicit subject' do
let(:generator) { double('generator') }
it 'sets a generator as subject' do
allow(group).to receive(:generator).and_return(generator)
expect(group.subject).to eq generator
end
end
it "allows you to override with explicity subject" do
group_class.subject { 'explicit' }
expect(group.subject).to eq 'explicit'
end
it 'allows delegation of destination root to ::Rails::Generators::TestCase' do
group.destination '/some/path'
expect(group.destination_root).to eq '/some/path'
end
describe 'working with files' do
let(:path_to_gem_root_tmp) { File.expand_path(__FILE__ + '../../../../../../../../tmp') }
before do
group.destination path_to_gem_root_tmp
FileUtils.rm_rf path_to_gem_root_tmp
FileUtils.mkdir path_to_gem_root_tmp
end
it 'should use destination to find relative root file' do
expect(group.file('app/model/post.rb')).to eq "#{path_to_gem_root_tmp}/app/model/post.rb"
end
describe 'migrations' do
before do
tmp_db_migrate = path_to_gem_root_tmp + '/db/migrate'
FileUtils.mkdir_p tmp_db_migrate
FileUtils.touch(tmp_db_migrate + '/20111010200000_create_comments.rb')
FileUtils.touch(tmp_db_migrate + '/20111010203337_create_posts.rb')
end
it 'should use destination to find relative root file' do
expect(group.migration_file('db/migrate/create_posts.rb')).to eq "#{path_to_gem_root_tmp}/db/migrate/20111010203337_create_posts.rb"
end
it 'should stick "TIMESTAMP" in when migration does not exist' do
expect(group.migration_file('db/migrate/migration_that_is_not_there.rb')).to eq "#{path_to_gem_root_tmp}/db/migrate/TIMESTAMP_migration_that_is_not_there.rb"
end
end
end
end
end
end
ammeter-1.1.5/.gitignore 0000644 0000041 0000041 00000000137 14022221032 015155 0 ustar www-data www-data tmp
doc
pkg
vendor
.bundle
*.gem
Gemfile.lock
gemfiles/*.lock
.rvmrc
**/.DS_Store
*~
*.swp
.rbx ammeter-1.1.5/Rakefile 0000644 0000041 0000041 00000004412 14022221032 014632 0 ustar www-data www-data require 'bundler'
require 'bundler/setup'
Bundler::GemHelper.install_tasks
require 'rdoc/task'
require 'rspec/core'
require 'rspec/core/rake_task'
require 'cucumber/rake/task'
task :cleanup_rcov_files do
rm_rf 'coverage.data'
end
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w[--color]
end
Cucumber::Rake::Task.new(:cucumber)
task :ensure_bundler_11 do
raise 'Bundler 1.1 is a development dependency to build ammeter. Please upgrade bundler.' unless Bundler::VERSION >= '1.1'
end
def create_gem(gem_name)
template_folder = "features/templates/#{gem_name}"
Dir.chdir("./tmp") do
sh "yes | bundle gem -t rspec #{gem_name}"
end
sh "cp '#{template_folder}/Gemfile' tmp/#{gem_name}"
sh "cp '#{template_folder}/#{gem_name}.gemspec' tmp/#{gem_name}"
sh "cp '#{template_folder}/Rakefile' tmp/#{gem_name}"
sh "mkdir -p tmp/#{gem_name}/spec"
sh "cp '#{template_folder}/spec/spec_helper.rb' tmp/#{gem_name}/spec"
Dir.chdir("./tmp/#{gem_name}") do
Bundler.unbundled_system 'bundle install'
end
end
namespace :generate do
desc "generate a fresh app with rspec installed"
task :app => :ensure_bundler_11 do |t|
sh "bundle exec rails new ./tmp/example_app -m 'features/templates/generate_example_app.rb' --skip-test-unit --skip-bootsnap --skip-spring --skip-webpack-install"
sh "cp 'features/templates/rspec.rake' ./tmp/example_app/lib/tasks"
Dir.chdir("./tmp/example_app/") do
Bundler.unbundled_system 'bundle install'
Bundler.unbundled_system 'rake db:migrate'
Bundler.unbundled_system 'rails g rspec:install'
end
end
desc "generate a fresh gem that depends on railties"
task :railties_gem => :ensure_bundler_11 do |t|
create_gem('my_railties_gem')
end
desc "generate a fresh gem that depends on rails"
task :rails_gem => :ensure_bundler_11 do |t|
create_gem('my_rails_gem')
end
end
task :generate => [:'generate:app', :'generate:railties_gem', :'generate:rails_gem']
namespace :clobber do
desc "clobber the generated app"
task :app do
rm_rf "tmp/example_app"
end
task :gem do
rm_rf "tmp/my_railties_gem"
rm_rf "tmp/my_rails_gem"
end
end
task :clobber => [:'clobber:app', :'clobber:gem']
task :ci => [:spec, :clobber, :generate, :cucumber]
task :default => :ci
ammeter-1.1.5/lib/ 0000755 0000041 0000041 00000000000 14022221032 013732 5 ustar www-data www-data ammeter-1.1.5/lib/ammeter/ 0000755 0000041 0000041 00000000000 14022221032 015364 5 ustar www-data www-data ammeter-1.1.5/lib/ammeter/version.rb 0000644 0000041 0000041 00000000047 14022221032 017377 0 ustar www-data www-data module Ammeter
VERSION = "1.1.5"
end
ammeter-1.1.5/lib/ammeter/init.rb 0000644 0000041 0000041 00000000641 14022221032 016655 0 ustar www-data www-data require 'rspec/core'
require 'rspec/rails'
if RSpec::Core::Version::STRING < '3'
require 'ammeter/rspec/rspec_2_compatibility' # if rspec2
end
require 'rails'
require 'ammeter/output_capturer.rb'
require 'ammeter/rspec/generator/example.rb'
require 'ammeter/rspec/generator/matchers.rb'
if Rails.respond_to?(:application) && Rails.application.respond_to?(:load_generators)
Rails.application.load_generators
end
ammeter-1.1.5/lib/ammeter/railtie.rb 0000644 0000041 0000041 00000000234 14022221032 017341 0 ustar www-data www-data module Ammeter
class Railtie < Rails::Railtie
initializer 'my_engine.interact_with_routes' do |app|
require 'ammeter/init.rb'
end
end
end
ammeter-1.1.5/lib/ammeter/output_capturer.rb 0000644 0000041 0000041 00000001403 14022221032 021154 0 ustar www-data www-data module Ammeter
class OutputCapturer
# Is this thread safe!?!?
# This won't work with sub-processes
def self.capture(io, &block)
case io
when :stdout
capture_stdout(&block)
when :stderr
capture_stderr(&block)
else
raise "Unknown IO #{io}"
end
end
def self.capture_stdout(&block)
captured_stream = StringIO.new
orginal_io, $stdout = $stdout, captured_stream
block.call
captured_stream.string
ensure
$stdout = orginal_io
end
def self.capture_stderr(&block)
captured_stream = StringIO.new
orginal_io, $stderr = $stderr, captured_stream
block.call
captured_stream.string
ensure
$stderr = orginal_io
end
end
end ammeter-1.1.5/lib/ammeter/rspec/ 0000755 0000041 0000041 00000000000 14022221032 016500 5 ustar www-data www-data ammeter-1.1.5/lib/ammeter/rspec/generator/ 0000755 0000041 0000041 00000000000 14022221032 020466 5 ustar www-data www-data ammeter-1.1.5/lib/ammeter/rspec/generator/matchers.rb 0000644 0000041 0000041 00000000424 14022221032 022621 0 ustar www-data www-data require 'ammeter/rspec/generator/matchers/be_a_migration'
require 'ammeter/rspec/generator/matchers/contain'
require 'ammeter/rspec/generator/matchers/exist'
require 'ammeter/rspec/generator/matchers/have_method'
require 'ammeter/rspec/generator/matchers/have_correct_syntax'
ammeter-1.1.5/lib/ammeter/rspec/generator/matchers/ 0000755 0000041 0000041 00000000000 14022221032 022274 5 ustar www-data www-data ammeter-1.1.5/lib/ammeter/rspec/generator/matchers/be_a_migration.rb 0000644 0000041 0000041 00000000612 14022221032 025557 0 ustar www-data www-data RSpec::Matchers.define :be_a_migration do
match do |file_path|
dirname, file_name = File.dirname(file_path), File.basename(file_path)
if file_name =~ /\d+_.*\.rb/
migration_file_path = file_path
else
migration_file_path = Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}$/).first
end
migration_file_path && File.exist?(migration_file_path)
end
end
ammeter-1.1.5/lib/ammeter/rspec/generator/matchers/have_method.rb 0000644 0000041 0000041 00000000472 14022221032 025107 0 ustar www-data www-data RSpec::Matchers.define :have_method do |method|
chain :containing do |content|
@method_content = content.strip
end
match do |file_path|
content = File.read(file_path)
content =~ /(\s+)def #{method}(\(.+\))?(.*?)\n\1end/m && (@method_content.nil? ? true : $3.include?(@method_content))
end
end
ammeter-1.1.5/lib/ammeter/rspec/generator/matchers/have_correct_syntax.rb 0000644 0000041 0000041 00000002351 14022221032 026674 0 ustar www-data www-data RSpec::Matchers.define :have_correct_syntax do
match do |file_path|
source = File.read(file_path)
extension = File.extname(file_path)
case extension
when '.rb', '.rake'
check_ruby_syntax(source)
when '.rhtml', '.erb'
check_erb_syntax(source)
when '.haml'
check_haml_syntax(source)
else
raise "Checking syntax for #{extension} files is not yet supported"
end
end
define_method :check_ruby_syntax do |code|
begin
eval('__crash_me__;' + code)
rescue SyntaxError
false
rescue NameError
true
end
end
define_method :check_erb_syntax do |code|
require 'action_view'
require 'ostruct'
begin
if Rails::VERSION::STRING < "6.0"
view = ActionView::Template::Handlers::ERB.call(OpenStruct.new(:source => code))
else
view = ActionView::Template::Handlers::ERB.call(OpenStruct.new, code)
end
eval('__crash_me__; ' + view)
rescue SyntaxError
false
rescue NameError
true
end
end
define_method :check_haml_syntax do |code|
require 'haml'
begin
Haml::Engine.new(code)
rescue Haml::SyntaxError
false
rescue NameError
true
end
end
end
ammeter-1.1.5/lib/ammeter/rspec/generator/matchers/exist.rb 0000644 0000041 0000041 00000000135 14022221032 023754 0 ustar www-data www-data RSpec::Matchers.define :exist do
match do |file_path|
File.exist?(file_path)
end
end
ammeter-1.1.5/lib/ammeter/rspec/generator/matchers/contain.rb 0000644 0000041 0000041 00000002773 14022221032 024265 0 ustar www-data www-data RSpec::Matchers.define :contain do |*expected_contents|
match do |file_path|
@actual_contents = File.read(file_path)
not_found_expectations.empty?
end
match_when_negated do |file_path|
@actual_contents = File.read(file_path)
found_expectations.empty?
end
failure_message do |file_path|
"expected the file #{file_path} to contain " +
if expected_contents.many?
"#{expected_contents.map(&:inspect).to_sentence} but " +
"#{not_found_expectations.map(&:inspect).to_sentence} #{not_found_expectations.many? ? 'were' : 'was'} not found"
else
"#{expected_contents.first.inspect} but it contained #{@actual_contents.inspect}"
end
end
failure_message_when_negated do |file_path|
"expected the file #{file_path} to not contain " +
if expected_contents.many?
"#{expected_contents.map(&:inspect).to_sentence} but " +
"#{found_expectations.map(&:inspect).to_sentence} #{found_expectations.many? ? 'were' : 'was'} found"
else
"#{expected_contents.first.inspect} but it did"
end
end
define_method :found_expectations do
@found_expectations ||= expected_contents.select do |expected_content|
case expected_content
when String
@actual_contents.include? expected_content
when Regexp
@actual_contents =~ expected_content
end
end
end
define_method :not_found_expectations do
@not_found_expectations ||= expected_contents - found_expectations
end
end
ammeter-1.1.5/lib/ammeter/rspec/generator/example.rb 0000644 0000041 0000041 00000001760 14022221032 022452 0 ustar www-data www-data require 'rspec/core'
require 'ammeter/rspec/generator/example/generator_example_group'
require 'ammeter/rspec/generator/example/generator_example_helpers'
RSpec::configure do |c|
def c.escaped_path(*parts)
Regexp.compile(parts.join('[\\\/]') + '[\\\/]')
end
generator_path_regex = c.escaped_path(%w[spec generators])
if RSpec::Core::Version::STRING >= '3'
c.include Ammeter::RSpec::Rails::GeneratorExampleHelpers,
:type => :generator
c.include Ammeter::RSpec::Rails::GeneratorExampleGroup,
:type => :generator
c.include Ammeter::RSpec::Rails::GeneratorExampleGroup,
:file_path => lambda { |file_path, metadata|
metadata[:type].nil? && generator_path_regex =~ file_path
}
else #rspec2
c.include Ammeter::RSpec::Rails::GeneratorExampleHelpers, :type => :generator
c.include Ammeter::RSpec::Rails::GeneratorExampleGroup, :type => :generator, :example_group => {
:file_path => generator_path_regex
}
end
end
ammeter-1.1.5/lib/ammeter/rspec/generator/example/ 0000755 0000041 0000041 00000000000 14022221032 022121 5 ustar www-data www-data ammeter-1.1.5/lib/ammeter/rspec/generator/example/generator_example_helpers.rb 0000644 0000041 0000041 00000002070 14022221032 027670 0 ustar www-data www-data require 'rails/generators'
require 'active_support/core_ext'
module Ammeter
module RSpec
module Rails
# Delegates to Rails::Generators::TestCase to work with RSpec.
module GeneratorExampleHelpers
# Sets return values for basic shell commands (ex. ask, yes?, no?).
# Does this by mocking return values using RSpec's `and_return` method
#
# ===== Parameters =====
# Generator w/ @shell attribute
# Hash { command_name: input_value, ask: "Testing", yes?: true }
# The values for each element in the hash can be set as an array as well.
# This will allow for different return values upon each call.
#
# ex. set_shell_prompt_responses(generator, { yes?: [true, false] })
# would respond with true to the first yes? call, but false to the second
def set_shell_prompt_responses(generator, command_args={})
command_args.each do |k,v|
allow(generator.shell).to receive(k.to_sym).and_return(*v)
end
end
end
end
end
end
ammeter-1.1.5/lib/ammeter/rspec/generator/example/generator_example_group.rb 0000644 0000041 0000041 00000006035 14022221032 027367 0 ustar www-data www-data require 'rails/generators'
require 'active_support/core_ext'
require 'tmpdir'
require 'fileutils'
module Ammeter
module RSpec
module Rails
# Delegates to Rails::Generators::TestCase to work with RSpec.
module GeneratorExampleGroup
extend ActiveSupport::Concern
include ::RSpec::Rails::RailsExampleGroup
DELEGATED_METHODS = [:destination_root, :current_path, :generator_class]
module ClassMethods
mattr_accessor :test_unit_test_case_delegate
delegate :default_arguments, :to => :'self.test_unit_test_case_delegate'
DELEGATED_METHODS.each do |method|
delegate method, :to => :'self.test_unit_test_case_delegate'
end
delegate :destination, :arguments, :to => ::Rails::Generators::TestCase
def prepare_destination
self.test_unit_test_case_delegate.send :prepare_destination
end
def ensure_current_path
self.test_unit_test_case_delegate.send :ensure_current_path
end
def initialize_delegate
self.test_unit_test_case_delegate = ::Rails::Generators::TestCase.new 'pending'
self.test_unit_test_case_delegate.class.tests(described_class)
@generator = nil
end
def generator(given_args=self.default_arguments, config={})
@generator ||= begin
args, opts = Thor::Options.split(given_args)
self.test_unit_test_case_delegate.generator(args, opts, config)
end
end
def run_generator(given_args=self.default_arguments, config={})
OutputCapturer.capture(:stdout) { generator(given_args, config).invoke_all }
end
end
def invoke_task name
OutputCapturer.capture(:stdout) { generator.invoke_task(generator_class.all_tasks[name.to_s]) }
end
included do
delegate :generator, :run_generator, :destination, :arguments, :ensure_current_path, :to => :'self.class'
DELEGATED_METHODS.each do |method|
delegate method, :to => :'self.class'
end
def prepare_destination
self.class.send :prepare_destination
end
::Rails::Generators::TestCase.destination File.expand_path('ammeter', Dir.tmpdir)
initialize_delegate
before do
self.class.initialize_delegate
prepare_destination
end
after do
# ensure_current_path
end
metadata[:type] = :generator
end
def file relative
File.expand_path(relative, destination_root)
end
def migration_file relative
file_path = file(relative)
migration_file = Dir.glob("#{File.dirname(file_path)}/[0-9]*_#{File.basename(file_path)}").first
migration_file = "#{File.dirname(file_path)}/TIMESTAMP_#{File.basename(file_path)}" if migration_file.nil?
migration_file
end
def subject
generator
end
end
end
end
end
ammeter-1.1.5/lib/ammeter/rspec/rspec_2_compatibility.rb 0000644 0000041 0000041 00000001041 14022221032 023307 0 ustar www-data www-data require 'rspec/expectations'
if RSpec::Expectations::Version::STRING < '3'
{ :match_when_negated => :match_for_should_not,
:failure_message => :failure_message_for_should,
:failure_message_when_negated => :failure_message_for_should_not
}.each do |rspec3_method, rspec2_method|
RSpec::Matchers::DSL::Matcher.send :alias_method, rspec3_method, rspec2_method
end
module RSpec2CoreMemoizedHelpers
def is_expected
expect(subject)
end
end
RSpec::Core::MemoizedHelpers.extend RSpec2CoreMemoizedHelpers
end ammeter-1.1.5/lib/ammeter.rb 0000644 0000041 0000041 00000000271 14022221032 015711 0 ustar www-data www-data require 'rails'
require 'active_support'
# Ensure that Rails has loaded & Initialized
if Rails::VERSION::STRING >= "3.1"
require 'ammeter/railtie'
else
require 'ammeter/init'
end
ammeter-1.1.5/Gemfile 0000644 0000041 0000041 00000001624 14022221032 014462 0 ustar www-data www-data source 'https://rubygems.org'
rspec_version = ENV['RSPEC_VERSION'] || '~> 4.0'
rails_version = ENV['RAILS_VERSION'] || '>= 5.1.0'
if rspec_version == 'master'
gem "rspec-rails", :git => 'git://github.com/rspec/rspec-rails.git'
gem "rspec", :git => 'git://github.com/rspec/rspec.git'
gem "rspec-core", :git => 'git://github.com/rspec/rspec-core.git'
gem "rspec-expectations", :git => 'git://github.com/rspec/rspec-expectations.git'
gem "rspec-mocks", :git => 'git://github.com/rspec/rspec-mocks.git'
gem "rspec-collection_matchers", :git => 'git://github.com/rspec/rspec-collection_matchers.git'
gem "rspec-support", :git => 'git://github.com/rspec/rspec-support.git'
else
gem 'rspec-rails', rspec_version
end
gem 'rails', rails_version
gem 'uglifier'
gem 'rake'
gem 'coffee-rails'
gem 'sass-rails'
gem 'jquery-rails'
gem 'haml-rails'
# Specify your gem's dependencies in ammeter.gemspec
gemspec
ammeter-1.1.5/ammeter.gemspec 0000644 0000041 0000041 00000003163 14022221032 016166 0 ustar www-data www-data # -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "ammeter/version"
Gem::Specification.new do |s|
s.name = "ammeter"
s.version = Ammeter::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Alex Rothenberg"]
s.email = ["alex@alexrothenberg.com"]
s.homepage = ""
s.summary = %q{Write specs for your Rails 3+ generators}
s.description = %q{Write specs for your Rails 3+ generators}
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.add_runtime_dependency 'railties', '>= 3.0'
s.add_runtime_dependency 'activesupport', '>= 3.0'
s.add_runtime_dependency 'rspec-rails', '>= 2.2'
s.add_development_dependency 'rspec', '>= 2.2'
s.add_development_dependency 'rails', '>= 3.1'
s.add_development_dependency 'uglifier', '>= 1.2.4'
s.add_development_dependency 'rake', '>= 0.9.2.2'
s.add_development_dependency 'coffee-rails', '>= 3.2.2'
s.add_development_dependency 'sass-rails', '>= 3.2.5'
s.add_development_dependency 'jquery-rails', '>= 2.0.2'
s.add_development_dependency 'haml-rails', '>= 0.4'
s.add_development_dependency 'cucumber', '>= 0.10'
s.add_development_dependency 'aruba', '>= 0.3'
case RUBY_PLATFORM
when 'java'
s.add_development_dependency 'activerecord-jdbcsqlite3-adapter'
s.add_development_dependency 'therubyrhino'
else
s.add_development_dependency 'sqlite3', '>= 1'
end
end
ammeter-1.1.5/.ruby-version 0000644 0000041 0000041 00000000006 14022221032 015625 0 ustar www-data www-data 2.5.8
ammeter-1.1.5/features/ 0000755 0000041 0000041 00000000000 14022221032 015002 5 ustar www-data www-data ammeter-1.1.5/features/templates/ 0000755 0000041 0000041 00000000000 14022221032 017000 5 ustar www-data www-data ammeter-1.1.5/features/templates/rspec.rake 0000644 0000041 0000041 00000000130 14022221032 020752 0 ustar www-data www-data require 'rspec/core/rake_task'
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) ammeter-1.1.5/features/templates/generate_example_app.rb 0000644 0000041 0000041 00000001660 14022221032 023475 0 ustar www-data www-data rspec_version = ENV['RSPEC_VERSION']
rspec_major_version = (rspec_version && rspec_version != 'master') ? rspec_version.scan(/\d+/).first : '3'
if rspec_version == 'master'
gem "rspec-rails", :git => 'git://github.com/rspec/rspec-rails.git'
gem "rspec", :git => 'git://github.com/rspec/rspec.git'
gem "rspec-core", :git => 'git://github.com/rspec/rspec-core.git'
gem "rspec-expectations", :git => 'git://github.com/rspec/rspec-expectations.git'
gem "rspec-mocks", :git => 'git://github.com/rspec/rspec-mocks.git'
gem "rspec-collection_matchers", :git => 'git://github.com/rspec/rspec-collection_matchers.git'
gem "rspec-support", :git => 'git://github.com/rspec/rspec-support.git'
else
gem 'rspec-rails', rspec_version
end
gem "i18n", '< 0.7.0' if RUBY_VERSION < '1.9.3'
gem 'ammeter', :path=>'../..'
if defined?(Rails) && Rails::VERSION::STRING.to_f < 4
# Execjs is causing problems on 1.8.7
gem 'execjs', '~> 2.0.0'
end
ammeter-1.1.5/features/templates/my_railties_gem/ 0000755 0000041 0000041 00000000000 14022221032 022151 5 ustar www-data www-data ammeter-1.1.5/features/templates/my_railties_gem/spec/ 0000755 0000041 0000041 00000000000 14022221032 023103 5 ustar www-data www-data ammeter-1.1.5/features/templates/my_railties_gem/spec/spec_helper.rb 0000644 0000041 0000041 00000000253 14022221032 025721 0 ustar www-data www-data require 'bundler/setup'
require 'rails/all'
require 'ammeter/init'
Bundler.require
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
ammeter-1.1.5/features/templates/my_railties_gem/my_railties_gem.gemspec 0000644 0000041 0000041 00000001314 14022221032 026666 0 ustar www-data www-data # -*- encoding: utf-8 -*-
require File.expand_path('../lib/my_railties_gem/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Alex Rothenberg"]
gem.email = ["alex@alexrothenberg.com"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "my_railties_gem"
gem.require_paths = ["lib"]
gem.version = MyRailtiesGem::VERSION
gem.add_runtime_dependency 'railties', '>= 3.2'
end
ammeter-1.1.5/features/templates/my_railties_gem/Rakefile 0000644 0000041 0000041 00000000255 14022221032 023620 0 ustar www-data www-data require "bundler/gem_tasks"
require 'rspec'
require 'rspec/core/rake_task'
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w[--color]
end
ammeter-1.1.5/features/templates/my_railties_gem/Gemfile 0000644 0000041 0000041 00000001633 14022221032 023447 0 ustar www-data www-data source 'https://rubygems.org'
rspec_version = ENV['RSPEC_VERSION'] || '~> 4.0'
rails_version = ENV['RAILS_VERSION'] || '>= 5.1.0'
if rspec_version == 'master'
gem "rspec-rails", :git => 'git://github.com/rspec/rspec-rails.git'
gem "rspec", :git => 'git://github.com/rspec/rspec.git'
gem "rspec-core", :git => 'git://github.com/rspec/rspec-core.git'
gem "rspec-expectations", :git => 'git://github.com/rspec/rspec-expectations.git'
gem "rspec-mocks", :git => 'git://github.com/rspec/rspec-mocks.git'
gem "rspec-collection_matchers", :git => 'git://github.com/rspec/rspec-collection_matchers.git'
gem "rspec-support", :git => 'git://github.com/rspec/rspec-support.git'
else
gem 'rspec-rails', rspec_version
end
gem 'railties', rails_version
# Specify your gem's dependencies in my_gem.gemspec
gemspec
# we cannot add a development dependency with a path to my_gem.gemspec
gem 'ammeter', :path => '../..'
ammeter-1.1.5/features/templates/my_rails_gem/ 0000755 0000041 0000041 00000000000 14022221032 021447 5 ustar www-data www-data ammeter-1.1.5/features/templates/my_rails_gem/spec/ 0000755 0000041 0000041 00000000000 14022221032 022401 5 ustar www-data www-data ammeter-1.1.5/features/templates/my_rails_gem/spec/spec_helper.rb 0000644 0000041 0000041 00000000712 14022221032 025217 0 ustar www-data www-data require 'bundler/setup'
require 'rails/all'
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
begin
# This prevents 'uninitialized constant Jquery::Rails::Railtie::PROTOTYPE_JS (NameError)'
require 'jquery/rails'
rescue LoadError
end
module MyRailsGem
module TestApp
class Application < Rails::Application
config.root = File.dirname(__FILE__)
end
end
end
require 'ammeter/init'
Bundler.require
ammeter-1.1.5/features/templates/my_rails_gem/Rakefile 0000644 0000041 0000041 00000000255 14022221032 023116 0 ustar www-data www-data require "bundler/gem_tasks"
require 'rspec'
require 'rspec/core/rake_task'
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w[--color]
end
ammeter-1.1.5/features/templates/my_rails_gem/Gemfile 0000644 0000041 0000041 00000001630 14022221032 022742 0 ustar www-data www-data source 'https://rubygems.org'
rspec_version = ENV['RSPEC_VERSION'] || '~> 4.0'
rails_version = ENV['RAILS_VERSION'] || '>= 5.1.0'
if rspec_version == 'master'
gem "rspec-rails", :git => 'git://github.com/rspec/rspec-rails.git'
gem "rspec", :git => 'git://github.com/rspec/rspec.git'
gem "rspec-core", :git => 'git://github.com/rspec/rspec-core.git'
gem "rspec-expectations", :git => 'git://github.com/rspec/rspec-expectations.git'
gem "rspec-mocks", :git => 'git://github.com/rspec/rspec-mocks.git'
gem "rspec-collection_matchers", :git => 'git://github.com/rspec/rspec-collection_matchers.git'
gem "rspec-support", :git => 'git://github.com/rspec/rspec-support.git'
else
gem 'rspec-rails', rspec_version
end
gem 'rails', rails_version
# Specify your gem's dependencies in my_gem.gemspec
gemspec
# we cannot add a development dependency with a path to my_gem.gemspec
gem 'ammeter', :path => '../..'
ammeter-1.1.5/features/templates/my_rails_gem/my_rails_gem.gemspec 0000644 0000041 0000041 00000001300 14022221032 025455 0 ustar www-data www-data # -*- encoding: utf-8 -*-
require File.expand_path('../lib/my_rails_gem/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Alex Rothenberg"]
gem.email = ["alex@alexrothenberg.com"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "my_rails_gem"
gem.require_paths = ["lib"]
gem.version = MyRailsGem::VERSION
gem.add_runtime_dependency 'rails', '>= 3.2'
end
ammeter-1.1.5/features/generator_in_a_gem.feature 0000644 0000041 0000041 00000006004 14022221032 022163 0 ustar www-data www-data Feature: Gems can contain generators
Within a Gem Rails may often not be loaded (but railties is)
We should still be able to write a spec for these generators
@railties_gem
Scenario: A generator with "railties" dependency
Given a file named "lib/generators/awesome/awesome_generator.rb" with:
"""
class AwesomeGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
class_option :super, :type => :boolean, :default => false
def create_awesomeness
template 'awesome.html', File.join('public', name, "#{"super_" if options[:super]}awesome.html")
end
end
"""
And a file named "lib/generators/awesome/templates/awesome.html" with:
"""
This is an awesome file
"""
And a file named "spec/generators/awesome_generator_spec.rb" with:
"""
require "spec_helper"
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
before { run_generator %w(my_dir) }
describe 'public/my_dir/awesome.html' do
subject { file('public/my_dir/awesome.html') }
it { expect(subject).to exist }
it { expect(subject).to contain 'This is an awesome file' }
it { expect(subject).to_not contain 'This text is not in the file' }
end
end
"""
When I run `rake spec`
Then the output should contain "3 examples, 0 failures"
@rails_gem
Scenario: A generator that uses "hook_for"
Given a file named "lib/generators/resourceful/resourceful_generator.rb" with:
"""
class ResourcefulGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
class_option :super, :type => :boolean, :default => false
hook_for :orm, :in => :rails, :as => :model, :required => true
def create_resourceful_controller
template 'controller.rb', File.join('app/controllers', "#{plural_file_name}_controller.rb")
end
end
"""
And a file named "lib/generators/resourceful/templates/controller.rb" with:
"""
class <%= class_name.pluralize %>Controller < ResourcefulController
end
"""
And a file named "spec/generators/resourceful_generator_spec.rb" with:
"""
require "spec_helper"
require 'generators/resourceful/resourceful_generator'
describe ResourcefulGenerator do
before { run_generator %w(post) }
describe 'app/controller/posts_controller.rb' do
subject { file('app/controllers/posts_controller.rb') }
it { expect(subject).to exist }
it { expect(subject).to contain 'class PostsController < ResourcefulController' }
end
describe 'app/models/post.rb' do
subject { file('app/models/post.rb') }
it { expect(subject).to exist }
it { expect(subject).to contain 'class Post < ' }
end
end
"""
When I run `rake spec`
Then the output should contain "4 examples, 0 failures"
ammeter-1.1.5/features/generator_with_shell_prompts_spec.feature 0000644 0000041 0000041 00000004004 14022221032 025363 0 ustar www-data www-data @example_app
Feature: generator with shell prompts spec
Generator specs live in spec/generators. In order to access
the generator's methods you can call them on the "generator" object.
Background: A simple generator
Given a file named "lib/generators/awesome/lamest_generator.rb" with:
"""
class LamestGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
class_option :super, :type => :boolean, :default => false
def create_lamest
@lame = false
@awesome = false
@user_name = ask("What is your name?")
if yes?("Are you the lamest?")
@lame = true
end
if yes?("Are you awesome?")
@awesome = true
end
template 'lamest.html.erb', File.join('public', name, "#{"super_" if options[:super]}lamest.html")
end
end
"""
And a file named "lib/generators/awesome/templates/lamest.html.erb" with:
"""
<%= @user_name %> <%= "is the lamest" if @lame %> and <%= "is not awesome!" if @awesome == false %>
"""
Scenario: A spec that runs the entire generator
Given a file named "spec/generators/lamest_generator_spec.rb" with:
"""
require "rails_helper"
require 'generators/awesome/lamest_generator'
describe LamestGenerator do
describe 'invoke' do
before do
gen = generator %w(my_dir)
set_shell_prompt_responses(gen, { :ask => "Thomas", :yes? => [true, false] })
run_generator
end
describe 'public/my_dir/lamest.html' do
subject { file('public/my_dir/lamest.html') }
it { expect(subject).to exist }
it { expect(subject).to contain 'Thomas is the lamest and is not awesome!' }
it { expect(subject).to_not contain 'This text is not in the file' }
end
end
end
"""
When I run `rake spec`
Then the output should contain "3 examples, 0 failures"
ammeter-1.1.5/features/generator_spec.feature 0000644 0000041 0000041 00000016753 14022221032 021373 0 ustar www-data www-data @example_app
Feature: generator spec
Generator specs live in spec/generators. In order to access
the generator's methods you can call them on the "generator" object.
Background: A simple generator
Given a file named "lib/generators/awesome/awesome_generator.rb" with:
"""
class AwesomeGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
class_option :super, :type => :boolean, :default => false
class_option :someone, :type => :string
def create_awesomeness
template 'awesome.html', File.join('public', name, "#{"super_" if options[:super]}awesome.html")
end
def create_lameness
template 'lame.html.erb', File.join('public', name, "#{"super_" if options[:super]}lame.html")
end
end
"""
And a file named "lib/generators/awesome/templates/awesome.html" with:
"""
This is an awesome file
"""
And a file named "lib/generators/awesome/templates/lame.html.erb" with:
"""
<%= options[:someone] %> is lame
"""
Scenario: A spec that runs the entire generator
Given a file named "spec/generators/awesome_generator_spec.rb" with:
"""
require "rails_helper"
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
describe 'invoke' do
before { run_generator %w(my_dir --someone Alex) }
describe 'public/my_dir/awesome.html' do
subject { file('public/my_dir/awesome.html') }
it { expect(subject).to exist }
it { expect(subject).to contain 'This is an awesome file' }
it { expect(subject).to_not contain 'This text is not in the file' }
end
describe 'public/my_dir/lame.html' do
subject { file('public/my_dir/lame.html') }
it { expect(subject).to exist }
it { expect(subject).to contain 'Alex is lame' }
it { expect(subject).to_not contain 'This text is not in the file' }
end
end
describe 'revoke' do
subject { file('public/my_dir/awesome.html') }
it 'can run a reverse migration' do
FileUtils.mkdir_p(File.dirname(subject))
# File.write is not in 1.8.7 use File.open
File.open(subject, 'w') do |f|
f.write "test file"
end
expect(subject).to exist
run_generator %w(my_dir --someone Alex), :behavior => :revoke
expect(subject).not_to exist
end
end
end
"""
When I run `rake spec`
Then the output should contain "7 examples, 0 failures"
Scenario: A spec that runs one task in the generator
Given a file named "spec/generators/another_awesome_generator_spec.rb" with:
"""
require "rails_helper"
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
arguments %w(another_dir)
before { invoke_task :create_awesomeness }
describe 'public/another_dir/awesome.html' do
subject { file('public/another_dir/awesome.html') }
it { expect(subject).to exist }
it { expect(subject).to contain 'This is an awesome file' }
it { expect(subject).to_not contain 'This text is not in the file' }
end
describe 'public/another_dir/lame.html' do
subject { file('public/another_dir/lame.html') }
it { should_not exist }
end
end
"""
When I run `rake spec`
Then the output should contain "4 examples, 0 failures"
Scenario: A spec with some failures shows nice error messages
Given a file named "spec/generators/awesome_generator_spec.rb" with:
"""
require "rails_helper"
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
before { run_generator %w(my_dir) }
describe 'public/my_dir/awesome.html' do
subject { file('public/my_dir/awesome.html') }
it { expect(subject).to_not contain 'This is an awesome file' }
it { expect(subject).to contain 'This text is not in the file' }
it { expect(subject).to_not exist }
end
describe 'public/my_dir/non_existent.html' do
subject { file('public/my_dir/non_existent.html') }
it { expect(subject).to exist }
end
describe 'db/migrate/non_existent_migration.rb' do
subject { migration_file('db/migrate/non_existent_migration.rb') }
it { expect(subject).to exist }
end
end
"""
When I run `rake spec`
Then the output should contain "5 examples, 5 failures"
And the output should contain:
"""
/public/my_dir/awesome.html to not contain "This is an awesome file" but it did
"""
And the output should contain:
"""
/public/my_dir/awesome.html to contain "This text is not in the file" but it contained "This is an awesome file"
"""
And the output should contain:
"""
/public/my_dir/awesome.html" not to exist
"""
And the output should contain:
"""
/public/my_dir/non_existent.html" to exist
"""
And the output should contain:
"""
db/migrate/TIMESTAMP_non_existent_migration.rb" to exist
"""
Scenario: Can specify arguments separately from running the generator
Given a file named "spec/generators/awesome_generator_spec.rb" with:
"""
require "rails_helper"
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
arguments %w(my_dir --super)
before { generator.invoke_all }
describe 'public/my_dir/super_awesome.html' do
subject { file('public/my_dir/super_awesome.html') }
it { expect(subject).to exist }
end
describe 'public/my_dir/super_lame.html' do
subject { file('public/my_dir/super_lame.html') }
it { expect(subject).to exist }
end
end
"""
When I run `rake spec`
Then the output should contain "2 examples, 0 failures"
Scenario: A generator that creates a migration
Given a file named "spec/generators/a_migration_spec.rb" with:
"""
require "rails_helper"
require 'rails/generators/active_record/migration/migration_generator'
describe ActiveRecord::Generators::MigrationGenerator do
before { run_generator %w(create_posts) }
subject { migration_file('db/migrate/create_posts.rb') }
it { expect(subject).to exist }
it { expect(subject).to be_a_migration }
it { expect(subject).to contain 'class CreatePosts < ActiveRecord::Migration' }
end
"""
When I run `rake spec`
Then the output should contain "3 examples, 0 failures"
Scenario: Can tell the generator where to put its files
Given a file named "spec/generators/awesome_generator_spec.rb" with:
"""
require "rails_helper"
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
destination Rails.root + 'tmp/generated_files'
before { run_generator %w(my_dir --super) }
describe 'public/my_dir/super_awesome.html' do
subject { file('public/my_dir/super_awesome.html') }
it { expect(subject).to eq "#{Rails.root}/tmp/generated_files/public/my_dir/super_awesome.html" }
it { expect(subject).to exist }
end
end
"""
When I run `rake spec`
Then the output should contain "2 examples, 0 failures"
ammeter-1.1.5/features/hooking_into_other_generators.feature 0000644 0000041 0000041 00000003450 14022221032 024502 0 ustar www-data www-data @example_app
Feature: generator spec
Generator specs live in spec/generators. In order to access
the generator's methods you can call them on the "generator" object.
Background: A generator that uses "hook_for"
Given a file named "lib/generators/resourceful/resourceful_generator.rb" with:
"""
class ResourcefulGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
class_option :super, :type => :boolean, :default => false
hook_for :orm, :in => :rails, :as => :model, :required => true
def create_resourceful_controller
template 'controller.rb', File.join('app/controllers', "#{plural_file_name}_controller.rb")
end
end
"""
And a file named "lib/generators/resourceful/templates/controller.rb" with:
"""
class <%= class_name.pluralize %>Controller < ResourcefulController
end
"""
Scenario: A spec that runs the entire generator
Given a file named "spec/generators/resourceful_generator_spec.rb" with:
"""
require "rails_helper"
require 'generators/resourceful/resourceful_generator'
describe ResourcefulGenerator do
before { run_generator %w(post) }
describe 'app/controller/posts_controller.rb' do
subject { file('app/controllers/posts_controller.rb') }
it { expect(subject).to exist }
it { expect(subject).to contain 'class PostsController < ResourcefulController' }
end
describe 'app/models/post.rb' do
subject { file('app/models/post.rb') }
it { expect(subject).to exist }
it { expect(subject).to contain 'class Post < ' }
end
end
"""
When I run `rake spec`
Then the output should contain "4 examples, 0 failures"
ammeter-1.1.5/features/support/ 0000755 0000041 0000041 00000000000 14022221032 016516 5 ustar www-data www-data ammeter-1.1.5/features/support/env.rb 0000644 0000041 0000041 00000003222 14022221032 017632 0 ustar www-data www-data require 'aruba/cucumber'
Before do
if RUBY_VERSION == "1.9.3"
@aruba_timeout_seconds = 60
else
@aruba_timeout_seconds = 30
end
end
def aruba_path(file_or_dir, source_foldername)
File.expand_path("../../../#{file_or_dir.sub(source_foldername,'aruba')}", __FILE__)
end
def example_app_path(file_or_dir)
File.expand_path("../../../#{file_or_dir}", __FILE__)
end
def write_symlink(file_or_dir, source_foldername, filename=nil)
source = example_app_path(file_or_dir)
target = aruba_path(file_or_dir, source_foldername)
target = File.join(File.dirname(target), filename) if filename
system "ln -s #{source} #{target}"
end
def copy_to_aruba_from(gem_or_app_name)
steps %Q{
Given a directory named "spec"
}
rspec_version = ENV['RSPEC_VERSION']
rspec_major_version = (rspec_version && rspec_version != 'master') ? rspec_version.scan(/\d+/).first : '3'
Dir["tmp/#{gem_or_app_name}/*"].each do |file_or_dir|
if !(file_or_dir =~ /\/spec$/)
write_symlink(file_or_dir, gem_or_app_name)
end
end
write_symlink("tmp/#{gem_or_app_name}/spec/spec_helper.rb", gem_or_app_name)
if rspec_major_version == '2'
# rspec 2.x does not create rails_helper.rb so we create a symlink to avoid cluttering tests
write_symlink("tmp/#{gem_or_app_name}/spec/spec_helper.rb", gem_or_app_name, 'rails_helper.rb')
elsif rspec_major_version >= '3'
write_symlink("tmp/#{gem_or_app_name}/spec/rails_helper.rb", gem_or_app_name)
end
end
Before '@example_app' do
copy_to_aruba_from('example_app')
end
Before '@railties_gem' do
copy_to_aruba_from('my_railties_gem')
end
Before '@rails_gem' do
copy_to_aruba_from('my_rails_gem')
end
ammeter-1.1.5/features/support/aruba_timeout.rb 0000644 0000041 0000041 00000000555 14022221032 021710 0 ustar www-data www-data # see https://github.com/cucumber/aruba#jruby-tips
Aruba.configure do |config|
config.before_cmd do |cmd|
set_env('JRUBY_OPTS', "-X-C #{ENV['JRUBY_OPTS']}") # disable JIT since these processes are so short lived
set_env('JAVA_OPTS', "-d32 #{ENV['JAVA_OPTS']}") # force jRuby to use client JVM for faster startup times
end
end if RUBY_PLATFORM == 'java'
ammeter-1.1.5/History.md 0000644 0000041 0000041 00000003041 14022221032 015145 0 ustar www-data www-data ## Ammeter release history
### 1.0.0 / 2014-04-07
[full changelog](https://github.com/alexrothenberg/ammeter/compare/v0.2.9...v1.0.0)
* Compatible with RSpec 3 & RSpec 2
### 0.2.9 / 2013-06-06
[full changelog](https://github.com/alexrothenberg/ammeter/compare/v0.2.8...v0.2.9)
* Improves `contain` matcher for generated file contents
* Adds `ensure_current_path` method to GeneratorExampleGroup
### 0.2.8 / 2012-07-06
[full changelog](https://github.com/alexrothenberg/ammeter/compare/v0.2.7...v0.2.8)
Fixes regression with initialization for gems that create a test Rails.application (problem since 0.2.6)
### 0.2.7 / 2012-07-05
[full changelog](https://github.com/alexrothenberg/ammeter/compare/v0.2.6...v0.2.7)
* Fixed issue #13 - Railtie initializer preventing devise from loading
* Fixed issue #12 - Only load_generators if we Rails is available. Regression introduced in 0.2.6
### 0.2.6 / 2012-06-28
[full changelog](https://github.com/alexrothenberg/ammeter/compare/v0.2.5...v0.2.6)
* Fixed issue #11 - testing generators that depend on other generators using `hook_for`
### 0.2.5 / 2012-05-03
[full changelog](https://github.com/alexrothenberg/ammeter/compare/v0.2.4...v0.2.5)
* Fixed issue #9 with for rails 4
### 0.2.4 / 2012-04-06
[full changelog](https://github.com/alexrothenberg/ammeter/compare/v0.2.3...v0.2.4)
* Fixed issue #8 with migration_file and be_a_migration [@EppO]
### 0.2.3 / 2012-03-05
[full changelog](https://github.com/alexrothenberg/ammeter/compare/v0.2.2...v0.2.3)
* Fix Rails 3.2 deprecation warning [@yabawock]
ammeter-1.1.5/.github/ 0000755 0000041 0000041 00000000000 14022221032 014524 5 ustar www-data www-data ammeter-1.1.5/.github/workflows/ 0000755 0000041 0000041 00000000000 14022221032 016561 5 ustar www-data www-data ammeter-1.1.5/.github/workflows/ci.yml 0000644 0000041 0000041 00000003540 14022221032 017701 0 ustar www-data www-data name: Ammeter CI
on:
push:
branches:
- 'main'
pull_request:
branches:
- '*'
jobs:
test:
name: 'Ruby: ${{ matrix.ruby }}, Rspec: ${{matrix.env.RSPEC_VERSION}}, Rails: ${{ matrix.env.RAILS_VERSION }}'
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
include:
- ruby: 3.0
env:
RSPEC_VERSION: 'master'
RAILS_VERSION: '~> 6.1.0'
- ruby: 3.0
env:
RSPEC_VERSION: '~> 4.0'
RAILS_VERSION: '~> 6.1.0'
- ruby: 3.0
env:
RSPEC_VERSION: '~> 4.0'
RAILS_VERSION: '~> 6.0.0'
- ruby: 3.0
env:
RSPEC_VERSION: '~> 3.9'
RAILS_VERSION: '~> 6.0.0'
- ruby: 2.5
env:
RSPEC_VERSION: 'master'
RAILS_VERSION: '~> 6.1.0'
- ruby: 2.5
env:
RSPEC_VERSION: '~> 4.0'
RAILS_VERSION: '~> 6.1.0'
- ruby: 2.5
env:
RSPEC_VERSION: '~> 4.0'
RAILS_VERSION: '~> 6.0.0'
- ruby: 2.5
env:
RSPEC_VERSION: '~> 4.0'
RAILS_VERSION: '~> 5.2.0'
- ruby: 2.5
env:
RSPEC_VERSION: '~> 3.9'
RAILS_VERSION: '~> 6.0.0'
- ruby: 2.5
env:
RSPEC_VERSION: '~> 3.9'
RAILS_VERSION: '~> 5.2.0'
- ruby: ruby-head
env:
RSPEC_VERSION: 'master'
RAILS_VERSION: '~> 6.1.0'
env: ${{ matrix.env }}
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bundle install
- run: bundle exec rake ci
continue-on-error: ${{ matrix.allow_failure || false }}
ammeter-1.1.5/LICENSE.txt 0000644 0000041 0000041 00000002042 14022221032 015005 0 ustar www-data www-data Copyright (c) 2011 Alex Rothenberg
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.