bee 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,298 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # Copyright 2006 Michel Casabianca <casa@sweetohm.net>
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require 'test/unit'
18
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
19
- require 'bee.rb'
20
- require 'fileutils'
21
-
22
- class TestBeeDefaultTasks < Test::Unit::TestCase
23
-
24
- # Temporary directory.
25
- TEMP_DIR = 'tmp'
26
-
27
- # Constructor.
28
- def initialize(*args)
29
- super(*args)
30
- @home = File.expand_path(File.dirname(__FILE__))
31
- @working_dir = Dir.getwd
32
- @tmp_dir = File.join(@home, TEMP_DIR)
33
- # try to create temporary directory and disable tests if we can't
34
- begin
35
- FileUtils.makedirs(@tmp_dir)
36
- @run_tests = true
37
- rescue
38
- @run_tests = false
39
- end
40
- end
41
-
42
- # Run before any test:
43
- # - Create temporary directory.
44
- # - Create a context object and load tasks in it.
45
- def setup
46
- FileUtils.makedirs(@tmp_dir)
47
- @context = Bee::Context.new
48
- script = File.read(File.join(@home, '..', 'lib', 'bee_default_context.rb'))
49
- @context.evaluate_script(script)
50
- end
51
-
52
- # Run after any test: delete temporary directory.
53
- def teardown
54
- FileUtils.rm_rf(@tmp_dir)
55
- Dir.chdir(@working_dir)
56
- end
57
-
58
- ############################ UTILITY FUNCTIONS ##########################
59
-
60
- # Clean temporary directory.
61
- def clean_tmp
62
- FileUtils.rm_rf(@tmp_dir)
63
- FileUtils.makedirs(@tmp_dir)
64
- end
65
-
66
- # Create a given file in tmp directory with its name as contents.
67
- # - name: file name, relative to temporary directory.
68
- def create_file(name)
69
- File.open(name, 'w') { |file| file.write(name) }
70
- end
71
-
72
- # Assert file exists and test its contents.
73
- def assert_file(file, contents)
74
- assert(File.exists?(file))
75
- text = File.read(file)
76
- assert_equal(contents, text)
77
- end
78
-
79
- ############################ TEST FUNCTIONS #############################
80
-
81
- # Test task print.
82
- # TODO: implement.
83
- def test_task_print
84
- return unless @run_tests
85
- end
86
-
87
- # Test task cat.
88
- # TODO: implement.
89
- def test_task_cat
90
- return unless @run_tests
91
- end
92
-
93
- # Test task cd.
94
- def test_task_cd
95
- return unless @run_tests
96
- working_dir = File.expand_path(File.dirname(__FILE__))
97
- # test nominal case
98
- @context.cd(@tmp_dir)
99
- expected = @tmp_dir
100
- actual = Dir.getwd
101
- assert_equal(expected, actual)
102
- # test changing to a directory that doesn't exists
103
- begin
104
- dir = File.join(@tmp_dir, 'lkjqzcjv')
105
- @context.cd(dir)
106
- flunk "Should have raised an Errno::ENOENT"
107
- rescue Errno::ENOENT
108
- end
109
- # test bad arguments
110
- begin
111
- @context.cd(Object.new)
112
- flunk "Should have raised a BuildError"
113
- rescue Bee::BuildError
114
- expected = 'cd parameter is a String'
115
- actual = $!.message
116
- assert_equal(expected, actual)
117
- end
118
- end
119
-
120
- # Test task mkdir.
121
- def test_task_mkdir
122
- return unless @run_tests
123
- # nominal case with a single recursive dir
124
- dir = File.join(@tmp_dir, 'foo', 'bar')
125
- @context.mkdir(dir)
126
- expected = true
127
- actual = File.exists?(dir) and File.directory?(dir)
128
- assert_equal(expected, actual)
129
- # nominal case with a list of directories
130
- dirs = [File.join(@tmp_dir, 'toto'), File.join(@tmp_dir, 'titi')]
131
- @context.mkdir(dirs)
132
- found = Dir.entries(@tmp_dir)
133
- assert(found.member?('toto'))
134
- assert(found.member?('titi'))
135
- # error case
136
- dir = Object.new
137
- begin
138
- @context.mkdir(dir)
139
- flunk "Should have raised a BuildError"
140
- rescue Bee::BuildError => e
141
- expected = 'mkdir parameter must a String or an array of Strings'
142
- actual = $!.message
143
- assert_equal(expected, actual)
144
- end
145
- # another error case
146
- dirs = [File.join(@tmp_dir, 'foo'), Object.new]
147
- begin
148
- @context.mkdir(dirs)
149
- flunk "Should have raised a BuildError"
150
- rescue Bee::BuildError => e
151
- expected = 'mkdir parameter must a String or an array of Strings'
152
- actual = $!.message
153
- assert_equal(expected, actual)
154
- end
155
- end
156
-
157
- # Test task cp.
158
- def test_task_cp
159
- return unless @run_tests
160
- # nominal case for a src and dest file
161
- src = File.join(@tmp_dir, 'src.txt')
162
- create_file(src)
163
- dest = File.join(@tmp_dir, 'dest.txt')
164
- @context.cp({ 'src' => src, 'dest' => dest })
165
- assert_file(dest, src)
166
- # nominal case for src files and destination directory
167
- clean_tmp
168
- src2 = File.join(@tmp_dir, 'src2.txt')
169
- dest = File.join(@tmp_dir, 'dir')
170
- create_file(src)
171
- create_file(src2)
172
- FileUtils.makedirs(dest)
173
- @context.cp({ 'src' => [src, src2], 'dest' => dest })
174
- assert_file(File.join(dest, 'src.txt'), src)
175
- assert_file(File.join(dest, 'src2.txt'), src2)
176
- # nominal case for src and dest directory
177
- clean_tmp
178
- src = File.join(@tmp_dir, 'dir')
179
- dest = File.join(@tmp_dir, 'todir')
180
- FileUtils.makedirs(src)
181
- FileUtils.makedirs(dest)
182
- file1 = File.join(src, 'file1.txt')
183
- file2 = File.join(src, 'file2.txt')
184
- create_file(file1)
185
- create_file(file2)
186
- @context.cp({ 'src' => src, 'dest' => dest })
187
- assert_file(File.join(dest, 'dir', 'file1.txt'), file1)
188
- assert_file(File.join(dest, 'dir', 'file2.txt'), file2)
189
- end
190
-
191
- # Test task mv.
192
- def test_task_mv
193
- return unless @run_tests
194
- # nominal case for a src and dest file
195
- src = File.join(@tmp_dir, 'src.txt')
196
- create_file(src)
197
- dest = File.join(@tmp_dir, 'dest.txt')
198
- @context.mv({ 'src' => src, 'dest' => dest })
199
- assert_file(dest, src)
200
- assert(!File.exists?(File.join(@tmp_dir, src)))
201
- # nominal case with two source files
202
- clean_tmp
203
- src = File.join(@tmp_dir, 'src.txt')
204
- src2 = File.join(@tmp_dir, 'src2.txt')
205
- dest = File.join(@tmp_dir, 'dir')
206
- create_file(src)
207
- create_file(src2)
208
- FileUtils.makedirs(dest)
209
- @context.mv({ 'src' => File.join(@tmp_dir, '*.txt'), 'dest' => dest })
210
- assert_file(File.join(dest, 'src.txt'), src)
211
- assert_file(File.join(dest, 'src2.txt'), src2)
212
- assert(!File.exists?(src))
213
- assert(!File.exists?(src2))
214
- # nominal case with directory for src and dest
215
- clean_tmp
216
- src = File.join(@tmp_dir, 'dir1')
217
- dest = File.join(@tmp_dir, 'dir2')
218
- FileUtils.makedirs(src)
219
- create_file(File.join(src, 'test.txt'))
220
- @context.mv({ 'src' => src, 'dest' => dest })
221
- assert(File.exists?(dest))
222
- assert(File.directory?(dest))
223
- assert_file(File.join(dest, 'test.txt'), File.join(src, 'test.txt'))
224
- assert(!File.exists?(src))
225
- end
226
-
227
- # Test task rm.
228
- def test_task_rm
229
- return unless @run_tests
230
- # nominal case with a glob
231
- file = File.join(@tmp_dir, 'test.txt')
232
- create_file(file)
233
- @context.rm(File.join(@tmp_dir, file))
234
- assert(!File.exists?(File.join(@tmp_dir, file)))
235
- # nominal case with two globs and three files
236
- clean_tmp
237
- file1 = File.join(@tmp_dir, 'test1.txt')
238
- file2 = File.join(@tmp_dir, 'test2.txt')
239
- file3 = File.join(@tmp_dir, 'test3.log')
240
- create_file(file1)
241
- create_file(file2)
242
- create_file(file3)
243
- @context.rm([File.join(@tmp_dir, '**/*.txt'),
244
- File.join(@tmp_dir, '**/*.log')])
245
- assert(!File.exists?(file1))
246
- assert(!File.exists?(file2))
247
- assert(!File.exists?(file3))
248
- end
249
-
250
- # Test task rmdir.
251
- def test_task_rmdir
252
- return unless @run_tests
253
- # nominal case with one dir
254
- dir = File.join(@tmp_dir, 'dir')
255
- FileUtils.makedirs(dir)
256
- @context.rmdir(dir)
257
- assert(!File.exists?(dir))
258
- # nominal case with two dirs
259
- clean_tmp
260
- dir1 = File.join(@tmp_dir, 'dir1')
261
- dir2 = File.join(@tmp_dir, 'dir2')
262
- FileUtils.makedirs(dir1)
263
- FileUtils.makedirs(dir2)
264
- @context.rmdir([dir1, dir2])
265
- assert(!File.exists?(dir1))
266
- assert(!File.exists?(dir2))
267
- end
268
-
269
- # Test task find.
270
- def test_task_find
271
- return unless @run_tests
272
- file1 = File.join(@tmp_dir, 'file1.txt')
273
- file2 = File.join(@tmp_dir, 'file2.txt')
274
- dir = File.join(@tmp_dir, 'dir')
275
- file3 = File.join(dir, 'file3.txt')
276
- create_file(file1)
277
- create_file(file2)
278
- FileUtils.makedirs(dir)
279
- create_file(file3)
280
- @context.find({ 'files' => "#{File.join(@tmp_dir, '**/*.txt')}",
281
- 'toprop' => 'list'})
282
- list = @context.get('list').sort
283
- expected = [file1, file2, file3].sort
284
- assert_equal(expected, list)
285
- end
286
-
287
- # Test task erb.
288
- def test_task_erb
289
- return unless @run_tests
290
- @context.set('test', 'TEST')
291
- source = 'This is a <%= test %>'
292
- @context.erb({ 'source' => source, 'toprop' => 'result' })
293
- actual = @context.get('result')
294
- expected = 'This is a TEST'
295
- assert_equal(expected, actual)
296
- end
297
-
298
- end