Class: MonkeyTest::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/monkeytest/cli.rb

Constant Summary collapse

TESTLOADER =

Path to the test loader script

File.dirname(__FILE__) + '/test_loader.rb'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



15
16
17
18
19
# File 'lib/monkeytest/cli.rb', line 15

def initialize
  @options = parse_options
  @cmd = build_cmd
  fling!
end

Class Method Details

.fling!Object



11
12
13
# File 'lib/monkeytest/cli.rb', line 11

def self.fling!
  new
end

Instance Method Details

#build_cmdObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/monkeytest/cli.rb', line 26

def build_cmd
  cmd = "ruby -rubygems -e \"require 'monkeytest'"
  cmd << "; COLOR=#{@options.color}"
  cmd << "; OUTPUT_LEVEL=:#{@options.outputLevel.to_s}"
  cmd << "; OUTPUT_FORMAT=:#{@options.outputFormat.to_s}"
  cmd << "; FILE=#{@options.outputFile}" unless @options.outputFile.nil?
  cmd << "; FILE=nil" if @options.outputFile.nil?
  cmd << "\" \"#{TESTLOADER}\" \"#{@options.testFiles.join('" "')}\" --runner=monkey"
  return cmd
end

#find_files(directory, pattern) ⇒ Object



188
189
190
191
# File 'lib/monkeytest/cli.rb', line 188

def find_files(directory,pattern)
  files = directory.find_all { |f| f =~ pattern and !File.directory?(directory.path + "/" + f) }
  files.collect { |f| directory.path + "/" + f }
end

#find_files_r(directory, pattern) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/monkeytest/cli.rb', line 177

def find_files_r(directory,pattern)
  directories = directory.find_all { |f| !(f =~ /^\./) and File.directory?(directory.path + "/" + f) }
  files = directory.find_all { |f| f =~ pattern and !File.directory?(directory.path + "/" + f) }
  files.collect! { |f| directory.path + "/" + f }
  if directories.empty?
    return files
  else
    return(files.push directories.collect { |dir| find_files_r(Dir.new(directory.path + "/" + dir),pattern) })
  end
end

#find_test_files(options) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/monkeytest/cli.rb', line 162

def find_test_files(options)
  files = []
  if !options.unit.nil? or !options.integration.nil? or !options.functional.nil?
    files.push find_files(Dir.new(options.directory.path + "/unit"), options.pattern) if File.exists?(options.directory.path + "/unit") unless options.unit.nil?
    files.push find_files(Dir.new(options.directory.path + "/functional"), options.pattern) if File.exists?(options.directory.path + "/functional") unless options.functional.nil?
    files.push find_files(Dir.new(options.directory.path + "/integration"), options.pattern) if File.exists?(options.directory.path + "/integration") unless options.integration.nil?
    files.flatten!
  elsif options.recursive
    files = find_files_r(options.directory, options.pattern)
  else
    files = find_files(options.directory, options.pattern)
  end
  return files.flatten
end

#fling!Object



21
22
23
24
# File 'lib/monkeytest/cli.rb', line 21

def fling!
  #puts @cmd
  system @cmd
end

#parse_options(args = ARGV) ⇒ Object



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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/monkeytest/cli.rb', line 37

def parse_options(args=ARGV)
  options = OpenStruct.new
  
  # Setup default options
  options.outputFormat = :console
  options.outputLevel = :normal
  options.color = true
  if File.exists?("./test")
    options.directory = Dir.new("./test")
  else
    options.directory = Dir.new(".")
  end
  options.pattern = /^.+_test\.rb$/
  options.recursive = true
  
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: monkeytest [options]"
    
    # Options for specifying which tests will be run  
    opts.separator ""
    opts.separator "Test selection criteria options:"
     
    opts.on("-d DIRECTORY", "--directory DIRECTORY", "Run tests found in the given directory") { |dir|
      begin
        options.directory = Dir.new(dir)
      rescue Exception => e
        puts e
        exit
      end
    }
    
    opts.on("-d", "--directory DIRECTORY","Search DIRECTORY for tests") { |dir|
      options.directory = Dir.new(dir)
    }
    
    opts.on("-r", "--[no-]recursive", "[Don't] Search directories recursively") { |r|
      options.recursive = r
    }
    
    opts.on("-u", "--[no-]unit", "[Don't] Run unit tests") { |u|
      options.unit = u
    }
    
    opts.on("-f", "--[no-]functional", "[Don't] Run functional tests") { |f|
      options.functional = f
    }
    
    opts.on("-i", "--[no-]integration", "[Don't] Run integration tests") { |i|
      options.integration = i
    }
    
    opts.on("-p PATTERN", "--pattern PATTERN", Regexp, "Run test files which match PATTERN\n#{' '*37}(use quotes for safety!)") { |regex|
      options.pattern = regex
    }
    
    # Options for output
    opts.separator ""
    opts.separator "Output options:"
            
    opts.on("-q", "--quiet", "Output summary of failures and errors only") { |q|
      if options.outputLevel == :verbose
        puts "Only one of --quiet or --verbose is allowed."
        exit!(1)
      end
      options.outputLevel = :quiet
    }
    
    opts.on("-v", "--verbose", "Output full fault messages") { |s|
      if options.outputLevel == :quiet
        puts "Only one of --quiet or --verbose is allowed."
        exit!(1)
      end
      options.outputLevel = :verbose
    }
    
    opts.on("--[no-]color", "[Don't] Show colored output") { |c|
      options.color = c
    }

    #opts.on("--html", "--html [FILE]", "Output results in HTML\n#{' '*37}(optionally to the given file)") { |html|
    #  options.outputFormat = :html
    #  options.outputFile = html
    #}
    
    #opts.on("--rss", "--rss [FILE]", "Output results in RSS\n#{' '*37}(optionally to the given file)") { |rss|
    #  options.outputFormat = :rss
    #  options.outputFile = rss
    #}
    
    # Other options
    opts.separator ""
    opts.separator "Other options:"
    opts.on("-h", "--help", "Displays this help message") {
      puts opts
      exit!(0)
    }
    
    opts.on("-V", "--version",
      "Display monkey test version info"
    ) do
      require 'monkeytest/version'
      puts "Monkey Test v#{MonkeyTest::Version::STRING}"
      exit!(0)
    end
  end
  
  begin
    opts.parse!(args)
  rescue OptionParser::MissingArgument => e
    puts "Option #{e.to_s.match(/\-[\w]+/).to_s} is missing a required argument."
    exit!(1)
  rescue Exception => e
    puts e
    exit!(1)
  end
  
  options.testFiles = find_test_files(options)
  if options.testFiles.empty?
    puts "No test files were found with the given criteria."
    exit!(0)
  end

  return options
end