Top Level Namespace

Defined Under Namespace

Modules: Fig, GitHelper

Constant Summary collapse

FIG_SPEC_BASE_DIRECTORY =
Dir.mktmpdir 'fig-rspec-'
CURRENT_DIRECTORY =

“Current” as in current directory when running fig.

FIG_SPEC_BASE_DIRECTORY + '/current-directory'
USER_HOME =
FIG_SPEC_BASE_DIRECTORY + '/user-home'
FIG_HOME =
FIG_SPEC_BASE_DIRECTORY + '/fig-home'
FIG_DOWNLOAD_DIR =

For split URL behavior - using distinct directories to catch incorrect URL usage

File.join(FIG_SPEC_BASE_DIRECTORY, 'remote')
FIG_UPLOAD_DIR =
File.join(FIG_SPEC_BASE_DIRECTORY, 'upload')
FIG_DOWNLOAD_URL =
%Q<file://#{FIG_DOWNLOAD_DIR}>
FIG_UPLOAD_URL =
%Q<file://#{FIG_UPLOAD_DIR}>
FIG_FILE_GUARANTEED_TO_EXIST =

Needed for testing of resources.

File.expand_path(CURRENT_DIRECTORY + '/file-guaranteed-to-exist')

Instance Method Summary collapse

Instance Method Details

#_fig_input_options(first_extra, rest_extra) ⇒ Object

A bit of ruby magic to make invoking fig() nicer; this takes advantage of the hash assignment syntax so you can call it like any of

fig([arguments])
fig([arguments], input)
fig([arguments], input, :no_raise_on_error => true)
fig([arguments], :no_raise_on_error => true)


138
139
140
141
142
143
144
145
146
# File 'lib/fig/spec_utils.rb', line 138

def _fig_input_options(first_extra, rest_extra)
  return nil, rest_extra || {} if first_extra.nil?

  if first_extra.is_a? Hash
    return nil, first_extra
  end

  return first_extra, rest_extra || {}
end

#_run_command(command_line, input, options) ⇒ Object



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
# File 'lib/fig/spec_utils.rb', line 148

def _run_command(command_line, input, options)
  out = err = exit_code = exit_string = nil

  if options.fetch(:fork, true)
    out, err, exit_code, exit_string =
      _run_command_externally command_line, input, options
  else
    out, err, exit_code, exit_string =
      _run_command_internally command_line, input, options
  end

  if exit_string
    # Common scenario during development is that the fig process will fail for
    # whatever reason, but the RSpec expectation is checking whether a file was
    # created, etc. meaning that we don't see stdout, stderr, etc. but RSpec's
    # failure message for the expectation, which isn't informative.  Throwing
    # an exception that RSpec will catch will correctly integrate the fig
    # output with the rest of the RSpec output.
    fig_failure = "Fig process failed:\n"
    fig_failure << "command: #{command_line.join(' ')}\n"
    fig_failure << "result: #{exit_string}\n"
    fig_failure << "stdout: #{out.nil? ? '<nil>' : out}\n"
    fig_failure << "stderr: #{err.nil? ? '<nil>' : err}\n"
    if input
      fig_failure << "input: #{input}\n"
    end

    raise fig_failure
  end

  if ! options[:dont_strip_output]
    err.strip!
    out.strip!
  end

  return out, err, exit_code
end

#_run_command_externally(command_line, input, options) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/fig/spec_utils.rb', line 186

def _run_command_externally(command_line, input, options)
  full_command_line = BASE_FIG_COMMAND_LINE + command_line
  out, err, result = Fig::ExternalProgram.capture(full_command_line, input)

  exit_code   = result.nil? ? 0 : result.exitstatus
  exit_string = nil
  if result && ! result.success? && ! options[:no_raise_on_error]
    exit_string = result.to_s
  end

  # Hooray for Windows line endings!  Not.
  if out
    out.gsub!(/\r+\n/, "\n")
  end
  if err
    err.gsub!(/\r+\n/, "\n")
  end

  return out, err, exit_code, exit_string
end

#_run_command_internally(command_line, input, options) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/fig/spec_utils.rb', line 207

def _run_command_internally(command_line, input, options)
  original_stdin  = $stdin
  original_stdout = $stdout
  original_stderr = $stderr

  begin
    stdin     = input ? StringIO.new(input) : StringIO.new
    stdout    = StringIO.new
    stderr    = StringIO.new
    exit_code = nil

    $stdin  = stdin
    $stdout = stdout
    $stderr = stderr

    if ENV['FIG_SPEC_DEBUG']
      exit_code = FIG_COMMAND_CLASS.new.run_fig command_line
    else
      exit_code =
        FIG_COMMAND_CLASS.new.run_fig_with_exception_handling command_line
    end

    exit_string = nil
    if exit_code != 0 && ! options[:no_raise_on_error]
      exit_string = exit_code.to_s
    end

    return stdout.string, stderr.string, exit_code, exit_string
  ensure
    $stdin  = original_stdin
    $stdout = original_stdout
    $stderr = original_stderr
  end
end

#clean_up_test_environmentObject



266
267
268
269
270
# File 'lib/fig/spec_utils.rb', line 266

def clean_up_test_environment()
  FileUtils.rm_rf(FIG_SPEC_BASE_DIRECTORY)

  return
end

