class MIPSUnit::MSpec::Runner

runs mspec: Parses the command line and executes the desired commands

Author

Zachary Kurmas

Copyright

Copyright © 2012

Constants

ASSEMBLY_FILE_MISSING
BAD_FILE
BAD_USAGE

value returned if program is not given enough / correct command-line arguments

INVALID_SPEC_FILE

value returned when the spec file parses, but running the test generates an exception

META_OUTPUT

result of running –version, –help, etc.

NAME

Name used in the “usage” output

UNPARSABLE_SPEC_FILE

value returned when the input file is not valid Ruby code

Public Class Methods

process_file(file_name) click to toggle source
# File lib/mipsunit/mspec/runner.rb, line 58
def self.process_file(file_name)
  if file_name == '-'
    run_and_rescue { eval($stdin.read) }
  else
    if !File.exist?(file_name) then
      quit("File \"#{file_name}\" does not exist.", BAD_FILE)
    end

    if !File.readable?(file_name) then
      quit("File \"#{file_name}\" is not readable.", BAD_FILE)
    end

    if !File.file?(file_name) then
      quit("File \"#{file_name}\" is not a regular file.", BAD_FILE)
    end

    run_and_rescue { load(file_name) }
  end

  run_and_rescue do
    io = StringIO.new
    Generator.generate(MIPSUnit::MSpec.global.groups, io)
    $stdout.puts io.string
  end

  #begin
  #  # The StringIO object allows us to defer printing anything to stdout
  #  # until we are sure that there aren't any problems with the spec.
  #  # (In other words, we don't want the output stream to print part of a test
  #  # file, only to bail when an exception gets raised.)
  #  io = StringIO.new
  #  Generator.generate(MIPSUnit::MSpec.global.groups, io)
  #  $stdout.puts io.string
  #rescue InvalidSpecException => e
  #  $stderr.puts "Invalid spec file."
  #  $stderr.puts e
  #  exit INVALID_SPEC_FILE
  #end
end
quit(message, value) click to toggle source
# File lib/mipsunit/mspec/runner.rb, line 36
def self.quit(message, value)
  $stderr.puts(message)
  exit_wrapper(value)
end
run(args) click to toggle source
# File lib/mipsunit/mspec/runner.rb, line 100
def self.run(args)
  if args.include?("--version")
    quit("MIPSUnit::MSpec version #{MIPSUnit::MSpec::VERSION}", META_OUTPUT)
  elsif (args.size < 1)
    usage
  else
    file_name = args[0]
    process_file(file_name)
  end
end
run_and_rescue() { || ... } click to toggle source
# File lib/mipsunit/mspec/runner.rb, line 45
def self.run_and_rescue
  begin
    yield
  rescue InvalidSpecException => e
    $stderr.puts "Invalid spec file."
    $stderr.puts e.message
    # prints the backtrace so users can see the offending line of their file
    $stderr.puts
    $stderr.puts e.backtrace
    exit_wrapper INVALID_SPEC_FILE
  end
end
usage() click to toggle source
# File lib/mipsunit/mspec/runner.rb, line 41
def self.usage
  quit("Usage: #{NAME} filename", BAD_USAGE)
end