From ed358009fc25d7d7be92883b732446e281fe3412 Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Wed, 18 Jul 2018 01:40:52 +1000 Subject: [PATCH] Add shallow_link_path to inspect symlink direct link (#309) * Add shallow_link_path to inspect symlink direct link * Add tests * Remove safety nets to please rubocop Signed-off-by: Colin Hebert --- lib/train/file/local.rb | 5 +++++ lib/train/file/remote/aix.rb | 6 ++++++ lib/train/file/remote/unix.rb | 5 +++++ lib/train/file/remote/windows.rb | 4 ++++ test/unit/file/local_test.rb | 21 ++++++++++++++++----- test/unit/file/remote/aix_test.rb | 7 +++++++ 6 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/train/file/local.rb b/lib/train/file/local.rb index 80191e9c..df85c329 100644 --- a/lib/train/file/local.rb +++ b/lib/train/file/local.rb @@ -27,6 +27,11 @@ def link_path end end + def shallow_link_path + return nil unless symlink? + @link_path ||= ::File.readlink(@path) + end + def block_device? ::File.blockdev?(@path) end diff --git a/lib/train/file/remote/aix.rb b/lib/train/file/remote/aix.rb index ddd92bfe..652e2dab 100644 --- a/lib/train/file/remote/aix.rb +++ b/lib/train/file/remote/aix.rb @@ -12,6 +12,12 @@ def link_path @backend.run_command("perl -e 'print readlink shift' #{@spath}").stdout.chomp end + def shallow_link_path + return nil unless symlink? + @shallow_link_path ||= + @backend.run_command("perl -e 'print readlink shift' #{@spath}").stdout.chomp + end + def mounted @mounted ||= @backend.run_command("lsfs -c #{@spath}") end diff --git a/lib/train/file/remote/unix.rb b/lib/train/file/remote/unix.rb index 5ecc8f39..87395c78 100644 --- a/lib/train/file/remote/unix.rb +++ b/lib/train/file/remote/unix.rb @@ -60,6 +60,11 @@ def link_path symlink? ? path : nil end + def shallow_link_path + return nil unless symlink? + @shallow_link_path ||= ::File.readlink(@path) + end + def unix_mode_mask(owner, type) o = UNIX_MODE_OWNERS[owner.to_sym] return nil if o.nil? diff --git a/lib/train/file/remote/windows.rb b/lib/train/file/remote/windows.rb index ab8070da..e3129ef9 100644 --- a/lib/train/file/remote/windows.rb +++ b/lib/train/file/remote/windows.rb @@ -77,6 +77,10 @@ def link_path nil end + def shallow_link_path + nil + end + def mounted nil end diff --git a/test/unit/file/local_test.rb b/test/unit/file/local_test.rb index 84d9eb80..a25e85e7 100644 --- a/test/unit/file/local_test.rb +++ b/test/unit/file/local_test.rb @@ -10,7 +10,7 @@ File.stub :read, res do connection.file(rand.to_s).content.must_equal(res) end - end + end { exist?: :exist?, @@ -33,13 +33,13 @@ it 'returns the type block_device if it is block device' do File.stub :ftype, "blockSpecial" do connection.file(rand.to_s).type.must_equal :block_device - end + end end it 'returns the type character_device if it is character device' do File.stub :ftype, "characterSpecial" do connection.file(rand.to_s).type.must_equal :character_device - end + end end it 'returns the type symlink if it is symlink' do @@ -77,7 +77,7 @@ connection.file(rand.to_s).type.must_equal :unknown end end - end + end describe '#path' do it 'returns the path if it is not a symlink' do @@ -107,4 +107,15 @@ end end end -end \ No newline at end of file + + describe '#shallow_shlink_path' do + it 'returns file\'s direct link path' do + out = rand.to_s + File.stub :readlink, out do + File.stub :symlink?, true do + connection.file(rand.to_s).shallow_link_path.must_equal out + end + end + end + end +end diff --git a/test/unit/file/remote/aix_test.rb b/test/unit/file/remote/aix_test.rb index 723eee9f..47d5605f 100644 --- a/test/unit/file/remote/aix_test.rb +++ b/test/unit/file/remote/aix_test.rb @@ -23,4 +23,11 @@ backend.mock_command("perl -e 'print readlink shift' path", 'our_link_path') file.link_path.must_equal 'our_link_path' end + + it 'returns a correct shallow_link_path' do + file = cls.new(backend, 'path') + file.stubs(:symlink?).returns(true) + backend.mock_command("perl -e 'print readlink shift' path", 'our_link_path') + file.link_path.must_equal 'our_link_path' + end end