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 +4 -4
- data/Rakefile +2 -2
- data/bin/discard +3 -1
- data/lib/file_discard.rb +11 -5
- data/lib/file_discard_version.rb +2 -2
- data/spec/file_discard_spec.rb +61 -30
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: baf2cacff4155deb6ead06ec337cd3cbade7b4ab
|
4
|
+
data.tar.gz: e1cf01705708d7ad0d526191288874b705f00066
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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
|
-
|
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
|
-
|
143
|
-
|
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
|
-
|
162
|
-
|
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)
|
data/lib/file_discard_version.rb
CHANGED
data/spec/file_discard_spec.rb
CHANGED
@@ -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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
26
|
-
FileDiscard.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
|
-
|
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(
|
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(
|
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
|
-
|
64
|
-
|
65
|
-
@trash.mkdir
|
73
|
+
def sorted_trash
|
74
|
+
trash.children(false).collect(&:to_s).sort
|
66
75
|
end
|
67
76
|
|
68
|
-
|
69
|
-
|
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 =
|
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 =
|
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(
|
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 =
|
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 =
|
158
|
+
f = trash.join('file.txt')
|
124
159
|
f.open('w') {|io| io.puts 'nothing'}
|
125
160
|
|
126
|
-
f =
|
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 =
|
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 =
|
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
|
-
|
155
|
-
|
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 =
|
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.
|
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-
|
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
|