Skip to content

Commit

Permalink
Added mspec tag --list TAG and --list-all.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ford committed Aug 6, 2008
1 parent d8c753c commit 8ca2838
Show file tree
Hide file tree
Showing 17 changed files with 509 additions and 162 deletions.
48 changes: 42 additions & 6 deletions lib/mspec/commands/mspec-tag.rb
Expand Up @@ -12,6 +12,7 @@ def initialize
config[:tagger] = :add
config[:tag] = 'fails:'
config[:outcome] = :fail
config[:ltags] = []
end

def options(argv=ARGV)
Expand All @@ -38,7 +39,33 @@ def options(argv=ARGV)
options.verbose

options.doc "\n What action to perform and when to perform it"
options.tagging
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.doc "\n Help!"
options.version MSpec::VERSION
Expand All @@ -60,12 +87,21 @@ def options(argv=ARGV)
end

def register
super

tag = SpecTag.new config[:tag]
tagger = TagAction.new(config[:tagger], config[:outcome], tag.tag, tag.comment,
config[:atags], config[:astrings])
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] = nil
else
raise ArgumentError, "No recognized action given"
end
tagger.register

super
end

def run
Expand Down
1 change: 1 addition & 0 deletions lib/mspec/runner/actions.rb
Expand Up @@ -2,5 +2,6 @@
require 'mspec/runner/actions/timer'
require 'mspec/runner/actions/filter'
require 'mspec/runner/actions/tag'
require 'mspec/runner/actions/taglist'
require 'mspec/runner/actions/debug'
require 'mspec/runner/actions/gdb'
2 changes: 1 addition & 1 deletion lib/mspec/runner/actions/filter.rb
Expand Up @@ -24,7 +24,7 @@ def load
@tfilter = nil
return if @tags.empty?

desc = MSpec.read_tags(*@tags).map { |t| t.description }
desc = MSpec.read_tags(@tags).map { |t| t.description }
return if desc.empty?

@tfilter = MatchFilter.new(nil, *desc)
Expand Down
56 changes: 56 additions & 0 deletions lib/mspec/runner/actions/taglist.rb
@@ -0,0 +1,56 @@
require 'mspec/runner/actions/filter'

# TagListAction - prints out the descriptions for any specs
# tagged with +tags+. If +tags+ is an empty list, prints out
# descriptions for any specs that are tagged.
class TagListAction
def initialize(tags=nil)
@tags = tags.nil? || tags.empty? ? nil : Array(tags)
@filter = nil
end

# Returns true. This enables us to match any tag when loading
# tags from the file.
def include?(arg)
true
end

# Returns true if any tagged descriptions matches +string+.
def ===(string)
@filter === string
end

# Prints a banner about matching tagged specs.
def start
if @tags
print "\nListing specs tagged with #{@tags.map { |t| "'#{t}'" }.join(", ") }\n\n"
else
print "\nListing all tagged specs\n\n"
end
end

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

# Prints the spec description if it matches the filter.
def after(state)
return unless self === state.description
print state.description, "\n"
end

def register
MSpec.register :start, self
MSpec.register :load, self
MSpec.register :after, self
end

def unregister
MSpec.unregister :start, self
MSpec.unregister :load, self
MSpec.unregister :after, self
end
end
2 changes: 1 addition & 1 deletion lib/mspec/runner/filters/tag.rb
Expand Up @@ -7,7 +7,7 @@ def initialize(what, *tags)
end

def load
desc = MSpec.read_tags(*@tags).map { |t| t.description }
desc = MSpec.read_tags(@tags).map { |t| t.description }

@filter = MatchFilter.new(@what, *desc)
@filter.register
Expand Down
4 changes: 3 additions & 1 deletion lib/mspec/runner/mspec.rb
Expand Up @@ -237,7 +237,9 @@ def self.tags_file
end
end

def self.read_tags(*keys)
# Returns a list of tags matching any tag string in +keys+ based
# on the return value of <tt>keys.include?("tag_name")</tt>
def self.read_tags(keys)
tags = []
file = tags_file
if File.exist? file
Expand Down
23 changes: 0 additions & 23 deletions lib/mspec/utils/options.rb
Expand Up @@ -369,29 +369,6 @@ def verify
end
end

def tagging
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
on("-R", "--del", "TAG",
"Delete TAG (see -Q, -F, -L)") do |o|
config[:tagger] = :del
config[:tag] = "#{o}:"
config[:outcome] = :pass
end
on("-Q", "--pass", "Apply action to specs that pass (default for --del)") do
config[:outcome] = :pass
end
on("-F", "--fail", "Apply action to specs that fail (default for --add)") do
config[:outcome] = :fail
end
on("-L", "--all", "Apply action to all specs") do
config[:outcome] = :all
end
end

def action_filters
on("-K", "--action-tag", "TAG",
"Spec descriptions marked with TAG will trigger the specified action") do |o|
Expand Down
3 changes: 2 additions & 1 deletion lib/mspec/utils/script.rb
Expand Up @@ -27,6 +27,7 @@ def initialize
config[:xprofiles] = []
config[:atags] = []
config[:astrings] = []
config[:ltags] = []
config[:abort] = true
end

Expand All @@ -51,7 +52,7 @@ def load(name)
end

def register
config[:formatter].new(config[:output]).register
config[:formatter].new(config[:output]).register if config[:formatter]

MatchFilter.new(:include, *config[:includes]).register unless config[:includes].empty?
MatchFilter.new(:exclude, *config[:excludes]).register unless config[:excludes].empty?
Expand Down

0 comments on commit 8ca2838

Please sign in to comment.