vtools 0.0.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.
- data/INSTALL +0 -0
- data/LICENSE +20 -0
- data/README.md +131 -0
- data/Rakefile +29 -0
- data/bin/vtools +22 -0
- data/doc/CONFIG.md +36 -0
- data/doc/HOOKS.md +37 -0
- data/doc/LIB_EXAMPLE.md +109 -0
- data/extconf.rb +7 -0
- data/lib/vtools.rb +79 -0
- data/lib/vtools/config.rb +91 -0
- data/lib/vtools/convert_options.rb +155 -0
- data/lib/vtools/converter.rb +98 -0
- data/lib/vtools/errors.rb +21 -0
- data/lib/vtools/handler.rb +43 -0
- data/lib/vtools/harvester.rb +71 -0
- data/lib/vtools/job.rb +48 -0
- data/lib/vtools/options.rb +101 -0
- data/lib/vtools/shared_methods.rb +131 -0
- data/lib/vtools/storage.rb +67 -0
- data/lib/vtools/thumbnailer.rb +93 -0
- data/lib/vtools/thumbs_options.rb +80 -0
- data/lib/vtools/version.rb +6 -0
- data/lib/vtools/version.rb~ +4 -0
- data/lib/vtools/video.rb +158 -0
- data/setup.rb +1585 -0
- data/spec/config_spec.rb +142 -0
- data/spec/convert_options_spec.rb +284 -0
- data/spec/converter_spec.rb +167 -0
- data/spec/errors_spec.rb +39 -0
- data/spec/fixtures/outputs/file_with_iso-8859-1.txt +35 -0
- data/spec/fixtures/outputs/file_with_no_audio.txt +18 -0
- data/spec/fixtures/outputs/file_with_non_supported_audio.txt +29 -0
- data/spec/fixtures/outputs/file_with_start_value.txt +19 -0
- data/spec/fixtures/outputs/file_with_surround_sound.txt +19 -0
- data/spec/handler_spec.rb +81 -0
- data/spec/harvester_spec.rb +189 -0
- data/spec/job_spec.rb +130 -0
- data/spec/options_spec.rb +52 -0
- data/spec/shared_methods_spec.rb +351 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/storage_spec.rb +106 -0
- data/spec/thumbnailer_spec.rb +178 -0
- data/spec/thumbs_options_spec.rb +159 -0
- data/spec/video_spec.rb +274 -0
- data/vtools.gemspec +29 -0
- metadata +177 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "options"
|
3
|
+
|
4
|
+
describe VTools::Options do
|
5
|
+
|
6
|
+
# hooks
|
7
|
+
before :all do
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
# around to test inside the methods
|
12
|
+
|
13
|
+
before do
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
after do
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
after :all do
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
# specs
|
26
|
+
context "#parse!" do
|
27
|
+
|
28
|
+
it "creates valid options" do
|
29
|
+
opts = ["server_commnad", "-option_one", "--", "-app_option", "-app_option_two",]
|
30
|
+
opts_v = opts.dup << "-v"
|
31
|
+
opts_h = opts_v.dup << "-h"
|
32
|
+
|
33
|
+
@test = nil
|
34
|
+
OptionParser.stub!(:new).and_return nil
|
35
|
+
|
36
|
+
VTools::Options.stub!(:argv=).and_return do |arr|
|
37
|
+
arr.should_not include ["server_commnad", "-option_one", "--"]
|
38
|
+
arr.should include ["-app_option", "-app_option_two"]
|
39
|
+
end
|
40
|
+
|
41
|
+
VTools::Options.parse! opts
|
42
|
+
|
43
|
+
VTools::Options.stub!(:argv=).and_return { |arr| arr.should == ["-v"] }
|
44
|
+
VTools::Options.parse! opts_v
|
45
|
+
|
46
|
+
VTools::Options.stub!(:argv=).and_return { |arr| arr.should == ["-h"] }
|
47
|
+
VTools::Options.parse! opts_h
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,351 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "shared_methods"
|
3
|
+
|
4
|
+
describe VTools::SharedMethods do
|
5
|
+
|
6
|
+
class Tested
|
7
|
+
end
|
8
|
+
|
9
|
+
# hooks
|
10
|
+
before do
|
11
|
+
@class = Tested.new
|
12
|
+
end
|
13
|
+
|
14
|
+
# specs
|
15
|
+
context "::Common" do
|
16
|
+
|
17
|
+
class Tested
|
18
|
+
include VTools::SharedMethods::Common
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
@@logger = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_logger
|
25
|
+
@@logger
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# hooks
|
30
|
+
before do
|
31
|
+
@class = Tested.new
|
32
|
+
end
|
33
|
+
|
34
|
+
context "#logger=" do
|
35
|
+
|
36
|
+
it "appends logger" do
|
37
|
+
@class.logger = "test.logger"
|
38
|
+
@class.test_logger.should == "test.logger"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "#log" do
|
43
|
+
|
44
|
+
let(:tester) { double(nil) }
|
45
|
+
|
46
|
+
def stub_methods
|
47
|
+
tester.stub(:level=).and_return nil
|
48
|
+
Logger.stub!(:new).and_return { tester }
|
49
|
+
end
|
50
|
+
|
51
|
+
before do
|
52
|
+
VTools::CONFIG[:logging] = nil
|
53
|
+
end
|
54
|
+
|
55
|
+
it "skips log (logging disable)" do
|
56
|
+
tester.stub(:send).and_return nil
|
57
|
+
|
58
|
+
@class.logger = tester
|
59
|
+
tester.should_not_receive :send
|
60
|
+
|
61
|
+
@class.log :test, "test"
|
62
|
+
end
|
63
|
+
|
64
|
+
it "creates logger and send message" do
|
65
|
+
VTools::CONFIG[:logging] = true
|
66
|
+
|
67
|
+
stub_methods
|
68
|
+
Logger::INFO = "test.level"
|
69
|
+
|
70
|
+
Logger.should_receive(:new).with(STDOUT, 1000, 1024000).once
|
71
|
+
tester.should_receive(:level=).with("test.level").once
|
72
|
+
tester.should_receive(:send).with(:test_level, "test.log").once
|
73
|
+
|
74
|
+
@class.log :test_level, "test.log"
|
75
|
+
end
|
76
|
+
|
77
|
+
it "creates valid log message to existing logger" do
|
78
|
+
VTools::CONFIG[:logging] = true
|
79
|
+
|
80
|
+
stub_methods
|
81
|
+
|
82
|
+
Logger.should_not_receive(:new)
|
83
|
+
tester.should_not_receive(:level=)
|
84
|
+
tester.should_receive(:send).with(:test_level, "test.log").once
|
85
|
+
|
86
|
+
@class.logger = tester
|
87
|
+
@class.log :test_level, "test.log"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "#json_to_obj" do
|
92
|
+
|
93
|
+
it "calls valid chain" do
|
94
|
+
@class.stub(:parse_json).and_return { |jstr| "parsed.#{jstr}" }
|
95
|
+
@class.stub(:hash_to_obj).and_return { |js| "hashed.#{js}" }
|
96
|
+
|
97
|
+
@class.json_to_obj("JSON").should == "hashed.parsed.JSON"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context "#hash_to_obj" do
|
102
|
+
|
103
|
+
it "calls exception" do
|
104
|
+
OpenStruct.stub!(:new).and_return { raise "test exception" }
|
105
|
+
expect { @class.hash_to_obj "test" }.to raise_error VTools::ConfigError, "Can't convert setup to object"
|
106
|
+
end
|
107
|
+
it "accepts hash" do
|
108
|
+
OpenStruct.stub!(:new).and_return "hash accepted"
|
109
|
+
expect { @class.hash_to_obj(:test_key => "test.value").should == "hash accepted" }.to_not raise_error
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context "#parse_json" do
|
114
|
+
|
115
|
+
it "raises exception on invalid json" do
|
116
|
+
JSON.stub!(:parse).and_return { raise "test.execption" }
|
117
|
+
expect { @class.parse_json("invalid json") }.to raise_error VTools::ConfigError, "Invalid JSON"
|
118
|
+
end
|
119
|
+
|
120
|
+
it "parses json" do
|
121
|
+
JSON.stub!(:parse).and_return "JSON.parsed"
|
122
|
+
expect { @class.parse_json("valid json").should == "JSON.parsed" }.to_not raise_error
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "#keys_to_sym" do
|
127
|
+
|
128
|
+
it "fails execution on non Hash" do
|
129
|
+
@class.keys_to_sym("non Hash").should == "non Hash"
|
130
|
+
end
|
131
|
+
|
132
|
+
it "converts string keys to symbols" do
|
133
|
+
@class.keys_to_sym(:key => "one", "other_key" => "two").should == { :key => "one", :other_key => "two" }
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
context "#config" do
|
138
|
+
|
139
|
+
it "returns config index" do
|
140
|
+
VTools::CONFIG[:test_index] = "test.value"
|
141
|
+
|
142
|
+
@class.config(:test_index).should == "test.value"
|
143
|
+
@class.config(:nonexistent_index).should be nil
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context "#network_call" do
|
148
|
+
|
149
|
+
let(:sock) { double nil }
|
150
|
+
let(:tcp_request) { "GET / HTTP/1.0\r\n\r\n" }
|
151
|
+
|
152
|
+
def validate_request body
|
153
|
+
sock.should_receive(:print).with( tcp_request ).once
|
154
|
+
sock.should_receive(:read).once.and_return "headers\r\n\r\n#{body}"
|
155
|
+
sock.should_receive(:close).once
|
156
|
+
end
|
157
|
+
|
158
|
+
it "creates tcp request with default settings" do
|
159
|
+
|
160
|
+
TCPSocket.should_receive(:open).with("localhost", 80).once.and_return {sock}
|
161
|
+
validate_request "body"
|
162
|
+
expect do
|
163
|
+
@class.network_call("tcp://localhost").should == "body"
|
164
|
+
end.to_not raise_error
|
165
|
+
end
|
166
|
+
|
167
|
+
it "creates tcp request" do
|
168
|
+
tcp_request["/"] = "/test/address"
|
169
|
+
|
170
|
+
TCPSocket.should_receive(:open).with("server.com", 8080).once.and_return {sock}
|
171
|
+
validate_request "test body"
|
172
|
+
expect do
|
173
|
+
@class.network_call("http://server.com:8080/test/address").should == "test body"
|
174
|
+
end.to_not raise_error
|
175
|
+
end
|
176
|
+
|
177
|
+
it "fails tcp request" do
|
178
|
+
|
179
|
+
TCPSocket.should_receive(:open).once.and_return { raise "test exception" }
|
180
|
+
@class.should_receive(:log).once.and_return {|l| l.should == :error}
|
181
|
+
expect { @class.network_call("") }.to_not raise_error
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context "#generate_path" do
|
186
|
+
|
187
|
+
it "returns valid string" do
|
188
|
+
VTools::CONFIG[:video_storage] = "test/path/"
|
189
|
+
VTools::CONFIG[:thumb_storage] = "/thumb/path"
|
190
|
+
|
191
|
+
@class.generate_path("test.filename").should == "test/path"
|
192
|
+
@class.generate_path("test.filename", "thumb").should == "/thumb/path"
|
193
|
+
|
194
|
+
VTools::CONFIG[:thumb_storage] = ''
|
195
|
+
|
196
|
+
VTools::CONFIG[:PWD] = "test/pwd"
|
197
|
+
@class.generate_path("test.filename", "thumb").should == "test/pwd"
|
198
|
+
|
199
|
+
VTools::CONFIG[:PWD] = nil
|
200
|
+
VTools::CONFIG[:thumb_storage] = nil
|
201
|
+
|
202
|
+
@class.generate_path("test.filename", "thumb").should == ""
|
203
|
+
end
|
204
|
+
|
205
|
+
it "executes block" do
|
206
|
+
prc = proc do |file|
|
207
|
+
file.should == "test.filename"
|
208
|
+
self.class.should == Tested
|
209
|
+
"test/path"
|
210
|
+
end
|
211
|
+
|
212
|
+
VTools::CONFIG[:video_storage] = prc
|
213
|
+
@class.generate_path("test.filename", "video").should == "test/path"
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
context "#path_generator" do
|
218
|
+
|
219
|
+
before do
|
220
|
+
VTools::CONFIG[:video_storage] = nil
|
221
|
+
VTools::CONFIG[:thumb_storage] = nil
|
222
|
+
end
|
223
|
+
|
224
|
+
let(:block) { proc { nil } }
|
225
|
+
|
226
|
+
it "appends generator to thumbs" do
|
227
|
+
@class.path_generator "thumb", &block
|
228
|
+
VTools::CONFIG[:thumb_storage].should == block
|
229
|
+
end
|
230
|
+
|
231
|
+
it "appends generator to thumbs (invalid placeholedr given)" do
|
232
|
+
@class.path_generator "invalid", &block
|
233
|
+
VTools::CONFIG[:thumb_storage].should == block
|
234
|
+
VTools::CONFIG[:video_storage].should be nil
|
235
|
+
end
|
236
|
+
|
237
|
+
it "appends generator to video" do
|
238
|
+
@class.path_generator "video", &block
|
239
|
+
VTools::CONFIG[:video_storage].should == block
|
240
|
+
VTools::CONFIG[:thumb_storage].should be nil
|
241
|
+
end
|
242
|
+
|
243
|
+
it "appends generator to both (default)" do
|
244
|
+
@class.path_generator &block
|
245
|
+
VTools::CONFIG[:video_storage].should == block
|
246
|
+
VTools::CONFIG[:thumb_storage].should == block
|
247
|
+
end
|
248
|
+
|
249
|
+
it "skips generator appending" do
|
250
|
+
@class.path_generator
|
251
|
+
VTools::CONFIG[:video_storage].should be nil
|
252
|
+
VTools::CONFIG[:thumb_storage].should be nil
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
context "#fix_encoding" do
|
257
|
+
|
258
|
+
it "fixes encoding" do
|
259
|
+
path = "#{File.realpath(File.dirname(__FILE__))}/fixtures/outputs/"
|
260
|
+
source = File.open("#{path}file_with_iso-8859-1.txt", "r").read
|
261
|
+
@class.fix_encoding(source).encoding.to_s.should match(/iso-8859-1/i)
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end # ::Common
|
265
|
+
|
266
|
+
context "::Static" do
|
267
|
+
|
268
|
+
class Tested
|
269
|
+
include VTools::SharedMethods::Static
|
270
|
+
end
|
271
|
+
|
272
|
+
# hooks
|
273
|
+
before do
|
274
|
+
@class = Tested.new
|
275
|
+
end
|
276
|
+
|
277
|
+
context "#load_libs" do
|
278
|
+
|
279
|
+
# CONFIG[:library]
|
280
|
+
# require lib
|
281
|
+
# rescue LoadError => e
|
282
|
+
# print "Library file could not be found (#{lib})\n"
|
283
|
+
# rescue SyntaxError => e
|
284
|
+
# print "Library may contain non ascii characters (#{lib}).\n\n" +
|
285
|
+
# "Try to set file encoding to 'binary'.\n\n"
|
286
|
+
# end
|
287
|
+
before do
|
288
|
+
VTools::CONFIG[:library] = []
|
289
|
+
end
|
290
|
+
|
291
|
+
it "requires libraries" do
|
292
|
+
libs = ["one", "two", "three"]
|
293
|
+
VTools::CONFIG[:library] = libs
|
294
|
+
|
295
|
+
libs.each do |lib|
|
296
|
+
@class.should_receive(:require).with(lib)
|
297
|
+
end
|
298
|
+
|
299
|
+
@class.load_libs
|
300
|
+
end
|
301
|
+
|
302
|
+
it "raises not found error" do
|
303
|
+
VTools::CONFIG[:library] << "one"
|
304
|
+
|
305
|
+
@class.stub(:require).and_return { raise LoadError }
|
306
|
+
@class.should_receive(:print).with("Library file could not be found (one)\n")
|
307
|
+
@class.load_libs
|
308
|
+
end
|
309
|
+
|
310
|
+
it "raises syntax error" do
|
311
|
+
VTools::CONFIG[:library] << "one"
|
312
|
+
|
313
|
+
@class.stub(:require).and_return { raise SyntaxError }
|
314
|
+
@class.should_receive(:print).with("Library may contain non ascii characters (one).\n\nTry to set file encoding to 'binary'.\n\n")
|
315
|
+
@class.load_libs
|
316
|
+
end
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
context "::Instance" do
|
321
|
+
end
|
322
|
+
|
323
|
+
context "#included" do
|
324
|
+
it "appends methods correctly" do
|
325
|
+
class Tested
|
326
|
+
include VTools::SharedMethods
|
327
|
+
end
|
328
|
+
|
329
|
+
class CommonTest
|
330
|
+
include VTools::SharedMethods::Common
|
331
|
+
end
|
332
|
+
|
333
|
+
class StaticTest
|
334
|
+
include VTools::SharedMethods::Static
|
335
|
+
end
|
336
|
+
|
337
|
+
class InstanceTest
|
338
|
+
include VTools::SharedMethods::Instance
|
339
|
+
end
|
340
|
+
|
341
|
+
(CommonTest.methods + StaticTest.methods - Object.methods).each do |m|
|
342
|
+
Tested.should respond_to m
|
343
|
+
end
|
344
|
+
|
345
|
+
@tested = Tested.new
|
346
|
+
(CommonTest.methods + InstanceTest.methods - Object.methods).each do |m|
|
347
|
+
@tested.should respond_to m
|
348
|
+
end
|
349
|
+
end
|
350
|
+
end
|
351
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: binary -*-
|
2
|
+
|
3
|
+
["..", "../../lib", "../../lib/vtools"].each do |path|
|
4
|
+
dir = File.expand_path(path, __FILE__)
|
5
|
+
$:.unshift dir if dir and not $:.include?(dir)
|
6
|
+
end
|
7
|
+
|
8
|
+
require "vtools"
|
9
|
+
require "rspec"
|
10
|
+
require "stringio"
|
11
|
+
|
12
|
+
# helpers utilities
|
13
|
+
#
|
14
|
+
module Helpers
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
RSpec.configure do |c|
|
19
|
+
c.include Helpers
|
20
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "storage"
|
3
|
+
|
4
|
+
describe VTools::Storage do
|
5
|
+
|
6
|
+
# hooks
|
7
|
+
before do
|
8
|
+
VTools::Storage.instance_variable_set(:@actions, {})
|
9
|
+
end
|
10
|
+
|
11
|
+
# specs
|
12
|
+
let(:block) { proc {} }
|
13
|
+
|
14
|
+
def valid_call method
|
15
|
+
VTools::Storage.instance_variable_set( :@actions, { method.to_sym => block } )
|
16
|
+
block.should_receive(:call)
|
17
|
+
expect { VTools::Storage.method(method.to_sym).call }.to_not raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
def validate_exception method
|
21
|
+
VTools::Storage.should_receive(:fails).with(method.to_sym).and_return { raise "test.error" }
|
22
|
+
expect { VTools::Storage.method(method.to_sym).call }.to raise_error
|
23
|
+
end
|
24
|
+
|
25
|
+
context "#connect" do
|
26
|
+
|
27
|
+
it "calls connect" do
|
28
|
+
valid_call "connect"
|
29
|
+
end
|
30
|
+
|
31
|
+
it "raises exception" do
|
32
|
+
validate_exception "connect"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "#recv" do
|
37
|
+
|
38
|
+
it "calls recv" do
|
39
|
+
valid_call "recv"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "raises exception" do
|
43
|
+
validate_exception "recv"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "#send" do
|
48
|
+
|
49
|
+
it "calls send" do
|
50
|
+
VTools::Storage.instance_variable_set( :@actions, :send => block )
|
51
|
+
block.should_receive(:call).with("test.data")
|
52
|
+
expect { VTools::Storage.send("test.data") }.to_not raise_error
|
53
|
+
end
|
54
|
+
|
55
|
+
it "raises exception" do
|
56
|
+
VTools::Storage.should_receive(:fails).with(:send).and_return { raise "test.error" }
|
57
|
+
expect { VTools::Storage.send("data") }.to raise_error("test.error")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def valid_connector scope
|
62
|
+
VTools::Storage.method(scope.to_sym).call &block
|
63
|
+
scope["_action"] = ""
|
64
|
+
VTools::Storage.instance_variable_get(:@actions).should == { scope.to_sym => block }
|
65
|
+
end
|
66
|
+
|
67
|
+
context "#connect_action" do
|
68
|
+
|
69
|
+
it "connects proc" do
|
70
|
+
valid_connector "connect_action"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "#recv_action" do
|
75
|
+
|
76
|
+
it "connects proc" do
|
77
|
+
valid_connector "recv_action"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context "#send_action" do
|
82
|
+
|
83
|
+
it "connects proc" do
|
84
|
+
valid_connector "send_action"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "#setup" do
|
89
|
+
|
90
|
+
it "executes valid scope" do
|
91
|
+
VTools::Storage.should_receive(:instance_eval).once
|
92
|
+
|
93
|
+
VTools::Storage.setup do
|
94
|
+
self.should be_kind_of(VTools::Storage)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context "#fails" do
|
100
|
+
it "raises error with valid message" do
|
101
|
+
expect do
|
102
|
+
VTools::Storage.method(:fails).call("test_method")
|
103
|
+
end.to raise_error NotImplementedError, "VTools::Storage#test_method_action must be set"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|