file_discard 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80106d989c491a8d79a5b2f6eca34d4a73a12bd1
4
- data.tar.gz: 262dc75d7c97871b948ea70064df0f0021f07dcc
3
+ metadata.gz: baf2cacff4155deb6ead06ec337cd3cbade7b4ab
4
+ data.tar.gz: e1cf01705708d7ad0d526191288874b705f00066
5
5
  SHA512:
6
- metadata.gz: e4370a3a0c0228c31b4f402224a1d3edf5d1918f4ee5c502ba6983fefc4bd27abf2e9c8c897f27310713229c4e1f1cbeda65ed2e5c127c4d2334f20550cb0bfb
7
- data.tar.gz: 1f84dc4c065f51175c48332aacc3976b29a40c0db72f331bc79498ab6ae2dfcf1cb68aa1f1f94caa3ee54d42feabe59c81964e00eff2fda8af8d406141fd84a3
6
+ metadata.gz: f769d69f5ccea663f6e6df22b1dc15a7f6f7851ba9fed5880462f0f9805bc09fd57fa0d4d1973220223e951bb5d1c7b85677435031c83a7a43efa83c4d053aa3
7
+ data.tar.gz: 336a23836b05be91779c45265b535c71fd8d5cb25eff795fa5283910d11854dbbe56b6bb1e6745a074ca6d7e181c0608a5b0a1543e1531419af673d7e9044fe3
data/Rakefile CHANGED
@@ -8,13 +8,13 @@ task default: :test
8
8
  task spec: :test
9
9
  task build: :package
10
10
 
11
- PKG_VERSION = '0.1.3'
11
+ PKG_VERSION = '0.1.4'
12
12
  NOW = Time.now.utc
13
13
 
14
14
  # delay updating the version file unless building the gem or package
15
15
  VER_FN = 'lib/file_discard_version.rb'
16
16
  task :update_version do
17
- File.open(VER_FN,'w') do |f|
17
+ File.open(VER_FN,'w',0644) do |f|
18
18
  f.puts <<EOF
19
19
  module FileDiscard
20
20
  # :nodoc:
data/bin/discard CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'optparse'
4
4
  require 'ostruct'
5
- require 'file_discard'
5
+ require_relative '../lib/file_discard'
6
6
 
7
7
  options = OpenStruct.new
8
8
  parser = OptionParser.new do |opts|
@@ -47,6 +47,8 @@ if ARGV.size < 1
47
47
  exit 64 # use same exit status as rm with no files
48
48
  end
49
49
 
50
+ FileDiscard.create_trash_when_missing = true
51
+
50
52
  status = 0
51
53
  ARGV.each do |file|
52
54
  begin
data/lib/file_discard.rb CHANGED
@@ -21,7 +21,6 @@
21
21
  # SOFTWARE.
22
22
 
23
23
  require 'pathname'
24
- require 'fileutils'
25
24
 
26
25
  module FileDiscard
27
26
 
@@ -139,8 +138,14 @@ module FileDiscard
139
138
  end
140
139
 
141
140
  def find_trash_for(pn)
142
- pd = pn.expand_path.realpath.dirname
143
- mp = mountpoint_of pd
141
+ pn_path = pn.expand_path
142
+ if pn_path.symlink?
143
+ # Use the containing directory's real path for symbolic links, not the target of the link
144
+ pd = pn_path.dirname.realpath
145
+ else
146
+ pd = pn_path.realpath.dirname
147
+ end
148
+ mp = mountpoint_of(pd)
144
149
  return @home_trash if mp == @home_mountpoint
145
150
  mp.join(@mountpoint_trash_fmt % Process.uid)
146
151
  end
@@ -158,8 +163,9 @@ module FileDiscard
158
163
  def move(src, dst, options)
159
164
  src = src.expand_path
160
165
  dst = uniquify(dst.join(src.basename))
