tap 0.7.9 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. data/History +28 -0
  2. data/MIT-LICENSE +1 -1
  3. data/README +71 -43
  4. data/Rakefile +81 -64
  5. data/Tutorial +235 -0
  6. data/bin/tap +80 -44
  7. data/lib/tap.rb +41 -12
  8. data/lib/tap/app.rb +243 -246
  9. data/lib/tap/file_task.rb +357 -118
  10. data/lib/tap/generator.rb +88 -29
  11. data/lib/tap/generator/generators/config/config_generator.rb +4 -2
  12. data/lib/tap/generator/generators/config/templates/config.erb +1 -2
  13. data/lib/tap/generator/generators/file_task/file_task_generator.rb +3 -18
  14. data/lib/tap/generator/generators/file_task/templates/task.erb +22 -15
  15. data/lib/tap/generator/generators/file_task/templates/test.erb +13 -2
  16. data/{test/test/inference_methods/test_assert_files_exist/input/input_1.txt → lib/tap/generator/generators/generator/USAGE} +0 -0
  17. data/lib/tap/generator/generators/generator/generator_generator.rb +21 -0
  18. data/lib/tap/generator/generators/generator/templates/generator.erb +23 -0
  19. data/lib/tap/generator/generators/generator/templates/usage.erb +1 -0
  20. data/{test/test/inference_methods/test_assert_files_exist/input/input_2.txt → lib/tap/generator/generators/package/USAGE} +0 -0
  21. data/lib/tap/generator/generators/package/package_generator.rb +38 -0
  22. data/lib/tap/generator/generators/package/templates/package.erb +186 -0
  23. data/lib/tap/generator/generators/root/root_generator.rb +14 -9
  24. data/lib/tap/generator/generators/root/templates/Rakefile +20 -14
  25. data/{test/test/inference_methods/test_infer_glob/expected/file.yml → lib/tap/generator/generators/root/templates/ReadMe.txt} +0 -0
  26. data/lib/tap/generator/generators/root/templates/tap.yml +82 -0
  27. data/lib/tap/generator/generators/root/templates/test/tap_test_helper.rb +0 -1
  28. data/lib/tap/generator/generators/root/templates/test/tap_test_suite.rb +2 -1
  29. data/{test/test/inference_methods/test_infer_glob/expected/file_1.txt → lib/tap/generator/generators/script/USAGE} +0 -0
  30. data/lib/tap/generator/generators/script/script_generator.rb +17 -0
  31. data/lib/tap/generator/generators/script/templates/script.erb +42 -0
  32. data/lib/tap/generator/generators/task/task_generator.rb +1 -1
  33. data/lib/tap/generator/generators/task/templates/task.erb +24 -16
  34. data/lib/tap/generator/generators/task/templates/test.erb +13 -17
  35. data/lib/tap/generator/generators/workflow/templates/task.erb +10 -10
  36. data/lib/tap/generator/generators/workflow/templates/test.erb +1 -1
  37. data/lib/tap/generator/generators/workflow/workflow_generator.rb +3 -18
  38. data/lib/tap/root.rb +108 -146
  39. data/lib/tap/script.rb +362 -0
  40. data/lib/tap/script/console.rb +28 -0
  41. data/lib/tap/script/destroy.rb +13 -1
  42. data/lib/tap/script/generate.rb +13 -1
  43. data/lib/tap/script/run.rb +100 -57
  44. data/lib/tap/support/batch_queue.rb +0 -3
  45. data/lib/tap/support/logger.rb +6 -3
  46. data/lib/tap/support/rake.rb +54 -0
  47. data/lib/tap/support/task_configuration.rb +169 -0
  48. data/lib/tap/support/tdoc.rb +198 -0
  49. data/lib/tap/support/tdoc/config_attr.rb +338 -0
  50. data/lib/tap/support/tdoc/tdoc_html_generator.rb +38 -0
  51. data/lib/tap/support/tdoc/tdoc_html_template.rb +42 -0
  52. data/lib/tap/support/versions.rb +33 -1
  53. data/lib/tap/task.rb +339 -227
  54. data/lib/tap/test.rb +86 -128
  55. data/lib/tap/test/env_vars.rb +16 -5
  56. data/lib/tap/test/file_methods.rb +373 -0
  57. data/lib/tap/test/subset_methods.rb +299 -180
  58. data/lib/tap/version.rb +2 -1
  59. data/lib/tap/workflow.rb +2 -0
  60. data/test/app/lib/app_test_task.rb +1 -0
  61. data/test/app_test.rb +327 -83
  62. data/test/check/binding_eval.rb +23 -0
  63. data/test/check/define_method_check.rb +22 -0
  64. data/test/check/dependencies_check.rb +175 -0
  65. data/test/check/inheritance_check.rb +22 -0
  66. data/test/file_task_test.rb +524 -291
  67. data/test/{test/inference_methods/test_infer_glob/expected/file_2.txt → root/glob/one.txt} +0 -0
  68. data/test/root/glob/two.txt +0 -0
  69. data/test/root_test.rb +330 -262
  70. data/test/script_test.rb +194 -0
  71. data/test/support/audit_test.rb +5 -2
  72. data/test/support/combinator_test.rb +10 -10
  73. data/test/support/rake_test.rb +35 -0
  74. data/test/support/task_configuration_test.rb +272 -0
  75. data/test/support/tdoc_test.rb +363 -0
  76. data/test/support/templater_test.rb +2 -2
  77. data/test/support/versions_test.rb +32 -0
  78. data/test/tap_test_helper.rb +39 -0
  79. data/test/task_base_test.rb +115 -0
  80. data/test/task_class_test.rb +56 -4
  81. data/test/task_execute_test.rb +29 -0
  82. data/test/task_test.rb +89 -70
  83. data/test/test/env_vars_test.rb +48 -0
  84. data/test/test/{inference_methods → file_methods}/test_assert_expected/expected/file.txt +0 -0
  85. data/test/test/{inference_methods → file_methods}/test_assert_expected/expected/folder/file.txt +0 -0
  86. data/test/test/{inference_methods → file_methods}/test_assert_expected/input/file.txt +0 -0
  87. data/test/test/{inference_methods → file_methods}/test_assert_expected/input/folder/file.txt +0 -0
  88. data/test/test/file_methods/test_assert_files_exist/input/input_1.txt +0 -0
  89. data/test/test/file_methods/test_assert_files_exist/input/input_2.txt +0 -0
  90. data/test/test/file_methods/test_assert_output_files_equal/expected/one.txt +1 -0
  91. data/test/test/file_methods/test_assert_output_files_equal/expected/two.txt +1 -0
  92. data/test/test/file_methods/test_assert_output_files_equal/input/one.txt +1 -0
  93. data/test/test/file_methods/test_assert_output_files_equal/input/two.txt +1 -0
  94. data/test/test/{inference_methods → file_methods}/test_file_compare/expected/output_1.txt +0 -0
  95. data/test/test/{inference_methods → file_methods}/test_file_compare/expected/output_2.txt +0 -0
  96. data/test/test/{inference_methods → file_methods}/test_file_compare/input/input_1.txt +0 -0
  97. data/test/test/{inference_methods → file_methods}/test_file_compare/input/input_2.txt +0 -0
  98. data/test/test/file_methods/test_infer_glob/expected/file.yml +0 -0
  99. data/test/test/file_methods/test_infer_glob/expected/file_1.txt +0 -0
  100. data/test/test/file_methods/test_infer_glob/expected/file_2.txt +0 -0
  101. data/test/test/file_methods/test_method_glob/expected/file.yml +0 -0
  102. data/test/test/file_methods/test_method_glob/expected/file_1.txt +0 -0
  103. data/test/test/file_methods/test_method_glob/expected/file_2.txt +0 -0
  104. data/test/test/{inference_methods → file_methods}/test_yml_compare/expected/output_1.yml +0 -0
  105. data/test/test/{inference_methods → file_methods}/test_yml_compare/expected/output_2.yml +0 -0
  106. data/test/test/{inference_methods → file_methods}/test_yml_compare/input/input_1.yml +0 -0
  107. data/test/test/{inference_methods → file_methods}/test_yml_compare/input/input_2.yml +0 -0
  108. data/test/test/file_methods_test.rb +204 -0
  109. data/test/test/subset_methods_test.rb +93 -33
  110. data/test/test/test_assert_expected_result_files/expected/task/name/a.txt +1 -0
  111. data/test/test/test_assert_expected_result_files/expected/task/name/b.txt +1 -0
  112. data/test/test/test_assert_expected_result_files/input/a.txt +1 -0
  113. data/test/test/test_assert_expected_result_files/input/b.txt +1 -0
  114. data/test/test/test_file_task_test/expected/one.txt +1 -0
  115. data/test/test/test_file_task_test/expected/two.txt +1 -0
  116. data/test/test/test_file_task_test/input/one.txt +1 -0
  117. data/test/test/test_file_task_test/input/two.txt +1 -0
  118. data/test/test_test.rb +143 -3
  119. data/test/workflow_test.rb +2 -0
  120. data/vendor/rails_generator.rb +56 -0
  121. data/vendor/rails_generator/base.rb +263 -0
  122. data/vendor/rails_generator/commands.rb +581 -0
  123. data/vendor/rails_generator/generated_attribute.rb +42 -0
  124. data/vendor/rails_generator/lookup.rb +209 -0
  125. data/vendor/rails_generator/manifest.rb +53 -0
  126. data/vendor/rails_generator/options.rb +143 -0
  127. data/vendor/rails_generator/scripts.rb +83 -0
  128. data/vendor/rails_generator/scripts/destroy.rb +7 -0
  129. data/vendor/rails_generator/scripts/generate.rb +7 -0
  130. data/vendor/rails_generator/scripts/update.rb +12 -0
  131. data/vendor/rails_generator/simple_logger.rb +46 -0
  132. data/vendor/rails_generator/spec.rb +44 -0
  133. metadata +180 -196
  134. data/lib/tap/generator/generators/root/templates/app.yml +0 -19
  135. data/lib/tap/generator/generators/root/templates/config/process_tap_request.yml +0 -4
  136. data/lib/tap/generator/generators/root/templates/lib/process_tap_request.rb +0 -26
  137. data/lib/tap/generator/generators/root/templates/public/images/nav.jpg +0 -0
  138. data/lib/tap/generator/generators/root/templates/public/stylesheets/color.css +0 -57
  139. data/lib/tap/generator/generators/root/templates/public/stylesheets/layout.css +0 -108
  140. data/lib/tap/generator/generators/root/templates/public/stylesheets/normalize.css +0 -40
  141. data/lib/tap/generator/generators/root/templates/public/stylesheets/typography.css +0 -21
  142. data/lib/tap/generator/generators/root/templates/server/config/environment.rb +0 -60
  143. data/lib/tap/generator/generators/root/templates/server/lib/tasks/clear_database_prerequisites.rake +0 -5
  144. data/lib/tap/generator/generators/root/templates/server/test/test_helper.rb +0 -53
  145. data/lib/tap/script/server.rb +0 -12
  146. data/lib/tap/support/rap.rb +0 -38
  147. data/lib/tap/test/inference_methods.rb +0 -298
  148. data/test/task/config/task_with_config.yml +0 -1
  149. data/test/test/inference_methods_test.rb +0 -311
