Module: CmdLineTest::Macros

Included in:
Test::Unit::TestCase
Defined in:
lib/cmd_line_test.rb

Overview

InstanceMethods

Instance Method Summary collapse

Instance Method Details

#run_and_fail(*args, &block) ⇒ Object



84
85
86
87
88
89
# File 'lib/cmd_line_test.rb', line 84

def run_and_fail(*args, &block)
  run_with_argv(*args) do
    assert_failed_run
    instance_eval(&block) if block
  end
end

#run_and_succeed(*args, &block) ⇒ Object



77
78
79
80
81
82
# File 'lib/cmd_line_test.rb', line 77

def run_and_succeed(*args, &block)
  run_with_argv(*args) do
    assert_successful_run
    instance_eval(&block) if block
  end
end

#run_with_argv(*args, &block) ⇒ Object

Raises:

  • (RuntimeError)


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/cmd_line_test.rb', line 39

def run_with_argv(*args, &block)
  raise RuntimeError, "Command is not defined: 'run_command_line_as()' must be called first." unless 
    self.cli_block
  options = args.last.is_a?(Hash) ? args.pop : {}
  argv = args
  test_name = options[:name] || "after running with arguments '#{argv.join(', ')}'"
  generate_test test_name do 
    with_ARGV_set_to *argv do
      begin 
        self.class.cli_exit_status = 0
        self.class.cli_error = ""
        self.class.cli_error_stack = ""
        self.class.cli_output = ""
        $test_class = self.class
        #It used to be $stdout = StringIO.new(...), but it does not work well with rdebug
        class <<$stdout
          alias_method :original_write, :write
          def write(*s)
            original_write(s) if $test_class.show_output 
            $test_class.cli_output << s.to_s if $test_class.cli_output
          end
        end
        execute
      rescue SystemExit => exit_error
        self.class.cli_exit_status = exit_error.status
      rescue Exception => error
        self.class.cli_error_stack = error.backtrace.join("\n")
        self.class.cli_error = error.message
      ensure
        class <<$stdout
          remove_method :write, :original_write
        end
      end
      instance_eval(&block)
    end
  end
end