Module: RunfileTasks::Testing

Extended by:
Testing
Included in:
Testing
Defined in:
lib/runfile-tasks/testing/rspec.rb,
lib/runfile-tasks/testing/cucumber.rb,
lib/runfile-tasks/testing/minitest.rb

Instance Method Summary collapse

Instance Method Details

#cucumberObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/runfile-tasks/testing/cucumber.rb', line 8

def cucumber
  usage  "(feature|features) [TAG_OR_FILE --list --fast]"
  help   "Run cucumber feature tests. Optionally, specify a tag or a filename to run. Tags should be prefixed with @."
  option "--list", "Show list of available features"
  option "--fast", "Abort on first failure"
  action :feature, :features do |args|
    if args['--list']
      show_cucumber_features
    else
      run_cucumber_features args['TAG_OR_FILE'], args['--fast']
    end
  end
end

#cucumber_stepdefs(filename = 'stepdefs.md') ⇒ Object



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
# File 'lib/runfile-tasks/testing/cucumber.rb', line 22

def cucumber_stepdefs(filename='stepdefs.md')
  usage  "stepdefs"
  help   "Generate step definitions markdown document.\n" +
         "Comments in the step definition file that start with two or " +
         "more # characters, will also be added to the output " +
         "document, as captions."
  action :stepdefs do
    step = /^(Given|When|Then)\((\/.*\/)\) do.*$/
    caption = /^(\#{2,6} .*)$/
    files = Dir['features/step_definitions/**/*.rb']
    doc = []
    doc << "# Cucumber Step Definitions Summary\n"
    doc << "Generated by `run stepdefs`."
    files.each do |file|
      doc << "\n## #{File.basename(file, '.rb')}\n"
      File.readlines(file).each do |line|
        if matches = step.match(line)
          clause = matches.captures[0]
          definition = matches.captures[1] # .gsub /(".*?")/, '`__\1__`'
          doc << "- __`#{clause}`__ `#{definition}`"
        end
        if matches = caption.match(line)
          title = matches.captures[0]
          doc << "\n#{title}\n"
        end
      end
    end
    doc = doc.join "\n"
    File.write filename, doc
    puts "Generated #{filename}"
  end
end

#minitest(pattern = "./test/*_test.rb") ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/runfile-tasks/testing/minitest.rb', line 8

def minitest(pattern="./test/*_test.rb")
  usage  "test [NAME]"
  help   "Run all tests or a single test file."
  action :test do |args|
    if args['NAME'] 
      file = pattern.sub "*", args['NAME']
      puts "g`Using:` p`#{file}`".in_color
      require file
    else
      Dir[pattern].each do |file| 
        puts "g`Using:` p`#{file}`".in_color
        require file
      end
    end
  end
end

#rspec(opts = {}) ⇒ Object



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
# File 'lib/runfile-tasks/testing/rspec.rb', line 8

def rspec(opts={})
  opts = { action: opts } if opts.is_a? String
  
  opts = {
    action:  'spec', 
    pattern: './spec/**/*_spec.rb',
    command: 'rspec'
  }.merge opts

  usage  opts[:action] == :global ? "[NAME] [TAG]" : "#{opts[:action]} [NAME] [TAG]"
  help   "Run all specs or a single spec file matching a regex. You can provide a tag to run only specific tests. If you wish to provide a tag only, without a file pattern, simply prefix the tag with a colon, like 'run spec :focus'"
  action opts[:action].to_sym do |args|
    file = args['NAME'] 
    tag  = args['TAG'] 

    if file and file[0] == ':'
      tag = file
      file = nil
    end

    if tag and tag[0] == ':'
      tag = tag[1..-1]
    end

    if file
      files = Dir[opts[:pattern]]  
      files.select! { |f| f =~ /#{file}/i }
      abort "Cannot find a matching spec file with #{opts[:pattern]}" if files.empty?
      file = files.first
      cmd = "#{opts[:command]} \"#{file}\""
    else
      cmd = "#{opts[:command]}"
    end
    cmd = "#{cmd} --tag #{tag}" if tag
    puts "g`Running:` p`#{cmd}`".in_color
    exec cmd
  end
end

#run_cucumber_features(tag_or_file, fast = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/runfile-tasks/testing/cucumber.rb', line 62

def run_cucumber_features(tag_or_file, fast=false)
  cmd = "cucumber"
  if tag_or_file
    if tag_or_file[0] == '@' 
      puts "g`Running features tagged #{tag_or_file}`".in_color
      cmd = "#{cmd} --tags #{tag_or_file}"
    else
      puts "g`Running #{tag_or_file} features`".in_color
      cmd = "#{cmd} 'features/#{tag_or_file}.feature'"
    end
  end
  cmd = "#{cmd} --fail-fast" if fast
  exec cmd
end

#show_cucumber_featuresObject



55
56
57
58
59
60
# File 'lib/runfile-tasks/testing/cucumber.rb', line 55

def show_cucumber_features
  puts "g`Available Features:`".in_color
  Dir['features/**/*.feature'].each do |file|
    puts "- " + File.basename("#{file}", '.feature')
  end
end