summaryrefslogtreecommitdiff
path: root/spec/mspec/lib/mspec/commands/mspec-tag.rb
blob: 8bc3382e91d63cebfbe8590aaa45047a96998188 (plain)
1
2
3
4
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
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
#!/usr/bin/env ruby

require 'mspec/version'
require 'mspec/utils/options'
require 'mspec/utils/script'


class MSpecTag < MSpecScript
  def initialize
    super

    config[:tagger]  = :add
    config[:tag]     = 'fails:'
    config[:outcome] = :fail
    config[:ltags]   = []
  end

  def options(argv=ARGV)
    options = MSpecOptions.new "mspec tag [options] (FILE|DIRECTORY|GLOB)+", 30, config

    options.doc " Ask yourself:"
    options.doc "  1. What specs to run?"
    options.doc "  2. How to modify the execution?"
    options.doc "  3. How to display the output?"
    options.doc "  4. What tag action to perform?"
    options.doc "  5. When to perform it?"

    options.doc "\n What specs to run"
    options.filters

    options.doc "\n How to modify the execution"
    options.configure { |f| load f }
    options.pretend
    options.unguarded
    options.interrupt

    options.doc "\n How to display their output"
    options.formatters
    options.verbose

    options.doc "\n What action to perform and when to perform it"
    options.on("-N", "--add", "TAG",
       "Add TAG with format 'tag' or 'tag(comment)' (see -Q, -F, -L)") do |o|
      config[:tagger] = :add
      config[:tag] = "#{o}:"
    end
    options.on("-R", "--del", "TAG",
       "Delete TAG (see -Q, -F, -L)") do |o|
      config[:tagger] = :del
      config[:tag] = "#{o}:"
      config[:outcome] = :pass
    end
    options.on("-Q", "--pass", "Apply action to specs that pass (default for --del)") do
      config[:outcome] = :pass
    end
    options.on("-F", "--fail", "Apply action to specs that fail (default for --add)") do
      config[:outcome] = :fail
    end
    options.on("-L", "--all", "Apply action to all specs") do
      config[:outcome] = :all
    end
    options.on("--list", "TAG", "Display descriptions of any specs tagged with TAG") do |t|
      config[:tagger] = :list
      config[:ltags] << t
    end
    options.on("--list-all", "Display descriptions of any tagged specs") do
      config[:tagger] = :list_all
    end
    options.on("--purge", "Remove all tags not matching any specs") do
      config[:tagger] = :purge
    end

    options.doc "\n Help!"
    options.debug
    options.version MSpec::VERSION
    options.help

    options.doc "\n Custom options"
    custom_options options

    options.doc "\n How might this work in the real world?"
    options.doc "\n   1. To add the 'fails' tag to failing specs"
    options.doc "\n     $ mspec tag path/to/the_file_spec.rb"
    options.doc "\n   2. To remove the 'fails' tag from passing specs"
    options.doc "\n     $ mspec tag --del fails path/to/the_file_spec.rb"
    options.doc "\n   3. To display the descriptions for all specs tagged with 'fails'"
    options.doc "\n     $ mspec tag --list fails path/to/the/specs"
    options.doc ""

    patterns = options.parse argv
    if patterns.empty?
      puts options
      puts "No files specified."
      exit 1
    end
    @files = files patterns
  end

  def register
    require 'mspec/runner/actions'

    case config[:tagger]
    when :add, :del
      tag = SpecTag.new config[:tag]
      tagger = TagAction.new(config[:tagger], config[:outcome], tag.tag, tag.comment,
                             config[:atags], config[:astrings])
    when :list, :list_all
      tagger = TagListAction.new config[:tagger] == :list_all ? nil : config[:ltags]
      MSpec.register_mode :pretend
      config[:formatter] = false
    when :purge
      tagger = TagPurgeAction.new
      MSpec.register_mode :pretend
      MSpec.register_mode :unguarded
      config[:formatter] = false
    else
      raise ArgumentError, "No recognized action given"
    end
    tagger.register

    super
  end

  def run
    MSpec.register_tags_patterns config[:tags_patterns]
    MSpec.register_files @files

    MSpec.process
    exit MSpec.exit_code
  end
end