File: core.rb

package info (click to toggle)
ruby-aruba 2.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 968 kB
  • sloc: javascript: 6,850; ruby: 4,010; makefile: 5
file content (229 lines) | stat: -rw-r--r-- 7,398 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# frozen_string_literal: true

require 'rspec/expectations'
require 'aruba/runtime'
require 'aruba/errors'
require 'aruba/setup'

# Aruba
module Aruba
  # Api
  module Api
    # Core methods of aruba
    #
    # Those methods do not depend on any other API method of aruba
    module Core
      include ::RSpec::Matchers

      # Aruba Runtime
      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

      # 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.
      def setup_aruba(clobber = true)
        Aruba::Setup.new(aruba).call(clobber)

        self
      end

      # Execute block in Aruba's current directory
      #
      # @yield
      #   The block which should be run in current directory
      def in_current_directory(&block)
        create_directory '.' unless directory?('.')
        cd('.', &block)
      end

      # Switch to directory
      #
      # @param [String] dir
      #   The directory
      #
      # @example Normal directory
      #   cd 'dir'
      #
      # @example Move up
      #   cd '..'
      #
      # @example Run code in directory
      #   result = cd('some-dir') { Dir.getwd }
      #
      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 file name
      #
      # @param [String] file_name
      #   Name of file
      #
      # @param [String] dir_string
      #   Name of directory to use as starting point, otherwise current directory is used.
      #
      # @return [String]
      #   The full path
      #
      # @example Single file name
      #
      #   expand_path('file')
      #   # => <path>/tmp/aruba/file
      #
      # @example Single Dot
      #
      #   expand_path('.')
      #   # => <path>/tmp/aruba
      #
      # @example using home directory
      #
      #   expand_path('~/file')
      #   # => <path>/home/<name>/file
      #
      # @example using fixtures directory
      #
      #   expand_path('%/file')
      #   # => <path>/test/fixtures/file
      #
      # @example Absolute directory (requires aruba.config.allow_absolute_paths
      # to be set)
      #
      #   expand_path('/foo/bar')
      #   # => /foo/bar
      #
      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

      # Run block with environment
      #
      # @param [Hash] env (optional)
      #   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)
        aruba.environment.nest do |nested_env|
          nested_env.update(env)
          Aruba.platform.with_replaced_environment nested_env.to_h, &block
        end
      end
    end
  end
end