161
- FileUtils.mv src, dst, options
162
- yield src, dst if block_given?
166
+ puts "#{src} => #{dst}" if options[:verbose]
167
+ File.rename(src, dst)
168
+ yield(src, dst) if block_given?
163
169
  end
164
170
 
165
171
  def uniquify(pn)
@@ -1,6 +1,6 @@
1
1
  module FileDiscard
2
2
  # :nodoc:
3
- VERSION = '0.1.3'
3
+ VERSION = '0.1.4'
4
4
  # :nodoc:
5
- RELEASE = '168a436:20140607032530'
5
+ RELEASE = '72f0d7a:20141010011727'
6
6
  end
@@ -16,19 +16,29 @@ load File.expand_path(File.join(File.dirname(__FILE__),'..','lib','file_discard.
16
16
  describe FileDiscard do
17
17
 
18
18
  describe :Discarder do
19
- before do
20
- @base = Pathname.new(Dir.mktmpdir(File.basename(__FILE__,'.rb') + '_'))
21
- @home = @base.join('home')
22
- @home.mkdir
23
- @home_trash = '.Trash'
19
+ let(:base) { Pathname.new(Dir.mktmpdir(File.basename(__FILE__,'.rb') + '_')) }
20
+ let(:home) do
21
+ h = base.join('home')
22
+ h.mkdir
23
+ h
24
+ end
25
+
26
+ let(:home_trash) { '.Trash' }
27
+ let(:trash) do
28
+ t = home.join(home_trash)
29
+ t.mkdir
30
+ t
31
+ end
32
+
33
+ let(:discarder) { FileDiscard::OsxDiscarder.new(home) }
24
34
 
25
- @discarder = FileDiscard::OsxDiscarder.new(@home)
26
- FileDiscard.discarder = @discarder
35
+ before do
36
+ FileDiscard.discarder = discarder
27
37
  FileDiscard.create_trash_when_missing = false
28
38
  end
29
39
 
30
40
  after do
31
- @base.rmtree if @base && @base.exist?
41
+ base.rmtree
32
42
  end
33
43
 
34
44
  it 'should not allow removal of special directories' do
@@ -49,35 +59,34 @@ describe FileDiscard do
49
59
  end
50
60
 
51
61
  it 'should fail without trash' do
52
- f = File.new(@base.join('file.txt').to_s, 'w')
62
+ f = File.new(base.join('file.txt').to_s, 'w')
53
63
  ->{ f.discard }.must_raise FileDiscard::TrashMissing
54
64
  end
55
65
 
56
66
  it 'should support creating missing trash' do
57
67
  FileDiscard.create_trash_when_missing = true
58
- f = File.new(@base.join('file.txt').to_s, 'w')
68
+ f = File.new(base.join('file.txt').to_s, 'w')
59
69
  f.discard
60
70
  end
61
71
 
62
72
  describe 'with trash in the home' do
63
- before do
64
- @trash = @home.join(@home_trash)
65
- @trash.mkdir
73
+ def sorted_trash
74
+ trash.children(false).collect(&:to_s).sort
66
75
  end
67
76
 
68
- def sorted_trash
69
- @trash.children(false).collect(&:to_s).sort
77
+ before do
78
+ trash # ensure trash is created, but as late as possible for let-overrides
70
79
  end
71
80
 
72
81
  it 'should conditionally allow removal of empty directories' do
73
- d = @base.join('foozy')
82
+ d = base.join('foozy')
74
83
  d.mkdir
75
84
  ->{ FileDiscard.discard(d) }.must_raise Errno::EISDIR
76
85
  FileDiscard.discard(d, directory: true)
77
86
  end
78
87
 
79
88
  it 'should conditionally allow removal of non-empty directories' do
80
- d = @base.join('foozy')
89
+ d = base.join('foozy')
81
90
  d.mkdir
82
91
  f = d.join('stuff.txt')
83
92
  f.open('w') {|io| io.puts 'stuff'}
@@ -86,18 +95,44 @@ describe FileDiscard do
86
95
  end
87
96
 
88
97
  it 'should discard a file' do
89
- f = File.new(@base.join('file.txt').to_s, 'w')
98
+ f = File.new(base.join('file.txt').to_s, 'w')
90
99
  f.discard
91
100
  sorted_trash.must_equal ['file.txt']
92
101
  end
93
102
 
94
103
  it 'should discard a pathname' do
95
- f = @base.join('file.txt')
104
+ f = base.join('file.txt')
96
105
  f.open('w') {|io| io.puts 'nothing'}
97
106
  f.discard
98
107
  sorted_trash.must_equal ['file.txt']
99
108
  end
100
109
 
110
+ describe 'with a symbolic links' do
111
+ let(:target) do
112
+ t = base.join('target')
113
+ t.open('w') {|io| io.puts 'nothing'}
114
+ t
115
+ end
116
+
117
+ let(:file) do
118
+ f = home.join('pointer')
119
+ f.make_symlink(target)
120
+ f
121
+ end
122
+
123
+ it 'should leave the target untouched' do
124
+ file.discard
125
+ sorted_trash.must_equal ['pointer']
126
+ target.exist?.must_equal true
127
+ end
128
+
129
+ it 'should not fail when target is missing' do
130
+ target.unlink
131
+ file.discard
132
+ sorted_trash.must_equal ['pointer']
133
+ end
134
+ end
135
+
101
136
  describe 'with control over time' do
102
137
  before do
103
138
  # replace Time's strftime which FileDiscard relies on for uniquify
@@ -120,10 +155,10 @@ describe FileDiscard do
120
155
  end
121
156
 
122
157
  it 'should not overwite other trashed files' do
123
- f = @trash.join('file.txt')
158
+ f = trash.join('file.txt')
124
159
  f.open('w') {|io| io.puts 'nothing'}
125
160
 
126
- f = @base.join('file.txt')
161
+ f = base.join('file.txt')
127
162
  f.open('w') {|io| io.puts 'nothing'}
128
163
  FileDiscard.discard(f.to_s)
129
164
  sorted_trash.must_equal ['file 9.8.1.txt','file.txt']
@@ -131,11 +166,11 @@ describe FileDiscard do
131
166
 
132
167
  it 'should use increasing precision for collisions' do
133
168
  3.times do |i|
134
- f = @trash.join(%{file#{i == 0 ? '' : " 9.8.#{i}"}.txt})
169
+ f = trash.join(%{file#{i == 0 ? '' : " 9.8.#{i}"}.txt})
135
170
  f.open('w') {|io| io.puts 'nothing'}
136
171
  end
137
172
 
138
- f = @base.join('file.txt')
173
+ f = base.join('file.txt')
139
174
  f.open('w') {|io| io.puts 'nothing'}
140
175
  File.discard(f.to_s)
141
176
  sorted_trash
@@ -151,15 +186,11 @@ describe FileDiscard do
151
186
  end
152
187
  end
153
188
 
154
- before do
155
- @trash = @base.join('mytrash-%s' % Process.uid)
156
- @trash.mkdir
157
- @discarder = MyDiscarder.new(@home, 'mytrash', 'mytrash-%s')
158
- FileDiscard.discarder = @discarder
159
- end
189
+ let(:home_trash) { base.join('mytrash-%s' % Process.uid) }
190
+ let(:discarder) { MyDiscarder.new(home, 'mytrash', 'mytrash-%s') }
160
191
 
161
192
  it 'should not use the home trash' do
162
- f = @base.join('file.txt')
193
+ f = base.join('file.txt')
163
194
  f.open('w') {|io| io.puts 'nothing'}
164
195
  f.discard
165
196
  sorted_trash.must_equal ['file.txt']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_discard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Robel-Forrest
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-07 00:00:00.000000000 Z
11
+ date: 2014-10-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Simple helper to move files to the trash folder.
14
14
  email: brad+filediscard@gigglewax.com