Skip to content

Commit 72c1d87

Browse files
Merge branch 'master' into fix-tmpdir
2 parents 5621396 + fea0bc6 commit 72c1d87

File tree

5 files changed

+92
-16
lines changed

5 files changed

+92
-16
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
source "http://rubygems.org"
22

33
platforms :rbx do
4-
gem 'rubysl', '~> 2.0'
4+
# gem 'rubysl', '~> 2.2'
55
gem 'rubinius-developer_tools'
66
end
77

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Spec (files in `spec/lib/generators` are recognized as generator type example gr
1616

1717
```ruby
1818
# spec/lib/generators/test/test_generator_spec.rb
19-
19+
2020
require "generator_spec"
2121

2222
describe TestGenerator, type: :generator do
@@ -33,13 +33,13 @@ describe TestGenerator, type: :generator do
3333
end
3434
end
3535
```
36-
36+
3737
An RSpec file matching DSL is also provided, taken with permission from [beard](https://github.com/carlhuda/beard/blob/master/spec/support/matcher.rb) by [carlhuda](https://github.com/carlhuda).
3838

3939
```ruby
4040
describe TestGenerator, "using custom matcher", type: :generator do
4141
destination File.expand_path("../../tmp", __FILE__)
42-
42+
4343
before do
4444
prepare_destination
4545
run_generator
@@ -52,6 +52,7 @@ describe TestGenerator, "using custom matcher", type: :generator do
5252
directory "initializers" do
5353
file "test.rb" do
5454
contains "# Initializer"
55+
does_not_contain "Something else"
5556
end
5657
end
5758
end
@@ -60,6 +61,7 @@ describe TestGenerator, "using custom matcher", type: :generator do
6061
file "123_create_tests.rb"
6162
migration "create_tests" do
6263
contains "class TestMigration"
64+
does_not_contain "Something else"
6365
end
6466
end
6567
end

lib/generator_spec/matcher.rb

+43-11
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ module GeneratorSpec
22
module Matcher
33
# Taken (with permission) from beard by Yahuda Katz
44
# https://github.com/carlhuda/beard
5-
5+
66
class File
7+
8+
def description
9+
'file attributes and content'
10+
end
11+
712
def initialize(name, &block)
813
@contents = []
914
@name = name
15+
@negative_contents = []
1016

1117
if block_given?
1218
instance_eval(&block)
@@ -17,16 +23,20 @@ def contains(text)
1723
@contents << text
1824
end
1925

26+
def does_not_contain(text)
27+
@negative_contents << text
28+
end
29+
2030
def matches?(root)
2131
unless root.join(@name).exist?
2232
throw :failure, root.join(@name)
2333
end
2434

2535
check_contents(root.join(@name))
2636
end
27-
37+
2838
protected
29-
39+
3040
def check_contents(file)
3141
contents = ::File.read(file)
3242

@@ -35,22 +45,32 @@ def check_contents(file)
3545
throw :failure, [file, string, contents]
3646
end
3747
end
48+
49+
@negative_contents.each do |string|
50+
if contents.include?(string)
51+
throw :failure, [:not, file, string, contents]
52+
end
53+
end
3854
end
3955
end
40-
56+
4157
class Migration < File
58+
def description
59+
'valid migration file'
60+
end
61+
4262
def matches?(root)
4363
file_name = migration_file_name(root, @name)
44-
64+
4565
unless file_name && file_name.exist?
4666
throw :failure, @name
4767
end
48-
68+
4969
check_contents(file_name)
5070
end
51-
71+
5272
protected
53-
73+
5474
def migration_file_name(root, name) #:nodoc:
5575
directory, file_name = ::File.dirname(root.join(name)), ::File.basename(name).sub(/\.rb$/, '')
5676
migration = Dir.glob("#{directory}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
@@ -61,6 +81,10 @@ def migration_file_name(root, name) #:nodoc:
6181
class Directory
6282
attr_reader :tree
6383

84+
def description
85+
'has directory structure'
86+
end
87+
6488
def initialize(root = nil, &block)
6589
@tree = {}
6690
@negative_tree = []
@@ -83,7 +107,7 @@ def no_file(name)
83107
def location(name)
84108
[@root, name].compact.join("/")
85109
end
86-
110+
87111
def migration(name, &block)
88112
@tree[name] = Migration.new(location(name), &block)
89113
end
@@ -110,9 +134,17 @@ def matches?(root)
110134
end
111135

112136
class Root < Directory
137+
def description
138+
'have specified directory structure'
139+
end
140+
113141
def failure_message
114142
if @failure.is_a?(Array) && @failure[0] == :not
115-
"Structure should not have had #{@failure[1]}, but it did"
143+
if @failure.length > 2
144+
"Structure should have #{@failure[1]} without #{@failure[2]}. It had:\n#{@failure[3]}"
145+
else
146+
"Structure should not have had #{@failure[1]}, but it did"
147+
end
116148
elsif @failure.is_a?(Array)
117149
"Structure should have #{@failure[0]} with #{@failure[1]}. It had:\n#{@failure[2]}"
118150
else
@@ -136,4 +168,4 @@ def have_structure(&block)
136168
Root.new(&block)
137169
end
138170
end
139-
end
171+
end

lib/generator_spec/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module GeneratorSpec
2-
VERSION = '0.9.4'
2+
VERSION = '0.9.5'
33
end

spec/generator_spec/matcher_spec.rb

+42
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module Matcher
7272
let(:location) { TMP_ROOT.join('test_file') }
7373

7474
context 'with no contains' do
75+
7576
it 'doesnt throw if the file exists' do
7677
write_file(location, '')
7778
expect {
@@ -84,6 +85,7 @@ module Matcher
8485
file.matches?(TMP_ROOT)
8586
}.to throw_symbol(:failure)
8687
end
88+
8789
end
8890

8991
context 'with contains' do
@@ -105,6 +107,26 @@ module Matcher
105107
}.to throw_symbol(:failure)
106108
end
107109
end
110+
111+
context 'with does_not_contain' do
112+
before do
113+
write_file(location, 'class CreatePosts')
114+
end
115+
116+
it 'doesnt throw if the contents dont include the string' do
117+
file.does_not_contain 'PostsMigration'
118+
expect {
119+
file.matches?(TMP_ROOT)
120+
}.to_not throw_symbol
121+
end
122+
123+
it 'throws :failure if the contents include the string' do
124+
file.does_not_contain 'CreatePosts'
125+
expect {
126+
file.matches?(TMP_ROOT)
127+
}.to throw_symbol(:failure)
128+
end
129+
end
108130
end
109131
end
110132

@@ -151,6 +173,26 @@ module Matcher
151173
}.to throw_symbol(:failure)
152174
end
153175
end
176+
177+
context 'with does_not_contain' do
178+
before do
179+
write_file(location, 'class CreatePosts')
180+
end
181+
182+
it 'doesnt throw if the migration doesnt include the given content' do
183+
migration.does_not_contain('CreateNotes')
184+
expect {
185+
migration.matches?(TMP_ROOT)
186+
}.to_not throw_symbol
187+
end
188+
189+
it 'throws failure if the migration includes the given content' do
190+
migration.does_not_contain('CreatePosts')
191+
expect {
192+
migration.matches?(TMP_ROOT)
193+
}.to throw_symbol(:failure)
194+
end
195+
end
154196
end
155197
end
156198

0 commit comments

Comments
 (0)