Skip to content

Commit 6b7d9f6

Browse files
committed
Added RSpec and tests. Added basic dearchiver code.
1 parent 45d745e commit 6b7d9f6

10 files changed

Lines changed: 163 additions & 2 deletions

File tree

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format=nested

Gemfile.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,27 @@ PATH
66
GEM
77
remote: https://rubygems.org/
88
specs:
9+
diff-lcs (1.1.3)
10+
multi_json (1.3.6)
11+
rake (0.9.2.2)
12+
rspec (2.11.0)
13+
rspec-core (~> 2.11.0)
14+
rspec-expectations (~> 2.11.0)
15+
rspec-mocks (~> 2.11.0)
16+
rspec-core (2.11.1)
17+
rspec-expectations (2.11.3)
18+
diff-lcs (~> 1.1.3)
19+
rspec-mocks (2.11.3)
20+
simplecov (0.6.4)
21+
multi_json (~> 1.0)
22+
simplecov-html (~> 0.5.3)
23+
simplecov-html (0.5.3)
924

1025
PLATFORMS
1126
ruby
1227

1328
DEPENDENCIES
1429
dearchiver!
30+
rake
31+
rspec (~> 2.6)
32+
simplecov (~> 0.5)

Rakefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
#!/usr/bin/env rake
2-
require "bundler/gem_tasks"
2+
require 'bundler'
3+
require 'bundler/gem_tasks'
4+
require 'rspec/core/rake_task'
5+
6+
RSpec::Core::RakeTask.new(:test) do |t|
7+
t.pattern = 'spec/*_spec.rb'
8+
t.verbose = false
9+
end
10+
11+
task :default => :test

dearchiver.gemspec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Gem::Specification.new do |gem|
88
gem.summary = %q{A simple Ruby Gem to decompress and check the CRC of compressed files.}
99
gem.homepage = ""
1010

11+
gem.add_development_dependency 'rake'
12+
gem.add_development_dependency 'rspec', '~> 2.6'
13+
gem.add_development_dependency 'simplecov', '~> 0.5'
14+
1115
gem.files = `git ls-files`.split($\)
1216
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
1317
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})

lib/dearchiver.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
require "dearchiver/processor"
12
require "dearchiver/version"
3+
require "dearchiver/configuration"
24

35
module Dearchiver
4-
# Your code goes here...
6+
7+
extend Dearchiver::Configuration
8+
9+
class << self
10+
# Shorthand to Dearchiver::Processor.new
11+
#
12+
# @return [Dearchiver::Processor]
13+
#
14+
def new(options={})
15+
Dearchiver::Processor.new(options)
16+
end
17+
end
518
end

lib/dearchiver/configuration.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Dearchiver
2+
module Configuration
3+
@@logger = false
4+
@@fetch_async_responses = true
5+
6+
# Set logger mode
7+
#
8+
# value - Enable/disable logger
9+
#
10+
def log_requests= (value)
11+
@@logger = value == true
12+
end
13+
14+
# Return current logger state
15+
#
16+
def log_requests
17+
@@logger
18+
end
19+
end
20+
end

lib/dearchiver/processor.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module Dearchiver
2+
class Processor
3+
4+
attr_reader :filename
5+
attr_reader :archive_type
6+
7+
# Initialize a new CloudDns::Client object
8+
#
9+
# options - Set of configuration options
10+
#
11+
# options[:filename] - The complete filename (with included path) to work with.
12+
# options[:archive_type] - Optional. See archive_options for recognized file types.
13+
#
14+
def initialize(options = {})
15+
@filename = options[:filename]
16+
raise ArgumentError, "Processor: :filename required!" unless options.has_key?(:filename) and not options[:filename].empty?
17+
18+
@archive_type = File.extname(@filename) if valid_file_type?
19+
@archive_type ||= options[:archive_type]
20+
raise ArgumentError, "Processor: :archive_type required. :filename does not contain a recognizable extension!" if @archive_type.nil? or @archive_type.empty?
21+
end
22+
23+
def crc_ok?
24+
type = File.extname(@filename)
25+
command = archive_options[type][:crc_check].gsub("<filename>", filename)
26+
crc_check = %x[#{command}]
27+
crc_check.include?(archive_options[type][:crc_ok]) ? true : false
28+
end
29+
30+
private
31+
32+
def archive_options
33+
{
34+
".zip" => {
35+
:crc_check => "unzip -t <filename>",
36+
:crc_ok => "No errors detected in compressed data"
37+
},
38+
".rar" => {
39+
:crc_check => "unrar t <filename>",
40+
:crc_ok => "All OK"
41+
},
42+
".7z" => {
43+
:crc_check => "7z t <filename>",
44+
:crc_ok => "Everything is Ok"
45+
},
46+
".tar.gz" => {
47+
:crc_check => "tar -tvzf <filename> > /dev/null",
48+
:crc_ok => ""
49+
}
50+
}
51+
end
52+
53+
def valid_file_type?
54+
archive_options.has_key?(File.extname(@filename))
55+
end
56+
57+
end
58+
end

spec/dearchiver_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require "spec_helper"
2+
3+
describe "Dearchiver::Processor" do
4+
it "should create an instance of Dearchiver via .new" do
5+
Dearchiver.respond_to?(:new).should == true
6+
Dearchiver.new(:filename => 'foo.zip').should be_a Dearchiver::Processor
7+
end
8+
9+
it "should create an instance if all the argmuments are correct" do
10+
Dearchiver.new(:filename => 'foo.bar', :archive_type => ".zip").should be_a Dearchiver::Processor
11+
end
12+
13+
it "should raise an error if no filename is passed as an argument" do
14+
expect { Dearchiver.new }.to raise_error(ArgumentError)
15+
end
16+
17+
it "should raise an error if a file type is not recognized and type not passed" do
18+
expect { Dearchiver.new(:filename => "foo.bar") }.to raise_error(ArgumentError)
19+
end
20+
21+
end

spec/processor_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require "rspec"
2+
3+
describe "Dearchiver::Processor" do
4+
5+
it "should verify for crc" do
6+
true.should == false
7+
end
8+
end

spec/spec_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
$:.unshift File.expand_path("../..", __FILE__)
2+
3+
require 'dearchiver'
4+
require 'simplecov'
5+
6+
SimpleCov.start do
7+
add_group 'DeArchiver', 'lib/dearchiver'
8+
end

0 commit comments

Comments
 (0)