ruby-magic 0.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@
5
5
  #
6
6
  # test_constants.rb
7
7
  #
8
- # Copyright 2013-2014 Krzysztof Wilczynski
8
+ # Copyright 2013-2015 Krzysztof Wilczynski
9
9
  #
10
10
  # Licensed under the Apache License, Version 2.0 (the "License");
11
11
  # you may not use this file except in compliance with the License.
@@ -27,19 +27,68 @@ rescue LoadError
27
27
  STDERR.puts 'The Coveralls gem is not installed, skipping ...'
28
28
  end
29
29
 
30
- gem 'test-unit', '>= 2.5.2'
30
+ gem 'test-unit', '>= 3.0.0'
31
31
 
32
32
  require 'test/unit'
33
33
  require 'magic'
34
34
 
35
35
  class MagicConstantsTest < Test::Unit::TestCase
36
36
  def setup
37
+ @version = Magic.version rescue nil
37
38
  end
38
39
 
39
- def teardown
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) }
40
70
  end
41
71
 
42
- def test_constants
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)
43
92
  end
44
93
  end
45
94
 
@@ -5,7 +5,7 @@
5
5
  #
6
6
  # test_magic.rb
7
7
  #
8
- # Copyright 2013-2014 Krzysztof Wilczynski
8
+ # Copyright 2013-2015 Krzysztof Wilczynski
9
9
  #
10
10
  # Licensed under the Apache License, Version 2.0 (the "License");
11
11
  # you may not use this file except in compliance with the License.
@@ -27,108 +27,261 @@ rescue LoadError
27
27
  STDERR.puts 'The Coveralls gem is not installed, skipping ...'
28
28
  end
29
29
 
30
- gem 'test-unit', '>= 2.5.2'
30
+ gem 'test-unit', '>= 3.0.0'
31
31
 
32
32
  require 'test/unit'
33
+ require "mocha/test_unit"
33
34
  require 'magic'
34
35
 
35
- DEFAULT_SINGLETON_METHODS = [
36
- :open,
37
- :mime,
38
- :type,
39
- :encoding,
40
- :compile,
41
- :check,
42
- :version,
43
- :version_to_a,
44
- :version_to_s
45
- ]
46
-
47
- DEFAULT_INSTANCE_METHODS = [
48
- :close,
49
- :closed?,
50
- :path,
51
- :flags,
52
- :flags_array,
53
- :file,
54
- :buffer,
55
- :descriptor,
56
- :load,
57
- :compile,
58
- :check
59
- ]
60
-
61
- DEFAULT_INTEGRATION_METHODS = [
62
- :magic,
63
- :mime,
64
- :type
65
- ]
36
+ require_relative 'helpers/magic_test_helper'
66
37
 
67
38
  class MagicTest < Test::Unit::TestCase
39
+ include MagicTestHelpers
40
+
68
41
  def setup
69
42
  @magic = Magic.new
43
+ @version = Magic.version rescue nil
70
44
  end
71
45
 
72
- def teardown
73
- @magic.close
46
+ def test_magic_alias
47
+ assert_same(FileMagic, Magic)
74
48
  end
75
49
 
76
- def test_magic_alias
77
- assert(FileMagic == Magic)
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)
78
64
  end
79
65
 
80
66
  def test_magic_singleton_methods
81
- assert_block do
82
- DEFAULT_SINGLETON_METHODS.all? {|i| Magic.respond_to?(i) }
83
- end
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) }
84
78
  end
85
79
 
86
80
  def test_magic_new_instance
87
81
  assert(@magic.class == Magic)
88
82
  end
89
83
 
90
- def test_magic_instance_methods
91
- assert_block do
92
- DEFAULT_INSTANCE_METHODS.all? {|i| @magic.respond_to?(i) }
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 {}
93
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) }
94
136
  end
95
137
 
96
138
  def test_magic_close
97
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
98
149
 
99
- assert(@magic.closed?)
150
+ def test_magic_close_error
151
+ @magic.close
100
152
  assert_raise Magic::LibraryError do
101
- @magic.file('')
153
+ @magic.flags
102
154
  end
103
155
  end
