Support tag filtering with value

This commit is contained in:
Michael Miller 2021-08-17 21:34:26 -06:00
parent b79dd4361e
commit a9a46c76ad
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
2 changed files with 9 additions and 4 deletions

View file

@ -139,11 +139,16 @@ module Spectator
# Adds the tag filter option to the parser.
private def tag_option(parser, builder)
parser.on("--tag TAG", "run examples with the specified TAG, or exclude examples by adding ~ before the TAG.") do |tag|
parser.on("--tag TAG[:VALUE]", "Run examples with the specified TAG, or exclude examples by adding ~ before the TAG.") do |tag|
negated = tag.starts_with?('~')
tag = tag.lchop('~')
Log.debug { "Filtering for example with tag #{tag}" }
filter = TagNodeFilter.new(tag)
parts = tag.split(':', 2, remove_empty: true)
if parts.size > 1
tag = parts.first
value = parts.last
end
filter = TagNodeFilter.new(tag, value)
builder.add_node_filter(filter)
end
end

View file

@ -5,12 +5,12 @@ module Spectator
class TagNodeFilter < NodeFilter
# Creates the filter.
# The *tag* indicates which tag the node must have in its metadata.
def initialize(@tag : String)
def initialize(@tag : String, @value : String? = nil)
end
# Checks whether the node satisfies the filter.
def includes?(node) : Bool
node.metadata.each_key.any? { |key| key.to_s == @tag }
node.metadata.any? { |key, value| key.to_s == @tag && (!@value || value == @value) }
end
end
end