Skip to content
This repository
Browse code

Refactoring tests: Closing tempfile in teardown and DRY the code.

  • Loading branch information...
commit 9c5e4b230311729357e0af51e816d87f966f0426 1 parent ec20e6a
janfri authored April 11, 2012
14  test/helpers_for_test.rb
... ...
@@ -1,6 +1,8 @@
1 1
 # -- encoding: utf-8 --
2 2
 require 'mini_exiftool'
3 3
 require 'test/unit'
  4
+require 'fileutils'
  5
+require 'tempfile'
4 6
 begin
5 7
   require 'turn'
6 8
 rescue LoadError
@@ -12,3 +14,15 @@
12 14
 end
13 15
 
14 16
 include Test::Unit
  17
+
  18
+module TempfileTest
  19
+  def setup
  20
+    @temp_file = Tempfile.new('test')
  21
+    @temp_filename = @temp_file.path
  22
+    @data_dir = File.dirname(__FILE__) + '/data'
  23
+  end
  24
+
  25
+  def teardown
  26
+    @temp_file.close
  27
+  end
  28
+end
8  test/test_save.rb
@@ -6,11 +6,11 @@
6 6
 
7 7
 class TestSave < TestCase
8 8
 
  9
+  include TempfileTest
  10
+
9 11
   def setup
10  
-    @temp_file = Tempfile.new('test')
11  
-    @temp_file.close
12  
-    @temp_filename = @temp_file.path
13  
-    @org_filename = File.dirname(__FILE__) + '/data/test.jpg'
  12
+    super
  13
+    @org_filename = @data_dir + '/test.jpg'
14 14
     FileUtils.cp(@org_filename, @temp_filename)
15 15
     @mini_exiftool = MiniExiftool.new @temp_filename
16 16
     @mini_exiftool_num = MiniExiftool.new @temp_filename, :numerical => true
13  test/test_special.rb
... ...
@@ -1,19 +1,16 @@
1 1
 # -- encoding: utf-8 --
2  
-require 'fileutils'
3  
-require 'tempfile'
4 2
 require 'helpers_for_test'
5 3
 
6 4
 class TestSpecial < TestCase
7 5
 
  6
+  include TempfileTest
  7
+
8 8
   CAPTION_ABSTRACT =  'Some text for caption abstract'
9 9
 
10 10
   def setup
11  
-    data_dir = File.dirname(__FILE__) + '/data'
12  
-    temp_file = Tempfile.new('test')
13  
-    temp_file.close
14  
-    @temp_filename = temp_file.path
15  
-    org_filename = data_dir + '/Canon.jpg'
16  
-    FileUtils.cp org_filename, @temp_filename
  11
+    super
  12
+    @org_filename = @data_dir + '/Canon.jpg'
  13
+    FileUtils.cp @org_filename, @temp_filename
17 14
     @canon = MiniExiftool.new @temp_filename
18 15
   end
19 16
 
15  test/test_special_dates.rb
... ...
@@ -1,18 +1,15 @@
1 1
 # -- encoding: utf-8 --
2 2
 require 'date'
3  
-require 'fileutils'
4  
-require 'tempfile'
5 3
 require 'helpers_for_test'
6 4
 
7 5
 class TestSpecialDates < TestCase
8 6
 
  7
+  include TempfileTest
  8
+
9 9
   def setup
10  
-    data_dir = File.dirname(__FILE__) + '/data'
11  
-    temp_file = Tempfile.new('test')
12  
-    temp_file.close
13  
-    @temp_filename = temp_file.path
14  
-    org_filename = data_dir + '/test_special_dates.jpg'
15  
-    FileUtils.cp org_filename, @temp_filename
  10
+    super
  11
+    @org_filename = @data_dir + '/test_special_dates.jpg'
  12
+    FileUtils.cp @org_filename, @temp_filename
16 13
     @mini_exiftool = MiniExiftool.new @temp_filename
17 14
     @mini_exiftool_datetime = MiniExiftool.new @temp_filename,
18 15
       :timestamps => DateTime
@@ -21,7 +18,7 @@ def setup
21 18
   # Catching bug [#16328] (1st part)
22 19
   # Thanks to unknown
23 20
   def test_datetime
24  
-    datetime_original = @mini_exiftool.datetime_original 
  21
+    datetime_original = @mini_exiftool.datetime_original
25 22
     if datetime_original
26 23
       assert_kind_of Time, datetime_original
27 24
     else

0 notes on commit 9c5e4b2

Please sign in to comment.
Something went wrong with that request. Please try again.