public
Description: RSpec-style specification for the Ruby programming language
Homepage: http://rubyspec.org
Clone URL: git://github.com/rubyspec/rubyspec.git
Search Repo:
Ensure files are closed before deleting them.
brixen (author)
Wed Jul 16 18:43:38 -0700 2008
commit  0d27c9c193cc0045895a9593dfba03d05ca88937
tree    cc1730445d346d695d197300d08741fc637dd918
parent  b2e1a2fc78570b2c3f6b4cdb3475d3ed6c28486e
...
1
2
3
4
5
6
7
 
 
 
 
 
 
8
9
10
11
12
 
 
 
 
 
13
14
15
16
17
 
 
 
18
19
20
 
 
 
21
22
23
24
25
26
 
 
27
28
 
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
 
 
43
 
 
 
 
 
 
44
...
1
2
3
 
 
 
 
4
5
6
7
8
9
10
 
 
 
 
11
12
13
14
15
16
17
 
 
 
18
19
20
21
 
 
22
23
24
25
 
 
 
 
 
26
27
28
 
29
30
 
 
 
 
 
 
 
 
 
 
 
 
 
31
32
33
34
35
36
37
38
39
40
41
0
@@ -1,44 +1,41 @@
0
 require File.dirname(__FILE__) + '/../../../spec_helper'
0
 
0
 describe "File::Stat#<=>" do
0
-
0
- it "is able to compare files by the same modification times" do
0
- f1 = File.new(tmp("i_exist"), "w")
0
- f2 = File.new(tmp("i_exist_too"), "w")
0
+ before :each do
0
+ @name1 = tmp("i_exist")
0
+ @name2 = tmp("i_exist_too")
0
+ @file1 = File.new @name1, "w"
0
+ @file2 = File.new @name2, "w"
0
+ end
0
 
0
- (f1.stat <=> f2.stat).should == 0
0
-
0
- File.delete(tmp("i_exist")) if File.exist?(tmp("i_exist"))
0
- File.delete(tmp("i_exist_too")) if File.exist?(tmp("i_exist_too"))
0
+ after :each do
0
+ @file1.close unless @file1.closed?
0
+ @file2.close unless @file2.closed?
0
+ File.delete @name1
0
+ File.delete @name2
0
   end
0
 
0
- it "is able to compare files by different modification times" do
0
- f1 = File.new(tmp("i_exist"), "w")
0
- f2 = File.new(tmp("i_exist_too"), "w")
0
+ it "is able to compare files by the same modification times" do
0
+ (@file1.stat <=> @file2.stat).should == 0
0
+ end
0
 
0
- File.utime(Time.now, Time.now + 100, tmp("i_exist_too"))
0
- (f1.stat <=> f2.stat).should == -1
0
+ it "is able to compare files by different modification times" do
0
+ File.utime(Time.now, Time.now + 100, @name2)
0
+ (@file1.stat <=> @file2.stat).should == -1
0
 
0
- File.utime(Time.now, Time.now - 100, tmp("i_exist_too"))
0
- (f1.stat <=> f2.stat).should == 1
0
-
0
- File.delete(tmp("i_exist")) if File.exist?(tmp("i_exist"))
0
- File.delete(tmp("i_exist_too")) if File.exist?(tmp("i_exist_too"))
0
+ File.utime(Time.now, Time.now - 100, @name2)
0
+ (@file1.stat <=> @file2.stat).should == 1
0
   end
0
-
0
+
0
   it "should also include Comparable and thus == shows mtime equality between two File::Stat objects" do
0
- f1 = File.new(tmp("i_exist"), "w")
0
- f2 = File.new(tmp("i_exist_too"), "w")
0
-
0
- (f1.stat == f2.stat).should == true
0
- (f1.stat == f1.stat).should == true
0
- (f2.stat == f2.stat).should == true
0
-
0
- File.utime(Time.now, Time.now + 100, tmp("i_exist_too"))
0
-
0
- (f1.stat == f2.stat).should == false
0
- (f1.stat == f1.stat).should == true
0
- (f2.stat == f2.stat).should == true
0
- end
0
+ (@file1.stat == @file2.stat).should == true
0
+ (@file1.stat == @file1.stat).should == true
0
+ (@file2.stat == @file2.stat).should == true
0
 
0
+ File.utime(Time.now, Time.now + 100, @name2)
0
+
0
+ (@file1.stat == @file2.stat).should == false
0
+ (@file1.stat == @file1.stat).should == true
0
+ (@file2.stat == @file2.stat).should == true
0
+ end
0
 end
...
8
9
10
11
12
 
 
 
13
14
15
...
19
20
21
22
 
