tap-test 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/History CHANGED
@@ -1,3 +1,14 @@
1
+ == 0.4.0 / 2009-19-09
2
+
3
+ Various improvements to the test modules. Note that FileTest now will
4
+ cleanup the method_root directory completely by default; to restore
5
+ the old behavior, set cleanup_dirs equal to [:output, :tmp].
6
+
7
+ * added set_env method to ShellTest
8
+ * added indent stripping to sh_test
9
+ * changed FileTest to cleanup method_root
10
+ completely by default
11
+
1
12
  == 0.3.0 / 2009-12-05
2
13
 
3
14
  Updates for Tap-0.19.0
data/README CHANGED
@@ -33,9 +33,7 @@ can be overlaid.
33
33
 
34
34
  Sets up a test-specific method_root for working with temporary files. Better
35
35
  in most cases than using Tempfile because you can flag temporary files to be
36
- saved on a failure (using ENV['KEEP_OUTPUTS']='true'). Also provides a new
37
- assertion to test that all the files in an expected directory are equal to
38
- those in an output directory.
36
+ saved on a failure (using ENV['KEEP_OUTPUTS']='true').
39
37
 
40
38
  require 'tap/test/unit'
41
39
  class FileTestTest < Test::Unit::TestCase
@@ -63,9 +61,9 @@ Simple testing of shell commands.
63
61
 
64
62
  def test_echo_with_an_alias
65
63
  sh_test %q{
66
- % alias goodnight moon
67
- goodnight moon
68
- }
64
+ % alias goodnight moon
65
+ goodnight moon
66
+ }
69
67
  end
70
68
  end
71
69
 
@@ -102,9 +100,9 @@ Sets up Tap::App.instance for testing tasks.
102
100
  class TapTestTest < Test::Unit::TestCase
103
101
  acts_as_tap_test
104
102
 
105
- def test_task
106
- t = Tap::Task.intern {|task| "result" }
107
- assert_equal "result", t.process
103
+ def test_node
104
+ n = app.node { "result" }
105
+ assert_equal "result", n.call
108
106
  end
109
107
  end
110
108
 
data/lib/tap/test.rb CHANGED
@@ -50,7 +50,7 @@ module Tap
50
50
  # sh_test_options.
51
51
  def acts_as_shell_test(options=nil)
52
52
  include Tap::Test::ShellTest
53
- self.sh_test_options = options
53
+ self.sh_test_options.merge!(options) if options
54
54
  end
55
55
 
56
56
  # Includes TapTest in the calling class and calls acts_as_file_test with
@@ -5,15 +5,15 @@ module Tap
5
5
  module Test
6
6
 
7
7
  # FileTest facilitates access and utilization of test-specific files and
8
- # directories. FileTest provides each test method with a Tap::Root
9
- # (method_root) specific for the method, and defines a new assertion method
10
- # (assert_files) to facilitate tests which involve the production and/or
11
- # modification of files.
8
+ # directories. FileTest provides each test method with a Tap::Root
9
+ # (method_root) specific for the method, and defines a new assertion
10
+ # method (assert_files) to facilitate tests which involve the production
11
+ # and/or modification of files.
12
12
  #
13
13
  # [file_test_doc_test.rb]
14
14
  # class FileTestDocTest < Test::Unit::TestCase
15
15
  # acts_as_file_test
16
- #
16
+ #
17
17
  # def test_something
18
18
  # # each test class has a class test root (ctr)
19
19
  # # and each test method has a method-specific
@@ -36,7 +36,7 @@ module Tap
36
36
  # # by the block the expected files, ensuring they
37
37
  # # are the same (see the documentation for the
38
38
  # # simplest use of assert_files)
39
- #
39
+ #
40
40
  # expected_file = method_root.prepare(:expected, 'output.txt') {|file| file << 'expected output' }
41
41
  #
42
42
  # # passes
