Class: Onceover::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/onceover/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, config, mode = [:spec, :acceptance]) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
# File 'lib/onceover/runner.rb', line 9

def initialize(repo, config, mode = [:spec, :acceptance])
  @repo   = repo
  @config = config
  @mode   = [mode].flatten
  @command_prefix = ENV['BUNDLE_GEMFILE'] ? 'bundle exec ' : ''
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/onceover/runner.rb', line 7

def config
  @config
end

#repoObject (readonly)

Returns the value of attribute repo.



6
7
8
# File 'lib/onceover/runner.rb', line 6

def repo
  @repo
end

Instance Method Details

#prepare!Object



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
# File 'lib/onceover/runner.rb', line 16

def prepare!
  # Remove the entire spec directory to make sure we have
  # all the latest tests
  FileUtils.rm_rf("#{@repo.tempdir}/spec")

  # Remove any previous failure log
  FileUtils.rm_f("#{@repo.tempdir}/failures.out")

  # Create the other directories we need
  FileUtils.mkdir_p("#{@repo.tempdir}/spec/classes")
  FileUtils.mkdir_p("#{@repo.tempdir}/spec/acceptance/nodesets")

  # Copy specified spec files over
  @config.copy_spec_files(@repo)

  # Create the Rakefile so that we can take advantage of the existing tasks
  @config.write_rakefile(@repo.tempdir, "spec/classes/**/*_spec.rb")

  # Create spec_helper.rb
  @config.write_spec_helper("#{@repo.tempdir}/spec", @repo)

  # Create spec_helper_accpetance.rb
  @config.write_spec_helper_acceptance("#{@repo.tempdir}/spec", @repo)

  if @mode.include?(:spec)
    # Verify all of the spec tests
    @config.spec_tests.each { |test| @config.verify_spec_test(@repo, test) }

    # Deduplicate and write the tests (Spec and Acceptance)
    @config.run_filters(Onceover::Test.deduplicate(@config.spec_tests)).each do |test|
      @config.write_spec_test("#{@repo.tempdir}/spec/classes", test)
    end
  end

  if @mode.include?(:acceptance)
    # Verify all of the acceptance tests
    @config.acceptance_tests.each { |test| @config.verify_acceptance_test(@repo, test) }

    # Write them out
    @config.write_acceptance_tests(
      "#{@repo.tempdir}/spec/acceptance",
      @config.run_filters(Onceover::Test.deduplicate(@config.acceptance_tests))
    )
  end

  # Parse the current hiera config, modify, and write it to the temp dir
  unless @repo.hiera_config == nil
    hiera_config = @repo.hiera_config
    hiera_config.each do |setting, value|
      if value.is_a?(Hash)
        if value.has_key?(:datadir)
          hiera_config[setting][:datadir] = "#{@repo.tempdir}/#{@repo.environmentpath}/production/#{value[:datadir]}"
        end
      end
    end
    File.write("#{@repo.tempdir}/#{@repo.environmentpath}/production/hiera.yaml", hiera_config.to_yaml)
  end

  @config.create_fixtures_symlinks(@repo)
end

#run_acceptance!Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/onceover/runner.rb', line 116

def run_acceptance!
  warn "[DEPRECATION] #{__method__} is deprecated due to the removal of Beaker"

  Dir.chdir(@repo.tempdir) do
    #`bundle install --binstubs`
    #`bin/rake spec_standalone`
    logger.debug "Running #{@command_prefix}rake acceptance from #{@repo.tempdir}"
    result = run_command(@command_prefix.strip.split, 'rake', 'acceptance')
  end

  # Finally exit and preserve the exit code
  exit result.status.exitstatus
end

#run_command(*args) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/onceover/runner.rb', line 130

def run_command(*args)
  begin
    STDERR.raw! if STDERR.isatty
    result = Backticks::Runner.new(interactive: true).run(args.flatten).join
  ensure
    STDERR.cooked! if STDERR.isatty
  end
end

#run_spec!Object



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
# File 'lib/onceover/runner.rb', line 77

def run_spec!
  Dir.chdir(@repo.tempdir) do
    # Disable warnings unless we are running in debug mode
    unless logger.level.zero?
      previous_rubyopt = ENV.fetch('RUBYOPT', nil)
      ENV['RUBYOPT']   = ENV['RUBYOPT'].to_s + ' -W0'
    end

    # NOTE: This is the way to provide options to rspec according to:
    # https://github.com/puppetlabs/puppetlabs_spec_helper/blob/master/lib/puppetlabs_spec_helper/rake_tasks.rb#L51
    ENV['CI_SPEC_OPTIONS'] = ENV['CI_SPEC_OPTIONS'].to_s + @config.filter_tags.map { |tag| " --tag #{tag}" }.join unless @config.filter_tags.nil?
    ENV['CI_SPEC_OPTIONS'] = ENV['CI_SPEC_OPTIONS'].to_s + ' --fail-fast' if @config.fail_fast

    if @config.opts[:parallel]
      logger.debug "Running #{@command_prefix}rake parallel_spec from #{@repo.tempdir}"
      result = run_command(@command_prefix.strip.split, 'rake', 'parallel_spec')
    else
      require 'io/console'
      logger.debug "Running #{@command_prefix}rake spec_standalone from #{@repo.tempdir}"
      result = run_command(@command_prefix.strip.split, 'rake', 'spec_standalone')
    end

    # Reset env to previous state if we modified it
    unless logger.level.zero?
      ENV['RUBYOPT'] = previous_rubyopt
    end

    # Print a summary if we were running in parallel
    if @config.formatters.include? 'OnceoverFormatterParallel'
      require 'onceover/rspec/formatters'
      formatter = OnceoverFormatterParallel.new(STDOUT)
      formatter.output_results("#{repo.tempdir}/parallel")
    end

    # Finally exit and preserve the exit code
    exit result.status.exitstatus
  end
end