Skip to content

Commit

Permalink
Add shallow_link_path to inspect symlink direct link (#309)
Browse files Browse the repository at this point in the history
* Add shallow_link_path to inspect symlink direct link
* Add tests
* Remove safety nets to please rubocop

Signed-off-by: Colin Hebert <hebert.colin@gmail.com>
  • Loading branch information
ColinHebert authored and jquick committed Jul 17, 2018
1 parent 340267d commit ed35800
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/train/file/local.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/train/file/remote/aix.rb
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/train/file/remote/unix.rb
Expand Up @@ -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?
Expand Down
4 changes: 4 additions & 0 deletions lib/train/file/remote/windows.rb
Expand Up @@ -77,6 +77,10 @@ def link_path
nil
end

def shallow_link_path
nil
end

def mounted
nil
end
Expand Down
21 changes: 16 additions & 5 deletions test/unit/file/local_test.rb
Expand Up @@ -10,7 +10,7 @@
File.stub :read, res do
connection.file(rand.to_s).content.must_equal(res)
end
end
end

{
exist?: :exist?,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -107,4 +107,15 @@
end
end
end
end

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
7 changes: 7 additions & 0 deletions test/unit/file/remote/aix_test.rb
Expand Up @@ -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

0 comments on commit ed35800

Please sign in to comment.