Class: VagrantPlugins::VagrantRspecCI::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-rspec-ci/command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



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
# File 'lib/vagrant-rspec-ci/command.rb', line 8

def execute
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: vagrant rpsec [vm-name]"
  end

  argv = parse_options(opts)
  return if !argv

  with_target_vms(argv[0]) do |vm|
    # vm.env.action_runner.run(Vagrant::Action::General::Validate, {:vm=>vm, :ui=>vm.ui})

    s = vm.state.short_description

    # TODO - VBox-specific machine state name?
    if s != 'running'
      vm.ui.error("VM not running. Not running tests.")        
    else
      
      # If rpsec isn't explicitly mentioned in the Vagrantfile, the config will get initted but 
      # not finalized.  Need to finalize to get defaults.  Harmless to re-finalize anyway.
      vm.config.rspec.finalize!

      tests = expand_test_list(vm.config.rspec)
      cmd = rspec_command(vm)
      tests.each do |testfile|
        vm.ui.info("Running rspec test: #{testfile}")
        cmd = "#{cmd} #{testfile}"
        env = prep_env(vm)
        #puts("Command: #{cmd}")
        #puts("Environment: #{env.inspect()}")
        system(env, cmd)
        result = $?
        # rspec exits 0 if all passed, 1 if some failed - and system gives nil if there was a problem starting the process
        if result.nil? then
          vm.ui.error "Unable to execute rspec command: #{$!} \n #{cmd}"
        elsif result.exitstatus == 1 then
          vm.ui.warn "Rspec test #{testfile} has at least one failure - see report output for details"
        elsif result.exitstatus == 0 then
          vm.ui.success "Rspec test #{testfile} passed"
        else 
          vm.ui.error "Unrecognized exit code from rspec: #{result.exitstatus}\nfor:#{cmd}"
        end
      end

      if tests.empty?
        vm.ui.info("No rspec tests found.")
      end

    end
  end
end