Skip to content

Commit

Permalink
🐛 Validating nested arrays in :flags & :defines
Browse files Browse the repository at this point in the history
YAML anchors and aliases lead to nested arrays in YAML config. Added support for these in `:defines` and `:flags` validation.
  • Loading branch information
mkarlesky committed Sep 17, 2024
1 parent 8d28b33 commit 7711e2b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/ceedling/config_matchinator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def matches?(hash:, filepath:, section:, context:, operation:nil)
end
end

return _values.flatten # Flatten to handle YAML aliases
# Flatten to handle list-nested YAML aliasing (should have already been flattened during validation)
return _values.flatten
end

### Private ###
Expand Down
38 changes: 35 additions & 3 deletions lib/ceedling/configurator_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ def validate_defines(_config)
# Only validate lists of compilation symbols in this block (look for matchers in next block)
next if config.class != Array

# Handle any YAML alias referencing causing a nested array
config.flatten!()

# Ensure each item in list is a string
config.each do |symbol|
if symbol.class != String
Expand Down Expand Up @@ -294,14 +297,27 @@ def validate_defines(_config)
# Skip processing if context isn't present or is present but is not a matcher hash
next if matchers.nil? or matchers.class != Hash

walk = @reportinator.generate_config_walk( [:defines, context] )

# Inspect each test matcher
matchers.each_pair do |matcher, symbols|

walk = @reportinator.generate_config_walk( [:defines, context, matcher] )

# Ensure container associated with matcher is a list
if symbols.class != Array
msg = "#{walk} entry '#{symbols}' is not a list of compilation symbols but a #{symbols.class.to_s.downcase}"
@loginator.log( msg, Verbosity::ERRORS )
valid = false

# Skip further validation if matcher value is not a list of symbols
next
end

# Handle any YAML alias nesting in array
symbols.flatten!()

# Ensure matcher itself is a Ruby symbol or string
if matcher.class != Symbol and matcher.class != String
msg = "#{walk} entry '#{matcher}' is not a string or symbol"
msg = "#{walk} matcher is not a string or symbol"
@loginator.log( msg, Verbosity::ERRORS )
valid = false

Expand Down Expand Up @@ -447,6 +463,9 @@ def validate_flags(_config)
# Only validate lists of flags in this block (look for matchers in next block)
next if flags.class != Array

# Handle any YAML alias referencing causing a nested array
flags.flatten!()

# Ensure each item in list is a string
flags.each do |flag|
if flag.class != String
Expand Down Expand Up @@ -500,6 +519,19 @@ def validate_flags(_config)
end

walk = @reportinator.generate_config_walk( [:flags, :test, operation, matcher] )

# Ensure container associated with matcher is a list
if flags.class != Array
msg = "#{walk} entry '#{flags}' is not a list of command line flags but a #{flags.class.to_s.downcase}"
@loginator.log( msg, Verbosity::ERRORS )
valid = false

# Skip further validation if matcher value is not a list of flags
next
end

# Handle any YAML alias nesting in array
flags.flatten!()

# Ensure each item in flags list for matcher is a string
flags.each do |flag|
Expand Down
3 changes: 2 additions & 1 deletion lib/ceedling/defineinator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def defines(topkey:@topkey, subkey:, filepath:nil, default:[])
defines = @config_matchinator.get_config(primary:topkey, secondary:subkey)

if defines == nil then return default
elsif defines.is_a?(Array) then return defines.flatten # Flatten to handle list-nested YAML aliases
# Flatten to handle list-nested YAML aliasing (should have already been flattened during validation)
elsif defines.is_a?(Array) then return defines.flatten
elsif defines.is_a?(Hash)
arg_hash = {
hash: defines,
Expand Down
3 changes: 2 additions & 1 deletion lib/ceedling/flaginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def flag_down(context:, operation:, filepath:nil, default:[])
flags = @config_matchinator.get_config(primary:@section, secondary:context, tertiary:operation)

if flags == nil then return default
elsif flags.is_a?(Array) then return flags.flatten # Flatten to handle list-nested YAML aliases
# Flatten to handle list-nested YAML aliasing (should have already been flattened during validation)
elsif flags.is_a?(Array) then return flags.flatten
elsif flags.is_a?(Hash)
arg_hash = {
hash: flags,
Expand Down
2 changes: 1 addition & 1 deletion lib/ceedling/tool_executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def build_arguments(tool_name, config, *args)

# Iterate through each argument

# The yaml blob array needs to be flattened so that yaml substitution is handled
# The yaml blob array needs to be flattened so that yaml alias substitution is handled
# correctly as it creates a nested array when an anchor is dereferenced
config.flatten.each do |element|
argument = ''
Expand Down

0 comments on commit 7711e2b

Please sign in to comment.