@@ -46,19 +46,39 @@ module Tap
46
46
  # end
47
47
  # end
48
48
  #
49
+ # ==== Cleanup
50
+ #
51
+ # By default the method_root directory is cleaned up at the end of the
52
+ # test, making FileTest very conveient for creating temporary test files.
53
+ # To prevent cleanup, either set the KEEP_OUTPUTS or KEEP_FAILURES ENV
54
+ # variable to 'true'. The cleanup directories can be specified manually
55
+ # using cleanup_dirs class variable:
56
+ #
57
+ # class LimitedCleanupTest < Test::Unit::TestCase
58
+ #
59
+ # # only cleanup the method_root[:output] directory
60
+ # acts_as_file_test :cleanup_dirs => [:output]
61
+ # end
62
+ #
63
+ # This technique is useful when you want to keep certain static files
64
+ # under version control, for instance.
65
+ #
66
+ # ==== Requirements
67
+ #
49
68
  # FileTest requires that a method_name method is provided by the including
50
- # class, in order to properly set the directory for method_root.
69
+ # class, in order to properly set the directory for method_root.
70
+ # Test::Unit::TestCase satisfies this requirement already.
51
71
  module FileTest
52
72
 
53
73
  def self.included(base) # :nodoc:
54
74
  super
55
75
  base.extend FileTest::ClassMethods
56
- base.cleanup_dirs = [:output, :tmp]
76
+ base.cleanup_dirs = [:root]
57
77
  end
58
78
 
59
- # The test-method-specific Tap::Root which may be used to
60
- # access test files. method_root is a duplicate of ctr
61
- # reconfigured so that method_root.root is ctr[method_name.to_sym]
79
+ # The test-method-specific Tap::Root which may be used to access test
80
+ # files. method_root is a duplicate of ctr reconfigured so that
81
+ # method_root.root is ctr[method_name.to_sym]
62
82
  attr_reader :method_root
63
83
 
64
84
  # Sets up method_root and calls cleanup. Be sure to call super when
@@ -70,9 +90,9 @@ module Tap
70
90
  end
71
91
 
72
92
  # Cleans up the method_root.root directory by removing the class
73
- # cleanup_dirs (by default :tmp and :output). The root directory
74
- # will also be removed if it is empty.
75
- #
93
+ # cleanup_dirs (by default :root, ie the method_root directory itself).
94
+ # The root directory will be removed if it is empty.
95
+ #
76
96
  # Override as necessary in subclasses.
77
97
  def cleanup
78
98
  self.class.cleanup_dirs.each do |dir|
@@ -81,15 +101,8 @@ module Tap
81
101
  try_remove_dir(method_root.root)
82
102
  end
83
103
 
84
- # Calls cleanup unless flagged otherwise by an ENV variable. To prevent
85
- # cleanup (when debugging for example), set the 'KEEP_OUTPUTS' or
86
- # 'KEEP_FAILURES' ENV variables:
87
- #
88
- # % rap test KEEP_OUTPUTS=true
89
- # % rap test KEEP_FAILURES=true
90
- #
91
- # Cleanup is only suppressed for failing tests when KEEP_FAILURES is
92
- # specified. Be sure to call super when overriding this method.
104
+ # Calls cleanup unless flagged otherwise by an ENV variable (see above).
105
+ # Be sure to call super when overriding this method.
93
106
  def teardown
94
107
  # check that method_root still exists (nil may
95
108
  # indicate setup was overridden without super)
@@ -116,7 +129,7 @@ module Tap
116
129
 
117
130
  # Runs a file-based test that compares files created by the block with
118
131
  # files in an expected directory. The block receives files from the
119
- # input directory, and should return a list of files relative to the
132
+ # input directory, and should return a list of files relative to the
120
133
  # output directory. Only the files returned by the block are compared;
121
134
  # additional files in the output directory are effectively ignored.
122
135
  #
@@ -126,7 +139,7 @@ module Tap
126
139
  #