23
24
25
...
8
9
10
 
 
11
12
13
14
15
16
...
20
21
22
 
23
24
25
26
0
@@ -8,8 +8,9 @@ describe "Logger::Application#level=" do
0
     @app = LoggerSpecs::TestApp.new("TestApp", @log_file)
0
 
0
   end
0
-
0
- after :all do
0
+
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@file_path) if File.exists?(@file_path)
0
   end
0
 
0
@@ -19,7 +20,7 @@ describe "Logger::Application#level=" do
0
     @app.log(Logger::WARN, "Don't show me")
0
     @app.log(Logger::ERROR, "Show me")
0
     @log_file.rewind
0
- messages = @log_file.readlines
0
+ messages = @log_file.readlines
0
     messages.length.should == 1
0
     LoggerSpecs::strip_date(messages.first).should == "ERROR -- TestApp: Show me\n"
0
   end
...
9
10
11
12
 
 
13
14
15
...
19
20
21
22
 
23
24
25
...
35
36
37
38
 
39
40
41
...
9
10
11
 
12
13
14
15
16
...
20
21
22
 
23
24
25
26
...
36
37
38
 
39
40
41
42
0
@@ -9,7 +9,8 @@ describe "Logger::Application#log" do
0
     @app.start
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@file_path) if File.exists?(@file_path)
0
   end
0
 
0
@@ -19,7 +20,7 @@ describe "Logger::Application#log" do
0
     message = @log_file.readlines.last
0
     LoggerSpecs::strip_date(message).should == "WARN -- TestApp: Test message\n"
0
   end
0
-
0
+
0
   it "receives a severity" do
0
     @app.log(Logger::INFO, "Info message")
0
     @app.log(Logger::DEBUG, "Debug message")
0
@@ -35,7 +36,7 @@ describe "Logger::Application#log" do
0
     LoggerSpecs::strip_date(messages[3]).should == "ERROR -- TestApp: Error message\n"
0
     LoggerSpecs::strip_date(messages[4]).should == "FATAL -- TestApp: Fatal message\n"
0
   end
0
-
0
+
0
   it "uses app name for Application Name" do
0
     @app.log(Logger::INFO, "Info message")
0
     @log_file.rewind
...
7
8
9
10
 
 
11
12
13
...
7
8
9
 
10
11
12
13
14
0
@@ -7,7 +7,8 @@ describe "Logger::Application.new" do
0
     @log_file = File.open(@file_path, "w+")
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@file_path) if File.exists?(@file_path)
0
   end
0
 
...
7
8
9
10
11
 
 
 
12
13
14
...
7
8
9
 
 
10
11
12
13
14
15
0
@@ -7,8 +7,9 @@ describe "Logger::Application#set_log"do
0
     @log_file = File.open(@file_path, "w+")
0
     @app = LoggerSpecs::TestApp.new("TestApp", @log_file)
0
   end
0
-
0
- after :all do
0
+
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@file_path) if File.exists?(@file_path)
0
   end
0
 
...
7
8
9
10
11
 
 
 
12
13
14
...
7
8
9
 
 
10
11
12
13
14
15
0
@@ -7,8 +7,9 @@ describe "Logger::Application#start" do
0
     @log_file = File.open(@file_path, "w+")
0
     @app = LoggerSpecs::TestApp.new("TestApp", @log_file)
0
   end
0
-
0
- after :all do
0
+
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@file_path) if File.exists?(@file_path)
0
   end
0
 
...
5
6
7
8
 
9
10
11
12
13
 
 
14
15
16
 
17
18
19
...
5
6
7
 
8
9
10
11
12
 
13
14
15
16
 
17
18
19
20
0
@@ -5,15 +5,16 @@ describe "Logger::LogDevice#close" do
0
   before :each do
0
     @file_path = tmp("test_log.log")
0
     @log_file = File.open(@file_path, "w+")
0
-
0
+
0
     # Avoid testing this with STDERR, we don't want to be closing that.
0
     @device = Logger::LogDevice.new(@log_file)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@file_path) if File.exists?(@file_path)
0
   end
0
-
0
+
0
   it "closes the LogDevice's stream" do
0
     @device.close
0
     lambda { @device.write("Test") }.should raise_error
...
7
8
9
10
 
 
11
12
13
...
35
36
37
38
 
39
40
 
41
42
 
43
44
45
...
7
8
9
 
10
11
12
13
14
...
36
37
38
 
39
40
 
41
42
 
43
44
45
46
0
@@ -7,7 +7,8 @@ describe "Logger::LogDevice#new" do
0
     @log_file = File.open(@file_path, "w+")
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@file_path) if File.exists?(@file_path)
0
   end
0
 
