ghaki-ext-file 2011.12.01.1 → 2011.12.03.1
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.
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
require 'ghaki/core_ext/file/with_temp'
|
3
|
+
|
1
4
|
module Ghaki #:nodoc:
|
2
5
|
module CoreExt #:nodoc:
|
3
6
|
module File #:nodoc:
|
@@ -6,9 +9,8 @@ module SpecHelper #:nodoc:
|
|
6
9
|
module FakeTemp
|
7
10
|
|
8
11
|
def setup_fake_tempfile
|
9
|
-
@fake_tempfile =
|
10
|
-
File.stubs(:with_opened_temp).yields(@fake_tempfile)
|
11
|
-
File.stubs(:with_named_temp).yields(@fake_tempfile)
|
12
|
+
@fake_tempfile = StringIO.new()
|
13
|
+
::File.stubs(:with_opened_temp).yields(@fake_tempfile)
|
12
14
|
end
|
13
15
|
|
14
16
|
end
|
@@ -5,29 +5,34 @@ module File #:nodoc:
|
|
5
5
|
# Helpers dealing with auto-cleaning up temp files.
|
6
6
|
module WithTemp
|
7
7
|
|
8
|
-
#
|
9
|
-
def
|
10
|
-
|
8
|
+
# Generate temp filename from given filename.
|
9
|
+
def make_temp_name final_name
|
10
|
+
::File.dirname(final_name) +
|
11
11
|
::File::Separator + '_tmp_' + $$.to_s + '.' +
|
12
12
|
::File.basename(final_name)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Calls block with temp filename then overwrites target filename if no exceptions occur.
|
16
|
+
def with_named_temp final_name
|
17
|
+
tmp_name = make_temp_name(final_name)
|
13
18
|
yield tmp_name
|
14
|
-
::File.rename tmp_name, final_name
|
19
|
+
::File.rename tmp_name, final_name if ::File.exists?(tmp_name)
|
15
20
|
ensure
|
16
21
|
::File.unlink tmp_name if ::File.exists?(tmp_name)
|
17
22
|
end
|
18
23
|
|
19
24
|
# Opens writable temp file, renames to proper name if no exceptions.
|
20
|
-
def with_opened_temp final_name, mode='w',
|
25
|
+
def with_opened_temp final_name, mode='w', *args
|
21
26
|
with_named_temp final_name do |tmp_name|
|
22
|
-
::File.open(tmp_name,mode) do |tmp_file|
|
27
|
+
::File.open(tmp_name,mode,*args) do |tmp_file|
|
23
28
|
yield tmp_file
|
24
29
|
end
|
25
30
|
end
|
26
31
|
end
|
27
32
|
|
28
33
|
# Simple helper for dumping a string to an output file using a temp.
|
29
|
-
def dump_via_temp data_str, final_name, mode='w',
|
30
|
-
with_opened_temp final_name, mode,
|
34
|
+
def dump_via_temp data_str, final_name, mode='w', *args
|
35
|
+
with_opened_temp final_name, mode, *args do |tmp_file|
|
31
36
|
tmp_file.puts data_str
|
32
37
|
end
|
33
38
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'ghaki/core_ext/file/spec_helper/fake_temp'
|
2
|
+
|
3
|
+
describe Ghaki::CoreExt::File::SpecHelper::FakeTemp do
|
4
|
+
include Ghaki::CoreExt::File::SpecHelper::FakeTemp
|
5
|
+
|
6
|
+
subject { self }
|
7
|
+
it { should respond_to :setup_fake_tempfile }
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
setup_fake_tempfile
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:final_name) { 'fake-final-name' }
|
14
|
+
let(:dump_data) { 'testing-data' }
|
15
|
+
|
16
|
+
describe 'setup_fake_tempfile' do
|
17
|
+
it 'creates fake temp file' do
|
18
|
+
@fake_tempfile.should be_kind_of(StringIO)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'File#with_opened_temp' do
|
23
|
+
it 'does not open temp file' do
|
24
|
+
File.expects(:open).never
|
25
|
+
File.with_opened_temp final_name do |put_file|
|
26
|
+
# do nothing
|
27
|
+
end
|
28
|
+
end
|
29
|
+
it 'yields fake temp file' do
|
30
|
+
File.with_opened_temp final_name do |put_file|
|
31
|
+
put_file.should == @fake_tempfile
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'File#dump_via_temp' do
|
37
|
+
it 'does not open temp file' do
|
38
|
+
File.expects(:open).never
|
39
|
+
File.dump_via_temp dump_data, final_name
|
40
|
+
end
|
41
|
+
it 'writes test data to fake output string' do
|
42
|
+
File.dump_via_temp dump_data, final_name
|
43
|
+
@fake_tempfile.rewind
|
44
|
+
@fake_tempfile.string.chomp.should == dump_data
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -1,12 +1,103 @@
|
|
1
1
|
require 'ghaki/core_ext/file/with_temp'
|
2
|
+
require 'stringio'
|
2
3
|
|
3
4
|
describe Ghaki::CoreExt::File::WithTemp do
|
4
5
|
|
5
6
|
context 'using extended File' do
|
6
7
|
subject { File }
|
7
|
-
it { should
|
8
|
-
|
9
|
-
|
8
|
+
it { should be_kind_of(Ghaki::CoreExt::File::WithTemp) }
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:final_name) { File.join(File.dirname(__FILE__), 'my-fake-file' ) }
|
12
|
+
let(:temp_name) { File.join(File.dirname(__FILE__), 'my-fake-temp' ) }
|
13
|
+
|
14
|
+
describe '#make_temp_name' do
|
15
|
+
subject { File.make_temp_name(final_name) }
|
16
|
+
|
17
|
+
it 'uses same dirname' do
|
18
|
+
File.dirname(subject).should == File.dirname(final_name)
|
19
|
+
end
|
20
|
+
it 'uses different basename' do
|
21
|
+
File.basename(subject).should_not == File.basename(final_name)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#with_named_temp' do
|
26
|
+
|
27
|
+
before(:each) do
|
28
|
+
File.expects(:make_temp_name).returns(temp_name).once
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'renames temp to final when successful' do
|
32
|
+
File.expects(:exists?).with(temp_name).returns(true,false).twice
|
33
|
+
File.expects(:rename).with(temp_name,final_name).once
|
34
|
+
File.expects(:unlink).never
|
35
|
+
File.with_named_temp final_name do
|
36
|
+
# do nothing
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'calls unlink on temp after failure' do
|
41
|
+
File.expects(:exists?).with(temp_name).
|
42
|
+
returns(true).once
|
43
|
+
File.expects(:unlink).with(temp_name).once
|
44
|
+
File.expects(:rename).never
|
45
|
+
lambda do
|
46
|
+
File.with_named_temp final_name do
|
47
|
+
raise RuntimeError, 'testing'
|
48
|
+
end
|
49
|
+
end.should raise_error(RuntimeError)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
describe '#with_opened_temp' do
|
55
|
+
|
56
|
+
before(:each) do
|
57
|
+
File.expects(:make_temp_name).returns(temp_name).once
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'renames temp to final when successful' do
|
61
|
+
File.expects(:exists?).with(temp_name).returns(true,false).at_most(2)
|
62
|
+
File.expects(:rename).with(temp_name,final_name).once
|
63
|
+
File.expects(:open).with(temp_name,'w').once
|
64
|
+
File.with_opened_temp final_name do
|
65
|
+
# doing nothing
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'opens temp file for writing' do
|
70
|
+
File.expects(:exists?).with(temp_name).returns(false).at_most(2)
|
71
|
+
File.expects(:open).with(temp_name,'w').once
|
72
|
+
File.with_opened_temp final_name do
|
73
|
+
# doing nothing
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
describe '#dump_via_temp' do
|
80
|
+
|
81
|
+
let(:dump_data) { 'data-to-dump' }
|
82
|
+
|
83
|
+
before(:each) do
|
84
|
+
File.expects(:make_temp_name).returns(temp_name).once
|
85
|
+
@tmp_file = StringIO.new
|
86
|
+
@tmp_file.expects(:puts).with(dump_data).once
|
87
|
+
File.expects(:open).with(temp_name,'w').yields(@tmp_file).once
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'renames temp to final when successful' do
|
91
|
+
File.expects(:exists?).with(temp_name).returns(true,false).at_most(2)
|
92
|
+
File.expects(:rename).with(temp_name,final_name).once
|
93
|
+
File.dump_via_temp dump_data, final_name
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'dumps data to opened temp file' do
|
97
|
+
File.expects(:exists?).with(temp_name).returns(false).at_most(2)
|
98
|
+
File.dump_via_temp dump_data, final_name
|
99
|
+
end
|
100
|
+
|
10
101
|
end
|
11
102
|
|
12
103
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ghaki-ext-file
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2011.12.
|
4
|
+
version: 2011.12.03.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-03 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &72774220 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.4.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *72774220
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rdoc
|
27
|
-
requirement: &
|
27
|
+
requirement: &72773990 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 3.9.4
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *72773990
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: mocha
|
38
|
-
requirement: &
|
38
|
+
requirement: &72773760 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 0.9.12
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *72773760
|
47
47
|
description: Collection of extensions for the core File class
|
48
48
|
email: gerald@kalafut.org
|
49
49
|
executables: []
|
@@ -55,6 +55,7 @@ files:
|
|
55
55
|
- lib/ghaki/core_ext/file/with_temp.rb
|
56
56
|
- README
|
57
57
|
- LICENSE
|
58
|
+
- spec/ghaki/core_ext/file/spec_helper/fake_temp_spec.rb
|
58
59
|
- spec/ghaki/core_ext/file/with_temp_spec.rb
|
59
60
|
- spec/spec_helper.rb
|
60
61
|
homepage: http://github.com/ghaki
|
@@ -82,5 +83,6 @@ signing_key:
|
|
82
83
|
specification_version: 3
|
83
84
|
summary: Core File extensions
|
84
85
|
test_files:
|
86
|
+
- spec/ghaki/core_ext/file/spec_helper/fake_temp_spec.rb
|
85
87
|
- spec/ghaki/core_ext/file/with_temp_spec.rb
|
86
88
|
- spec/spec_helper.rb
|