127
140
  # class FileTestDocTest < Test::Unit::TestCase
128
141
  # acts_as_file_test
129
- #
142
+ #
130
143
  # def test_assert_files
131
144
  # assert_files do |input_files|
132
145
  # input_files.collect do |path|
@@ -152,7 +165,7 @@ module Tap
152
165
  # `- input
153
166
  # |- one.txt
154
167
  # `- two.txt
155
- #
168
+ #
156
169
  # [input/one.txt]
157
170
  # test input 1
158
171
  #
@@ -165,7 +178,7 @@ module Tap
165
178
  # [expected/two.txt]
166
179
  # test output 2
167
180
  #
168
- # When you run the test, the assert_files passes the input files to the
181
+ # When you run the test, the assert_files passes the input files to the
169
182
  # block. When the block completes, assert_files compares the output
170
183
  # files returned by the block with the files in the expected directory.
171
184
  # In this case, the files are equal and the test passes.
@@ -181,7 +194,7 @@ module Tap
181
194
  #
182
195
  # === Options
183
196
  # A variety of options adjust the behavior of assert_files:
184
- #
197
+ #
185
198
  # :input_dir specify the directory to glob for input files
186
199
  # (default method_root[:input])
187
200
  # :output_dir specify the output directory
@@ -195,18 +208,18 @@ module Tap
195
208
  # :include_expected_directories specifies directories to be included in the
196
209
  # expected-output file list comparison
197
210
  # (by default dirs are excluded)
198
- #
211
+ #
199
212
  # assert_files will fail if <tt>:expected_files</tt> was not specified
200
- # in the options and no files were found in <tt>:expected_dir</tt>. This
201
- # check tries to prevent silent false-positive results when you forget to
202
- # put expected files in their place.
213
+ # in the options and no files were found in <tt>:expected_dir</tt>. This
214
+ # check tries to prevent silent false-positive results when you forget
215
+ # to put expected files in their place.
203
216
  #
204
217
  # === File References
205
218
  #
206
- # Sometimes the same files will get used across multiple tests. To allow
207
- # separate management of test files and prevent duplication, file
208
- # references can be provided in place of test files. For instance, with a
209
- # test directory like:
219
+ # Sometimes the same files will get used across multiple tests. To
220
+ # allow separate management of test files and prevent duplication, file
221
+ # references can be provided in place of test files. For instance, with
222
+ # a test directory like:
210
223
  #
211
224
  # method_root
212
225
  # |- expected
@@ -218,7 +231,7 @@ module Tap
218
231
  # `- ref
219
232
  # |- one.txt
220
233
  # `- two.txt
221
- #
234
+ #
222
235
  # The input and expected files (all references in this case) can be
223
236
  # dereferenced to the 'ref' paths like so:
224
237
  #
@@ -236,21 +249,10 @@ module Tap
236
249
  # configurations; a reference_dir must be specified for dereferencing to
237
250
  # occur (see Utils.dereference for more details).
238
251
  #
239
- # === Keeping Outputs
240
- #
241
- # By default FileTest cleans up everything under method_root except the
242
- # input and expected directories. For ease in debugging, ENV variable
243
- # flags can be specified to prevent cleanup for all tests (KEEP_OUTPUTS)
244
- # or just tests that fail (KEEP_FAILURES). These flags can be specified
245
- # from the command line if you're running the tests with rake or rap:
246
- #
247
- # % rake test keep_outputs=true
248
- # % rap test keep_failures=true
249
- #
250
252
  #--
251
253
  # TODO:
252
- # * add debugging information to indicate, for instance,
253
- # when dereferencing is going on.
254
+ # * add debugging information to indicate, for instance, when
255
+ # dereferencing is going on.
254
256
  def assert_files(options={}, &block) # :yields: input_files
255
257
  transform_test(block, options) do |expected_file, output_file|
