templater 0.1.6 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,7 +46,7 @@ module Templater
46
46
  def matches?(actual)
47
47
  @actual = actual
48
48
  # Satisfy expectation here. Return false or raise an error if it's not met.
49
- @actual.actions.map{|t| t.destination }.include?(@expected)
49
+ @actual.all_actions.map{|t| t.destination }.include?(@expected)
50
50
  end
51
51
 
52
52
  def failure_message
@@ -0,0 +1,105 @@
1
+ require File.dirname(__FILE__) / '..' / 'spec_helper'
2
+
3
+ describe Templater::Actions::EmptyDirectory do
4
+
5
+ before do
6
+ @generator = mock('a generator')
7
+ @generator.stub!(:source_root).and_return('/tmp/source')
8
+ @generator.stub!(:destination_root).and_return('/tmp/destination')
9
+ end
10
+
11
+ describe '.new' do
12
+ it "sets name and destination" do
13
+ Templater::Actions::EmptyDirectory.new(@generator, :monkey, '/path/to/destination').
14
+ name.should == :monkey
15
+ end
16
+
17
+ it 'sets destination' do
18
+ Templater::Actions::EmptyDirectory.new(@generator, :monkey, '/path/to/destination').
19
+ destination.should == '/path/to/destination'
20
+ end
21
+ end
22
+
23
+ describe '#relative_destination' do
24
+ it "returns the destination relative to the generator's destination root" do
25
+ @generator.stub!(:destination_root).and_return('/path/to')
26
+ file = Templater::Actions::EmptyDirectory.new(@generator, :monkey, '/path/to/destination/with/some/more/subdirs')
27
+ file.relative_destination.should == 'destination/with/some/more/subdirs'
28
+ end
29
+ end
30
+
31
+ describe '#render' do
32
+ it 'does nothing for empty directories?'
33
+ end
34
+
35
+ describe '#exists?' do
36
+
37
+ it "should exist if the destination file exists" do
38
+ file = Templater::Actions::EmptyDirectory.new(@generator, :monkey, result_path('erb.rbs'))
39
+ file.should be_exists
40
+ end
41
+
42
+ it "should not exist if the destination file does not exist" do
43
+ file = Templater::Actions::EmptyDirectory.new(@generator, :monkey, result_path('some_weird_file.rbs'))
44
+ file.should_not be_exists
45
+ end
46
+ end
47
+
48
+ describe '#identical' do
49
+ it "should not be identical if the destination file doesn't exist" do
50
+ file = Templater::Actions::EmptyDirectory.new(@generator, :monkey, result_path('some_weird/path/that_does/not_exist'))
51
+ file.should_not be_identical
52
+ end
53
+
54
+ it "should not be identical if the destination file is not identical to the source file" do
55
+ file = Templater::Actions::EmptyDirectory.new(@generator, :monkey, result_path('simple_erb.rbs'))
56
+ file.should be_exists
57
+ file.should be_identical
58
+ end
59
+
60
+ it "should be identical if the destination file is identical to the source file" do
61
+ file= Templater::Actions::EmptyDirectory.new(@generator, :monkey, result_path('file.rbs'))
62
+ file.should be_exists
63
+ file.should be_identical
64
+ end
65
+ end
66
+
67
+ describe '#invoke!' do
68
+ it "should copy the source file to the destination" do
69
+ file = Templater::Actions::EmptyDirectory.new(@generator, :monkey, result_path('path/to/subdir/test2.rbs'))
70
+
71
+ file.invoke!
72
+
73
+ File.exists?(result_path('path/to/subdir/test2.rbs')).should be_true
74
+ end
75
+
76
+ it "should trigger before and after callbacks" do
77
+ @options = { :before => :ape, :after => :elephant }
78
+ file = Templater::Actions::EmptyDirectory.new(@generator, :monkey, result_path('path/to/subdir/test2.rbs'), @options)
79
+
80
+ @generator.should_receive(:ape).with(file).ordered
81
+ @generator.should_receive(:elephant).with(file).ordered
82
+
83
+ file.invoke!
84
+
85
+ File.exists?(result_path('path/to/subdir/test2.rbs')).should be_true
86
+ end
87
+
88
+ after do
89
+ FileUtils.rm_rf(result_path('path'))
90
+ end
91
+ end
92
+
93
+ describe '#revoke!' do
94
+ it "removes the destination directory" do
95
+ file = Templater::Actions::EmptyDirectory.new(@generator, :monkey, result_path('path/to/empty/subdir/'))
96
+
97
+ file.invoke!
98
+ File.exists?(result_path('path/to/empty/subdir/')).should be_true
99
+
100
+ file.revoke!
101
+ File.exists?(result_path('path/to/empty/subdir/')).should be_false
102
+ end
103
+ end
104
+
105
+ end
@@ -0,0 +1,112 @@
1
+ require File.dirname(__FILE__) / '..' / 'spec_helper'
2
+
3
+ describe Templater::Actions::File do
4
+
5
+ before do
6
+ @generator = mock('a generator')
7
+ @generator.stub!(:source_root).and_return('/tmp/source')
8
+ @generator.stub!(:destination_root).and_return('/tmp/destination')
9
+ end
10
+
11
+ describe '.new' do
12
+ it "should set name, source and destination" do
13
+ file = Templater::Actions::File.new(@generator, :monkey, '/path/to/source', '/path/to/destination')
14
+ file.name.should == :monkey
15
+ file.source.should == '/path/to/source'
16
+ file.destination.should == '/path/to/destination'
17
+ end
18
+ end
19
+
20
+ describe '#relative_destination' do
21
+ it "should get the destination relative to the generator's destination root" do
22
+ @generator.stub!(:destination_root).and_return('/path/to')
23
+ file = Templater::Actions::File.new(@generator, :monkey, '/path/to/source', '/path/to/destination/with/some/more/subdirs')
24
+ file.relative_destination.should == 'destination/with/some/more/subdirs'
25
+ end
26
+ end
27
+
28
+ describe '#render' do
29
+ it "should output the file" do
30
+ file = Templater::Actions::File.new(@generator, :monkey, template_path('simple_erb.rbt'), '/path/to/destination')
31
+ file.render.should == "test<%= 1+1 %>test"
32
+ end
33
+ end
34
+
35
+ describe '#exists?' do
36
+ it "should exist if the destination file exists" do
37
+ file = Templater::Actions::File.new(@generator, :monkey, template_path('simple.rbt'), result_path('erb.rbs'))
38
+ file.should be_exists
39
+ end
40
+
41
+ it "should not exist if the destination file does not exist" do
42
+ file = Templater::Actions::File.new(@generator, :monkey, template_path('simple.rbt'), result_path('some_weird_file.rbs'))
43
+ file.should_not be_exists
44
+ end
45
+ end
46
+
47
+ describe '#identical' do
48
+ it "should not be identical if the destination file doesn't exist" do
49
+ file = Templater::Actions::File.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('some_weird_file.rbs'))
50
+ file.should_not be_identical
51
+ end
52
+
53
+ it "should not be identical if the destination file is not identical to the source file" do
54
+ file = Templater::Actions::File.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('simple_erb.rbs'))
55
+ file.should be_exists
56
+ file.should_not be_identical
57
+ end
58
+
59
+ it "should be identical if the destination file is identical to the source file" do
60
+ file= Templater::Actions::File.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('file.rbs'))
61
+ file.should be_exists
62
+ file.should be_identical
63
+ end
64
+ end
65
+
66
+ describe '#invoke!' do
67
+ it "should copy the source file to the destination" do
68
+ file = Templater::Actions::File.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test2.rbs'))
69
+
70
+ file.invoke!
71
+
72
+ File.exists?(result_path('path/to/subdir/test2.rbs')).should be_true
73
+ FileUtils.identical?(template_path('simple_erb.rbt'), result_path('path/to/subdir/test2.rbs')).should be_true
74
+ end
75
+
76
+ it "should trigger before and after callbacks" do
77
+ @options = { :before => :ape, :after => :elephant }
78
+ file = Templater::Actions::File.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test2.rbs'), @options)
79
+
80
+ @generator.should_receive(:ape).with(file).ordered
81
+ @generator.should_receive(:elephant).with(file).ordered
82
+
83
+ file.invoke!
84
+ end
85
+
86
+ after do
87
+ FileUtils.rm_rf(result_path('path'))
88
+ end
89
+ end
90
+
91
+ describe '#revoke!' do
92
+ it "should remove the destination file" do
93
+ file = Templater::Actions::File.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test2.rbs'))
94
+
95
+ file.invoke!
96
+
97
+ File.exists?(result_path('path/to/subdir/test2.rbs')).should be_true
98
+ FileUtils.identical?(template_path('simple_erb.rbt'), result_path('path/to/subdir/test2.rbs')).should be_true
99
+
100
+ file.revoke!
101
+
102
+ File.exists?(result_path('path/to/subdir/test2.rbs')).should be_false
103
+ end
104
+
105
+ it "should do nothing when the destination file doesn't exist" do
106
+ file = Templater::Actions::File.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test2.rbs'))
107
+
108
+ lambda { file.revoke! }.should_not raise_error
109
+ end
110
+ end
111
+
112
+ end
@@ -0,0 +1,141 @@
1
+ require File.dirname(__FILE__) / '..' / 'spec_helper'
2
+
3
+ describe Templater::Actions::Template do
4
+
5
+ before do
6
+ @generator = mock('a generator')
7
+ @generator.stub!(:source_root).and_return('/tmp/source')
8
+ @generator.stub!(:destination_root).and_return('/tmp/destination')
9
+ end
10
+
11
+ describe '.new' do
12
+ it "should set name, source and destination" do
13
+ template = Templater::Actions::Template.new(@generator, :monkey, '/path/to/source', '/path/to/destination')
14
+ template.name.should == :monkey
15
+ template.source.should == '/path/to/source'
16
+ template.destination.should == '/path/to/destination'
17
+ end
18
+ end
19
+
20
+ describe '#relative_destination' do
21
+ it "should get the destination relative to the generator's destination root" do
22
+ @generator.stub!(:destination_root).and_return('/path/to')
23
+ template = Templater::Actions::Template.new(@generator, :monkey, '/path/to/source', '/path/to/destination/with/some/more/subdirs')
24
+ template.relative_destination.should == 'destination/with/some/more/subdirs'
25
+ end
26
+ end
27
+
28
+ describe '#render' do
29
+ it "should render a simple template" do
30
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple.rbt'), '/path/to/destination')
31
+ template.render.should == "Hello World"
32
+ end
33
+
34
+ it "should render some basic erb" do
35
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple_erb.rbt'), '/path/to/destination')
36
+ template.render.should == "test2test"
37
+ end
38
+
39
+ it "should render some erb and convert erb literals" do
40
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('literals_erb.rbt'), '/path/to/destination')
41
+ template.render.should == "test2test<%= 1+1 %>blah"
42
+ end
43
+
44
+ it "should render some erb fetching stuff from the context" do
45
+ @generator.should_receive(:funkydoodle).and_return('_doodle_')
46
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('erb.rbt'), '/path/to/destination')
47
+ template.render.should == "test_doodle_blah"
48
+ end
49
+ end
50
+
51
+ describe '#exists?' do
52
+ it "should exist if the destination file exists" do
53
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple.rbt'), result_path('erb.rbs'))
54
+ template.should be_exists
55
+ end
56
+
57
+ it "should not exist if the destination file does not exist" do
58
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple.rbt'), result_path('some_weird_file.rbs'))
59
+ template.should_not be_exists
60
+ end
61
+ end
62
+
63
+ describe '#identical' do
64
+ before do
65
+ @context = class << self; self end
66
+ end
67
+
68
+ it "should not be identical if the destination file doesn't exist" do
69
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('some_weird_file.rbs'))
70
+ template.should_not be_identical
71
+ end
72
+
73
+ it "should not be identical if the rendered content does not match the content of the file" do
74
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('random.rbs'))
75
+ template.should be_exists
76
+ template.should_not be_identical
77
+ end
78
+
79
+ it "should be identical if the rendered content matches the content of the file" do
80
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('simple_erb.rbs'))
81
+ template.should be_exists
82
+ template.should be_identical
83
+ end
84
+ end
85
+
86
+ describe '#invoke!' do
87
+ it "should render the template and copy it to the destination" do
88
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('test.rbs'))
89
+
90
+ template.invoke!
91
+
92
+ File.exists?(result_path('test.rbs')).should be_true
93
+ File.read(result_path('test.rbs')).should == "test2test"
94
+ end
95
+
96
+ it "should render the template and copy it to the destination, creating any required subdirectories" do
97
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test.rbs'))
98
+
99
+ template.invoke!
100
+
101
+ File.exists?(result_path('path/to/subdir/test.rbs')).should be_true
102
+ File.read(result_path('path/to/subdir/test.rbs')).should == "test2test"
103
+ end
104
+
105
+ it "should trigger before and after callbacks" do
106
+ @options = { :before => :ape, :after => :elephant }
107
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test.rbs'), @options)
108
+
109
+ @generator.should_receive(:ape).with(template).ordered
110
+ @generator.should_receive(:elephant).with(template).ordered
111
+
112
+ template.invoke!
113
+ end
114
+
115
+ after do
116
+ FileUtils.rm_rf(result_path('path'))
117
+ end
118
+ end
119
+
120
+ describe '#revoke!' do
121
+ it "should remove the destination file" do
122
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('test.rbs'))
123
+
124
+ template.invoke!
125
+
126
+ File.exists?(result_path('test.rbs')).should be_true
127
+ File.read(result_path('test.rbs')).should == "test2test"
128
+
129
+ template.revoke!
130
+
131
+ File.exists?(result_path('test.rbs')).should be_false
132
+ end
133
+
134
+ it "should do nothing when the destination file doesn't exist" do
135
+ template = Templater::Actions::Template.new(@generator, :monkey, template_path('simple_erb.rbt'), result_path('test.rbs'))
136
+
137
+ lambda { template.revoke! }.should_not raise_error
138
+ end
139
+ end
140
+
141
+ end
@@ -4,59 +4,108 @@ describe Templater::Generator, '#actions' do
4
4
 
