lib/aruba/api/core.rb in aruba-1.0.0.pre.alpha.5 vs lib/aruba/api/core.rb in aruba-1.0.0

- old
+ new

@@ -56,40 +56,37 @@ # # rubocop:disable Metrics/MethodLength def cd(dir, &block) if block_given? begin - unless Aruba.platform.directory? expand_path(dir) - fail ArgumentError, "#{expand_path(dir)} is not a directory or does not exist." - end + fail ArgumentError, "#{expand_path(dir)} is not a directory or does not exist." unless Aruba.platform.directory? expand_path(dir) old_directory = expand_path('.') aruba.current_directory << dir new_directory = expand_path('.') aruba.event_bus.notify Events::ChangedWorkingDirectory.new(old: old_directory, new: new_directory) old_dir = Aruba.platform.getwd - Aruba.platform.chdir File.join(aruba.root_directory, aruba.current_directory) + real_new_directory = File.expand_path(aruba.current_directory, aruba.root_directory) + Aruba.platform.chdir real_new_directory result = with_environment( 'OLDPWD' => old_dir, - 'PWD' => File.expand_path(File.join(aruba.root_directory, aruba.current_directory)), + 'PWD' => real_new_directory, &block ) ensure aruba.current_directory.pop Aruba.platform.chdir old_dir end return result end - unless Aruba.platform.directory? expand_path(dir) - fail ArgumentError, "#{expand_path(dir)} is not a directory or does not exist." - end + fail ArgumentError, "#{expand_path(dir)} is not a directory or does not exist." unless Aruba.platform.directory? expand_path(dir) old_directory = expand_path('.') aruba.current_directory << dir new_directory = expand_path('.') @@ -137,52 +134,56 @@ # # rubocop:disable Metrics/MethodLength # rubocop:disable Metrics/CyclomaticComplexity # rubocop:disable Metrics/PerceivedComplexity def expand_path(file_name, dir_string = nil) - # rubocop:disable Metrics/LineLength + # rubocop:disable Layout/LineLength message = %(Filename "#{file_name}" needs to be a string. It cannot be nil or empty either. Please use `expand_path('.')` if you want the current directory to be expanded.) - # rubocop:enable Metrics/LineLength + # rubocop:enable Layout/LineLength fail ArgumentError, message unless file_name.is_a?(String) && !file_name.empty? - # rubocop:disable Metrics/LineLength + # rubocop:disable Layout/LineLength fail %(Aruba's working directory does not exist. Maybe you forgot to run `setup_aruba` before using its API.) unless Aruba.platform.directory? File.join(aruba.config.root_directory, aruba.config.working_directory) - # rubocop:enable Metrics/LineLength + # rubocop:enable Layout/LineLength prefix = file_name[0] rest = file_name[2..-1] if aruba.config.fixtures_path_prefix == prefix path = File.join(*[aruba.fixtures_directory, rest].compact) - # rubocop:disable Metrics/LineLength + # rubocop:disable Layout/LineLength fail ArgumentError, %(Fixture "#{rest}" does not exist in fixtures directory "#{aruba.fixtures_directory}". This was the one we found first on your system from all possible candidates: #{aruba.config.fixtures_directories.map { |p| format('"%s"', p) }.join(', ')}.) unless Aruba.platform.exist? path - # rubocop:enable Metrics/LineLength + # rubocop:enable Layout/LineLength 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 unless aruba.config.allow_absolute_paths - aruba.logger.warn 'Using absolute paths in Aruba is not recommended.' \ - ' Set config.allow_absolute_paths = true to silence this warning' + caller_location = caller_locations(1, 1).first + caller_file_line = "#{caller_location.path}:#{caller_location.lineno}" + aruba.logger.warn "Aruba's `expand_path` method was called with an absolute path at #{caller_file_line}, which is not recommended." \ + ' Change the call to pass a relative path or set config.allow_absolute_paths = true to silence this warning' end file_name else - directory = File.join(aruba.root_directory, aruba.current_directory) - directory = File.expand_path(dir_string, directory) if dir_string - File.expand_path(file_name, directory) + with_environment do + directory = File.expand_path(aruba.current_directory, aruba.root_directory) + directory = File.expand_path(dir_string, directory) if dir_string + File.expand_path(file_name, directory) + end end end # rubocop:enable Metrics/MethodLength # rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/PerceivedComplexity @@ -193,17 +194,13 @@ # The variables to be used for block. # # @yield # The block of code which should be run with the changed environment variables def with_environment(env = {}, &block) - old_aruba_env = aruba.environment.to_h - - # make sure the old environment is really restored in "ENV" - Aruba.platform.with_environment aruba.environment.update(env).to_h, &block - ensure - # make sure the old environment is really restored in "aruba.environment" - aruba.environment.clear - aruba.environment.update old_aruba_env + aruba.environment.nest do |nested_env| + nested_env.update(env) + Aruba.platform.with_environment nested_env.to_h, &block + end end end end end