104
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
+
105
164
  def test_magic_closed?
106
- assert_equal(@magic.closed?, false)
165
+ assert_false(@magic.closed?)
107
166
  @magic.close
108
- assert_equal(@magic.closed?, true)
167
+ assert_true(@magic.closed?)
168
+ end
169
+
170
+ def test_magic_inspect_when_magic_open
171
+ assert_match(%r{^#<Magic:0x.+>$}, @magic.inspect)
109
172
  end
110
173
 
111
- def test_magic_inspect
112
- assert(@magic.inspect.match(%r{^#<Magic:0x.+>$}))
174
+ def test_magic_inspect_when_magic_closed
113
175
  @magic.close
114
- assert(@magic.inspect.match(%r{^#<Magic:0x.+ \(closed\)>$}))
176
+ assert_match(%r{^#<Magic:0x.+ \(closed\)>$}, @magic.inspect)
115
177
  end
116
178
 
117
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])
118
229
  end
119
230
 
120
- def test_magic_path_MAGIC_environment_variable
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])
121
235
  end
122
236
 
123
- def test_magic_flags
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'])
124
241
  end
125
242
 
126
- def test_magic_flags_to_a
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')
127
277
  end
128
278
 
129
279
  def test_magic_file
130
280
  end
131
281
 
282
+ def test_magic_file_with_ERROR_flag
283
+ end
284
+
132
285
  def test_magic_file_with_MAGIC_CONTINUE_flag
133
286
  end
134
287
 
@@ -147,19 +300,66 @@ class MagicTest < Test::Unit::TestCase
147
300
  def test_magic_load
148
301
  end
149
302
 
150
- def test_magic_load_MAGIC_environment_variable
303
+ def test_magic_load_with_custom_Magic_file_path
151
304
  end
152
305
 
153
- def test_magic_check
306
+ def test_magic_with_new_instance_custom_Magic_file_path
154
307
  end
155
308
 
156
- def test_magic_valid?
309
+ def test_magic_load_with_MAGIC_environment_variable
310
+ end
311
+
312
+ def test_magic_check
157
313
  end
158
314
 
159
315
  def test_magic_compile
160
316
  end
161
317
 
162
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)
163
363
  end
164
364
 
165
365
  def test_magic_singleton_open
@@ -198,25 +398,29 @@ class MagicTest < Test::Unit::TestCase
198
398
  def test_magic_singleton_check
199
399
  end
200
400
 
201
- def test_magic_flags_error
202
- end
203
-
204
401
  def test_magic_magic_error
205
- end
402
+ message = 'The quick brown fox jumps over the lazy dog'
403
+ error = Magic::Error.new(message)
206
404
 
207
- def test_magic_library_error
405
+ assert_respond_to(error, :errno)
406
+ assert_equal(error.message, message)
407
+ assert_nil(error.errno)
208
408
  end
209
409
 
210
- def test_magic_new_instance_with_arguments
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)
211
415
  end
212
416
 
213
- def test_file_integration
417
+ def test_magic_new_instance_with_arguments
214
418
  end
215
419
 
216
420
  def test_file_integration_magic
217
421
  end
218
422
 
219
- def test_file_integration_magic_custom_flag
423
+ def test_file_integration_magic_with_custom_flag
220
424
  end
221
425
 
222
426
  def test_file_integration_mime
@@ -228,7 +432,7 @@ class MagicTest < Test::Unit::TestCase
228
432
  def test_file_integration_singleton_magic
229
433
  end
230
434
 
231
- def test_file_integration_singleton_magic_custom_flag
435
+ def test_file_integration_singleton_magic_with_custom_flag
232
436
  end
233
437
 
234
438
  def test_file_integration_singleton_mime
@@ -237,13 +441,10 @@ class MagicTest < Test::Unit::TestCase
237
441
  def test_file_integration_singleton_type
238
442
  end
239
443
 
240
- def test_string_integration
241
- end
242
-
243
444
  def test_string_integration_magic
244
445
  end
245
446
 
246
- def test_string_integration_magic_custom_flag
447
+ def test_string_integration_magic_with_custom_flag
247
448
  end
248
449
 
249
450
  def test_string_integration_mime