test/filemagic_test.rb in ruby-filemagic-0.4.2 vs test/filemagic_test.rb in ruby-filemagic-0.5.0

- old
+ new

@@ -1,29 +1,26 @@ require 'test/unit' - -top_dir = File.join(File.dirname(__FILE__), '..') -$:.unshift(File.join(top_dir, 'lib'), File.join(top_dir, 'ext')) require 'filemagic' class TestFileMagic < Test::Unit::TestCase def test_file fm = FileMagic.new(FileMagic::MAGIC_NONE) res = fm.file(path_to('pyfile')) - assert_equal('a python script text executable', res) + assert_equal('Python script, ASCII text executable', res) if File.symlink?(path_to('pylink')) res = fm.file(path_to('pylink')) assert_equal("symbolic link to `pyfile'", res) end fm.close fm = FileMagic.new(FileMagic::MAGIC_SYMLINK) res = fm.file(path_to('pylink')) - assert_equal('a python script text executable', res) + assert_equal('Python script, ASCII text executable', res) fm.close fm = FileMagic.new(FileMagic::MAGIC_SYMLINK | FileMagic::MAGIC_MIME) res = fm.file(path_to('pylink')) @@ -31,25 +28,25 @@ fm.close fm = FileMagic.new(FileMagic::MAGIC_COMPRESS) res = fm.file(path_to('pyfile-compressed.gz')) - assert_match(/^a python script text executable \(gzip compressed data, was "pyfile-compressed", from Unix/, res) + assert_match(/^Python script, ASCII text executable \(gzip compressed data, was "pyfile-compressed", from Unix/, res) fm.close end def test_buffer fm = FileMagic.new(FileMagic::MAGIC_NONE) res = fm.buffer("#!/bin/sh\n") fm.close - assert_match(/shell script text executable$/, res) + assert_equal('POSIX shell script, ASCII text executable', res) end def test_check fm = FileMagic.new(FileMagic::MAGIC_NONE) - res = fm.check(path_to('perl')) + res = silence_stderr { fm.check(path_to('perl')) } fm.close assert_equal(0, res) end def test_compile @@ -65,11 +62,11 @@ block_fm = nil res = FileMagic.open(FileMagic::MAGIC_NONE) { |fm| block_fm = fm fm.file(path_to('pyfile')) } - assert_equal('a python script text executable', res) + assert_equal('Python script, ASCII text executable', res) assert block_fm.closed? end def test_setflags fm = FileMagic.new(FileMagic::MAGIC_NONE) @@ -98,11 +95,11 @@ # tests adapted from mahoro: def test_mahoro_file fm = FileMagic.new fm.flags = FileMagic::MAGIC_NONE - assert_equal('ASCII C program text', fm.file(path_to('mahoro.c'))) + assert_equal('C source, ASCII text', fm.file(path_to('mahoro.c'))) end def test_mahoro_mime_file fm = FileMagic.new fm.flags = FileMagic::MAGIC_MIME @@ -110,22 +107,22 @@ end def test_mahoro_buffer fm = FileMagic.new fm.flags = FileMagic::MAGIC_NONE - assert_equal('ASCII C program text', fm.buffer(File.read(path_to('mahoro.c')))) + assert_equal('C source, ASCII text', fm.buffer(File.read(path_to('mahoro.c')))) end def test_mahoro_mime_buffer fm = FileMagic.new fm.flags = FileMagic::MAGIC_MIME assert_equal('text/x-c; charset=us-ascii', fm.buffer(File.read(path_to('mahoro.c')))) end def test_mahoro_valid fm = FileMagic.new - assert(fm.valid?, 'Default database was not valid.') + assert(silence_stderr { fm.valid? }, 'Default database was not valid.') end # test abbreviating mime types def test_abbrev_mime_type @@ -135,15 +132,22 @@ assert_equal('text/plain; charset=us-ascii', fm.file(path_to('perl'))) fm.simplified = true assert fm.simplified? assert_equal('text/plain', fm.file(path_to('perl'))) - assert_equal('application/vnd.ms-office', fm.file(path_to('excel-example.xls'))) + assert_equal('application/msword', fm.file(path_to('excel-example.xls'))) end # utility methods: def path_to(file, dir = File.dirname(__FILE__)) File.join(dir, file) + end + + def silence_stderr + require 'nuggets/io/redirect' + $stderr.redirect { yield } + rescue LoadError + yield end end