@@ -1,298 +0,0 @@
1
- require 'tap/root'
2
- require 'test/unit'
3
- require 'active_support'
4
- require 'tap/test/env_vars'
5
-
6
- module Test # :nodoc:
7
- module Unit # :nodoc:
8
- class TestCase
9
- class << self
10
-
11
- # Causes a unit test to act as an io test -- resulting in the following:
12
- # - Definition of an Tap::Root object
13
- # - Tap::Test::InferenceMethods are included, providing methods to easily access
14
- # files according to a standard file structure.
15
- #
16
- # Note: Unless otherwise specified, +acts_as_io_test+ infers a root directory
17
- # based on the calling file. Therefore, to keep your directory structure from
18
- # referencing an unexpected locations, be sure to specify the root directory
19
- # explicitly if you call +acts_as_io_test+ from a file that is NOT your test file.
20
- def acts_as_inference_test(options={})
21
- options = {
22
- :root => infer_root,
23
- :directories => {},
24
- :keep_outputs => false,
25
- :keep_failures => false}.merge(options.symbolize_keys)
26
-
27
- # declare the testing directory structure
28
- directories = default_directories.merge(options[:directories])
29
- ifs = Tap::Root.new(options[:root], directories)
30
-
31
- write_inheritable_attribute(:ifs, ifs)
32
- class_inheritable_reader :ifs
33
-
34
- write_inheritable_attribute(:options, options)
35
- class_inheritable_reader :options
36
-
37
- include Tap::Test::InferenceMethods
38
- end
39
-
40
- # The default directories for an io test:
41
- #
42
- # :input => 'input'
43
- # :output => 'output'
44
- # :expected => 'expected'
45
- # :fail => 'fail'
46
- def default_directories
47
- {:input => 'input', :output => 'output', :expected => 'expected', :fail => 'fail'}
48
- end
49
-
50
- # Infers the root directory from the calling file by chomping the extension and '_test'. Ex:
51
- # 'some_class.rb' => 'some_class'
52
- # 'some_class_test.rb' => 'some_class'
53
- def infer_root
54
- # the calling file is not the direct caller of +infer_root+... this method is
55
- # only accessed from within another method call, hence the target caller is caller[1]
56
- # rather than caller[0].
57
-
58
- # caller[1] is considered the calling file (which should be the test case)
59
- # note that the output of calller.first is like:
60
- # ./path/to/file.rb:10
61
- # ./path/to/file.rb:10:in 'method'
62
- calling_file = caller[1].gsub(/:\d+[:in .*]?$/, "" )
63
- calling_file.chomp!("#{File.extname(calling_file)}")
64
- calling_file.chomp("_test")
65
- end
66
- end
67
- end
68
- end
69
- end
70
-
71
- module Tap
72
- module Test
73
- module InferenceMethods
74
- include Tap::Test::EnvVars
75
-
76
- def method_name
77
- @method_name
78
- end
79
-
80
- # Convenience accessor for the inference structure
81
- def ifs
82
- self.class.ifs
83
- end
84
-
85
- # Convenience accessor for the class options
86
- def options
87
- self.class.options
88
- end
89
-
90
- def make_directory_structure
91
- ifs.directories.values.each do |dir|
92
- FileUtils.mkdir_p(File.join(ifs.root, method_name, dir))
93
- end
94
- end
95
-
96
- # Teardown deletes the output and fail directories unless flagged otherwise. Note that teardown
97
- # also checks the environment variables for flags. To keep all outputs (or failures) for all tests,
98
- # flag keep outputs from the command line like:
99
- #
100
- # %rake test KEEP_OUTPUTS=true
101
- # %rake test KEEP_FAILURES=true
102
- def teardown
103
- # clear out the output folder if it exists, unless flagged otherwise
104
- output_dir = infer_dir(:output, method_name)
105
- unless !File.exists?(output_dir) || options[:keep_outputs] || env("KEEP_OUTPUTS")
106
- begin
107
- FileUtils.rm_r output_dir
108
- rescue
109
- raise("teardown failure: could not remove output files")
110
- end
111
- end
112
-
113
- # clear out the fail folder if it exists, unless flagged otherwise
114
- fail_dir = infer_dir(:fail, method_name)
115
- unless !File.exists?(fail_dir) || options[:keep_failures] || env("KEEP_FAILURES")
116
- begin
117
- FileUtils.rm_r fail_dir
118
- rescue
119
- raise("teardown failure: could not remove fail files")
120
- end
121
- end
122
-
123
- # Remove the directory if possible
124
- begin
125
- dir = ifs.filepath(method_name)
126
- if File.exists?(dir) && Dir.glob(File.join(dir, "*")).empty?
127
- FileUtils.rmdir dir
128
- end
129
- rescue
130
- # rescue cases where there is a hidden file, for example .svn
131
- end
132
- end
133
-
134
- def infer_root(method=method_name)
135
- ifs.filepath(method)
136
- end
137
-
138
- # The method directory is defined as 'dir/method', where method is the calling method
139
- # by default. infer_dir returns the method directory if it exists, otherwise it returns
140
- # ifs[dir].
141
- def infer_dir(dir, method=method_name)
142
- File.join(infer_root(method), ifs.directories[dir] || dir.to_s)
143
- end
144
-
145
- # Returns a glob of files matching the input pattern, underneath the method directory
146
- # if it exists, otherwise the <tt>ifs[dir]</tt> directory.
147
- def infer_glob(dir, *patterns)
148
- dir = ifs.relative_filepath(:root, infer_dir(dir))
149
- ifs.glob(dir, *patterns)
150
- end
151
-
152
- # Returns a filepath constructed from the method directory if it exists,
153
- # otherwise the filepath will be constructed from <tt>ifs[dir]</tt>.
154
- def infer_filepath(dir, *filenames)
155
- File.join(infer_dir(dir), *filenames)
156
- end
157
-
158
- # Removes the method directory from the input filepath, returning the resuting filename.
159
- # If the method directory does not exist, <tt>ifs[dir]</tt> will be removed.
160
- def infer_relative_filepath(dir, filepath)
161
- dir = ifs.relative_filepath(:root, infer_dir(dir))
162
- ifs.relative_filepath(dir, filepath)
163
- end
164
-
165
- # Returns an output file corresponding to the input file, translated from the
166
- # input directory to the output directory.
167
- #
168
- # If the input method directory exists, it will be removed from the filepath.
169
- # If the output method directory exists, it will be inserted in the filepath.
170
- def infer_translate(filepath, input_dir, output_dir)
171
- input_dir = ifs.relative_filepath(:root, infer_dir(input_dir))
172
- output_dir = ifs.relative_filepath(:root, infer_dir(output_dir))
173
- ifs.translate(filepath, input_dir, output_dir)
174
- end
175
-
176
- # Generates a temporary filepath formatted like "output_dir\filename.pid.n.ext" where n
177
- # is a counter that will be incremented from until a non-existant filepath is achieved.
178
- #
179
- # Notes:
180
- # - By default filename is the calling method
181
- # - The extension is chomped off the end of the filename
182
- # - If the directory for the filepath does not exist, the directory will be created
183
- # - Like all files in the output directory, tempfiles will be deleted by the default
184
- # +teardown+ method
185
- def tempfile(filename=method_name)
186
- n = 0
187
- ext = File.extname(filename)
188
- basename = filename.chomp(ext)
189
- filepath = make_tmpname(basename, n, ext)
190
- while File.exists?(filepath)
191
- n += 1
192
- filepath = make_tmpname(basename, n, ext)
193
- end
194
-
195
- dirname = File.dirname(filepath)
196
- FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
197
- filepath
198
- end
199
-
200
- # Copies the output file to the :fail directory if no block is given, or if the
201
- # given block returns true or raises an error. Segregate file assumes the output
202
- # file is in the :output directory and determines the final filepath by using
203
- # the +translate+ method.
204
- #
205
- # If the block raises an error, then the error will be re-raised after the
206
- # segregation.
207
- def segregate_file(output_file, &block)
208
- begin
209
- segregate = block_given? ? yield(output_file) : true
210
- rescue
211
- segregate = true
212
- error = $!
213
- ensure
214
- if segregate
215
- target = infer_translate(output_file, :output, :fail)
216
-
217
- dirname = File.dirname(target)
218
- FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
219
- FileUtils.cp(output_file, target)
220
-
221
- # re-raise the error for further handling
222
- raise $! if error
223
- end
224
- end
225
- end
226
-
227
- #
228
- def assert_expected(*expected_files, &block)
229
- errors = []
230
-
231
- check_all_files = expected_files.empty?
232
- if check_all_files
233
- expected_files = infer_glob(:expected)
234
- begin
235
- output_files = infer_glob(:output)
236
-
237
- efl = expected_files.collect { |file| infer_relative_filepath(:expected, file) }
238
- ofl = output_files.collect { |file| infer_relative_filepath(:output, file) }
239
-
240
- unless efl == ofl
241
- raise "Inconsistent globs.\nexpected: #{PP.singleline_pp(efl, '')}\nbut was: #{PP.singleline_pp(ofl, '')}\n"
242
- end
243
- rescue
244
- errors << $!.message
245
- end
246
- end
247
-
248
- expected_files.each do |expected_file|
249
- begin
250
- output_file = infer_translate(expected_file, :expected, :output)
251
-
252
- raise "Expected output file does not exist: #{output_file}" unless File.exists?(output_file)
253
-
254
- segregate_file(output_file) do |output_file|
255
- pass = block_given? ? yield(expected_file, output_file) : files_equal?(expected_file, output_file)
256
- !pass
257
- end unless File.directory?(output_file)
258
- rescue
259
- errors << $!.message
260
- end
261
- end
262
-
263
- unless errors.empty?
264
- flunk errors.join("\n")
265
- end
266
- end
267
-
268
- # Asserts that each pair of input files are equal, using FileUtils.cmp.
269
- def files_equal?(a, b)
270
- FileUtils.cmp(a, b)
271
- end
272
-
273
- # Asserts that each pair of input files produce an equivalent object when
274
- # loaded as a yaml file.
275
- def yml_equal?(a, b)
276
- YAML.load_file(a) == YAML.load_file(b)
277
- end
278
-
279
- # Yields to the input block for each pair of input files. An error is raised
280
- # if the input arrays do not have equal numbers of entries.
281
- def each_pair(a, b, &block)
282
- a = [a] unless a.kind_of?(Array)
283
- b = [b] unless b.kind_of?(Array)
284
-
285
- raise ArgumentError, "The input arrays must have an equal number of entries." unless a.length == b.length
286
- a.each_index do |index|
287
- yield(a[index], b[index])
288
- end
289
- end
290
-
291
- private
292
-
293
- def make_tmpname(basename, n, ext="")
294
- infer_filepath(:output, sprintf('%s%d.%d%s', basename, $$, n, ext))
295
- end
296
- end
297
- end
298
- end
@@ -1 +0,0 @@
1
- config: value
@@ -1,311 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '../tap_test_helper.rb')
2
- require 'tap/test/inference_methods'
3
-
4
- class InferenceMethodsTest < Test::Unit::TestCase
5
- include Tap::Test::SubsetMethods
6
- acts_as_inference_test
7
-
8
- def test_method_name_returns_test_method_name1
9
- assert_equal "test_method_name_returns_test_method_name1", method_name
10
- end
11
-
12
- def test_method_name_returns_test_method_name2
13
- assert_equal "test_method_name_returns_test_method_name2", method_name
14
- end
15
-
16
- def test_make_directory_structure
17
- root = File.join(File.dirname(__FILE__), "inference_methods")
18
- begin
19
- assert_equal root, ifs[:root]
20
- assert_equal({
21
- :input => 'input',
22
- :output => 'output',
23
- :expected => 'expected',
24
- :fail => 'fail'}, ifs.directories)
25
-
26
- ifs.directories.values.each do |dir|
27
- assert !File.exists?(File.join(root, "test_make_directory_structure", dir.to_s)), dir
28
- end
29
-
30
- make_directory_structure # alias for make_directory_structure
31
-
32
- ifs.directories.values.each do |dir|
33
- assert File.exists?(File.join(root, "test_make_directory_structure", dir.to_s)), dir
34
- end
35
- ensure
36
- dir = File.join(File.join(root, "test_make_directory_structure"))
37
- FileUtils.rm_r dir if File.exists?(dir)
38
- end
39
- end
40
-
41
- #
42
- # infer filepath tests
43
- #
44
-
45
- def test_infer_dir_adds_method_to_path
46
- assert_equal File.join(ifs.root, method_name, "input"), infer_dir(:input)
47
- end
48
-
49
- def test_infer_filepath_adds_method_to_path
50
- input_root = File.join(ifs.root, method_name, "input")
51
- assert_equal File.join(input_root, "file.txt"), infer_filepath(:input, 'file.txt')
52
- assert_equal File.join(input_root, "folder/file.txt"), infer_filepath(:input, 'folder', 'file.txt')
53
- end
54
-
55
- #
56
- # infer relative filepath tests
57
- #
58
-
59
- def test_infer_relative_filepath_removes_infer_dir
60
- input_root = File.join(ifs.root, method_name, "input")
61
- assert_equal 'file.txt', infer_relative_filepath(:input, File.join(input_root, "file.txt"))
62
- assert_equal 'folder/file.txt', infer_relative_filepath(:input, File.join(input_root, "folder/file.txt"))
63
- end
64
-
65
- def test_infer_relative_filepath_expands_filepaths
66
- input_root = File.join(ifs.root, method_name, "input")
67
- assert_equal 'file.txt', infer_relative_filepath(:input, File.join(input_root, "folder/.././file.txt") )
68
- end
69
-
70
- def test_infer_relative_filepath_raises_error_unless_filepath_begins_with_infer_dir
71
- assert_raise(RuntimeError) { infer_relative_filepath(:input, File.join('some/path', ifs[:input], 'file.txt') ) }
72
- end
73
-
74
- #
75
- # infer translate tests
76
- #
77
-
78
- def test_infer_translate
79
- acase_test('file.txt', 'folder/file.txt') do |path|
80
- filepath = File.join(ifs.root, method_name, "input", path)
81
- expected = File.join(ifs.root, method_name, "output", path)
82
-
83
- assert_equal expected, infer_translate(filepath, :input, :output)
84
- end
85
- end
86
-
87
- #
88
- # infer glob tests
89
- #
90
-
91
- def test_infer_glob
92
- case_test(
93
- [:expected] => ["file.yml", "file_1.txt", "file_2.txt"],
94
- [:expected, "*"] => ["file.yml", "file_1.txt", "file_2.txt"],
95
- [:expected, "*.txt"] => ["file_1.txt", "file_2.txt"],
96
- [:expected, "*.txt", "*.yml"] => ["file.yml", "file_1.txt", "file_2.txt"]
97
- ) do |testcase, expected|
98
- expected.collect! { |file| infer_filepath(:expected, file) }
99
-
100
- assert_equal expected.sort, infer_glob(*testcase).sort
101
- end
102
- end
103
-
104
- #
105
- # tempfile test
106
- #
107
-
108
- def test_tempfile_returns_new_file_in_output_dir
109
- output_root = File.join(ifs.root, method_name, "output")
110
-
111
- filepath =File.join(output_root, "file#{$$}.0")
112
- assert_equal filepath, tempfile('file')
113
-
114
- filepath = File.join(output_root, "file#{$$}.0")
115
- assert_equal filepath, tempfile('file')
116
-
117
- FileUtils.touch filepath
118
- filepath = File.join(output_root, "file#{$$}.1")
119
- assert_equal filepath, tempfile('file')
120
-
121
- FileUtils.touch filepath
122
- filepath = File.join(output_root, "file#{$$}.2")
123
- assert_equal filepath, tempfile('file')
124
- end
125
-
126
- #
127
- # segregate file test
128
- #
129
-
130
- def segregate_file_setup(path)
131
- filepath = File.join(ifs.root, method_name, "output", path)
132
- segregated = File.join(ifs.root, method_name, "fail", path)
133
-
134
- dirname = File.dirname(filepath)
135
- FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
136
- FileUtils.touch(filepath)
137
-
138
- [filepath, segregated]
139
- end
140
-
141
- def test_segregate_file
142
- acase_test('file.txt', 'folder/file.txt') do |path|
143
- filepath, segregated = segregate_file_setup(path)
144
-
145
- assert File.exists?(filepath)
146
- assert !File.exists?(segregated)
147
-
148
- segregate_file(filepath)
149
-
150
- assert File.exists?(filepath)
151
- assert File.exists?(segregated)
152
- end
153
- end
154
-
155
- def test_segregate_file_only_segregates_if_block_returns_true
156
- acase_test('file.txt', 'folder/file.txt') do |path|
157
- filepath, segregated = segregate_file_setup(path)
158
-
159
- assert File.exists?(filepath)
160
- assert !File.exists?(segregated)
161
-
162
- segregate_file(filepath) do |file|
163
- false
164
- end
165
-
166
- assert File.exists?(filepath)
167
- assert !File.exists?(segregated)
168
-
169
- segregate_file(filepath) do |file|
170
- true
171
- end
172
-
173
- assert File.exists?(filepath)
174
- assert File.exists?(segregated)
175
- end
176
- end
177
-
178
- def test_segregate_file_segregates_if_block_raises_error
179
- acase_test('file.txt', 'folder/file.txt') do |path|
180
- filepath, segregated = segregate_file_setup(path)
181
-
182
- assert File.exists?(filepath)
183
- assert !File.exists?(segregated)
184
-
185
- begin
186
- segregate_file(filepath) do |file|
187
- raise "error"
188
- end
189
-
190
- flunk("error was not re-raised")
191
- rescue
192
- assert File.exists?(filepath)
193
- assert File.exists?(segregated)
194
- end
195
- end
196
- end
197
-
198
- #
199
- # assert expected tests
200
- #
201
-
202
- def test_assert_expected
203
- target = nil
204
- infer_glob(:input).sort.each do |input_file|
205
- target = ifs.translate(input_file, "#{method_name}/input", "#{method_name}/output")
206
-
207
- if File.file?(input_file)
208
- dirname = File.dirname(target)
209
- FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
210
- FileUtils.cp(input_file, target)
211
- else
212
- FileUtils.mkdir_p(target)
213
- end
214
- end
215
-
216
- # all files
217
- assert_expected
218
-
219
- # files globed
220
- assert_expected(infer_filepath(:expected, 'file.txt'))
221
-
222
- # with block
223
- assert_expected do | expected, output|
224
- true
225
- end
226
-
227
- # now with a block that fails
228
- fail_file = ifs.translate(target, "#{method_name}/output", "#{method_name}/fail")
229
- begin
230
- assert_expected do |expected, output|
231
- false
232
- end
233
- flunk
234
- rescue
235
- assert File.exists?(fail_file)
236
- File.delete(fail_file)
237
- end
238
-
239
- # now with a file that is different
240
- File.open(target, 'a') {|file| file << "additional contents" }
241
- begin
242
- assert_expected
243
- flunk
244
- rescue
245
- assert File.exists?(fail_file)
246
- File.delete(fail_file)
247
- end
248
-
249
- # now with a missing file -- note there is no file to segregate
250
- File.delete(target)
251
- begin
252
- assert_expected
253
- flunk
254
- rescue
255
- assert $!.message =~ Regexp.new("Expected output file does not exist: #{target}")
256
- assert !File.exists?(fail_file)
257
- end
258
- end
259
-
260
- #
261
- # files equal test
262
- #
263
-
264
- def test_file_compare
265
- count = 0
266
- each_pair(infer_glob(:input, "*1*"), infer_glob(:expected, "*1*") ) do |a, b|
267
- assert files_equal?(a, b)
268
- count += 1
269
- end
270
- assert_equal 1, count
271
-
272
- each_pair(infer_glob(:input, "*2*"), infer_glob(:expected, "*2*") ) do |a, b|
273
- assert !files_equal?(a, b)
274
- count += 1
275
- end
276
- assert_equal 2, count
277
- end
278
-
279
- #
280
- # yml equal test
281
- #
282
-
283
- def test_yml_compare
284
- count = 0
285
- each_pair(infer_glob(:input, "*1*"), infer_glob(:expected, "*1*") ) do |a, b|
286
- assert yml_equal?(a, b)
287
- count += 1
288
- end
289
- assert_equal 1, count
290
-
291
- each_pair(infer_glob(:input, "*2*"), infer_glob(:expected, "*2*") ) do |a, b|
292
- assert !yml_equal?(a, b)
293
- count += 1
294
- end
295
- assert_equal 2, count
296
- end
297
- end
298
-
299
- class InferenceTestTestWithOptions < Test::Unit::TestCase
300
- acts_as_inference_test :root => "some/root/dir"
301
-
302
- def test_test_setup
303
- assert_equal "some/root/dir", ifs[:root]
304
- assert_equal({
305
- :input => 'input',
306
- :output => 'output',
307
- :expected => 'expected',
308
- :fail => 'fail'}, ifs.directories)
309
- end
310
- end
311
-