5
5
  before do
6
6
  @generator_class = Class.new(Templater::Generator)
7
- @generator_class.class_eval do
8
- def source_root
9
- '/tmp/source'
10
- end
11
- end
7
+ @generator_class.stub!(:source_root).and_return('/tmp/source')
8
+ end
9
+
10
+ it "should return all actions" do
11
+ @generator_class.template :one, 'template1.rb'
12
+ @generator_class.template :two, 'template2.rb'
13
+ @generator_class.file :three, 'file1.rb'
14
+ @generator_class.empty_directory :four, 'file2.rb'
15
+
16
+ instance = @generator_class.new('/tmp')
17
+
18
+ instance.actions.should have_names(:one, :two, :three, :four)
19
+ end
20
+
21
+ it "should return only a certain type of action" do
22
+ @generator_class.template :one, 'template1.rb'
23
+ @generator_class.template :two, 'template2.rb'
24
+ @generator_class.file :three, 'file1.rb'
25
+ @generator_class.empty_directory :four, 'file2.rb'
26
+
27
+ instance = @generator_class.new('/tmp')
28
+
29
+ instance.actions(:templates).should have_names(:one, :two)
30
+ end
31
+ end
32
+
33
+ describe Templater::Generator, '#all_actions' do
34
+
35
+ before do
36
+ @generator_class = Class.new(Templater::Generator)
37
+ @generator_class.stub!(:source_root).and_return('/tmp/source')
38
+
39
+ @generator_class2 = Class.new(Templater::Generator)
40
+ @generator_class2.stub!(:source_root).and_return('/tmp/source')
12
41
 
