ruby-magic 0.2.0 → 0.3.0

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.
@@ -1,35 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- # :enddoc:
4
-
5
- #
6
- # helpers.rb
7
- #
8
- # Copyright 2013-2015 Krzysztof Wilczynski
9
- #
10
- # Licensed under the Apache License, Version 2.0 (the "License");
11
- # you may not use this file except in compliance with the License.
12
- # You may obtain a copy of the License at
13
- #
14
- # http://www.apache.org/licenses/LICENSE-2.0
15
- #
16
- # Unless required by applicable law or agreed to in writing, software
17
- # distributed under the License is distributed on an "AS IS" BASIS,
18
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- # See the License for the specific language governing permissions and
20
- # limitations under the License.
21
- #
22
-
23
- require 'stringio'
24
-
25
- module MagicTestHelpers
26
- def capture_stderr
27
- saved_stderr, $stderr = $stderr, StringIO.new
28
- [yield, $stderr.string]
29
- ensure
30
- $stderr = saved_stderr
31
- end
32
- end
33
-
34
- # vim: set ts=2 sw=2 sts=2 et :
35
- # encoding: utf-8
@@ -1,96 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- # :enddoc:
4
-
5
- #
6
- # test_constants.rb
7
- #
8
- # Copyright 2013-2015 Krzysztof Wilczynski
9
- #
10
- # Licensed under the Apache License, Version 2.0 (the "License");
11
- # you may not use this file except in compliance with the License.
12
- # You may obtain a copy of the License at
13
- #
14
- # http://www.apache.org/licenses/LICENSE-2.0
15
- #
16
- # Unless required by applicable law or agreed to in writing, software
17
- # distributed under the License is distributed on an "AS IS" BASIS,
18
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- # See the License for the specific language governing permissions and
20
- # limitations under the License.
21
- #
22
-
23
- begin
24
- require 'coveralls'
25
- Coveralls.wear!
26
- rescue LoadError
27
- STDERR.puts 'The Coveralls gem is not installed, skipping ...'
28
- end
29
-
30
- gem 'test-unit', '>= 3.0.0'
31
-
32
- require 'test/unit'
33
- require 'magic'
34
-
35
- class MagicConstantsTest < Test::Unit::TestCase
36
- def setup
37
- @version = Magic.version rescue nil
38
- end
39
-
40
- def test_constants_defiend
41
- [
42
- :NONE,
43
- :DEBUG,
44
- :SYMLINK,
45
- :COMPRESS,
46
- :DEVICES,
47
- :MIME_TYPE,
48
- :CONTINUE,
49
- :CHECK,
50
- :PRESERVE_ATIME,
51
- :RAW,
52
- :ERROR,
53
- :MIME_ENCODING,
54
- :MIME,
55
- :APPLE,
56
- :NO_CHECK_COMPRESS,
57
- :NO_CHECK_TAR,
58
- :NO_CHECK_SOFT,
59
- :NO_CHECK_APPTYPE,
60
- :NO_CHECK_ELF,
61
- :NO_CHECK_TEXT,
62
- :NO_CHECK_CDF,
63
- :NO_CHECK_TOKENS,
64
- :NO_CHECK_ENCODING,
65
- :NO_CHECK_BUILTIN,
66
- :NO_CHECK_ASCII,
67
- :NO_CHECK_FORTRAN,
68
- :NO_CHECK_TROFF,
69
- ].each {|i| assert_const_defined(Magic, i) }
70
- end
71
-
72
- def test_MIME_constant
73
- assert_equal(Magic::MIME, Magic::MIME_TYPE | Magic::MIME_ENCODING)
74
- end
75
-
76
- def test_NO_CHECK_BUILTIN_constat
77
- # Any recent version of libmagic will have 0x37b000 by default.
78
- custom_NO_CHECK_BUILTIN = Magic::NO_CHECK_COMPRESS | Magic::NO_CHECK_TAR |
79
- Magic::NO_CHECK_APPTYPE | Magic::NO_CHECK_ELF | Magic::NO_CHECK_TEXT |
80
- Magic::NO_CHECK_CDF | Magic::NO_CHECK_TOKENS | Magic::NO_CHECK_ENCODING
81
-
82
- # Older versions of libmagic will have 0x3fb000 here historically ...
83
- if @version.nil? && Magic::NO_CHECK_BUILTIN != 0x37b000
84
- custom_NO_CHECK_BUILTIN ^= 0x080000 # 0x37b000 ^ 0x080000 is 0x3fb000
85
- end
86
-
87
- assert_equal(Magic::NO_CHECK_BUILTIN, custom_NO_CHECK_BUILTIN)
88
- end
89
-
90
- def check_NO_CHECK_ASCII_constant
91
- assert_equal(Magic::NO_CHECK_ASCII, Magic::NO_CHECK_TEXT)
92
- end
93
- end
94
-
95
- # vim: set ts=2 sw=2 sts=2 et :
96
- # encoding: utf-8
data/test/test_magic.rb DELETED
@@ -1,461 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- # :enddoc:
4
-
5
- #
6
- # test_magic.rb
7
- #
8
- # Copyright 2013-2015 Krzysztof Wilczynski
9
- #
10
- # Licensed under the Apache License, Version 2.0 (the "License");
11
- # you may not use this file except in compliance with the License.
12
- # You may obtain a copy of the License at
13
- #
14
- # http://www.apache.org/licenses/LICENSE-2.0
15
- #
16
- # Unless required by applicable law or agreed to in writing, software
17
- # distributed under the License is distributed on an "AS IS" BASIS,
18
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- # See the License for the specific language governing permissions and
20
- # limitations under the License.
21
- #
22
-
23
- begin
24
- require 'coveralls'
25
- Coveralls.wear!
26
- rescue LoadError
27
- STDERR.puts 'The Coveralls gem is not installed, skipping ...'
28
- end
29
-
30
- gem 'test-unit', '>= 3.0.0'
31
-
32
- require 'test/unit'
33
- require "mocha/test_unit"
34
- require 'magic'
35
-
36
- require_relative 'helpers/magic_test_helper'
37
-
38
- class MagicTest < Test::Unit::TestCase
39
- include MagicTestHelpers
40
-
41
- def setup
42
- @magic = Magic.new
43
- @version = Magic.version rescue nil
44
- end
45
-
46
- def test_magic_alias
47
- assert_same(FileMagic, Magic)
48
- end
49
-
50
- def test_magic_flags_alias
51
- assert_alias_method(@magic, :flags_array, :flags_to_a)
52
- end
53
-
54
- def test_magic_check_alias
55
- assert_alias_method(@magic, :valid?, :check)
56
- end
57
-
58
- def test_magic_version_to_a_alias
59
- assert_alias_method(Magic, :version_array, :version_to_a)
60
- end
61
-
62
- def test_magic_version_to_s_alias
63
- assert_alias_method(Magic, :version_string, :version_to_s)
64
- end
65
-
66
- def test_magic_singleton_methods
67
- [
68
- :open,
69
- :mime,
70
- :type,
71
- :encoding,
72
- :compile,
73
- :check,
74
- :version,
75
- :version_to_a,
76
- :version_to_s
77
- ].each {|i| assert_respond_to(Magic, i) }
78
- end
79
-
80
- def test_magic_new_instance
81
- assert(@magic.class == Magic)
82
- end
83
-
84
- def test_magic_new_instance_default_flags
85
- assert_equal(@magic.flags, 0)
86
- end
87
-
88
- def test_magic_new_with_block
89
- magic, output = capture_stderr do
90
- Magic.new {}
91
- end
92
-
93
- expected = "Magic::new() does not take block; use Magic::open() instead\n"
94
- assert_kind_of(Magic, magic)
95
- assert_equal(output.split(/\.rb:\d+\:\s+?|warning:\s+?/).pop, expected)
96
- end
97
-
98
- def test_magic_instance_methods
99
- [
100
- :close,
101
- :closed?,
102
- :path,
103
- :flags,
104
- :flags_array,
105
- :file,
106
- :buffer,
107
- :descriptor,
108
- :load,
109
- :compile,
110
- :check
111
- ].each {|i| assert_respond_to(@magic, i) }
112
- end
113
-
114
- def test_magic_string_integration_methods
115
- [
116
- :magic,
117
- :mime,
118
- :type
119
- ].each {|i| assert_respond_to(String.allocate, i) }
120
- end
121
-
122
- def test_magic_file_integration_singleton_methods
123
- [
124
- :magic,
125
- :mime,
126
- :type
127
- ].each {|i| assert_respond_to(File, i) }
128
- end
129
-
130
- def test_magic_file_integration_instance_methods
131
- [
132
- :magic,
133
- :mime,
134
- :type
135
- ].each {|i| assert_respond_to(File.allocate, i) }
136
- end
137
-
138
- def test_magic_close
139
- @magic.close
140
- assert_true(@magic.closed?)
141
- end
142
-
143
- def test_magic_close_twice
144
- assert_nothing_raised do
145
- @magic.close
146
- @magic.close
147
- end
148
- end
149
-
150
- def test_magic_close_error
151
- @magic.close
152
- assert_raise Magic::LibraryError do
153
- @magic.flags
154
- end
155
- end
156
-
157
- def test_magic_close_error_message
158
- @magic.close
159
- @magic.flags
160
- rescue Magic::LibraryError => error
161
- assert_equal(error.message, 'Magic library is not open')
162
- end
163
-
164
- def test_magic_closed?
165
- assert_false(@magic.closed?)
166
- @magic.close
167
- assert_true(@magic.closed?)
168
- end
169
-
170
- def test_magic_inspect_when_magic_open
171
- assert_match(%r{^#<Magic:0x.+>$}, @magic.inspect)
172
- end
173
-
174
- def test_magic_inspect_when_magic_closed
175
- @magic.close
176
- assert_match(%r{^#<Magic:0x.+ \(closed\)>$}, @magic.inspect)
177
- end
178
-
179
- def test_magic_path
180
- assert_kind_of(Array, @magic.path)
181
- assert_not_equal(@magic.path.size, 0)
182
- end
183
-
184
- def test_magic_path_with_MAGIC_environment_variable
185
- # XXX(krzysztof): How to override "MAGIC" environment
186
- # variable so that the C extension will pick it up?
187
- end
188
-
189
- def test_magic_flags_with_NONE_flag
190
- @magic.flags = 0x000000 # Flag: NONE
191
- assert_kind_of(Fixnum, @magic.flags)
192
- assert_equal(@magic.flags, Magic::NONE)
193
- end
194
-
195
- def test_magic_flags_with_MIME_TYPE_flag
196
- @magic.flags = 0x000010 # Flag: MIME_TYPE
197
- assert_kind_of(Fixnum, @magic.flags)
198
- assert_equal(@magic.flags, Magic::MIME_TYPE)
199
- end
200
-
201
- def test_magic_flags_with_MIME_ENCODING_flag
202
- @magic.flags = 0x000400 # Flag: MIME_ENCODING
203
- assert_kind_of(Fixnum, @magic.flags)
204
- assert_equal(@magic.flags, Magic::MIME_ENCODING)
205
- end
206
-
207
- def test_magic_flags_with_MIME_flag
208
- @magic.flags = 0x000410 # Flag: MIME_TYPE, MIME_ENCODING
209
- assert_kind_of(Fixnum, @magic.flags)
210
- assert_equal(@magic.flags, Magic::MIME)
211
- end
212
-
213
- def test_magic_flags_to_a_with_NONE_flag
214
- @magic.flags = 0x000000 # Flag: NONE
215
- assert_kind_of(Array, @magic.flags_to_a)
216
- assert_equal(@magic.flags_to_a, [Magic::NONE])
217
- end
218
-
219
- def test_magic_flags_to_a_with_MIME_TYPE_flag
220
- @magic.flags = 0x000010 # Flag: MIME_TYPE
221
- assert_kind_of(Array, @magic.flags_to_a)
222
- assert_equal(@magic.flags_to_a, [Magic::MIME_TYPE])
223
- end
224
-
225
- def test_magic_flags_to_a_with_MIME_ENCODING_flag
226
- @magic.flags = 0x000400 # Flag: MIME_ENCODING
227
- assert_kind_of(Array, @magic.flags_to_a)
228
- assert_equal(@magic.flags_to_a, [Magic::MIME_ENCODING])
229
- end
230
-
231
- def test_magic_flags_to_a_with_MIME_flag
232
- @magic.flags = 0x000410 # Flag: MIME_TYPE, MIME_ENCODING
233
- assert_kind_of(Array, @magic.flags_to_a)
234
- assert_equal(@magic.flags_to_a, [Magic::MIME_TYPE, Magic::MIME_ENCODING])
235
- end
236
-
237
- def test_magic_flags_to_a_with_NONE_flag_and_argument_true
238
- @magic.flags = 0x000000 # Flag: NONE
239
- assert_kind_of(Array, @magic.flags_to_a)
240
- assert_equal(@magic.flags_to_a(true), ['NONE'])
241
- end
242
-
243
- def test_magic_flags_to_a_with_MIME_TYPE_flag_and_argument_true
244
- @magic.flags = 0x000010 # Flag: MIME_TYPE
245
- assert_kind_of(Array, @magic.flags_to_a)
246
- assert_equal(@magic.flags_to_a(true), ['MIME_TYPE'])
247
- end
248
-
249
- def test_magic_flags_to_a_with_MIME_ENCODING_flag_and_argument_true
250
- @magic.flags = 0x000400 # Flag: MIME_ENCODING
251
- assert_kind_of(Array, @magic.flags_to_a)
252
- assert_equal(@magic.flags_to_a(true), ['MIME_ENCODING'])
253
- end
254
-
255
- def test_magic_flags_to_a_with_MIME_flag_and_argument_true
256
- @magic.flags = 0x000410 # Flag: MIME_TYPE, MIME_ENCODING
257
- assert_kind_of(Array, @magic.flags_to_a)
258
- assert_equal(@magic.flags_to_a(true), ['MIME_TYPE', 'MIME_ENCODING'])
259
- end
260
-
261
- def test_magic_flags_error_lower_boundary
262
- assert_raise Magic::FlagsError do
263
- @magic.flags = -0xffffff
264
- end
265
- end
266
-
267
- def test_magic_flags_error_upper_boundary
268
- assert_raise Magic::FlagsError do
269
- @magic.flags = 0xffffff
270
- end
271
- end
272
-
273
- def test_magic_flags_error_message
274
- @magic.flags = 0xffffff
275
- rescue Magic::FlagsError => error
276
- assert_equal(error.message, 'unknown or invalid flag specified')
277
- end
278
-
279
- def test_magic_file
280
- end
281
-
282
- def test_magic_file_with_ERROR_flag
283
- end
284
-
285
- def test_magic_file_with_MAGIC_CONTINUE_flag
286
- end
287
-
288
- def test_magic_buffer
289
- end
290
-
291
- def test_magic_buffer_with_MAGIC_CONTINUE_flag
292
- end
293
-
294
- def test_magic_descriptor
295
- end
296
-
297
- def test_magic_descriptor_with_MAGIC_CONTINUE_flag
298
- end
299
-
300
- def test_magic_load
301
- end
302
-
303
- def test_magic_load_with_custom_Magic_file_path
304
- end
305
-
306
- def test_magic_with_new_instance_custom_Magic_file_path
307
- end
308
-
309
- def test_magic_load_with_MAGIC_environment_variable
310
- end
311
-
312
- def test_magic_check
313
- end
314
-
315
- def test_magic_compile
316
- end
317
-
318
- def test_magic_version
319
- if @version.nil? || @version < 0
320
- Magic.stubs(:version).returns(518)
321
- end
322
-
323
- assert_kind_of(Fixnum, Magic.version)
324
- assert(Magic.version > 0)
325
- end
326
-
327
- def test_magic_version_error
328
- if @version
329
- Magic.stubs(:version).raises(Magic::NotImplementedError)
330
- end
331
-
332
- assert_raise Magic::NotImplementedError do
333
- Magic.version
334
- end
335
- end
336
-
337
- def test_magic_version_error_message
338
- if @version
339
- Magic.stubs(:version).raises(Magic::NotImplementedError, 'function is not implemented')
340
- end
341
-
342
- Magic.version
343
- rescue Magic::NotImplementedError => error
344
- assert_equal(error.message, 'function is not implemented')
345
- end
346
-
347
- def test_magic_version_to_a
348
- if @version.nil? || @version < 0
349
- Magic.stubs(:version).returns(518)
350
- end
351
-
352
- expected = [Magic.version / 100, Magic.version % 100]
353
- assert_equal(Magic.version_to_a, expected)
354
- end
355
-
356
- def test_magic_version_to_s
357
- if @version.nil? || @version < 0
358
- Magic.stubs(:version).returns(518)
359
- end
360
-
361
- expected = '%d.%02d' % Magic.version_to_a
362
- assert_equal(Magic.version_to_s, expected)
363
- end
364
-
365
- def test_magic_singleton_open
366
- end
367
-
368
- def test_magic_singleton_open_custom_flag
369
- end
370
-
371
- def test_magic_singleton_open_block
372
- end
373
-
374
- def test_magic_singleton_open_block_custom_flag
375
- end
376
-
377
- def test_magic_singleton_mime
378
- end
379
-
380
- def test_magic_singleton_mime_block
381
- end
382
-
383
- def test_magic_singleton_type
384
- end
385
-
386
- def test_magic_singleton_type_block
387
- end
388
-
389
- def test_magic_singleton_encoding
390
- end
391
-
392
- def test_magic_singleton_encoding_block
393
- end
394
-
395
- def test_magic_singleton_compile
396
- end
397
-
398
- def test_magic_singleton_check
399
- end
400
-
401
- def test_magic_magic_error
402
- message = 'The quick brown fox jumps over the lazy dog'
403
- error = Magic::Error.new(message)
404
-
405
- assert_respond_to(error, :errno)
406
- assert_equal(error.message, message)
407
- assert_nil(error.errno)
408
- end
409
-
410
- def test_magic_error_attribute_errno
411
- @magic.flags = 0xffffff # Should raise Magic::FlagsError.
412
- rescue Magic::Error => error
413
- assert_kind_of(Magic::FlagsError, error)
414
- assert_equal(error.errno, Errno::EINVAL::Errno)
415
- end
416
-
417
- def test_magic_new_instance_with_arguments
418
- end
419
-
420
- def test_file_integration_magic
421
- end
422
-
423
- def test_file_integration_magic_with_custom_flag
424
- end
425
-
426
- def test_file_integration_mime
427
- end
428
-
429
- def test_file_integration_type
430
- end
431
-
432
- def test_file_integration_singleton_magic
433
- end
434
-
435
- def test_file_integration_singleton_magic_with_custom_flag
436
- end
437
-
438
- def test_file_integration_singleton_mime
439
- end
440
-
441
- def test_file_integration_singleton_type
442
- end
443
-
444
- def test_string_integration_magic
445
- end
446
-
447
- def test_string_integration_magic_with_custom_flag
448
- end
449
-
450
- def test_string_integration_mime
451
- end
452
-
453
- def test_string_integration_type
454
- end
455
-
456
- def test_magic_mutex_unlocked
457
- end
458
- end
459
-
460
- # vim: set ts=2 sw=2 sts=2 et :
461
- # encoding: utf-8