0
@@ -35,11 +36,11 @@ describe "Logger::LogDevice#new" do
0
       f.readlines.should_not be_empty
0
     end
0
 
0
- File.unlink(path)
0
+ File.unlink(path)
0
   end
0
-
0
+
0
   it "receives options via a hash as second argument" do
0
- lambda { Logger::LogDevice.new(STDERR,
0
+ lambda { Logger::LogDevice.new(STDERR,
0
                                    { :shift_age => 8, :shift_size => 10
0
                                    })}.should_not raise_error
0
   end
...
9
10
11
12
 
 
13
14
15
...
24
25
26
27
 
28
29
30
...
9
10
11
 
12
13
14
15
16
...
25
26
27
 
28
29
30
31
0
@@ -9,7 +9,8 @@ describe "Logger::LogDevice#write" do
0
     @device = Logger::LogDevice.new(@log_file)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@file_path) if File.exists?(@file_path)
0
   end
0
 
0
@@ -24,7 +25,7 @@ describe "Logger::LogDevice#write" do
0
     logdevice = Logger::LogDevice.new(path)
0
     logdevice.write("")
0
     logdevice.close
0
-
0
+
0
     File.open(path) do |f|
0
       messages = f.readlines
0
       messages.size.should == 1
...
8
9
10
11
 
 
12
13
14
...
50
51
52
53
54
 
 
55
56
57
...
59
60
61
62
63
 
 
64
65
66
67
68
69
 
70
71
72
73
 
 
74
75
76
77
78
79
 
...
8
9
10
 
11
12
13
14
15
...
51
52
53
 
 
54
55
56
57
58
...
60
61
62
 
 
63
64
65
66
67
68
69
 
70
71
72
 
 
73
74
75
76
77
78
79
 
80
0
@@ -8,7 +8,8 @@ describe "Logger#add" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
0
@@ -50,8 +51,8 @@ describe "Logger#add" do
0
   end
0
 
0
   it "receives a block" do
0
- lambda {
0
- @logger.log(nil, "test", "TestApp") do
0
+ lambda {
0
+ @logger.log(nil, "test", "TestApp") do
0
         1+1
0
       end
0
     }.should_not raise_error
0
@@ -59,21 +60,21 @@ describe "Logger#add" do
0
 
0
   it "calls the block if message is nil" do
0
     temp = 0
0
- lambda {
0
- @logger.log(nil, nil, "TestApp") do
0
+ lambda {
0
+ @logger.log(nil, nil, "TestApp") do
0
         temp = 1+1
0
       end
0
     }.should_not raise_error
0
     temp.should == 2
0
   end
0
-
0
+
0
   it "ignores the block if the message is not nil" do
0
     temp = 0
0
- lambda {
0
- @logger.log(nil, "not nil", "TestApp") do
0
+ lambda {
0
+ @logger.log(nil, "not nil", "TestApp") do
0
         temp = 1+1
0
       end
0
     }.should_not raise_error
0
     temp.should == 0
0
   end
0
-end
0
+end
...
8
9
10
 
 
 
 
 
11
12
13
...
8
9
10
11
12
13
14
15
16
17
18
0
@@ -8,6 +8,11 @@ describe "Logger#close" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
+ File.unlink(@path) if File.exists?(@path)
0
+ end
0
+
0
   it "closes the logging device" do
0
     @logger.close
0
     lambda { @logger.add(nil, "Foo") }.should raise_error(IOError)
...
8
9
10
 
 
 
 
 
11
12
13
...
26
27
28
 
 
 
 
 
29
30
31
...
8
9
10
11
12
13
14
15
16
17
18
...
31
32
33
34
35
36
37
38
39
40
41
0
@@ -8,6 +8,11 @@ describe "Logger#datetime_format" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
+ File.unlink(@path) if File.exists?(@path)
0
+ end
0
+
0
   it "returns the date format used for the logs" do
0
     format = "%Y-%d"
0
     @logger.datetime_format = format
0
@@ -26,6 +31,11 @@ describe "Logger#datetime_format=" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
+ File.unlink(@path) if File.exists?(@path)
0
+ end
0
+
0
   it "sets the date format for the logs" do
0
     format = "%Y"
0
     @logger.datetime_format = "%Y"
...
8
9
10
11
 
 
12
13
14
...
30
31
32
33
 
 
34
35
36
...
45
46
47
48
49
...
8
9
10
 
11
12
13
14
15
...
31
32
33
 
34
35
36
37
38
...
47
48
49
 
50
0
@@ -8,7 +8,8 @@ describe "Logger#debug?" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
0
@@ -30,7 +31,8 @@ describe "Logger#debug" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
0
@@ -45,5 +47,4 @@ describe "Logger#debug" do
0
     @log_file.rewind
0
     LoggerSpecs::strip_date(@log_file.readlines.first).should == "DEBUG -- MyApp: Test message\n"
0
   end
0
-
0
 end
...
8
9
10
11
 
 
12
13
14
...
30
31
32
33
 
 
34
35
36
...
8
9
10
 
11
12
13
14
15
...
31
32
33
 
34
35
36
37
38
0
@@ -8,7 +8,8 @@ describe "Logger#error?" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
0
@@ -30,7 +31,8 @@ describe "Logger#error" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
...
8
9
10
11
 
 
12
13
14
...
30
31
32
33
 
 
34
35
36
...
8
9
10
 
11
12
13
14
15
...
31
32
33
 
34
35
36
37
38
0
@@ -8,7 +8,8 @@ describe "Logger#fatal?" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
0
@@ -30,7 +31,8 @@ describe "Logger#fatal" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
...
8
9
10
11
 
 
12
13
14
...
30
31
32
33
 
 
34
35
36
...
8
9
10
 
11
12
13
14
15
...
31
32
33
 
34
35
36
37
38
0
@@ -8,7 +8,8 @@ describe "Logger#info?" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
0
@@ -30,7 +31,8 @@ describe "Logger#info" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
...
8
9
10
11
 
 
12
13
14
...
8
9
10
 
11
12
13
14
15
0
@@ -8,7 +8,8 @@ describe "Logger#new" do
0
     @log_file = File.open(@file_path, "w+")
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@file_path) if File.exists?(@file_path)
0
   end
0
 
...
8
9
10
11
 
 
12
13
14
...
8
9
10
 
11
12
13
14
15
0
@@ -8,7 +8,8 @@ describe "Logger#unknown" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
...
8
9
10
11
 
 
12
13
14
...
30
31
32
33
 
 
34
35
36
...
8
9
10
 
11
12
13
14
15
...
31
32
33
 
34
35
36
37
38
0
@@ -8,7 +8,8 @@ describe "Logger#warn?" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
0
@@ -30,7 +31,8 @@ describe "Logger#warn" do
0
     @logger = Logger.new(@path)
0
   end
0
 
0
- after :all do
0
+ after :each do
0
+ @log_file.close unless @log_file.closed?
0
     File.unlink(@path) if File.exists?(@path)
0
   end
0
 
...
1
2
3
4
5
 
 
 
6
7
8
...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
 
 
54
55
...
1
2
 
 
 
3
4
5
6
7
8
...
14
15
16
 
 
 
 
 
 
 
 
17
18
19
...
31
32
33
 
 
 
 
 
 
 
 
 
 
 
 
34
35
36
37
38
0
@@ -1,8 +1,8 @@
0
 describe :file_identical, :shared => true do
0
   before :each do
0
- @file1 = 'test.txt'
0
- @file2 = 'test2.txt'
0
- @file3 = 'test.lnk'
0
+ @file1 = tmp('test.txt')
0
+ @file2 = tmp('test2.txt')
0
+ @file3 = tmp('test.lnk')
0
     File.delete(@file3) if File.exists?(@file3)
0
 
0
     File.open(@file1,"w+") { |f| f.puts "file1" }
0
@@ -14,14 +14,6 @@ describe :file_identical, :shared => true do
0
     File.unlink(@file3)
0
     File.delete(@file1) if File.exists?(@file1)
0
     File.delete(@file2) if File.exists?(@file2)
0
- @file1 = nil
0
- @file1 = nil
0
- @file1 = nil
0
- end
0
-
0
- it "return a Boolean class" do
0
- @object.send(@method, @file1, @file2).class.should == FalseClass
0
- @object.send(@method, @file1, @file1).class.should == TrueClass
0
   end
0
 
0
   it "return true if they are identical" do
0
@@ -39,17 +31,8 @@ describe :file_identical, :shared => true do
0
     lambda { @object.send(@method, 1,1) }.should raise_error(TypeError)
0
   end
0
 
0
- it "identical? should return true if both named files are identical" do
0
- begin
0
- file = tmp('i_exist')
0
- file2 = tmp('i_exist_too')
0
- File.open(file,'w'){|f| f.write 'rubinius'}
0
- File.open(file2,'w'){|f| f.write 'ruby'}
0
- @object.send(@method, file,file).should == true
0
- @object.send(@method, file,file2).should == false
0
- ensure
0
- File.delete(file) rescue nil
0
- File.delete(file2) rescue nil
0
- end
0
+ it "returns true if both named files are identical" do
0
+ @object.send(@method, @file1, @file1).should be_true
0
+ @object.send(@method, @file1, @file2).should be_false
0
   end
0
 end

Comments

    No one has commented yet.