13
- @generator1 = mock('a generator')
14
- @instance1 = mock('an instance of the generator')
15
- @generator1.stub!(:new).and_return(@instance1)
16
- @generator2 = mock('another generator')
17
- @instance2 = mock('an instance of another generator')
18
- @generator2.stub!(:new).and_return(@instance2)
42
+ @generator_class3 = Class.new(Templater::Generator)
43
+ @generator_class3.stub!(:source_root).and_return('/tmp/source')
19
44
 
20
- @manifold = mock('a manifold')
21
- @manifold.stub!(:generator).with(:one).and_return(@generator1)
22
- @manifold.stub!(:generator).with(:two).and_return(@generator2)
23
- @manifold.stub!(:generator).with(:three).and_return(@generator3)
45
+ @manifold = Object.new
46
+ @manifold.extend Templater::Manifold
47
+ @manifold.add(:one, @generator_class)
48
+ @manifold.add(:two, @generator_class2)
49
+ @manifold.add(:three, @generator_class3)
50
+ end
51
+
52
+ it "should return all actions" do
53
+ @generator_class.template :one, 'template1.rb'
54
+ @generator_class.template :two, 'template2.rb'
55
+ @generator_class.file :three, 'file1.rb'
56
+ @generator_class.empty_directory :four, 'file2.rb'
57
+ instance = @generator_class.new('/tmp')
24
58
 
