checksummer 0.2.4 → 0.2.5

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
1
+ 0.2.5
data/checksummer.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{checksummer}
8
- s.version = "0.2.4"
8
+ s.version = "0.2.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tobias Schwab"]
@@ -1,6 +1,7 @@
1
1
  require "time"
2
2
  require "fileutils"
3
3
  require "pathname"
4
+ require "digest/md5"
4
5
 
5
6
  class ChecksummerFile
6
7
  attr_accessor :modified_at, :size, :type, :path, :original_path, :original_line
@@ -17,17 +18,7 @@ class ChecksummerFile
17
18
  end
18
19
 
19
20
  def md5
20
- @md5 ||= md5_module.file(path).to_s
21
- end
22
-
23
- def md5_module
24
- if RUBY_VERSION.match(/^1\.9/)
25
- require "digest/md5"
26
- Digest::MD5
27
- else
28
- require "md5"
29
- MD5
30
- end
21
+ @md5 ||= Digest::MD5.file(path).to_s
31
22
  end
32
23
 
33
24
  def self.from_line(line)
@@ -63,11 +54,16 @@ class ChecksummerFile
63
54
  FileUtils.touch(path, :mtime => mtime)
64
55
  else
65
56
  status[:status] = :was_symlink
66
- status.merge!(:status => :was_symlink, :realpath => Pathname.new(path).realpath)
57
+ status[:realpath] = Pathname.new(path).realpath
58
+ status[:checksum] = File.basename(status[:realpath]) if valid_checksum?(File.basename(status[:realpath]))
67
59
  end
68
60
  status
69
61
  end
70
62
 
63
+ def valid_checksum?(checksum)
64
+ !checksum.to_s.match(/^[0-9a-f]{32}$/).nil?
65
+ end
66
+
71
67
  def md5_path(checksum_dir)
72
68
  "#{checksum_dir}/#{md5.split("")[0,4].join("/")}/#{md5}"
73
69
  end
@@ -162,9 +162,11 @@ describe ChecksummerFile do
162
162
  end
163
163
 
164
164
  describe "with the file being a symlink" do
165
+ let(:pathname) { double("rp", :realpath => "/some/real/path") }
165
166
  before(:each) do
166
167
  File.stub(:symlink?).with("/some/text.csv").and_return true
167
168
  File.stub!(:realpath).and_return "/some/real/path"
169
+ Pathname.stub!(:new).with("/some/text.csv").and_return pathname
168
170
  end
169
171
 
170
172
  it "does not copy the file when already exists" do
@@ -182,15 +184,12 @@ describe ChecksummerFile do
182
184
  file.checksum_to!("/tmp/data")
183
185
  end
184
186
 
185
- it "calls File.realpath with path" do
186
- rp = double("rp", :realpath => "/real_path")
187
- Pathname.stub!(:new).with("/some/text.csv").and_return rp
187
+ it "calls Pathname#realpath with path" do
188
+ pathname.should_receive(:realpath).and_return "/real_path"
188
189
  file.checksum_to!("/tmp/data")
189
190
  end
190
191
 
191
192
  it "sets the realpath" do
192
- rp = double("rp", :realpath => "/some/real/path")
193
- Pathname.stub!(:new).with("/some/text.csv").and_return rp
194
193
  file.checksum_to!("/tmp/data")[:realpath].should == "/some/real/path"
195
194
  end
196
195
 
@@ -202,6 +201,18 @@ describe ChecksummerFile do
202
201
  FileUtils.should_not_receive(:touch)
203
202
  file.checksum_to!("/tmp/data")
204
203
  end
204
+
205
+ it "sets the checksum when pointing to a checksummed file" do
206
+ pathname.stub!(:realpath).and_return "/tmp/data/fde8dad8ea43640b00cdd1e92e532ca9"
207
+ file.should_receive(:valid_checksum?).with("fde8dad8ea43640b00cdd1e92e532ca9").and_return true
208
+ file.checksum_to!("/tmp/data")[:checksum].should == "fde8dad8ea43640b00cdd1e92e532ca9"
209
+ end
210
+
211
+ it "does not set the checksum when not pointing to a checksummed path" do
212
+ pathname.stub!(:realpath).and_return "/data/fde8dad8ea43640b00cdd1e92e532ca"
213
+ file.should_receive(:valid_checksum?).with("fde8dad8ea43640b00cdd1e92e532ca").and_return false
214
+ file.checksum_to!("/tmp/data").should_not have_key(:checksum)
215
+ end
205
216
  end
206
217
 
207
218
  describe "with the file not existing" do
@@ -211,23 +222,33 @@ describe ChecksummerFile do
211
222
  end
212
223
  end
213
224
 
225
+ describe "valid_checksum?" do
226
+ {
227
+ "fde8dad8ea43640b00cdd1e92e532ca9" => true, "fde8dad8ea43640b00cdd1e92e532ca" => false,
228
+ "fde8dad8ea43640b00cdd1e92e532cax" => false
229
+ }.each do |checksum, result|
230
+ it "returns #{result} for #{checksum}" do
231
+ ChecksummerFile.new.valid_checksum?(checksum).should == result
232
+ end
233
+ end
234
+ end
235
+
214
236
  describe "#md5" do
215
237
  let(:md5_module) { double("md5 module", :file => "sometest") }
216
238
  let(:file) do
217
239
  file = ChecksummerFile.new(:path => "/some/path")
218
- file.stub!(:md5_module).and_return md5_module
219
240
  file
220
241
  end
221
242
 
222
243
 
223
244
  it "calls md5_module with correct file_path when no md5 set" do
224
- md5_module.should_receive(:file).with("/some/path")
245
+ Digest::MD5.should_receive(:file).with("/some/path")
225
246
  file.md5
226
247
  end
227
248
 
228
249
  it "saves the md5 in instance variable" do
229
250
  checksum = double("checksum", :to_s => "the checksum")
230
- md5_module.stub(:file).and_return checksum
251
+ Digest::MD5.stub(:file).and_return checksum
231
252
  file.md5
232
253
  file.instance_variable_get(:@md5).should == "the checksum"
233
254
  end
@@ -239,12 +260,6 @@ describe ChecksummerFile do
239
260
  end
240
261
  end
241
262
 
242
- describe "#md5_module" do
243
- it "returns Digest::MD5 for Ruby 1.9" do
244
- ChecksummerFile.new.md5_module.should == Digest::MD5
245
- end
246
- end
247
-
248
263
  describe "#path=" do
249
264
  it "sets the correct path" do
250
265
  file = ChecksummerFile.new
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: checksummer
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.4
5
+ version: 0.2.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Tobias Schwab
@@ -165,7 +165,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
165
165
  requirements:
166
166
  - - ">="
167
167
  - !ruby/object:Gem::Version
168
- hash: 2964293804353679217
168
+ hash: -2535468748837297976
169
169
  segments:
170
170
  - 0
171
171
  version: "0"