ammeter 1.1.0 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ci.yml +70 -0
- data/.ruby-version +1 -0
- data/Gemfile +11 -26
- data/README.md +23 -4
- data/Rakefile +6 -6
- data/features/generator_in_a_gem.feature +1 -1
- data/features/generator_spec.feature +31 -15
- data/features/generator_with_shell_prompts_spec.feature +56 -0
- data/features/hooking_into_other_generators.feature +1 -1
- data/features/support/env.rb +7 -3
- data/features/templates/generate_example_app.rb +20 -1
- data/features/templates/my_rails_gem/Gemfile +4 -13
- data/features/templates/my_rails_gem/Rakefile +0 -1
- data/features/templates/my_rails_gem/spec/spec_helper.rb +8 -2
- data/features/templates/my_railties_gem/Gemfile +4 -13
- data/features/templates/my_railties_gem/Rakefile +0 -1
- data/features/templates/my_railties_gem/spec/spec_helper.rb +2 -1
- data/lib/ammeter/init.rb +2 -0
- data/lib/ammeter/output_capturer.rb +40 -0
- data/lib/ammeter/rspec/generator/example.rb +6 -2
- data/lib/ammeter/rspec/generator/example/generator_example_group.rb +7 -8
- data/lib/ammeter/rspec/generator/example/generator_example_helpers.rb +29 -0
- data/lib/ammeter/rspec/generator/matchers/have_correct_syntax.rb +5 -1
- data/lib/ammeter/version.rb +1 -1
- data/spec/ammeter/rspec/generator/example/generator_example_helpers_spec.rb +27 -0
- data/spec/spec_helper.rb +2 -2
- data/spec/support/mock_generator.rb +10 -0
- metadata +17 -8
- data/.travis.yml +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 15c215e3ca3e6612c3a575d4867460591978e772edc6b25af049887adcab428f
|
4
|
+
data.tar.gz: 94c066d9357c99d88fb78ea07ddc4a0387248bd5eafc8992d49b81a5bc5fa007
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 490b1a299c1916d6e42fb88409ba9c65e8343c6b9696dcbf686b81a08054a5a042737ee993c163f9802c0ea513735238e8deb032af72a7be35b646d138399043
|
7
|
+
data.tar.gz: 3aefd4e01fb7e0fd75caefd5ed5b4b70d531176e83221d881fb088f42f7bfc787221527b6f0cab9e7c31ece610bf7fa75d023c7fb5f10f7f7aaeb3d8cd3b070a
|
@@ -0,0 +1,70 @@
|
|
1
|
+
name: Ammeter CI
|
2
|
+
on:
|
3
|
+
push:
|
4
|
+
branches:
|
5
|
+
- 'main'
|
6
|
+
pull_request:
|
7
|
+
branches:
|
8
|
+
- '*'
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
name: 'Ruby: ${{ matrix.ruby }}, Rspec: ${{matrix.env.RSPEC_VERSION}}, Rails: ${{ matrix.env.RAILS_VERSION }}'
|
12
|
+
runs-on: ubuntu-20.04
|
13
|
+
strategy:
|
14
|
+
fail-fast: false
|
15
|
+
matrix:
|
16
|
+
include:
|
17
|
+
- ruby: 3.0
|
18
|
+
env:
|
19
|
+
RSPEC_VERSION: 'master'
|
20
|
+
RAILS_VERSION: '~> 6.1.0'
|
21
|
+
- ruby: 3.0
|
22
|
+
env:
|
23
|
+
RSPEC_VERSION: '~> 4.0'
|
24
|
+
RAILS_VERSION: '~> 6.1.0'
|
25
|
+
- ruby: 3.0
|
26
|
+
env:
|
27
|
+
RSPEC_VERSION: '~> 4.0'
|
28
|
+
RAILS_VERSION: '~> 6.0.0'
|
29
|
+
- ruby: 3.0
|
30
|
+
env:
|
31
|
+
RSPEC_VERSION: '~> 3.9'
|
32
|
+
RAILS_VERSION: '~> 6.0.0'
|
33
|
+
- ruby: 2.5
|
34
|
+
env:
|
35
|
+
RSPEC_VERSION: 'master'
|
36
|
+
RAILS_VERSION: '~> 6.1.0'
|
37
|
+
- ruby: 2.5
|
38
|
+
env:
|
39
|
+
RSPEC_VERSION: '~> 4.0'
|
40
|
+
RAILS_VERSION: '~> 6.1.0'
|
41
|
+
- ruby: 2.5
|
42
|
+
env:
|
43
|
+
RSPEC_VERSION: '~> 4.0'
|
44
|
+
RAILS_VERSION: '~> 6.0.0'
|
45
|
+
- ruby: 2.5
|
46
|
+
env:
|
47
|
+
RSPEC_VERSION: '~> 4.0'
|
48
|
+
RAILS_VERSION: '~> 5.2.0'
|
49
|
+
- ruby: 2.5
|
50
|
+
env:
|
51
|
+
RSPEC_VERSION: '~> 3.9'
|
52
|
+
RAILS_VERSION: '~> 6.0.0'
|
53
|
+
- ruby: 2.5
|
54
|
+
env:
|
55
|
+
RSPEC_VERSION: '~> 3.9'
|
56
|
+
RAILS_VERSION: '~> 5.2.0'
|
57
|
+
- ruby: ruby-head
|
58
|
+
env:
|
59
|
+
RSPEC_VERSION: 'master'
|
60
|
+
RAILS_VERSION: '~> 6.1.0'
|
61
|
+
env: ${{ matrix.env }}
|
62
|
+
steps:
|
63
|
+
- uses: actions/checkout@v2
|
64
|
+
- uses: ruby/setup-ruby@v1
|
65
|
+
with:
|
66
|
+
ruby-version: ${{ matrix.ruby }}
|
67
|
+
bundler-cache: true
|
68
|
+
- run: bundle install
|
69
|
+
- run: bundle exec rake ci
|
70
|
+
continue-on-error: ${{ matrix.allow_failure || false }}
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.8
|
data/Gemfile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
rspec_version = ENV['RSPEC_VERSION']
|
4
|
-
|
3
|
+
rspec_version = ENV['RSPEC_VERSION'] || '~> 4.0'
|
4
|
+
rails_version = ENV['RAILS_VERSION'] || '>= 5.1.0'
|
5
5
|
|
6
6
|
if rspec_version == 'master'
|
7
7
|
gem "rspec-rails", :git => 'git://github.com/rspec/rspec-rails.git'
|
@@ -13,31 +13,16 @@ if rspec_version == 'master'
|
|
13
13
|
gem "rspec-support", :git => 'git://github.com/rspec/rspec-support.git'
|
14
14
|
else
|
15
15
|
gem 'rspec-rails', rspec_version
|
16
|
-
gem 'rspec', rspec_version
|
17
16
|
end
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
gem 'sass-rails', '~> 3.2'
|
27
|
-
gem 'jquery-rails', '~> 2.0'
|
28
|
-
gem 'haml-rails', '~> 0.4'
|
29
|
-
when '3'
|
30
|
-
gem 'rails', '>= 4.0'
|
31
|
-
gem 'uglifier', '>= 1.3'
|
32
|
-
gem 'rake', '>= 0.10'
|
33
|
-
gem 'coffee-rails', '>= 4.0'
|
34
|
-
gem 'sass-rails', '>= 4.0'
|
35
|
-
gem 'jquery-rails', '>= 3.0'
|
36
|
-
gem 'haml-rails', '>= 0.5'
|
37
|
-
else
|
38
|
-
raise "rspec version #{rspec_version} is not supported"
|
39
|
-
end
|
18
|
+
gem 'rails', rails_version
|
19
|
+
gem 'uglifier'
|
20
|
+
gem 'rake'
|
21
|
+
gem 'coffee-rails'
|
22
|
+
gem 'sass-rails'
|
23
|
+
gem 'jquery-rails'
|
24
|
+
gem 'haml-rails'
|
40
25
|
|
41
|
-
# Specify your gem's dependencies in
|
26
|
+
# Specify your gem's dependencies in ammeter.gemspec
|
42
27
|
gemspec
|
43
28
|
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Ammeter [![
|
1
|
+
# Ammeter [![Ammeter CI](https://github.com/alexrothenberg/ammeter/actions/workflows/ci.yml/badge.svg)](https://github.com/alexrothenberg/ammeter/actions/workflows/ci.yml) [![Code Climate](https://codeclimate.com/github/alexrothenberg/ammeter.png)](https://codeclimate.com/github/alexrothenberg/ammeter) [![Gem Version](https://badge.fury.io/rb/ammeter.png)](http://badge.fury.io/rb/ammeter)
|
2
2
|
|
3
3
|
|
4
4
|
A gem that makes it easy to write specs for your Rails 3 Generators.
|
@@ -13,6 +13,24 @@ and we think you may find it useful too.
|
|
13
13
|
An [ammeter](http://en.wikipedia.org/wiki/Ammeter) is used to measure electrical current and
|
14
14
|
electricity can be produced by a generator.
|
15
15
|
|
16
|
+
# Installation
|
17
|
+
Add this line to your Gemfile (or gemspec):
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'ammeter'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
```bash
|
25
|
+
$ bundle
|
26
|
+
```
|
27
|
+
|
28
|
+
Add:
|
29
|
+
```ruby
|
30
|
+
require 'ammeter/init'
|
31
|
+
```
|
32
|
+
To your `spec/spec_helper.rb`.
|
33
|
+
|
16
34
|
# Example
|
17
35
|
|
18
36
|
```ruby
|
@@ -21,7 +39,7 @@ require 'spec_helper'
|
|
21
39
|
# Generators are not automatically loaded by Rails
|
22
40
|
require 'generators/rspec/model/model_generator'
|
23
41
|
|
24
|
-
describe Rspec::Generators::ModelGenerator do
|
42
|
+
describe Rspec::Generators::ModelGenerator, :type => :generator do
|
25
43
|
# Tell the generator where to put its output (what it thinks of as Rails.root)
|
26
44
|
destination File.expand_path("../../../../../tmp", __FILE__)
|
27
45
|
before do
|
@@ -61,10 +79,11 @@ describe Rspec::Generators::ModelGenerator do
|
|
61
79
|
it { is_expected_to contain /describe Posts/ }
|
62
80
|
end
|
63
81
|
describe 'the migration' do
|
64
|
-
subject {
|
82
|
+
subject { migration_file('db/migrate/create_posts.rb') }
|
65
83
|
|
66
84
|
# is_expected_to be_a_migration - verifies the file exists with a migration timestamp as part of the filename
|
67
|
-
it { is_expected_to
|
85
|
+
it { is_expected_to exist }
|
86
|
+
it { is_expected_to contain /create_table/ }
|
68
87
|
end
|
69
88
|
end
|
70
89
|
end
|
data/Rakefile
CHANGED
@@ -27,7 +27,7 @@ def create_gem(gem_name)
|
|
27
27
|
template_folder = "features/templates/#{gem_name}"
|
28
28
|
|
29
29
|
Dir.chdir("./tmp") do
|
30
|
-
sh "bundle gem #{gem_name}"
|
30
|
+
sh "yes | bundle gem -t rspec #{gem_name}"
|
31
31
|
end
|
32
32
|
sh "cp '#{template_folder}/Gemfile' tmp/#{gem_name}"
|
33
33
|
sh "cp '#{template_folder}/#{gem_name}.gemspec' tmp/#{gem_name}"
|
@@ -35,19 +35,19 @@ def create_gem(gem_name)
|
|
35
35
|
sh "mkdir -p tmp/#{gem_name}/spec"
|
36
36
|
sh "cp '#{template_folder}/spec/spec_helper.rb' tmp/#{gem_name}/spec"
|
37
37
|
Dir.chdir("./tmp/#{gem_name}") do
|
38
|
-
Bundler.
|
38
|
+
Bundler.unbundled_system 'bundle install'
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
42
|
namespace :generate do
|
43
43
|
desc "generate a fresh app with rspec installed"
|
44
44
|
task :app => :ensure_bundler_11 do |t|
|
45
|
-
sh "bundle exec rails new ./tmp/example_app -m 'features/templates/generate_example_app.rb' --skip-test-unit"
|
45
|
+
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"
|
46
46
|
sh "cp 'features/templates/rspec.rake' ./tmp/example_app/lib/tasks"
|
47
47
|
Dir.chdir("./tmp/example_app/") do
|
48
|
-
Bundler.
|
49
|
-
Bundler.
|
50
|
-
Bundler.
|
48
|
+
Bundler.unbundled_system 'bundle install'
|
49
|
+
Bundler.unbundled_system 'rake db:migrate'
|
50
|
+
Bundler.unbundled_system 'rails g rspec:install'
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -74,7 +74,7 @@ Feature: Gems can contain generators
|
|
74
74
|
describe 'app/models/post.rb' do
|
75
75
|
subject { file('app/models/post.rb') }
|
76
76
|
it { expect(subject).to exist }
|
77
|
-
it { expect(subject).to contain 'class Post <
|
77
|
+
it { expect(subject).to contain 'class Post < ' }
|
78
78
|
end
|
79
79
|
end
|
80
80
|
"""
|
@@ -10,13 +10,14 @@ Feature: generator spec
|
|
10
10
|
class AwesomeGenerator < Rails::Generators::NamedBase
|
11
11
|
source_root File.expand_path('../templates', __FILE__)
|
12
12
|
class_option :super, :type => :boolean, :default => false
|
13
|
+
class_option :someone, :type => :string
|
13
14
|
|
14
15
|
def create_awesomeness
|
15
16
|
template 'awesome.html', File.join('public', name, "#{"super_" if options[:super]}awesome.html")
|
16
17
|
end
|
17
18
|
|
18
19
|
def create_lameness
|
19
|
-
template 'lame.html', File.join('public', name, "#{"super_" if options[:super]}lame.html")
|
20
|
+
template 'lame.html.erb', File.join('public', name, "#{"super_" if options[:super]}lame.html")
|
20
21
|
end
|
21
22
|
end
|
22
23
|
"""
|
@@ -24,9 +25,9 @@ Feature: generator spec
|
|
24
25
|
"""
|
25
26
|
This is an awesome file
|
26
27
|
"""
|
27
|
-
And a file named "lib/generators/awesome/templates/lame.html" with:
|
28
|
+
And a file named "lib/generators/awesome/templates/lame.html.erb" with:
|
28
29
|
"""
|
29
|
-
|
30
|
+
<%= options[:someone] %> is lame
|
30
31
|
"""
|
31
32
|
|
32
33
|
Scenario: A spec that runs the entire generator
|
@@ -36,23 +37,38 @@ Feature: generator spec
|
|
36
37
|
require 'generators/awesome/awesome_generator'
|
37
38
|
|
38
39
|
describe AwesomeGenerator do
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
describe 'invoke' do
|
41
|
+
before { run_generator %w(my_dir --someone Alex) }
|
42
|
+
describe 'public/my_dir/awesome.html' do
|
43
|
+
subject { file('public/my_dir/awesome.html') }
|
44
|
+
it { expect(subject).to exist }
|
45
|
+
it { expect(subject).to contain 'This is an awesome file' }
|
46
|
+
it { expect(subject).to_not contain 'This text is not in the file' }
|
47
|
+
end
|
48
|
+
describe 'public/my_dir/lame.html' do
|
49
|
+
subject { file('public/my_dir/lame.html') }
|
50
|
+
it { expect(subject).to exist }
|
51
|
+
it { expect(subject).to contain 'Alex is lame' }
|
52
|
+
it { expect(subject).to_not contain 'This text is not in the file' }
|
53
|
+
end
|
45
54
|
end
|
46
|
-
describe '
|
47
|
-
subject { file('public/my_dir/
|
48
|
-
it
|
49
|
-
|
50
|
-
|
55
|
+
describe 'revoke' do
|
56
|
+
subject { file('public/my_dir/awesome.html') }
|
57
|
+
it 'can run a reverse migration' do
|
58
|
+
FileUtils.mkdir_p(File.dirname(subject))
|
59
|
+
# File.write is not in 1.8.7 use File.open
|
60
|
+
File.open(subject, 'w') do |f|
|
61
|
+
f.write "test file"
|
62
|
+
end
|
63
|
+
expect(subject).to exist
|
64
|
+
run_generator %w(my_dir --someone Alex), :behavior => :revoke
|
65
|
+
expect(subject).not_to exist
|
66
|
+
end
|
51
67
|
end
|
52
68
|
end
|
53
69
|
"""
|
54
70
|
When I run `rake spec`
|
55
|
-
Then the output should contain "
|
71
|
+
Then the output should contain "7 examples, 0 failures"
|
56
72
|
|
57
73
|
Scenario: A spec that runs one task in the generator
|
58
74
|
Given a file named "spec/generators/another_awesome_generator_spec.rb" with:
|
@@ -0,0 +1,56 @@
|
|
1
|
+
@example_app
|
2
|
+
Feature: generator with shell prompts spec
|
3
|
+
|
4
|
+
Generator specs live in spec/generators. In order to access
|
5
|
+
the generator's methods you can call them on the "generator" object.
|
6
|
+
|
7
|
+
Background: A simple generator
|
8
|
+
Given a file named "lib/generators/awesome/lamest_generator.rb" with:
|
9
|
+
"""
|
10
|
+
class LamestGenerator < Rails::Generators::NamedBase
|
11
|
+
source_root File.expand_path('../templates', __FILE__)
|
12
|
+
class_option :super, :type => :boolean, :default => false
|
13
|
+
|
14
|
+
def create_lamest
|
15
|
+
@lame = false
|
16
|
+
@awesome = false
|
17
|
+
@user_name = ask("What is your name?")
|
18
|
+
if yes?("Are you the lamest?")
|
19
|
+
@lame = true
|
20
|
+
end
|
21
|
+
if yes?("Are you awesome?")
|
22
|
+
@awesome = true
|
23
|
+
end
|
24
|
+
template 'lamest.html.erb', File.join('public', name, "#{"super_" if options[:super]}lamest.html")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
"""
|
28
|
+
And a file named "lib/generators/awesome/templates/lamest.html.erb" with:
|
29
|
+
"""
|
30
|
+
<%= @user_name %> <%= "is the lamest" if @lame %> and <%= "is not awesome!" if @awesome == false %>
|
31
|
+
"""
|
32
|
+
|
33
|
+
Scenario: A spec that runs the entire generator
|
34
|
+
Given a file named "spec/generators/lamest_generator_spec.rb" with:
|
35
|
+
"""
|
36
|
+
require "rails_helper"
|
37
|
+
require 'generators/awesome/lamest_generator'
|
38
|
+
|
39
|
+
describe LamestGenerator do
|
40
|
+
describe 'invoke' do
|
41
|
+
before do
|
42
|
+
gen = generator %w(my_dir)
|
43
|
+
set_shell_prompt_responses(gen, { :ask => "Thomas", :yes? => [true, false] })
|
44
|
+
run_generator
|
45
|
+
end
|
46
|
+
describe 'public/my_dir/lamest.html' do
|
47
|
+
subject { file('public/my_dir/lamest.html') }
|
48
|
+
it { expect(subject).to exist }
|
49
|
+
it { expect(subject).to contain 'Thomas is the lamest and is not awesome!' }
|
50
|
+
it { expect(subject).to_not contain 'This text is not in the file' }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
"""
|
55
|
+
When I run `rake spec`
|
56
|
+
Then the output should contain "3 examples, 0 failures"
|
@@ -41,7 +41,7 @@ Feature: generator spec
|
|
41
41
|
describe 'app/models/post.rb' do
|
42
42
|
subject { file('app/models/post.rb') }
|
43
43
|
it { expect(subject).to exist }
|
44
|
-
it { expect(subject).to contain 'class Post <
|
44
|
+
it { expect(subject).to contain 'class Post < ' }
|
45
45
|
end
|
46
46
|
end
|
47
47
|
"""
|
data/features/support/env.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
require 'aruba/cucumber'
|
2
2
|
|
3
3
|
Before do
|
4
|
-
|
4
|
+
if RUBY_VERSION == "1.9.3"
|
5
|
+
@aruba_timeout_seconds = 60
|
6
|
+
else
|
7
|
+
@aruba_timeout_seconds = 30
|
8
|
+
end
|
5
9
|
end
|
6
10
|
|
7
11
|
def aruba_path(file_or_dir, source_foldername)
|
@@ -25,7 +29,7 @@ def copy_to_aruba_from(gem_or_app_name)
|
|
25
29
|
}
|
26
30
|
|
27
31
|
rspec_version = ENV['RSPEC_VERSION']
|
28
|
-
rspec_major_version = (rspec_version && rspec_version != 'master') ? rspec_version.
|
32
|
+
rspec_major_version = (rspec_version && rspec_version != 'master') ? rspec_version.scan(/\d+/).first : '3'
|
29
33
|
|
30
34
|
Dir["tmp/#{gem_or_app_name}/*"].each do |file_or_dir|
|
31
35
|
if !(file_or_dir =~ /\/spec$/)
|
@@ -38,7 +42,7 @@ def copy_to_aruba_from(gem_or_app_name)
|
|
38
42
|
if rspec_major_version == '2'
|
39
43
|
# rspec 2.x does not create rails_helper.rb so we create a symlink to avoid cluttering tests
|
40
44
|
write_symlink("tmp/#{gem_or_app_name}/spec/spec_helper.rb", gem_or_app_name, 'rails_helper.rb')
|
41
|
-
elsif rspec_major_version
|
45
|
+
elsif rspec_major_version >= '3'
|
42
46
|
write_symlink("tmp/#{gem_or_app_name}/spec/rails_helper.rb", gem_or_app_name)
|
43
47
|
end
|
44
48
|
end
|
@@ -1,2 +1,21 @@
|
|
1
|
-
|
1
|
+
rspec_version = ENV['RSPEC_VERSION']
|
2
|
+
rspec_major_version = (rspec_version && rspec_version != 'master') ? rspec_version.scan(/\d+/).first : '3'
|
3
|
+
|
4
|
+
if rspec_version == 'master'
|
5
|
+
gem "rspec-rails", :git => 'git://github.com/rspec/rspec-rails.git'
|
6
|
+
gem "rspec", :git => 'git://github.com/rspec/rspec.git'
|
7
|
+
gem "rspec-core", :git => 'git://github.com/rspec/rspec-core.git'
|
8
|
+
gem "rspec-expectations", :git => 'git://github.com/rspec/rspec-expectations.git'
|
9
|
+
gem "rspec-mocks", :git => 'git://github.com/rspec/rspec-mocks.git'
|
10
|
+
gem "rspec-collection_matchers", :git => 'git://github.com/rspec/rspec-collection_matchers.git'
|
11
|
+
gem "rspec-support", :git => 'git://github.com/rspec/rspec-support.git'
|
12
|
+
else
|
13
|
+
gem 'rspec-rails', rspec_version
|
14
|
+
end
|
15
|
+
|
16
|
+
gem "i18n", '< 0.7.0' if RUBY_VERSION < '1.9.3'
|
2
17
|
gem 'ammeter', :path=>'../..'
|
18
|
+
if defined?(Rails) && Rails::VERSION::STRING.to_f < 4
|
19
|
+
# Execjs is causing problems on 1.8.7
|
20
|
+
gem 'execjs', '~> 2.0.0'
|
21
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
rspec_version = ENV['RSPEC_VERSION']
|
4
|
-
|
3
|
+
rspec_version = ENV['RSPEC_VERSION'] || '~> 4.0'
|
4
|
+
rails_version = ENV['RAILS_VERSION'] || '>= 5.1.0'
|
5
5
|
|
6
6
|
if rspec_version == 'master'
|
7
7
|
gem "rspec-rails", :git => 'git://github.com/rspec/rspec-rails.git'
|
@@ -13,21 +13,12 @@ if rspec_version == 'master'
|
|
13
13
|
gem "rspec-support", :git => 'git://github.com/rspec/rspec-support.git'
|
14
14
|
else
|
15
15
|
gem 'rspec-rails', rspec_version
|
16
|
-
gem 'rspec', rspec_version
|
17
|
-
end
|
18
|
-
|
19
|
-
case rspec_major_version
|
20
|
-
when '2'
|
21
|
-
gem 'rails', '~> 3.2'
|
22
|
-
when '3'
|
23
|
-
gem 'rails', '>= 4.1'
|
24
|
-
else
|
25
|
-
raise "rspec version #{rspec_version} is not supported"
|
26
16
|
end
|
27
17
|
|
18
|
+
gem 'rails', rails_version
|
28
19
|
|
29
20
|
# Specify your gem's dependencies in my_gem.gemspec
|
30
21
|
gemspec
|
31
22
|
|
32
23
|
# we cannot add a development dependency with a path to my_gem.gemspec
|
33
|
-
gem 'ammeter', :path => '../..'
|
24
|
+
gem 'ammeter', :path => '../..'
|
@@ -1,7 +1,13 @@
|
|
1
1
|
require 'bundler/setup'
|
2
2
|
require 'rails/all'
|
3
|
-
|
4
|
-
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
|
5
|
+
|
6
|
+
begin
|
7
|
+
# This prevents 'uninitialized constant Jquery::Rails::Railtie::PROTOTYPE_JS (NameError)'
|
8
|
+
require 'jquery/rails'
|
9
|
+
rescue LoadError
|
10
|
+
end
|
5
11
|
|
6
12
|
module MyRailsGem
|
7
13
|
module TestApp
|
@@ -1,7 +1,7 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
rspec_version = ENV['RSPEC_VERSION']
|
4
|
-
|
3
|
+
rspec_version = ENV['RSPEC_VERSION'] || '~> 4.0'
|
4
|
+
rails_version = ENV['RAILS_VERSION'] || '>= 5.1.0'
|
5
5
|
|
6
6
|
if rspec_version == 'master'
|
7
7
|
gem "rspec-rails", :git => 'git://github.com/rspec/rspec-rails.git'
|
@@ -13,21 +13,12 @@ if rspec_version == 'master'
|
|
13
13
|
gem "rspec-support", :git => 'git://github.com/rspec/rspec-support.git'
|
14
14
|
else
|
15
15
|
gem 'rspec-rails', rspec_version
|
16
|
-
gem 'rspec', rspec_version
|
17
|
-
end
|
18
|
-
|
19
|
-
case rspec_major_version
|
20
|
-
when '2'
|
21
|
-
gem 'railties', '~> 3.2'
|
22
|
-
when '3'
|
23
|
-
gem 'railties', '>= 4.1'
|
24
|
-
else
|
25
|
-
raise "rspec version #{rspec_version} is not supported"
|
26
16
|
end
|
27
17
|
|
18
|
+
gem 'railties', rails_version
|
28
19
|
|
29
20
|
# Specify your gem's dependencies in my_gem.gemspec
|
30
21
|
gemspec
|
31
22
|
|
32
23
|
# we cannot add a development dependency with a path to my_gem.gemspec
|
33
|
-
gem 'ammeter', :path => '../..'
|
24
|
+
gem 'ammeter', :path => '../..'
|
data/lib/ammeter/init.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
require 'rspec/core'
|
2
|
+
require 'rspec/rails'
|
2
3
|
if RSpec::Core::Version::STRING < '3'
|
3
4
|
require 'ammeter/rspec/rspec_2_compatibility' # if rspec2
|
4
5
|
end
|
5
6
|
require 'rails'
|
7
|
+
require 'ammeter/output_capturer.rb'
|
6
8
|
require 'ammeter/rspec/generator/example.rb'
|
7
9
|
require 'ammeter/rspec/generator/matchers.rb'
|
8
10
|
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Ammeter
|
2
|
+
class OutputCapturer
|
3
|
+
# Is this thread safe!?!?
|
4
|
+
# This won't work with sub-processes
|
5
|
+
def self.capture(io, &block)
|
6
|
+
case io
|
7
|
+
when :stdout
|
8
|
+
capture_stdout(&block)
|
9
|
+
when :stderr
|
10
|
+
capture_stderr(&block)
|
11
|
+
else
|
12
|
+
raise "Unknown IO #{io}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.capture_stdout(&block)
|
17
|
+
captured_stream = StringIO.new
|
18
|
+
|
19
|
+
orginal_io, $stdout = $stdout, captured_stream
|
20
|
+
|
21
|
+
block.call
|
22
|
+
|
23
|
+
captured_stream.string
|
24
|
+
ensure
|
25
|
+
$stdout = orginal_io
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.capture_stderr(&block)
|
29
|
+
captured_stream = StringIO.new
|
30
|
+
|
31
|
+
orginal_io, $stderr = $stderr, captured_stream
|
32
|
+
|
33
|
+
block.call
|
34
|
+
|
35
|
+
captured_stream.string
|
36
|
+
ensure
|
37
|
+
$stderr = orginal_io
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'rspec/core'
|
2
2
|
require 'ammeter/rspec/generator/example/generator_example_group'
|
3
|
+
require 'ammeter/rspec/generator/example/generator_example_helpers'
|
3
4
|
|
4
5
|
RSpec::configure do |c|
|
5
6
|
def c.escaped_path(*parts)
|
@@ -8,14 +9,17 @@ RSpec::configure do |c|
|
|
8
9
|
|
9
10
|
generator_path_regex = c.escaped_path(%w[spec generators])
|
10
11
|
if RSpec::Core::Version::STRING >= '3'
|
12
|
+
c.include Ammeter::RSpec::Rails::GeneratorExampleHelpers,
|
13
|
+
:type => :generator
|
14
|
+
c.include Ammeter::RSpec::Rails::GeneratorExampleGroup,
|
15
|
+
:type => :generator
|
11
16
|
c.include Ammeter::RSpec::Rails::GeneratorExampleGroup,
|
12
|
-
:type => :generator,
|
13
17
|
:file_path => lambda { |file_path, metadata|
|
14
18
|
metadata[:type].nil? && generator_path_regex =~ file_path
|
15
19
|
}
|
16
20
|
|
17
21
|
else #rspec2
|
18
|
-
|
22
|
+
c.include Ammeter::RSpec::Rails::GeneratorExampleHelpers, :type => :generator
|
19
23
|
c.include Ammeter::RSpec::Rails::GeneratorExampleGroup, :type => :generator, :example_group => {
|
20
24
|
:file_path => generator_path_regex
|
21
25
|
}
|
@@ -1,7 +1,5 @@
|
|
1
1
|
require 'rails/generators'
|
2
2
|
require 'active_support/core_ext'
|
3
|
-
require 'rspec/rails/adapters'
|
4
|
-
require 'rspec/rails/example/rails_example_group'
|
5
3
|
require 'tmpdir'
|
6
4
|
require 'fileutils'
|
7
5
|
|
@@ -13,7 +11,7 @@ module Ammeter
|
|
13
11
|
extend ActiveSupport::Concern
|
14
12
|
include ::RSpec::Rails::RailsExampleGroup
|
15
13
|
|
16
|
-
DELEGATED_METHODS = [:
|
14
|
+
DELEGATED_METHODS = [:destination_root, :current_path, :generator_class]
|
17
15
|
module ClassMethods
|
18
16
|
mattr_accessor :test_unit_test_case_delegate
|
19
17
|
delegate :default_arguments, :to => :'self.test_unit_test_case_delegate'
|
@@ -44,12 +42,12 @@ module Ammeter
|
|
44
42
|
end
|
45
43
|
|
46
44
|
def run_generator(given_args=self.default_arguments, config={})
|
47
|
-
capture(:stdout) { generator(given_args, config).invoke_all }
|
45
|
+
OutputCapturer.capture(:stdout) { generator(given_args, config).invoke_all }
|
48
46
|
end
|
49
47
|
end
|
50
48
|
|
51
49
|
def invoke_task name
|
52
|
-
capture(:stdout) { generator.invoke_task(generator_class.all_tasks[name.to_s]) }
|
50
|
+
OutputCapturer.capture(:stdout) { generator.invoke_task(generator_class.all_tasks[name.to_s]) }
|
53
51
|
end
|
54
52
|
|
55
53
|
included do
|
@@ -65,8 +63,6 @@ module Ammeter
|
|
65
63
|
::Rails::Generators::TestCase.destination File.expand_path('ammeter', Dir.tmpdir)
|
66
64
|
initialize_delegate
|
67
65
|
|
68
|
-
subject { generator }
|
69
|
-
|
70
66
|
before do
|
71
67
|
self.class.initialize_delegate
|
72
68
|
prepare_destination
|
@@ -86,7 +82,10 @@ module Ammeter
|
|
86
82
|
migration_file = "#{File.dirname(file_path)}/TIMESTAMP_#{File.basename(file_path)}" if migration_file.nil?
|
87
83
|
migration_file
|
88
84
|
end
|
85
|
+
def subject
|
86
|
+
generator
|
87
|
+
end
|
89
88
|
end
|
90
89
|
end
|
91
90
|
end
|
92
|
-
end
|
91
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
|
4
|
+
module Ammeter
|
5
|
+
module RSpec
|
6
|
+
module Rails
|
7
|
+
# Delegates to Rails::Generators::TestCase to work with RSpec.
|
8
|
+
module GeneratorExampleHelpers
|
9
|
+
|
10
|
+
# Sets return values for basic shell commands (ex. ask, yes?, no?).
|
11
|
+
# Does this by mocking return values using RSpec's `and_return` method
|
12
|
+
#
|
13
|
+
# ===== Parameters =====
|
14
|
+
# Generator w/ @shell attribute
|
15
|
+
# Hash { command_name: input_value, ask: "Testing", yes?: true }
|
16
|
+
# The values for each element in the hash can be set as an array as well.
|
17
|
+
# This will allow for different return values upon each call.
|
18
|
+
#
|
19
|
+
# ex. set_shell_prompt_responses(generator, { yes?: [true, false] })
|
20
|
+
# would respond with true to the first yes? call, but false to the second
|
21
|
+
def set_shell_prompt_responses(generator, command_args={})
|
22
|
+
command_args.each do |k,v|
|
23
|
+
allow(generator.shell).to receive(k.to_sym).and_return(*v)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -30,7 +30,11 @@ RSpec::Matchers.define :have_correct_syntax do
|
|
30
30
|
require 'ostruct'
|
31
31
|
|
32
32
|
begin
|
33
|
-
|
33
|
+
if Rails::VERSION::STRING < "6.0"
|
34
|
+
view = ActionView::Template::Handlers::ERB.call(OpenStruct.new(:source => code))
|
35
|
+
else
|
36
|
+
view = ActionView::Template::Handlers::ERB.call(OpenStruct.new, code)
|
37
|
+
end
|
34
38
|
eval('__crash_me__; ' + view)
|
35
39
|
rescue SyntaxError
|
36
40
|
false
|
data/lib/ammeter/version.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module Ammeter::RSpec::Rails
|
4
|
+
describe GeneratorExampleHelpers do
|
5
|
+
include GeneratorExampleHelpers
|
6
|
+
let(:generator) { MockGenerator.new }
|
7
|
+
|
8
|
+
it "mocks return value of ask to response" do
|
9
|
+
set_shell_prompt_responses(generator, { :ask => "response" })
|
10
|
+
expect(generator.shell.ask).to eq("response")
|
11
|
+
end
|
12
|
+
context "arguments contain multiple shell commands" do
|
13
|
+
it "mocks return values response for ask and true for yes?" do
|
14
|
+
set_shell_prompt_responses(generator, { :ask => "response", :yes? => true })
|
15
|
+
expect(generator.shell.ask).to eq("response")
|
16
|
+
expect(generator.shell.yes?).to be true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
context "argument shell command contains an array" do
|
20
|
+
it "mocks sequential return values response1 and response2" do
|
21
|
+
set_shell_prompt_responses(generator, { :ask => ["response1", "response2"] })
|
22
|
+
expect(generator.shell.ask).to eq("response1")
|
23
|
+
expect(generator.shell.ask).to eq("response2")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rails/all'
|
2
2
|
require 'rspec/rails'
|
3
3
|
|
4
|
-
ActiveRecord::Base.establish_connection adapter
|
4
|
+
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
|
5
5
|
|
6
6
|
module TestApp
|
7
7
|
class Application < Rails::Application
|
@@ -22,4 +22,4 @@ end
|
|
22
22
|
|
23
23
|
def stub_file(filename, content)
|
24
24
|
allow(File).to receive(:read).with(filename).and_return(content)
|
25
|
-
end
|
25
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Used in generator_example_helpers_spec.rb
|
2
|
+
# set_shell_prompt_responses requires a generator w/ a shell
|
3
|
+
# as an argument. This class defines that generator w/ a shell.
|
4
|
+
class MockGenerator
|
5
|
+
attr_accessor :shell
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@shell = Object.new
|
9
|
+
end
|
10
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ammeter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Rothenberg
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -213,8 +213,9 @@ executables: []
|
|
213
213
|
extensions: []
|
214
214
|
extra_rdoc_files: []
|
215
215
|
files:
|
216
|
+
- ".github/workflows/ci.yml"
|
216
217
|
- ".gitignore"
|
217
|
-
- ".
|
218
|
+
- ".ruby-version"
|
218
219
|
- Gemfile
|
219
220
|
- History.md
|
220
221
|
- LICENSE.txt
|
@@ -223,6 +224,7 @@ files:
|
|
223
224
|
- ammeter.gemspec
|
224
225
|
- features/generator_in_a_gem.feature
|
225
226
|
- features/generator_spec.feature
|
227
|
+
- features/generator_with_shell_prompts_spec.feature
|
226
228
|
- features/hooking_into_other_generators.feature
|
227
229
|
- features/support/aruba_timeout.rb
|
228
230
|
- features/support/env.rb
|
@@ -238,9 +240,11 @@ files:
|
|
238
240
|
- features/templates/rspec.rake
|
239
241
|
- lib/ammeter.rb
|
240
242
|
- lib/ammeter/init.rb
|
243
|
+
- lib/ammeter/output_capturer.rb
|
241
244
|
- lib/ammeter/railtie.rb
|
242
245
|
- lib/ammeter/rspec/generator/example.rb
|
243
246
|
- lib/ammeter/rspec/generator/example/generator_example_group.rb
|
247
|
+
- lib/ammeter/rspec/generator/example/generator_example_helpers.rb
|
244
248
|
- lib/ammeter/rspec/generator/matchers.rb
|
245
249
|
- lib/ammeter/rspec/generator/matchers/be_a_migration.rb
|
246
250
|
- lib/ammeter/rspec/generator/matchers/contain.rb
|
@@ -250,6 +254,7 @@ files:
|
|
250
254
|
- lib/ammeter/rspec/rspec_2_compatibility.rb
|
251
255
|
- lib/ammeter/version.rb
|
252
256
|
- spec/ammeter/rspec/generator/example/generator_example_group_spec.rb
|
257
|
+
- spec/ammeter/rspec/generator/example/generator_example_helpers_spec.rb
|
253
258
|
- spec/ammeter/rspec/generator/matchers/be_a_migration_spec.rb
|
254
259
|
- spec/ammeter/rspec/generator/matchers/contain_spec.rb
|
255
260
|
- spec/ammeter/rspec/generator/matchers/exist_spec.rb
|
@@ -259,10 +264,11 @@ files:
|
|
259
264
|
- spec/support/helpers.rb
|
260
265
|
- spec/support/matchers.rb
|
261
266
|
- spec/support/memoized_helpers_rspec2compatibility.rb
|
267
|
+
- spec/support/mock_generator.rb
|
262
268
|
homepage: ''
|
263
269
|
licenses: []
|
264
270
|
metadata: {}
|
265
|
-
post_install_message:
|
271
|
+
post_install_message:
|
266
272
|
rdoc_options: []
|
267
273
|
require_paths:
|
268
274
|
- lib
|
@@ -277,14 +283,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
277
283
|
- !ruby/object:Gem::Version
|
278
284
|
version: '0'
|
279
285
|
requirements: []
|
280
|
-
rubyforge_project:
|
281
|
-
rubygems_version: 2.
|
282
|
-
signing_key:
|
286
|
+
rubyforge_project:
|
287
|
+
rubygems_version: 2.7.6.2
|
288
|
+
signing_key:
|
283
289
|
specification_version: 4
|
284
290
|
summary: Write specs for your Rails 3+ generators
|
285
291
|
test_files:
|
286
292
|
- features/generator_in_a_gem.feature
|
287
293
|
- features/generator_spec.feature
|
294
|
+
- features/generator_with_shell_prompts_spec.feature
|
288
295
|
- features/hooking_into_other_generators.feature
|
289
296
|
- features/support/aruba_timeout.rb
|
290
297
|
- features/support/env.rb
|
@@ -299,6 +306,7 @@ test_files:
|
|
299
306
|
- features/templates/my_railties_gem/spec/spec_helper.rb
|
300
307
|
- features/templates/rspec.rake
|
301
308
|
- spec/ammeter/rspec/generator/example/generator_example_group_spec.rb
|
309
|
+
- spec/ammeter/rspec/generator/example/generator_example_helpers_spec.rb
|
302
310
|
- spec/ammeter/rspec/generator/matchers/be_a_migration_spec.rb
|
303
311
|
- spec/ammeter/rspec/generator/matchers/contain_spec.rb
|
304
312
|
- spec/ammeter/rspec/generator/matchers/exist_spec.rb
|
@@ -308,3 +316,4 @@ test_files:
|
|
308
316
|
- spec/support/helpers.rb
|
309
317
|
- spec/support/matchers.rb
|
310
318
|
- spec/support/memoized_helpers_rspec2compatibility.rb
|
319
|
+
- spec/support/mock_generator.rb
|
data/.travis.yml
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
script: "bundle exec rake ci"
|
2
|
-
rvm:
|
3
|
-
- ruby-head
|
4
|
-
- 2.1.1
|
5
|
-
- 2.0.0
|
6
|
-
- 1.9.3
|
7
|
-
- 1.8.7
|
8
|
-
- jruby-head
|
9
|
-
- jruby-19mode
|
10
|
-
- rbx-19mode
|
11
|
-
env:
|
12
|
-
- RSPEC_VERSION=master
|
13
|
-
- RSPEC_VERSION=2.99.0.beta2
|
14
|
-
- RSPEC_VERSION=2.14
|
15
|
-
matrix:
|
16
|
-
allow_failures:
|
17
|
-
- rvm: 1.8.7
|
18
|
-
- rvm: 1.9.3
|
19
|
-
- rvm: ruby-head
|
20
|
-
- rvm: jruby-head
|
21
|
-
- rvm: jruby-19mode
|
22
|
-
- rvm: rbx-19mode
|