25
- @generator_class.stub!(:manifold).and_return(@manifold)
59
+ instance.all_actions.should have_names(:one, :two, :three, :four)
26
60
  end
61
+
62
+ it "should return only a certain type of action" do
63
+ @generator_class.template :one, 'template1.rb'
64
+ @generator_class.template :two, 'template2.rb'
65
+ @generator_class.file :three, 'file1.rb'
66
+ @generator_class.empty_directory :four, 'file2.rb'
67
+
68
+ instance = @generator_class.new('/tmp')
69
+
70
+ instance.all_actions(:templates).should have_names(:one, :two)
71
+ end
72
+
73
+ it "should return all actions recursively for all invocations" do
74
+ @generator_class.invoke :two
75
+ @generator_class2.invoke :three
76
+
77
+ @generator_class.template :one, 'template1.rb'
78
+ @generator_class.file :two, 'file1.rb'
79
+ @generator_class.empty_directory :three, 'file2.rb'
80
+
81
+ @generator_class2.file :four, 'file2.rb'
82
+ @generator_class2.template :five, 'file2.rb'
83
+
84
+ @generator_class3.template :six, 'fds.rb'
85
+ @generator_class3.empty_directory :seven, 'dfsd.rb'
27
86
 
28
- it "should return all templates and files" do
29
87
  instance = @generator_class.new('/tmp')
