Module: Aruba::Api::Core

Includes:
RSpec::Matchers
Included in:
Aruba::Api
Defined in:
lib/aruba/api/core.rb

Overview

Core methods of aruba

Those methods do not depend on any other API method of aruba

Instance Method Summary collapse

Instance Method Details

#arubaObject

Aruba Runtime



17
18
19
20
21
22
23
# File 'lib/aruba/api/core.rb', line 17

def aruba
  # TODO: Check this variable being accessed inconsistently. Should only
  # be using the memo!
  # Renaming this to `aruba` causes 100's of rspec failures. Needs a
  # deeper dive, approach with caution!
  @_aruba_runtime ||= Runtime.new
end

#cd(dir, &block) ⇒ Object

Switch to directory

Examples:

Normal directory

cd 'dir'

Move up

cd '..'

Run code in directory

result = cd('some-dir') { Dir.getwd }

Parameters:

  • dir (String)

    The directory



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/aruba/api/core.rb', line 59

def cd(dir, &block)
  if block
    begin
      unless Aruba.platform.directory?(expand_path(dir))
        raise ArgumentError,
              "#{expand_path(dir)} is not a directory or does not exist."
      end

      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

      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" => 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))
    raise ArgumentError, "#{expand_path(dir)} is not a directory or does not exist."
  end

  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)

  self
end

#expand_path(file_name, dir_string = nil) ⇒ String

Expand file name

to be set)

expand_path('/foo/bar') # => /foo/bar

Examples:

Single file name


expand_path('file')
# => <path>/tmp/aruba/file

Single Dot


expand_path('.')
# => <path>/tmp/aruba

using home directory


expand_path('~/file')
# => <path>/home/<name>/file

using fixtures directory


expand_path('%/file')
# => <path>/test/fixtures/file

Absolute directory (requires aruba.config.allow_absolute_paths

Parameters:

  • file_name (String)

    Name of file

  • dir_string (String) (defaults to: nil)

    Name of directory to use as starting point, otherwise current directory is used.

Returns:

  • (String)

    The full path



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/aruba/api/core.rb', line 144

def expand_path(file_name, dir_string = nil)
  unless file_name.is_a?(String) && !file_name.empty?
    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."

    raise ArgumentError, message
  end

  unless Aruba.platform.directory? File.join(aruba.config.root_directory,
                                             aruba.config.working_directory)
    raise "Aruba's working directory does not exist." \
          " Maybe you forgot to run `setup_aruba` before using its API."
  end

  prefix = file_name[0]

  if prefix == aruba.config.fixtures_path_prefix
    rest = file_name[2..]
    path = File.join(*[aruba.fixtures_directory, rest].compact)
    unless Aruba.platform.exist? path
      aruba_fixture_candidates = aruba.config.fixtures_directories
                                      .map { |p| format('"%s"', p) }.join(", ")

      raise 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_fixture_candidates}."
    end

    path
  elsif prefix == "~"
    path = with_environment do
      File.expand_path(file_name)
    end

    raise ArgumentError, 'Expanding "~/" to "/" is not allowed' if path == "/"

    unless Aruba.platform.absolute_path? path
      raise ArgumentError,
            "Expanding \"~\" to a relative path \"#{path}\" is not allowed"
    end

    path.to_s
  elsif absolute?(file_name)
    unless aruba.config.allow_absolute_paths
      caller_location = caller_locations(1, 1).first
      caller_file_line = "#{caller_location.path}:#{caller_location.lineno}"
      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
    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

#in_current_directory { ... } ⇒ Object

Execute block in Aruba's current directory

Yields:

  • The block which should be run in current directory



40
41
42
43
# File 'lib/aruba/api/core.rb', line 40

def in_current_directory(&block)
  create_directory "." unless directory?(".")
  cd(".", &block)
end

#setup_aruba(clobber = true) ⇒ Object

Clean the working directory of aruba

This will only clean up aruba's working directory to remove all artifacts of your tests. This does NOT clean up the current working directory.



30
31
32
33
34
# File 'lib/aruba/api/core.rb', line 30

def setup_aruba(clobber = true)
  Aruba::Setup.new(aruba).call(clobber)

  self
end

#with_environment(env = {}) { ... } ⇒ Object

Run block with environment

Parameters:

  • env (Hash) (defaults to: {})

    (optional) The variables to be used for block.

Yields:

  • The block of code which should be run with the changed environment variables



219
220
221
222
223
224
# File 'lib/aruba/api/core.rb', line 219

def with_environment(env = {}, &block)
  aruba.environment.nest do |nested_env|
    nested_env.update(env)
    Aruba.platform.with_environment nested_env.to_h, &block
  end
end