Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make absolute file name warning an error #783

Merged
merged 2 commits into from
Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions features/04_aruba_api/core/expand_path.feature
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Feature: Expand paths with aruba
When I run `rspec`
Then the specs should all pass

Scenario: Warn when using absolute path
Scenario: Raise error when using absolute path
Given a file named "spec/expand_path_spec.rb" with:
"""ruby
require 'spec_helper'
Expand All @@ -63,15 +63,15 @@ Feature: Expand paths with aruba
end
"""
When I run `rspec`
Then the specs should all pass
Then the specs should not all pass
And the output should contain:
"""
Aruba's `expand_path` method was called with an absolute path
"""

Scenario: Silence warning about using absolute path
Scenario: Silence error about using absolute path

You can use config.allow_absolute_paths to silence the warning about the
You can use config.allow_absolute_paths to silence the error about the
use of absolute paths.

Given a file named "spec/expand_path_spec.rb" with:
Expand Down
19 changes: 11 additions & 8 deletions lib/aruba/api/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,29 @@ def cd(dir, &block)
#
# @example Single file name
#
# # => <path>/tmp/aruba/file
# expand_path('file')
# # => <path>/tmp/aruba/file
#
# @example Single Dot
#
# # => <path>/tmp/aruba
# expand_path('.')
# # => <path>/tmp/aruba
#
# @example using home directory
#
# # => <path>/home/<name>/file
# expand_path('~/file')
# # => <path>/home/<name>/file
#
# @example using fixtures directory
#
# # => <path>/test/fixtures/file
# expand_path('%/file')
# # => <path>/test/fixtures/file
#
# @example Absolute directory
# @example Absolute directory (requires aruba.config.allow_absolute_paths
# to be set)
#
# # => /foo/bar
# expand_path('/foo/bar')
# # => /foo/bar
#
def expand_path(file_name, dir_string = nil)
unless file_name.is_a?(String) && !file_name.empty?
Expand All @@ -157,7 +158,7 @@ def expand_path(file_name, dir_string = nil)

prefix = file_name[0]

if aruba.config.fixtures_path_prefix == prefix
if prefix == aruba.config.fixtures_path_prefix
rest = file_name[2..-1]
path = File.join(*[aruba.fixtures_directory, rest].compact)
unless Aruba.platform.exist? path
Expand Down Expand Up @@ -189,11 +190,13 @@ def expand_path(file_name, dir_string = nil)
unless aruba.config.allow_absolute_paths
caller_location = caller_locations(1, 1).first
caller_file_line = "#{caller_location.path}:#{caller_location.lineno}"
aruba.logger.warn \
message =
"Aruba's `expand_path` method was called with an absolute path" \
" at #{caller_file_line}, which is not recommended." \
" The path passed was '#{file_name}'." \
" Change the call to pass a relative path or set "\
"`config.allow_absolute_paths = true` to silence this warning"
raise UserError, message
end
file_name
else
Expand Down
15 changes: 5 additions & 10 deletions spec/aruba/api/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,16 @@
let(:logger) { @aruba.aruba.logger }

before do
allow(@aruba.aruba.logger).to receive :warn
allow(logger).to receive :warn
end

it { expect(@aruba.expand_path(@file_path)).to eq @file_path }

it "warns about it" do
@aruba.expand_path(@file_path)
expect(logger).to have_received(:warn)
.with(/Aruba's `expand_path` method was called with an absolute path/)
it "raises a UserError" do
expect { @aruba.expand_path(@file_path) }.to raise_error Aruba::UserError
end

it "does not warn about it if told not to" do
it "does not raise an error if told not to" do
@aruba.aruba.config.allow_absolute_paths = true
@aruba.expand_path(@file_path)
expect(logger).not_to have_received(:warn)
expect { @aruba.expand_path(@file_path) }.not_to raise_error
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/aruba/matchers/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def announcer(*args)
end

context "when multiple commands output hello world on stdout" do
context "and all comands must have the output" do
context "and all commands must have the output" do
before do
run_command(cmd)
run_command(cmd)
Expand All @@ -63,7 +63,7 @@ def announcer(*args)
it { expect(all_commands).to all have_output output }
end

context "and any comand can have the output" do
context "and any command can have the output" do
before do
run_command(cmd)
run_command("echo hello universe")
Expand Down