svn-fixture 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ module SvnFixture
2
+ # A Revision of the Repository. It can be added via Repository#revision to
3
+ # a specific Repository
4
+ #
5
+ # repo = SvnFixture::Repository.get('test') do
6
+ # revision(1, 'log msg')
7
+ # end
8
+ # repo.commit
9
+ #
10
+ # or specified in Repository#commit:
11
+ #
12
+ # rev = SvnFixture::Revision.new(1, 'log msg')
13
+ # SvnFixture::Repository.get('test').commit(rev)
14
+ class Revision
15
+ attr_reader :name
16
+
17
+ # Initialize a Revision (normally called by Repository#revision).
18
+ #
19
+ # - +name+: A name of the Revision, can be given in Array to
20
+ # Repository#commit instead of the Revision itself. Can be a revision
21
+ # number, but this does not affect the actual revision number in the
22
+ # Subversion repository.
23
+ # - +message+: Log message. Defaults to empty String.
24
+ # - +options+:
25
+ # - +:author+: The Revision's author
26
+ # - +:date+: The date and time of the commit
27
+ # - Optionally accepts a block. The block, if given, is run at the time
28
+ # #commit is called, within the context of the root directory of the
29
+ # Repository, which is an instance of SvnFixture::Directory. For example:
30
+ #
31
+ # SvnFixture::Revision.new(1, 'log msg') do
32
+ # dir('test') # Or any other SvnFixture::Directory instance method.
33
+ # end
34
+ def initialize(name, message = "", options = {}, &block)
35
+ @name, @message, @block = name, message, block
36
+ @author = options.delete(:author)
37
+ @date = SvnFixture.svn_time(options.delete(:date))
38
+ end
39
+
40
+ # Processes the changes made in this revision. Normally these would be made
41
+ # in a block given to Revision.new. #commit runs that block against the root
42
+ # directory of the working copy. This method is usually called by
43
+ # Repository#commit instead of directly. Also sets Revision properties for
44
+ # log message and, optionally, author and date, based on arguments
45
+ # to +.new+. If there are no changes, the commit fails and a warning to that
46
+ # effect.
47
+ #
48
+ # Only argument is an instance of SvnFixture::Repository that is the
49
+ # Repository to which this revision is committed.
50
+ def commit(repo)
51
+ root = Directory.new(repo.ctx, repo.wc_path)
52
+ root.instance_eval(&@block) if @block
53
+ ci = repo.ctx.ci(repo.wc_path)
54
+ unless ci.revision == Svn::Core::INVALID_REVNUM
55
+ rev = ci.revision
56
+ repo.repos.fs.set_prop('svn:log', @message, rev) if @message
57
+ repo.repos.fs.set_prop('svn:author', @author, rev) if @author
58
+ repo.repos.fs.set_prop('svn:date', @date, rev) if @date
59
+ else
60
+ puts "Warning: No change in revision #{name} (SvnFixture::Revision#commit)"
61
+ end
62
+ return true
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec'
2
+ require 'svn-fixture'
3
+
4
+ Spec::Runner.configure do |config|
5
+
6
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SvnFixture.config" do
4
+ before(:each) do
5
+ # Force to default state
6
+ SvnFixture.instance_variable_set(:@config, nil)
7
+ end
8
+
9
+ after(:all) do
10
+ # Force to default state for other specs
11
+ SvnFixture.instance_variable_set(:@config, nil)
12
+ end
13
+
14
+ it 'should initialize with CONFIG_DEFAULTS' do
15
+ SvnFixture.config.should == SvnFixture::CONFIG_DEFAULTS
16
+ end
17
+
18
+ it 'should be writable' do
19
+ SvnFixture.config[:base_path] = '/tmp/elsewhere'
20
+ SvnFixture.instance_variable_get(:@config)[:base_path].
21
+ should == '/tmp/elsewhere'
22
+ end
23
+
24
+ describe ':base_path' do
25
+ it 'should default to "#{Dir.tmpdir)/#{svn-fixture}"' do
26
+ SvnFixture.config[:base_path].should == File.join(Dir.tmpdir, 'svn-fixture')
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,194 @@
1
+ require 'spec_helper'
2
+
3
+ describe SvnFixture::Directory do
4
+ before(:each) do
5
+ @klass = SvnFixture::Directory
6
+ SvnFixture::Repository.destroy_all
7
+
8
+ # Set up repo
9
+ @repos = SvnFixture::repo('dir_test') do
10
+ revision(1, 'create directory and file') do
11
+ dir('test-dir') do
12
+ dir('subdir')
13
+ file('file.txt')
14
+ end
15
+ end
16
+ end
17
+ @repos.commit
18
+ @repos_path = @repos.instance_variable_get(:@repos_path)
19
+ @wc_path = @repos.instance_variable_get(:@wc_path)
20
+ @ctx = @repos.ctx
21
+ end
22
+
23
+ before(:each) do
24
+ @path = File.join(@wc_path, 'test-dir')
25
+ @full_repos_path = "file://#{File.join(@repos_path, 'test-dir')}"
26
+ @dir = @klass.new(@ctx, @path)
27
+ end
28
+
29
+ after(:all) do
30
+ SvnFixture::Repository.destroy_all
31
+ end
32
+
33
+ describe '#initialize' do
34
+ it 'should set ctx and path' do
35
+ dir = @klass.new(@ctx, '/tmp/path/')
36
+ dir.instance_variable_get(:@ctx).should == @ctx
37
+ dir.instance_variable_get(:@path).should == '/tmp/path/'
38
+ end
39
+
40
+ it 'should add a trailing slash to path if needed' do
41
+ dir = @klass.new(@ctx, '/tmp/path')
42
+ dir.instance_variable_get(:@path).should == '/tmp/path/'
43
+ end
44
+ end
45
+
46
+ describe '#dir' do
47
+ before(:each) do
48
+ @subdir_path = File.join(@path, 'subdir')
49
+ end
50
+
51
+ it 'should create a new directory if needed (and add to Subversion)' do
52
+ new_path = File.join(@path, 'subdir2')
53
+ File.exist?(new_path).should be_false
54
+ new_dir = @dir.dir('subdir2')
55
+ @ctx.commit(@wc_path)
56
+ File.exist?(new_path).should be_true
57
+ @repos.repos.fs.root.dir?('test-dir/subdir2').should be_true
58
+ end
59
+
60
+ it 'should use an existing directory' do
61
+ FileUtils.should_not_receive(:mkdir_p)
62
+ @ctx.should_not_receive(:add)
63
+ @dir.dir('subdir')
64
+ end
65
+
66
+ it 'should setup a new SvnFixture::Directory' do
67
+ SvnFixture::Directory.should_receive(:new).with(@ctx, @subdir_path)
68
+ @dir.dir('subdir')
69
+ end
70
+
71
+ it 'should return the Directory' do
72
+ sub = @dir.dir('subdir')
73
+ sub.should be_kind_of(SvnFixture::Directory)
74
+ sub.instance_variable_get(:@path).should == @subdir_path + '/'
75
+ end
76
+
77
+ it 'should run block if given' do
78
+ SvnFixture::File.should_receive(:new).with(@ctx, File.join(@subdir_path, 'test.txt'))
79
+ @dir.dir('subdir') do
80
+ file('test.txt')
81
+ end
82
+ end
83
+ end
84
+
85
+ describe '#file' do
86
+ before(:each) do
87
+ @file_path = File.join(@path, 'file.txt')
88
+ end
89
+
90
+ it 'should create a new file if needed (and add to Subversion)' do
91
+ new_path = File.join(@path, 'file2.txt')
92
+ File.exist?(new_path).should be_false
93
+ f = @dir.file('file2.txt')
94
+ @ctx.commit(@wc_path)
95
+ File.exist?(new_path).should be_true
96
+ @repos.repos.fs.root.file?('test-dir/file2.txt').should be_true
97
+ end
98
+
99
+ it 'should use an existing file' do
100
+ FileUtils.should_not_receive(:touch)
101
+ @ctx.should_not_receive(:add)
102
+ @dir.file('file.txt')
103
+ end
104
+
105
+ it 'should setup a new SvnFixture::File' do
106
+ SvnFixture::File.should_receive(:new).with(@ctx, @file_path)
107
+ @dir.file('file.txt')
108
+ end
109
+
110
+ it 'should return the File' do
111
+ f = @dir.file('file.txt')
112
+ f.should be_kind_of(SvnFixture::File)
113
+ f.instance_variable_get(:@path).should == @file_path
114
+ end
115
+
116
+ it 'should run block if given' do
117
+ @dir.file('file.txt') do
118
+ body('Test')
119
+ end
120
+ File.read(@file_path).should == 'Test'
121
+ end
122
+ end
123
+
124
+ describe '#move' do
125
+ before(:each) do
126
+ @file_path = File.join(@path, 'file.txt')
127
+ end
128
+
129
+ it 'should move node in FS and Subversion' do
130
+ new_path = File.join(@path, 'subdir', 'file2.txt')
131
+ File.exist?(new_path).should be_false
132
+ File.exist?(@file_path).should be_true
133
+ @dir.move('file.txt', 'subdir/file2.txt')
134
+ File.exist?(new_path).should be_true
135
+ File.exist?(@file_path).should be_false
136
+ @ctx.commit(@wc_path)
137
+ @repos.repos.fs.root.file?('test-dir/file.txt').should be_false
138
+ @repos.repos.fs.root.file?('test-dir/subdir/file2.txt').should be_true
139
+ end
140
+ end
141
+
142
+ describe '#copy' do
143
+ before(:each) do
144
+ @file_path = File.join(@path, 'file.txt')
145
+ end
146
+
147
+ it 'should copy node in FS and Subversion' do
148
+ new_path = File.join(@path, 'subdir', 'file2.txt')
149
+ File.exist?(new_path).should be_false
150
+ File.exist?(@file_path).should be_true
151
+ @dir.copy('file.txt', 'subdir/file2.txt')
152
+ File.exist?(new_path).should be_true
153
+ File.exist?(@file_path).should be_true
154
+ @ctx.commit(@wc_path)
155
+ @repos.repos.fs.root.file?('test-dir/file.txt').should be_true
156
+ @repos.repos.fs.root.file?('test-dir/subdir/file2.txt').should be_true
157
+ end
158
+ end
159
+
160
+ describe '#delete' do
161
+ before(:each) do
162
+ @file_path = File.join(@path, 'file.txt')
163
+ end
164
+
165
+ it 'should copy node in FS and Subversion' do
166
+ File.exist?(@file_path).should be_true
167
+ @dir.delete('file.txt')
168
+ File.exist?(@file_path).should be_false
169
+ @ctx.commit(@wc_path)
170
+ @repos.repos.fs.root.file?('test-dir/file.txt').should be_false
171
+ end
172
+ end
173
+
174
+ describe '#prop' do
175
+ it 'should set a property' do
176
+ @dir.prop('prop:name', 'Prop Value')
177
+ rev = @ctx.ci(@wc_path).revision
178
+ @ctx.propget('prop:name', @path, rev)[@full_repos_path].should ==
179
+ 'Prop Value'
180
+
181
+ @dir.prop('prop:name', 'New Value')
182
+ rev = @ctx.ci(@wc_path).revision
183
+ @ctx.propget('prop:name', @path, rev)[@full_repos_path].should ==
184
+ 'New Value'
185
+ end
186
+
187
+ it 'should format a Time correctly' do
188
+ @dir.prop('prop:timeval', Time.parse('2009-06-18 14:00'))
189
+ rev = @ctx.ci(@wc_path).revision
190
+ @ctx.propget('prop:timeval', @path, rev)[@full_repos_path].should ==
191
+ '2009-06-18T14:00:00.000000Z'
192
+ end
193
+ end
194
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe SvnFixture::File do
4
+ before(:all) do
5
+ @klass = SvnFixture::File
6
+ SvnFixture::Repository.destroy_all
7
+
8
+ # Set up repo
9
+ @repos = SvnFixture::repo('file_test') do
10
+ revision(1, 'create file') do
11
+ file('test.txt')
12
+ end
13
+ end
14
+ @repos.commit
15
+ @repos_path = @repos.instance_variable_get(:@repos_path)
16
+ @wc_path = @repos.instance_variable_get(:@wc_path)
17
+ @ctx = @repos.ctx
18
+ end
19
+
20
+ before(:each) do
21
+ @path = File.join(@wc_path, 'test.txt')
22
+ @full_repos_path = "file://#{File.join(@repos_path, 'test.txt')}"
23
+ @file = @klass.new(@ctx, @path)
24
+ end
25
+
26
+ after(:all) do
27
+ SvnFixture::Repository.destroy_all
28
+ end
29
+
30
+ describe '#initialize' do
31
+ it 'should set ctx and path' do
32
+ file = @klass.new(@ctx, '/tmp/path')
33
+ file.instance_variable_get(:@ctx).should == @ctx
34
+ file.instance_variable_get(:@path).should == '/tmp/path'
35
+ end
36
+ end
37
+
38
+ describe '#prop' do
39
+ it 'should set a property' do
40
+ @file.prop('prop:name', 'Prop Value')
41
+ rev = @ctx.ci(@wc_path).revision
42
+ @ctx.propget('prop:name', @path, rev)[@full_repos_path].should ==
43
+ 'Prop Value'
44
+
45
+ @file.prop('prop:name', 'New Value')
46
+ rev = @ctx.ci(@wc_path).revision
47
+ @ctx.propget('prop:name', @path, rev)[@full_repos_path].should ==
48
+ 'New Value'
49
+ end
50
+
51
+ it 'should format a Time correctly' do
52
+ @file.prop('prop:timeval', Time.parse('2009-06-18 14:00'))
53
+ rev = @ctx.ci(@wc_path).revision
54
+ @ctx.propget('prop:timeval', @path, rev)[@full_repos_path].should ==
55
+ '2009-06-18T14:00:00.000000Z'
56
+ end
57
+ end
58
+
59
+ describe '#body' do
60
+ it "should update the File's contents" do
61
+ @file.body('Test Content')
62
+ File.read(@path).should == 'Test Content'
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,43 @@
1
+ # For use by integration_spec
2
+
3
+ SvnFixture::repo('hello_world') do
4
+ revision(1, 'Create directories',
5
+ :date => Time.parse('2009-01-01 12:00:00Z')) do
6
+ dir 'app'
7
+ dir 'docs'
8
+ dir 'lib'
9
+ end
10
+
11
+ revision 2, 'Add some files' do
12
+ dir 'app' do
13
+ prop 'full_name', 'Application'
14
+
15
+ file 'hello.rb' do
16
+ prop 'is_ruby', 'Yes'
17
+ body 'puts "Hello World"'
18
+ end
19
+
20
+ file 'goodbye.rb' do
21
+ body 'puts "Goodbye World"'
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ SvnFixture::repo('hello_world') do
28
+ revision 3, 'Edit hello.rb', :author => "the.author" do
29
+ dir 'app' do
30
+ file 'hello.rb' do
31
+ prop 'is_ruby', 'Probably'
32
+ body 'puts "Howdy World"'
33
+ end
34
+ end
35
+ end
36
+
37
+ revision 4, 'Copy and move' do
38
+ dir 'app' do
39
+ move 'goodbye.rb', 'bye.rb'
40
+ copy 'hello.rb', 'hello2.rb'
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ # Test again fixtures/hello_world.rb to follow a complete fixture generation.
4
+ describe 'SvnFixture integration' do
5
+ before(:all) do
6
+ SvnFixture::Repository.instance_variable_set(:@repositories, {})
7
+ load File.dirname(__FILE__) + '/fixtures/hello_world.rb'
8
+ SvnFixture::repo('hello_world').commit
9
+ repos_path = File.join(Dir.tmpdir, 'svn-fixture', 'repo_hello_world')
10
+ @repos = ::Svn::Repos.open(repos_path)
11
+ @fs = @repos.fs
12
+ end
13
+
14
+ after(:all) do
15
+ SvnFixture::Repository.destroy_all
16
+ end
17
+
18
+ describe 'revision 1' do
19
+ before(:each) do
20
+ @root = @fs.root(1)
21
+ end
22
+
23
+ it 'should have 3 directories' do
24
+ @root.dir?('app').should be_true
25
+ @root.dir?('docs').should be_true
26
+ @root.dir?('lib').should be_true
27
+ end
28
+
29
+ it 'should not have files' do
30
+ @root.file?('app/hello.rb').should be_false
31
+ end
32
+
33
+ it 'should have specified date at rev 1' do
34
+ @fs.prop(Svn::Core::PROP_REVISION_DATE, 1).should ==
35
+ Time.parse('2009-01-01 12:00:00Z')
36
+ end
37
+ end
38
+
39
+ describe 'revision 2' do
40
+ before(:each) do
41
+ @root = @fs.root(2)
42
+ end
43
+
44
+ it 'should have files' do
45
+ @root.file?('app/hello.rb').should be_true
46
+ @root.file_contents('app/hello.rb') do |f|
47
+ f.read.should == 'puts "Hello World"'
48
+ end
49
+ @root.file?('app/goodbye.rb').should be_true
50
+ end
51
+
52
+ it 'should have property for hello.rb' do
53
+ @root.node_proplist('app/hello.rb')['is_ruby'].should == 'Yes'
54
+ end
55
+ end
56
+
57
+ describe 'revision 3' do
58
+ before(:each) do
59
+ @root = @fs.root(3)
60
+ end
61
+
62
+ it 'should change text for hello.rb' do
63
+ @root.file_contents('app/hello.rb') do |f|
64
+ f.read.should == 'puts "Howdy World"'
65
+ end
66
+ end
67
+
68
+ it 'should change property for hello.rb' do
69
+ @root.node_proplist('app/hello.rb')['is_ruby'].should == 'Probably'
70
+ end
71
+
72
+ it 'should set author' do
73
+ @fs.prop(Svn::Core::PROP_REVISION_AUTHOR, 3).should ==
74
+ 'the.author'
75
+ end
76
+ end
77
+
78
+ describe 'revision 4' do
79
+ before(:each) do
80
+ @root = @fs.root(4)
81
+ end
82
+
83
+ it 'should copy/move files' do
84
+ @root.file?('app/hello.rb').should be_true
85
+ @root.file?('app/hello2.rb').should be_true
86
+ @root.file_contents('app/hello2.rb') do |f|
87
+ f.read.should == 'puts "Howdy World"'
88
+ end
89
+ @root.file?('app/goodbye.rb').should be_false
90
+ @root.file?('app/bye.rb').should be_true
91
+ end
92
+
93
+ it 'should have expected dirs, files' do
94
+ @root.dir?('app').should be_true
95
+ @root.file?('app/hello.rb').should be_true
96
+ @root.file?('app/hello2.rb').should be_true
97
+ @root.file?('app/bye.rb').should be_true
98
+ @root.dir?('docs').should be_true
99
+ @root.dir?('lib').should be_true
100
+ end
101
+ end
102
+ end