#cleanup_home_and_remote(unified: true) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/fig/spec_utils.rb', line 272

def cleanup_home_and_remote(unified: true)
  FileUtils.rm_rf(FIG_HOME)
  
  # Clean up split URL directories
  FileUtils.rm_rf(FIG_DOWNLOAD_DIR)
  FileUtils.rm_rf(FIG_UPLOAD_DIR)
  
  # Create base directories for split URLs
  FileUtils.mkdir_p(FIG_UPLOAD_DIR)
  FileUtils.mkdir_p(File.join(FIG_UPLOAD_DIR, Fig::Repository::METADATA_SUBDIRECTORY))

  if unified
    # use symlink to simulate an aggregated artifactory repo 
    FileUtils.ln_s(FIG_UPLOAD_DIR, FIG_DOWNLOAD_DIR)
  else
    FileUtils.mkdir_p(FIG_DOWNLOAD_DIR)
    FileUtils.mkdir_p(File.join(FIG_DOWNLOAD_DIR, Fig::Repository::METADATA_SUBDIRECTORY))
  end
  
  return
end

#fig(command_line, first_extra = nil, rest_extra = nil) ⇒ Object

Options:

:current_directory  What the current directory should be when starting fig.

:figrc              Value of the --figrc option.  If not specified,
                    --no-figrc will be passed to fig.

:no_raise_on_error  Normally an exception is thrown if fig returns an error
                    code.  If this option is true, then no exception will
                    be raised, allowing testing of failure states/output.

:fork               If specified as false, don't run fig as an external
                    process, but in-process instead.  This will run faster,
                    but will screw up the test suite if fig invokes
                    Kernel#exec (due to a command statement) or otherwise
                    depends upon at-exit behavior.


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
# File 'lib/fig/spec_utils.rb', line 95

def fig(command_line, first_extra = nil, rest_extra = nil)
  input, options = _fig_input_options(first_extra, rest_extra)

  $fig_run_count += 1
  ENV['FIG_COVERAGE_RUN_COUNT'] = $fig_run_count.to_s

  out = err = exit_code = nil

  current_directory = options[:current_directory] || CURRENT_DIRECTORY
  Dir.chdir current_directory do
    standard_options = []
    standard_options.concat %w< --log-level warn >
    standard_options.concat %w< --file - > if input

    figrc = options[:figrc]
    if figrc
      standard_options << '--figrc' << figrc
    else
      standard_options << '--no-figrc'
    end

    if command_line.include?('--update-lock-response')
      if ! options.fetch(:fork, true)
        raise 'Cannot specify both ":fork => false" and --update-lock-response.'
      end
    elsif ! options.fetch(:fork, true) || Fig::OperatingSystem.windows?
      standard_options.concat %w< --update-lock-response ignore >
    end

    command_line = [standard_options, command_line].flatten
    out, err, exit_code = _run_command command_line, input, options
  end

  return out, err, exit_code
end

#set_local_repository_format_to_future_versionObject



294
295
296
297
298
299
300
301
302
# File 'lib/fig/spec_utils.rb', line 294

def set_local_repository_format_to_future_version()
  version_file = File.join(FIG_HOME, Fig::Repository::VERSION_FILE_NAME)
  FileUtils.mkdir_p(FIG_HOME)
  File.open(version_file, 'w') {
    |handle| handle.write(Fig::Repository::LOCAL_VERSION_SUPPORTED + 1)
  }

  return
end

#set_remote_repository_format_to_future_versionObject



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/fig/spec_utils.rb', line 304

def set_remote_repository_format_to_future_version()
  # Set future version in download dir
  version_file = File.join(FIG_DOWNLOAD_DIR, Fig::Repository::VERSION_FILE_NAME)
  FileUtils.mkdir_p(FIG_DOWNLOAD_DIR)
  File.open(version_file, 'w') {
    |handle| handle.write(Fig::Repository::REMOTE_VERSION_SUPPORTED + 1)
  }
  
  # Set future version in upload dir
  version_file = File.join(FIG_UPLOAD_DIR, Fig::Repository::VERSION_FILE_NAME)
  FileUtils.mkdir_p(FIG_UPLOAD_DIR)
  File.open(version_file, 'w') {
    |handle| handle.write(Fig::Repository::REMOTE_VERSION_SUPPORTED + 1)
  }

  return
end

#set_up_test_environmentObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/fig/spec_utils.rb', line 242

def set_up_test_environment()
  FileUtils.mkdir_p FIG_SPEC_BASE_DIRECTORY
  FileUtils.mkdir_p CURRENT_DIRECTORY
  FileUtils.mkdir_p USER_HOME
  FileUtils.mkdir_p FIG_HOME
  FileUtils.mkdir_p FIG_UPLOAD_DIR
  FileUtils.ln_s FIG_UPLOAD_DIR, FIG_DOWNLOAD_DIR

  FileUtils.touch FIG_FILE_GUARANTEED_TO_EXIST

   =
    File.join FIG_UPLOAD_DIR, Fig::Repository::METADATA_SUBDIRECTORY
  FileUtils.mkdir_p 

  File.open(
    File.join(FIG_UPLOAD_DIR, Fig::FigRC::REPOSITORY_CONFIGURATION), 'w'
  ) do
    |handle|
    handle.puts '{}' # Empty Javascript/JSON object
  end

  return
end