From 42a36ac00c926dffb909fa2c0cfe76ffa0307251 Mon Sep 17 00:00:00 2001 From: Rick Ohnemus Date: Fri, 16 Sep 2022 15:17:14 -0700 Subject: [PATCH] create test/pylink if system supports symbolic links Don't package test/pylink (a symbolic link to test/pyfile). Gem packaging creates a dangling symbolic link. Also, symbolic links might even be supported on the installation system. Create the link if it is needed when tests are run. If the system doesn't support symbolic links, then the link won't be created and the symbolic link tests will be skipped. --- ruby-filemagic.gemspec | 2 +- test/filemagic_test.rb | 88 ++++++++++++++++++++++++++---------------- test/pylink | 1 - 3 files changed, 56 insertions(+), 35 deletions(-) delete mode 120000 test/pylink diff --git a/ruby-filemagic.gemspec b/ruby-filemagic.gemspec index 734dd4b..26fc5ca 100644 --- a/ruby-filemagic.gemspec +++ b/ruby-filemagic.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.email = "jens.wille@gmail.com".freeze s.extensions = ["ext/filemagic/extconf.rb".freeze] s.extra_rdoc_files = ["README".freeze, "ChangeLog".freeze, "ext/filemagic/filemagic.c".freeze] - s.files = ["CONTRIBUTING.md".freeze, "ChangeLog".freeze, "Dockerfile".freeze, "README".freeze, "Rakefile".freeze, "TODO".freeze, "ext/filemagic/extconf.rb".freeze, "ext/filemagic/filemagic.c".freeze, "ext/filemagic/filemagic.h".freeze, "lib/filemagic.rb".freeze, "lib/filemagic/ext.rb".freeze, "lib/filemagic/magic.mgc".freeze, "lib/filemagic/version.rb".freeze, "lib/ruby-filemagic.rb".freeze, "test/excel-example.xls".freeze, "test/filemagic_test.rb".freeze, "test/leaktest.rb".freeze, "test/mahoro.c".freeze, "test/perl".freeze, "test/perl.mgc".freeze, "test/pyfile".freeze, "test/pyfile-compressed.gz".freeze, "test/pylink".freeze] + s.files = ["CONTRIBUTING.md".freeze, "ChangeLog".freeze, "Dockerfile".freeze, "README".freeze, "Rakefile".freeze, "TODO".freeze, "ext/filemagic/extconf.rb".freeze, "ext/filemagic/filemagic.c".freeze, "ext/filemagic/filemagic.h".freeze, "lib/filemagic.rb".freeze, "lib/filemagic/ext.rb".freeze, "lib/filemagic/magic.mgc".freeze, "lib/filemagic/version.rb".freeze, "lib/ruby-filemagic.rb".freeze, "test/excel-example.xls".freeze, "test/filemagic_test.rb".freeze, "test/leaktest.rb".freeze, "test/mahoro.c".freeze, "test/perl".freeze, "test/perl.mgc".freeze, "test/pyfile".freeze, "test/pyfile-compressed.gz".freeze] s.homepage = "http://github.com/blackwinter/ruby-filemagic".freeze s.licenses = ["Ruby".freeze] s.post_install_message = "\nruby-filemagic-0.7.3 [2022-01-07]:\n\n* Dockerfile to build native extension (pull request #26 by Pavel Lobashov).\n* Include paths for ARM-based Apple Macs (Apple Silicon) (pull request #35 by\n @545ch4).\n\n".freeze diff --git a/test/filemagic_test.rb b/test/filemagic_test.rb index 0fd497e..9eb7c24 100644 --- a/test/filemagic_test.rb +++ b/test/filemagic_test.rb @@ -24,25 +24,36 @@ def test_file res = fm.file(path_to('pyfile')) assert_equal(python_script, res) - if File.symlink?(path_to('pylink')) - res = fm.file(path_to('pylink')) - assert_equal(match_version( - 0 => "symbolic link to `pyfile'", - 5.22 => 'symbolic link to pyfile' - ), res.strip) - end - - fm.close - fm = FileMagic.new(FileMagic::MAGIC_SYMLINK) + # The following block ensures that symlinks are only tested on systems + # that support them. + begin + assert(File.writable?('test'), "can't write to test directory") + File.symlink('pyfile', 'test/pylink') + + if File.symlink?(path_to('pylink')) + res = fm.file(path_to('pylink')) + assert_equal(match_version( + 0 => "symbolic link to `pyfile'", + 5.22 => 'symbolic link to pyfile' + ), res.strip) + end + + fm.close + fm = FileMagic.new(FileMagic::MAGIC_SYMLINK) - res = fm.file(path_to('pylink')) - assert_equal(python_script, res) + res = fm.file(path_to('pylink')) + assert_equal(python_script, res) - fm.close - fm = FileMagic.new(FileMagic::MAGIC_SYMLINK | FileMagic::MAGIC_MIME) + fm.close + fm = FileMagic.new(FileMagic::MAGIC_SYMLINK | FileMagic::MAGIC_MIME) - res = fm.file(path_to('pylink')) - assert_equal('text/plain; charset=us-ascii', res) + res = fm.file(path_to('pylink')) + assert_equal('text/plain; charset=us-ascii', res) + rescue NotImplementedError + # ignore + ensure + File.unlink('test/pylink') if File.exist?('test/pylink') + end fm.close fm = FileMagic.new(FileMagic::MAGIC_COMPRESS) @@ -81,28 +92,39 @@ def test_descriptor assert_equal(python_script, res) } - if File.symlink?(path_to('pylink')) + # The following block ensures that symlinks are only tested on systems + # that support them. + begin + assert(File.writable?('test'), "can't write to test directory") + File.symlink('pyfile', 'test/pylink') + + if File.symlink?(path_to('pylink')) + fd_for('pylink') { |fd| + res = fm.descriptor(fd) + assert_equal(python_script, res.strip) + } + end + + fm.close + fm = FileMagic.new(FileMagic::MAGIC_SYMLINK) + fd_for('pylink') { |fd| res = fm.descriptor(fd) - assert_equal(python_script, res.strip) + assert_equal(python_script, res) } - end - fm.close - fm = FileMagic.new(FileMagic::MAGIC_SYMLINK) - - fd_for('pylink') { |fd| - res = fm.descriptor(fd) - assert_equal(python_script, res) - } - - fm.close - fm = FileMagic.new(FileMagic::MAGIC_SYMLINK | FileMagic::MAGIC_MIME) + fm.close + fm = FileMagic.new(FileMagic::MAGIC_SYMLINK | FileMagic::MAGIC_MIME) - fd_for('pylink') { |fd| - res = fm.descriptor(fd) - assert_equal('text/plain; charset=us-ascii', res) - } + fd_for('pylink') { |fd| + res = fm.descriptor(fd) + assert_equal('text/plain; charset=us-ascii', res) + } + rescue NotImplementedError + # ignore + ensure + File.unlink('test/pylink') if File.exist?('test/pylink') + end fm.close fm = FileMagic.new(FileMagic::MAGIC_COMPRESS) diff --git a/test/pylink b/test/pylink deleted file mode 120000 index 6939077..0000000 --- a/test/pylink +++ /dev/null @@ -1 +0,0 @@ -pyfile \ No newline at end of file