Class: ConfiguratorValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/ceedling/configurator_validator.rb

Instance Method Summary collapse

Instance Method Details

#exists?(config, *keys) ⇒ Boolean

Walk into config hash verify existence of data at key depth

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ceedling/configurator_validator.rb', line 19

def exists?(config, *keys)
  hash, _  = @config_walkinator.fetch_value( *keys, hash:config )
  exist = !hash.nil?

  if (not exist)
    walk = @reportinator.generate_config_walk( keys )
    @loginator.log( "Required config file entry #{walk} does not exist.", Verbosity::ERRORS )    
  end
  
  return exist
end

#validate_filepath_simple(path, *keys) ⇒ Object

Simple path verification



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ceedling/configurator_validator.rb', line 147

def validate_filepath_simple(path, *keys)
  validate_path = path
  
  if (not @file_wrapper.exist?(validate_path))
    walk = @reportinator.generate_config_walk( keys, keys.size )
    @loginator.log("Config path '#{validate_path}' associated with #{walk} does not exist in the filesystem.", Verbosity::ERRORS ) 
    return false
  end 
  
  return true
end

#validate_files_entries(config, key) ⇒ Object

Validate :files entries, exercising each entry as FileList glob



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ceedling/configurator_validator.rb', line 110

def validate_files_entries(config, key)
  valid = true
  keys = [:files, key]
  walk = @reportinator.generate_config_walk( keys )

  list, _ = @config_walkinator.fetch_value( *keys, hash:config )

  # Return early if we couldn't walk into hash and find a value
  return false if (list.nil?)
  
  list.each do |path|
    # Trim add/subtract notation
    _path = FilePathUtils::no_aggregation_decorators( path )

    if @file_wrapper.exist?( _path ) and @file_wrapper.directory?( _path )
      # Path is a simple directory path (and is naturally ignored by FileList without a glob pattern)
      warning = "#{walk} => '#{_path}' is a directory path and will be ignored (FYI :files is file-oriented while :paths is directory-oriented)"
      @loginator.log( warning, Verbosity::COMPLAIN )

      next # Skip to next path
    end      

    filelist = @file_wrapper.instantiate_file_list(_path)

    # If file list is empty, complain
    if (filelist.size == 0)
      error = "#{walk} => '#{_path}' yielded no files -- matching glob is malformed or files do not exist"
      @loginator.log( error, Verbosity::ERRORS ) 
      valid = false
    end 
  end
  
  return valid
end

#validate_matcher(matcher) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ceedling/configurator_validator.rb', line 175

def validate_matcher(matcher)
  case matcher
  
  # Handle regex-based matcher
  when /\/.+\//
    # Ensure regex is well-formed by trying to compile it
    begin
      Regexp.compile( matcher[1..-2] )
    rescue Exception => ex
      # Re-raise with our own message formatting
      raise "invalid regular expression:: #{ex.message}"
    end
  
  # Handle wildcard / substring matchers
  else
    # Strip out allowed characters
    invalid = matcher.gsub( /[a-z0-9 \/\.\-_\*]/i, '' )

    # If there's any characters left, then we found invalid characters
    if invalid.length() > 0
      # Format invalid characters into a printable list with no duplicates
      _invalid = invalid.chars.uniq.map{|c| "'#{c}'"}.join( ', ')

      raise "invalid substring or wilcard characters #{_invalid}"
    end
  end
end

#validate_path_list(config, *keys) ⇒ Object

Walk into config hash. verify existence of path(s) at given key depth. Paths are either full simple paths or a simple portion of a path up to a glob.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ceedling/configurator_validator.rb', line 33

def validate_path_list(config, *keys)
  exist = true
  list, depth = @config_walkinator.fetch_value( *keys, hash:config )

  # Return early if we couldn't walk into hash and find a value
  return false if (list.nil?)
  
  list.each do |path|
    # Trim add/subtract notation & glob specifiers
    _path = FilePathUtils::no_decorators( path )

    next if _path.empty? # Path begins with or is entirely a glob, skip it

    # If (partial) path does not exist, complain
    if (not @file_wrapper.exist?( _path ))
      walk = @reportinator.generate_config_walk( keys, depth )
      @loginator.log( "Config path #{walk} => '#{_path}' does not exist in the filesystem.", Verbosity::ERRORS ) 
      exist = false
    end 
  end
  
  return exist
end

#validate_paths_entries(config, key) ⇒ Object

Validate :paths entries, exercising each entry as Ceedling directory glob (variation of Ruby glob)



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
# File 'lib/ceedling/configurator_validator.rb', line 59

def validate_paths_entries(config, key)
  valid = true
  keys = [:paths, key]
  walk = @reportinator.generate_config_walk( keys )

  list, _ = @config_walkinator.fetch_value( *keys, hash:config )

  # Return early if we couldn't walk into hash and find a value
  return false if (list.nil?)
  
  list.each do |path|
    dirs = [] # Working list

    # Trim add/subtract notation
    _path = FilePathUtils::no_aggregation_decorators( path )

    if @file_wrapper.exist?( _path ) and !@file_wrapper.directory?( _path )
      # Path is a simple filepath (not a directory)
      warning = "#{walk} => '#{_path}' is a filepath and will be ignored (FYI :paths is directory-oriented while :files is file-oriented)"
      @loginator.log( warning, Verbosity::COMPLAIN )

      next # Skip to next path
    end

    # Expand paths using Ruby's Dir.glob()
    #  - A simple path will yield that path
    #  - A path glob will expand to one or more paths
    _reformed = FilePathUtils::reform_subdirectory_glob( _path )
    @file_wrapper.directory_listing( _reformed ).each do |entry|
      # For each result, add it to the working list *if* it's a directory
      dirs << entry if @file_wrapper.directory?(entry)
    end
    
    # Handle edge case of subdirectories glob but not subdirectories
    # (Containing parent directory will still exist)
    next if dirs.empty? and _path =~ /\/\*{1,2}$/

    # Path did not work -- must be malformed glob or glob referencing path that does not exist.
    # (An earlier step validates all simple directory paths).
    if dirs.empty?
      error = "#{walk} => '#{_path}' yielded no directories -- matching glob is malformed or directories do not exist"
      @loginator.log( error, Verbosity::ERRORS )
      valid = false
    end
  end
  
  return valid
end

#validate_tool(config:, key:, respect_optional: true) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ceedling/configurator_validator.rb', line 159

def validate_tool(config:, key:, respect_optional:true)
  # Get tool
  walk = [:tools, key]
  tool, _ = @config_walkinator.fetch_value( *walk, hash:config )

  arg_hash = {
    tool: tool,
    name: @reportinator.generate_config_walk( walk ),
    extension: config[:extension][:executable],
    respect_optional: respect_optional
  }

  return @tool_validator.validate( **arg_hash )
end