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



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

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



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

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
    say "Generated #{filename}"  
  end
end

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



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

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']
      say "!txtgrn!Using: !txtpur!#{file}"
      require file
    else
      Dir[pattern].each do |file| 
        say "!txtgrn!Using: !txtpur!#{file}"
        require file
      end
    end
  end
end

#rspec(opts = {}) ⇒ Object



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

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]} [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
    say "!txtgrn!Running: !txtpur!#{cmd}"
    exec cmd
  end
end

#run_cucumber_features(tag_or_file, fast = false) ⇒ Object



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

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

#show_cucumber_featuresObject



52
53
54
55
56
57
# File 'lib/runfile-tasks/testing/cucumber.rb', line 52

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