aboutsummaryrefslogtreecommitdiffstats
path: root/spec/mspec/lib/mspec/runner/actions/tagpurge.rb
blob: f4587de6bce68549bb517b38c86c2b546a12647e (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
require 'mspec/runner/actions/filter'
require 'mspec/runner/actions/taglist'

# TagPurgeAction - removes all tags not matching any spec
# descriptions.
class TagPurgeAction < TagListAction
  attr_reader :matching

  def initialize
    @matching = []
    @filter   = nil
    @tags     = nil
  end

  # Prints a banner about purging tags.
  def start
    print "\nRemoving tags not matching any specs\n\n"
  end

  # Creates a MatchFilter for all tags.
  def load
    @filter = nil
    @tags = MSpec.read_tags self
    desc = @tags.map { |t| t.description }
    @filter = MatchFilter.new(nil, *desc) unless desc.empty?
  end

  # Saves any matching tags
  def after(state)
    @matching << state.description if self === state.description
  end

  # Rewrites any matching tags. Prints non-matching tags.
  # Deletes the tag file if there were no tags (this cleans
  # up empty or malformed tag files).
  def unload
    if @filter
      matched = @tags.select { |t| @matching.any? { |s| s == t.description } }
      MSpec.write_tags matched

      (@tags - matched).each { |t| print t.description, "\n" }
    else
      MSpec.delete_tags
    end
  end

  def register
    super
    MSpec.register :unload, self
  end

  def unregister
    super
    MSpec.unregister :unload, self
  end
end