Class: RSpec::Pipeline::FixtureLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/pipeline/fixture_loader.rb

Overview

This class configures and loads fixtures. It this moment, the only fixtures we have are repositories usually utilized for remote templates.

Class Method Summary collapse

Class Method Details

.git_pull_and_checkout(repo_definition, dest) ⇒ Object



73
74
75
76
77
78
# File 'lib/rspec/pipeline/fixture_loader.rb', line 73

def self.git_pull_and_checkout(repo_definition, dest)
  `git -C #{dest} pull`

  ref_to_checkout = (repo_definition[:ref] || 'main')
  `git -C #{dest} checkout #{ref_to_checkout}`
end

.load(verbose: true) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rspec/pipeline/fixture_loader.rb', line 9

def self.load(verbose: true)
  ['spec', File.join('spec', 'fixtures')].each { |dir| safe_mkdir(dir, verbose: verbose) }

  RSpec.configuration.fixtures_repos.each do |repo_definition|
    if repo_definition[:path]
      safe_cp_r(repo_definition[:path], File.join('spec', 'fixtures', repo_definition[:name]), verbose: verbose)
    elsif repo_definition[:repo]
      safe_git_clone(repo_definition, verbose: verbose)
    else
      warn "!! invalid fixture repository definition: #{repo_definition}"
    end
  end
end

.safe_cp_r(src, dest, verbose: true) ⇒ Object

rubocop:disable Metrics/MethodLength



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rspec/pipeline/fixture_loader.rb', line 37

def self.safe_cp_r(src, dest, verbose: true) # rubocop:disable Metrics/MethodLength
  if File.exist? dest
    warn "!! #{dest} already exists and is not a directory" unless File.directory? dest
  elsif !File.exist? src
    warn "!! #{src} not found"
  else
    begin
      FileUtils.cp_r src, dest
    rescue Errno::EEXIST => e
      raise e unless File.directory? dest
    end

    puts " + #{dest}/" if verbose
  end
end

.safe_git_clone(repo_definition, verbose: true) ⇒ Object

rubocop:disable Metrics/MethodLength



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rspec/pipeline/fixture_loader.rb', line 53

def self.safe_git_clone(repo_definition, verbose: true) # rubocop:disable Metrics/MethodLength
  dest = File.join('spec', 'fixtures', repo_definition[:name])

  if File.exist? dest
    if File.directory? dest
      if File.exist? "#{dest}/.git"
        git_pull_and_checkout(repo_definition, dest)
      else
        warn "!! #{dest} already exists and is not a git repo"
      end
    else
      warn "!! #{dest} already exists and is not a directory"
    end
  else
    `git clone #{repo_definition[:repo]} #{dest}`

    puts " + #{dest}/" if verbose
  end
end

.safe_mkdir(dir, verbose: true) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rspec/pipeline/fixture_loader.rb', line 23

def self.safe_mkdir(dir, verbose: true)
  if File.exist? dir
    warn "!! #{dir} already exists and is not a directory" unless File.directory? dir
  else
    begin
      FileUtils.mkdir dir
    rescue Errno::EEXIST => e
      raise e unless File.directory? dir
    end

    puts " + #{dir}/" if verbose
  end
end