Skip to content

Commit 9983657

Browse files
author
Kevin Paulisse
committed
Add filename and line into reference validation error
1 parent af38f33 commit 9983657

2 files changed

Lines changed: 40 additions & 20 deletions

File tree

lib/octocatalog-diff/catalog.rb

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,29 +201,47 @@ def validate_references
201201
end
202202
return if missing.empty?
203203

204-
# At this point there is at least one broken/missing reference. Format an error message and
205-
# raise. Error message will look like this:
206-
# ---
207-
# Catalog has broken references: exec[subscribe caller 1] -> subscribe[Exec[subscribe target]];
208-
# exec[subscribe caller 2] -> subscribe[Exec[subscribe target]]; exec[subscribe caller 2] ->
209-
# subscribe[Exec[subscribe target 2]]
210-
# ---
204+
# At this point there is at least one broken/missing reference. Format an error message and raise.
205+
errors = format_missing_references(missing)
206+
plural = errors =~ /;/ ? 's' : ''
207+
raise OctocatalogDiff::Errors::ReferenceValidationError, "Catalog has broken reference#{plural}: #{errors}"
208+
end
209+
210+
private
211+
212+
# Private method: Format the missing references into human-readable text
213+
# Error message will look like this:
214+
# ---
215+
# Catalog has broken references: exec[subscribe caller 1](file:line) -> subscribe[Exec[subscribe target]];
216+
# exec[subscribe caller 2](file:line) -> subscribe[Exec[subscribe target]]; exec[subscribe caller 2](file:line) ->
217+
# subscribe[Exec[subscribe target 2]]
218+
# ---
219+
# @param missing [Array] Array of missing references
220+
# @return [String] Formatted references
221+
def format_missing_references(missing)
222+
unless missing.is_a?(Array) && missing.any?
223+
raise ArgumentError, 'format_missing_references() requires a non-empty array as input'
224+
end
225+
211226
formatted_references = missing.map do |obj|
212227
# obj[:target_value] can be a string or an array. If it's an array, create a
213228
# separate error message per element of that array. This allows the total number
214229
# of errors to be correct.
215-
src = "#{obj[:source]['type'].downcase}[#{obj[:source]['title']}]"
230+
src_ref = "#{obj[:source]['type'].downcase}[#{obj[:source]['title']}]"
231+
src_file = if obj[:source]['file'].nil? || obj[:source]['file'].empty?
232+
''
233+
elsif compilation_dir && obj[:source]['file'].start_with?(compilation_dir)
234+
"(#{obj[:source]['file'][compilation_dir.length..-1]}:#{obj[:source]['line']})"
235+
else
236+
"(#{obj[:source]['file']}:#{obj[:source]['line']})"
237+
end
238+
216239
target_val = obj[:target_value].is_a?(Array) ? obj[:target_value] : [obj[:target_value]]
217-
target_val.map { |tv| "#{src} -> #{obj[:target_type].downcase}[#{tv}]" }
218-
end
219-
formatted_references.flatten!
220-
plural = formatted_references.size == 1 ? '' : 's'
221-
errors = formatted_references.join('; ')
222-
raise OctocatalogDiff::Errors::ReferenceValidationError, "Catalog has broken reference#{plural}: #{errors}"
240+
target_val.map { |tv| "#{src_ref}#{src_file} -> #{obj[:target_type].downcase}[#{tv}]" }
241+
end.flatten
242+
formatted_references.join('; ')
223243
end
224244

225-
private
226-
227245
# Private method: Given a list of resources to check, return the references from
228246
# that list that are missing from the catalog. (An empty array returned would indicate
229247
# all references are present in the catalog.)

spec/octocatalog-diff/tests/catalog_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,13 @@
471471
json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/reference-validation-broken.json'))
472472
}
473473
catalog = OctocatalogDiff::Catalog.new(opts)
474+
catalog.compilation_dir = '/var/folders/dw/5ftmkqk972j_kw2fdjyzdqdw0000gn/T/d20161223-46780-x10xaf/environments/production'
474475
error_str = [
475-
'Catalog has broken references: exec[subscribe caller 1] -> subscribe[Exec[subscribe target]]',
476-
'exec[subscribe caller 2] -> subscribe[Exec[subscribe target]]',
477-
'exec[subscribe caller 2] -> subscribe[Exec[subscribe target 2]]',
478-
'exec[subscribe caller 3] -> subscribe[Exec[subscribe target]]'
476+
'Catalog has broken references: exec[subscribe caller 1](/modules/test/manifests/subscribe_callers.pp:2)' \
477+
' -> subscribe[Exec[subscribe target]]',
478+
'exec[subscribe caller 2](/modules/test/manifests/subscribe_callers.pp:7) -> subscribe[Exec[subscribe target]]',
479+
'exec[subscribe caller 2](/modules/test/manifests/subscribe_callers.pp:7) -> subscribe[Exec[subscribe target 2]]',
480+
'exec[subscribe caller 3](/modules/test/manifests/subscribe_callers.pp:15) -> subscribe[Exec[subscribe target]]'
479481
].join('; ')
480482
expect { catalog.validate_references }.to raise_error(OctocatalogDiff::Errors::ReferenceValidationError, error_str)
481483
end

0 commit comments

Comments
 (0)