Class: Wagn::Commands::RspecCommand::Parser

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/wagn/commands/rspec_command/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Parser

Returns a new instance of Parser.



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wagn/commands/rspec_command/parser.rb', line 8

def initialize opts
  super() do |parser|
    parser.banner = "Usage: wagn rspec [WAGN ARGS] -- [RSPEC ARGS]\n\n" \
                    "RSPEC ARGS"
    parser.separator <<-EOT

    WAGN ARGS

    You don't have to give a full path for FILENAME, the basename is enough
      If FILENAME does not include '_spec' rspec searches for the
      corresponding spec file.
      The line number always referes to example in the (corresponding) spec
      file.

    EOT

    parser.on("-d", "--spec FILENAME(:LINE)",
              "Run spec for a Wagn deck file") do |file|
      opts[:files] = find_spec_file(file, "#{Wagn.root}/mod")
    end
    parser.on("-c", "--core-spec FILENAME(:LINE)",
              "Run spec for a Wagn core file") do |file|
      opts[:files] = find_spec_file(file, Cardio.gem_root)
    end
    parser.on("-m", "--mod MODNAME",
              "Run all specs for a mod or matching a mod") do |file|
      opts[:files] =
        if File.exist?("mod/#{file}")
          "#{Cardio.gem_root}/mod/#{file}"
        elsif File.exist?("#{Cardio.gem_root}/mod/#{file}")
          "#{Cardio.gem_root}/mod/#{file}"
        elsif (files = find_spec_file(file, "mod")) && files.present?
          files
        else
          find_spec_file(file, "#{Cardio.gem_root}/mod")
        end
    end
    parser.on("-s", "--[no-]simplecov", "Run with simplecov") do |s|
      opts[:simplecov] = s ? "" : "COVERAGE=false"
    end
    parser.on("--rescue", "Run with pry-rescue") do
      if opts[:executer] == "spring"
        puts "Disabled pry-rescue. Not compatible with spring."
      else
        opts[:rescue] = "rescue "
      end
    end
    parser.on("--[no-]spring", "Run with spring") do |spring|
      if spring
        opts[:executer] = "spring"
        if opts[:rescue]
          opts[:rescue] = ""
          puts "Disabled pry-rescue. Not compatible with spring."
        end
      else
        opts[:executer] = "bundle exec"
      end
    end
    parser.separator "\n"
  end
end

Instance Method Details

#find_spec_file(filename, base_dir) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/wagn/commands/rspec_command/parser.rb', line 70

def find_spec_file filename, base_dir
  file, line = filename.split(":")
  if file.include?("_spec.rb") && File.exist?(file)
    filename
  else
    file = File.basename(file, ".rb").sub(/_spec$/, "")
    Dir.glob("#{base_dir}/**/#{file}_spec.rb").flatten.map do |spec_file|
      line ? "#{spec_file}:#{line}" : file
    end.join(" ")
  end
end