256
258
  unless FileUtils.cmp(expected_file, output_file)
@@ -71,27 +71,37 @@ module Tap
71
71
  quiet && quiet =~ /^true$/i ? true : false
72
72
  end
73
73
 
74
- # Sets the specified ENV variables for the duration of the block.
74
+ # Sets the specified ENV variables and returns the *current* env.
75
75
  # If replace is true, current ENV variables are replaced; otherwise
76
76
  # the new env variables are simply added to the existing set.
77
- def with_env(env={}, replace=false)
77
+ def set_env(env={}, replace=false)
78
78
  current_env = {}
79
79
  ENV.each_pair do |key, value|
80
80
  current_env[key] = value
81
81
  end
82
82
 
83
+ ENV.clear if replace
84
+
85
+ env.each_pair do |key, value|
86
+ ENV[key] = value
87
+ end if env
88
+
89
+ current_env
90
+ end
91
+
92
+ # Sets the specified ENV variables for the duration of the block.
93
+ # If replace is true, current ENV variables are replaced; otherwise
94
+ # the new env variables are simply added to the existing set.
95
+ #
96
+ # Returns the block return.
97
+ def with_env(env={}, replace=false)
98
+ current_env = nil
83
99
  begin
84
- ENV.clear if replace
85
- env.each_pair do |key, value|
86
- ENV[key] = value
87
- end if env
88
-
100
+ current_env = set_env(env, replace)
89
101
  yield
90
-
91
102
  ensure
92
- ENV.clear
93
- current_env.each_pair do |key, value|
94
- ENV[key] = value
103
+ if current_env
104
+ set_env(current_env, true)
95
105
  end
96
106
  end
97
107
  end
@@ -160,6 +170,29 @@ module Tap
160
170
  # ["hello", "world"]
161
171
  # }, opts
162
172
  #
173
+ # ==== Indents
174
+ #
175
+ # To improve the readability of tests, sh_test will lstrip each line in the
176
+ # expected output to the same degree as the command line. So for instance
177
+ # these all pass:
178
+ #
179
+ # sh_test %Q{
180
+ # % argv_inspect hello world
181
+ # ["hello", "world"]
182
+ # }, opts
183
+ #
184
+ # sh_test %Q{
185
+ # % argv_inspect hello world
186
+ # ["hello", "world"]
187
+ # }, opts
188
+ #
189
+ # sh_test %Q{
190
+ # % argv_inspect hello world
191
+ # ["hello", "world"]
192
+ # }, opts
193
+ #
194
+ # Turn off indent stripping by specifying :indent => false.
195
+ #
163
196
  # ==== ENV variables
164
197
  #
165
198
  # Options may specify a hash of env variables that will be set in the
@@ -179,8 +212,15 @@ module Tap
179
212
  def sh_test(cmd, options={})
180
213
  options = sh_test_options.merge(options)
181
214
 
182
- cmd, expected = cmd.lstrip.split(/^/, 2)
183
- cmd.strip!
215
+ if cmd =~ /\A\s*?\n?(\s*)(.*?\n)(.*)\z/m
216
+ indent, cmd, expected = $1, $2, $3
217
+ cmd.strip!
218
+
219
+ if indent.length > 0 && options[:indents]
220
+ expected.gsub!(/^\s{0,#{indent.length}}/, '')
221
+ end
222
+ end
223
+
184
224
  result = sh(cmd, options)
185
225
 
186
226
  assert_equal(expected, result, cmd) if expected
@@ -11,7 +11,8 @@ module Tap
11
11
  def sh_test_options
12
12
  @sh_test_options ||= {
13
13
  :cmd_pattern => '% ',
14
- :cmd => '2>&1 '
14
+ :cmd => '2>&1 ',
15
+ :indents => true
15
16
  }
16
17
  end
17
18
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tap-test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Chiang
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-05 00:00:00 -07:00
12
+ date: 2009-12-19 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency