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.



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

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.



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

def config
  @config
end

#repoObject (readonly)

Returns the value of attribute repo.



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

def repo
  @repo
end

Instance Method Details

#prepare!Object



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

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 our entire spec directory over
  FileUtils.cp_r("#{@repo.spec_dir}", "#{@repo.tempdir}")

  # 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)

  # TODO: Remove all tests that do not match set tags

  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



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/onceover/runner.rb', line 111

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 = Backticks::Runner.new(interactive:true).run(@command_prefix.strip.split, 'rake', 'acceptance').join
  end

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

#run_spec!Object



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

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

    #`bundle install --binstubs`
    #`bin/rake spec_standalone`
    if @config.opts[:parallel]
      logger.debug "Running #{@command_prefix}rake parallel_spec from #{@repo.tempdir}"
      result = Backticks::Runner.new(interactive:true).run(@command_prefix.strip.split, 'rake', 'parallel_spec').join
    else
      logger.debug "Running #{@command_prefix}rake spec_standalone from #{@repo.tempdir}"
      result = Backticks::Runner.new(interactive:true).run(@command_prefix.strip.split, 'rake', 'spec_standalone').join
    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