Skip to content

Commit

Permalink
Make sure aruba's own helpers don't pass absolute paths to expand_path
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed Jan 1, 2020
1 parent 4a3a826 commit 1c9f4ae
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-01-01 19:49:41 +0100 using RuboCop version 0.78.0.
# on 2020-01-01 19:56:38 +0100 using RuboCop version 0.78.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -46,7 +46,7 @@ Metrics/MethodLength:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 175
Max: 180

# Offense count: 3
# Configuration parameters: CountKeywordArgs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ Feature: Check existence of directories
When I run `rspec`
Then the specs should all pass

Scenario: Does not warn when passed an absolute path
Given a file named "spec/create_directory_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'Check that no warnings are printed when passed an absolute path', :type => :aruba do
let(:directory) { 'dir.d' }
let(:expanded_directory) { File.expand_path(directory) }
before(:each) { create_directory(expanded_directory) }
it { expect(directory?(expanded_directory)).to be true }
end
"""
When I run `rspec`
Then the specs should all pass
And the output should not contain:
"""
Aruba's `expand_path` method was called with an absolute path
"""

Scenario: Is file, but should be directory and exist
Given a file named "spec/create_directory_spec.rb" with:
"""ruby
Expand Down
21 changes: 21 additions & 0 deletions features/04_aruba_api/filesystem/check_if_path_is_file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ Feature: Check existence of files
When I run `rspec`
Then the specs should all pass

Scenario: Does not warn when passed an absolute path
Given a file named "spec/create_directory_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'Check that no warnings are printed when passed an absolute path', :type => :aruba do
let(:file) { 'file.txt' }
let(:expanded_file) { File.expand_path(file) }
before(:each) { touch(expanded_file) }
it { expect(file?(expanded_file)).to be true }
end
"""
When I run `rspec`
Then the specs should all pass
And the output should not contain:
"""
Aruba's `expand_path` method was called with an absolute path
"""

Scenario: Is directory, but should be file and exist
Given a file named "spec/create_directory_spec.rb" with:
"""ruby
Expand Down
16 changes: 12 additions & 4 deletions lib/aruba/api/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def exist?(file_or_directory)
# @param [String] file
# The file/directory which should exist
def file?(file)
Aruba.platform.file? expand_path(file)
Aruba.platform.file? expand_path_if_relative(file)
end

# Check if directory exist and is directory
#
# @param [String] file
# The file/directory which should exist
def directory?(file)
Aruba.platform.directory? expand_path(file)
Aruba.platform.directory? expand_path_if_relative(file)
end

# Check if file exist and is executable
Expand Down Expand Up @@ -154,7 +154,7 @@ def touch(*args)

args.each { |p| create_directory(File.dirname(p)) }

Aruba.platform.touch(args.map { |p| expand_path(p) }, options)
Aruba.platform.touch(args.map { |p| expand_path_if_relative(p) }, options)

self
end
Expand Down Expand Up @@ -333,7 +333,7 @@ def append_to_file(file_name, file_content)
# @param [String] directory_name
# The name of the directory which should be created
def create_directory(directory_name)
Aruba.platform.mkdir expand_path(directory_name)
Aruba.platform.mkdir expand_path_if_relative(directory_name)

self
end
Expand Down Expand Up @@ -413,6 +413,14 @@ def file_size(name)

Aruba.platform.determine_file_size expand_path(name)
end

private

def expand_path_if_relative(path)
return path unless relative?(path)

expand_path(path)
end
end
end
end

0 comments on commit 1c9f4ae

Please sign in to comment.