Skip to content

Commit

Permalink
Handle absolute paths and home directory in ArubaPath
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed Jan 2, 2020
1 parent 9d4ea18 commit 86de4cb
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 51 deletions.
6 changes: 3 additions & 3 deletions lib/aruba/api/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ def expand_path(file_name, dir_string = nil)
path
elsif prefix == '~'
path = with_environment do
ArubaPath.new(File.expand_path(file_name))
File.expand_path(file_name)
end

fail ArgumentError, 'Expanding "~/" to "/" is not allowed' if path.to_s == '/'
fail ArgumentError, %(Expanding "~/" to a relative path "#{path}" is not allowed) unless path.absolute?
fail ArgumentError, 'Expanding "~/" to "/" is not allowed' if path == '/'
fail ArgumentError, %(Expanding "~/" to a relative path "#{path}" is not allowed) unless Aruba.platform.absolute_path? path

path.to_s
elsif absolute? file_name
Expand Down
44 changes: 27 additions & 17 deletions lib/aruba/aruba_path.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
require 'pathname'
require 'delegate'

# Aruba
module Aruba
# Pathname for aruba files and directories
#
# @private
class ArubaPath < Delegator
class ArubaPath
def initialize(path)
obj = [path.to_s].flatten

super obj

@delegate_sd_obj = obj
@obj = [path.to_s].flatten
end

# Get path
def __getobj__
::Pathname.new(::File.join(*@delegate_sd_obj))
def to_str
to_pathname.to_s
end

# Set path
def __setobj__(obj)
@delegate_sd_obj = [obj.to_s].flatten
def to_s
to_str
end

# Add directory/file to path
Expand All @@ -37,18 +30,19 @@ def __setobj__(obj)
# puts path
# # => path/to/dir.d/subdir.d
def push(p)
@delegate_sd_obj << p
@obj << p
end
alias << push

# Remove last component of path
# Remove last pushed component of path
#
# @example
# path = ArubaPath.new 'path/to/dir.d'
# path = ArubaPath.new 'path/to'
# path.push 'dir'
# path.pop
# puts path # => path/to
def pop
@delegate_sd_obj.pop
@obj.pop
end

# Return string at index
Expand All @@ -57,5 +51,21 @@ def pop
def [](index)
to_s[index]
end

private

# Get path
def to_pathname
current_path = @obj.inject do |path, element|
if element.start_with? '~'
element
elsif Aruba.platform.absolute_path? element
element
else
File.join(path, element)
end
end
::Pathname.new(current_path)
end
end
end
28 changes: 28 additions & 0 deletions spec/aruba/api/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@
expect(ENV.values_at(*keys)).to eq old_values
end
end

it 'expands "~" to the aruba home directory' do
full_path = aruba.config.home_directory
@aruba.cd '~' do
expect(Dir.pwd).to eq full_path
end
expect(Dir.pwd).not_to eq full_path
end
end

context 'with no block given' do
it "sets aruba's current directory to the given directory" do
@aruba.create_directory @directory_name
full_path = File.expand_path(@directory_path)

@aruba.cd @directory_name

expect(File.expand_path @aruba.aruba.current_directory).to eq full_path
end

it 'expands "~" to the aruba home directory' do
full_path = aruba.config.home_directory
@aruba.cd '~'

require 'pry'
binding.pry
expect(File.expand_path @aruba.aruba.current_directory).to eq full_path
end
end
end

Expand Down
61 changes: 30 additions & 31 deletions spec/aruba/aruba_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,47 @@
require 'aruba/aruba_path'

RSpec.describe Aruba::ArubaPath do
subject(:path) { described_class.new(new_path) }

let(:new_path) { 'path/to/dir' }

it { expect(path).to be }

describe '#to_s' do
describe '#pop' do
it "pops a previously pushed path element" do
path = described_class.new('path/to/dir')
path.push 'subdir'
popped = path.pop

aggregate_failures do
expect(popped).to eq 'subdir'
expect(path.to_s).to eq 'path/to/dir'
end
end
end

describe '#to_s' do
context 'when string is used' do
it { expect(path.to_s).to eq new_path }
it "returns the path given in the initializer" do
path = described_class.new('path/to/dir')
expect(path.to_s).to eq 'path/to/dir'
end

# make it compatible with the old API
context 'when array is used' do
let(:net_path) { %w(path to dir) }

it { expect(path.to_s).to eq File.join(*new_path) }
it "combines pushed relative path elements" do
path = described_class.new('path/to')
path << 'dir'
expect(path.to_s).to eq 'path/to/dir'
end
end

describe '#push' do
before(:each) { path.push 'subdir' }

it { expect(path.to_s).to eq 'path/to/dir/subdir' }
end

describe '#<<' do
before(:each) { path << 'subdir' }

it { expect(path.to_s).to eq 'path/to/dir/subdir' }
end

describe '#pop' do
before(:each) { path << 'subdir' }
before(:each) { path.pop }
it "replaces earlier elements when an absolute path was pushed" do
path = described_class.new('path/to')
path << '/foo'
expect(path.to_s).to eq '/foo'
end

it { expect(path.to_s).to eq 'path/to/dir' }
it "replaces earlier elements when a home directory-relative path was pushed" do
path = described_class.new('path/to')
path << '~/foo'
expect(path.to_s).to eq '~/foo'
end
end

describe '#[]' do
let(:path) { described_class.new('path/to/dir') }

context 'when single index' do
it { expect(path[0]).to eq 'p' }
end
Expand Down

0 comments on commit 86de4cb

Please sign in to comment.