30
- instance.should_receive(:templates).at_least(:once).and_return(['template1', 'template2'])
31
- instance.should_receive(:files).at_least(:once).and_return(['file1', 'file2'])
32
88
 
33
- instance.actions.should include('template1')
34
- instance.actions.should include('template2')
35
- instance.actions.should include('file1')
36
- instance.actions.should include('file2')
89
+ instance.all_actions.should have_names(:one, :two, :three, :four, :five, :six, :seven)
37
90
  end
38
91
 
39
- it "should return all templates and files recursively for all invocations" do
92
+ it "should return only a certain type of actions recursively for all invocations" do
93
+ @generator_class.invoke :two
94
+ @generator_class2.invoke :three
95
+
96
+ @generator_class.template :one, 'template1.rb'
97
+ @generator_class.file :two, 'file1.rb'
98
+ @generator_class.empty_directory :three, 'file2.rb'
99
+
100
+ @generator_class2.file :four, 'file2.rb'
101
+ @generator_class2.template :five, 'file2.rb'
102
+
103
+ @generator_class3.template :six, 'fds.rb'
104
+ @generator_class3.empty_directory :seven, 'dfsd.rb'
105
+
40
106
  instance = @generator_class.new('/tmp')
41
- instance.should_receive(:templates).at_least(:once).and_return(['template1', 'template2'])
42
- instance.should_receive(:files).at_least(:once).and_return(['file1', 'file2'])
43
- instance.should_receive(:empty_directories).at_least(:once).and_return(['public', 'bin'])
44
- instance.should_receive(:invocations).at_least(:once).and_return([@instance1, @instance2])
45
-
46
- @instance1.should_receive(:actions).at_least(:once).and_return(['subtemplate1', 'subfile1'])
47
- @instance2.should_receive(:actions).at_least(:once).and_return(['subtemplate2', 'subfile2'])
48
-
49
- instance.actions.should include('template1')
50
- instance.actions.should include('template2')
51
- instance.actions.should include('subtemplate1')
52
- instance.actions.should include('subtemplate2')
53
- instance.actions.should include('file1')
54
- instance.actions.should include('file2')
55
- instance.actions.should include('subfile1')
56
- instance.actions.should include('subfile2')
57
-
58
- instance.actions.should include('public')
59
- instance.actions.should include('bin')
107
+
108
+ instance.all_actions(:templates).should have_names(:one, :five, :six)
60
109
  end
61
110
 
62
111
  end