Skip to content

Commit 5755b16

Browse files
author
Kevin Paulisse
committed
Separate out file name format and remove first slash
1 parent ffad401 commit 5755b16

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

lib/octocatalog-diff/catalog.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ def validate_references
209209

210210
private
211211

212+
# Private method: Format the name of the source file and line number, based on compilation directory and
213+
# other settings. This is used by format_missing_references.
214+
# @param source_file [String] Raw source file name from catalog
215+
# @param line_number [Fixnum] Line number from catalog
216+
# @return [String] Formatted source file
217+
def format_source_file_line(source_file, line_number)
218+
return '' if source_file.nil? || source_file.empty?
219+
filename = if compilation_dir && source_file.start_with?(compilation_dir)
220+
stripped_file = source_file[compilation_dir.length..-1]
221+
stripped_file.start_with?('/') ? stripped_file[1..-1] : stripped_file
222+
else
223+
source_file
224+
end
225+
"(#{filename}:#{line_number})"
226+
end
227+
212228
# Private method: Format the missing references into human-readable text
213229
# Error message will look like this:
214230
# ---
@@ -228,14 +244,7 @@ def format_missing_references(missing)
228244
# separate error message per element of that array. This allows the total number
229245
# of errors to be correct.
230246
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-
247+
src_file = format_source_file_line(obj[:source]['file'], obj[:source]['line'])
239248
target_val = obj[:target_value].is_a?(Array) ? obj[:target_value] : [obj[:target_value]]
240249
target_val.map { |tv| "#{src_ref}#{src_file} -> #{obj[:target_type].downcase}[#{tv}]" }
241250
end.flatten

spec/octocatalog-diff/integration/reference_validation_spec.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ def self.catalog_contains_resource(result, type, title)
106106
# rubocop:disable Metrics/LineLength
107107
it 'should have formatted error messages' do
108108
msg = @result.exception.message
109-
expect(msg).to match(%r{exec\[subscribe caller 1\]\(/environments/production/modules/test/manifests/subscribe_callers.pp:(2|5)\) -> subscribe\[Exec\[subscribe target\]\]})
110-
expect(msg).to match(%r{exec\[subscribe caller 2\]\(/environments/production/modules/test/manifests/subscribe_callers.pp:(7|13)\) -> subscribe\[Exec\[subscribe target\]\]})
111-
expect(msg).to match(%r{exec\[subscribe caller 2\]\(/environments/production/modules/test/manifests/subscribe_callers.pp:(7|13)\) -> subscribe\[Exec\[subscribe target 2\]\]})
112-
expect(msg).to match(%r{exec\[subscribe caller 3\]\(/environments/production/modules/test/manifests/subscribe_callers.pp:(15|21)\) -> subscribe\[Exec\[subscribe target\]\]})
109+
expect(msg).to match(%r{exec\[subscribe caller 1\]\(modules/test/manifests/subscribe_callers.pp:(2|5)\) -> subscribe\[Exec\[subscribe target\]\]})
110+
expect(msg).to match(%r{exec\[subscribe caller 2\]\(modules/test/manifests/subscribe_callers.pp:(7|13)\) -> subscribe\[Exec\[subscribe target\]\]})
111+
expect(msg).to match(%r{exec\[subscribe caller 2\]\(modules/test/manifests/subscribe_callers.pp:(7|13)\) -> subscribe\[Exec\[subscribe target 2\]\]})
112+
expect(msg).to match(%r{exec\[subscribe caller 3\]\(modules/test/manifests/subscribe_callers.pp:(15|21)\) -> subscribe\[Exec\[subscribe target\]\]})
113113
expect(msg).not_to match(/exec\[subscribe caller 3\].+subscribe\[Exec\[subscribe caller 1\]\]/)
114114
end
115115
# rubocop:enable Metrics/LineLength
@@ -131,7 +131,7 @@ def self.catalog_contains_resource(result, type, title)
131131
# rubocop:disable Metrics/LineLength
132132
it 'should have formatted error messages' do
133133
msg = @result.exception.message
134-
expect(msg).to match(%r{Catalog has broken reference: exec\[before caller\]\(/environments/production/modules/test/manifests/before_callers.pp:(2|5)\) -> before\[Exec\[before target\]\]})
134+
expect(msg).to match(%r{Catalog has broken reference: exec\[before caller\]\(modules/test/manifests/before_callers.pp:(2|5)\) -> before\[Exec\[before target\]\]})
135135
end
136136
# rubocop:enable Metrics/LineLength
137137
end
@@ -152,7 +152,7 @@ def self.catalog_contains_resource(result, type, title)
152152
# rubocop:disable Metrics/LineLength
153153
it 'should have formatted error messages' do
154154
msg = @result.exception.message
155-
expect(msg).to match(%r{exec\[notify caller\]\(/environments/production/modules/test/manifests/notify_callers.pp:(2|4)\) -> notify\[Test::Foo::Bar\[notify target\]\]})
155+
expect(msg).to match(%r{exec\[notify caller\]\(modules/test/manifests/notify_callers.pp:(2|4)\) -> notify\[Test::Foo::Bar\[notify target\]\]})
156156
end
157157
# rubocop:enable Metrics/LineLength
158158
end
@@ -173,9 +173,9 @@ def self.catalog_contains_resource(result, type, title)
173173
# rubocop:disable Metrics/LineLength
174174
it 'should have formatted error messages' do
175175
msg = @result.exception.message
176-
expect(msg).to match(%r{exec\[require caller\]\(/environments/production/modules/test/manifests/require_callers.pp:(2|5)\) -> require\[Exec\[require target\]\]})
177-
expect(msg).to match(%r{exec\[require caller 3\]\(/environments/production/modules/test/manifests/require_callers.pp:(12|18)\) -> require\[Exec\[require target\]\]})
178-
expect(msg).to match(%r{exec\[require caller 4\]\(/environments/production/modules/test/manifests/require_callers.pp:(12|18)\) -> require\[Exec\[require target\]\]})
176+
expect(msg).to match(%r{exec\[require caller\]\(modules/test/manifests/require_callers.pp:(2|5)\) -> require\[Exec\[require target\]\]})
177+
expect(msg).to match(%r{exec\[require caller 3\]\(modules/test/manifests/require_callers.pp:(12|18)\) -> require\[Exec\[require target\]\]})
178+
expect(msg).to match(%r{exec\[require caller 4\]\(modules/test/manifests/require_callers.pp:(12|18)\) -> require\[Exec\[require target\]\]})
179179
expect(msg).not_to match(/exec\[require caller 2\]/)
180180
expect(msg).not_to match(/-> require\[Exec\[require caller\]\]/)
181181
end
@@ -235,10 +235,10 @@ def self.catalog_contains_resource(result, type, title)
235235
# rubocop:disable Metrics/LineLength
236236
it 'should have formatted error messages' do
237237
msg = @result.exception.message
238-
expect(msg).to match(%r{exec\[before alias caller\]\(/environments/production/modules/test/manifests/alias_callers.pp:(2|5)\) -> before\[Exec\[before alias target\]\]})
239-
expect(msg).to match(%r{exec\[notify alias caller\]\(/environments/production/modules/test/manifests/alias_callers.pp:(7|10)\) -> before\[Exec\[notify alias target\]\]})
240-
expect(msg).to match(%r{exec\[require alias caller\]\(/environments/production/modules/test/manifests/alias_callers.pp:(12|15)\) -> before\[Exec\[require alias target\]\]})
241-
expect(msg).to match(%r{exec\[subscribe alias caller\]\(/environments/production/modules/test/manifests/alias_callers.pp:(17|20)\) -> before\[Exec\[subscribe alias target\]\]})
238+
expect(msg).to match(%r{exec\[before alias caller\]\(modules/test/manifests/alias_callers.pp:(2|5)\) -> before\[Exec\[before alias target\]\]})
239+
expect(msg).to match(%r{exec\[notify alias caller\]\(modules/test/manifests/alias_callers.pp:(7|10)\) -> before\[Exec\[notify alias target\]\]})
240+
expect(msg).to match(%r{exec\[require alias caller\]\(modules/test/manifests/alias_callers.pp:(12|15)\) -> before\[Exec\[require alias target\]\]})
241+
expect(msg).to match(%r{exec\[subscribe alias caller\]\(modules/test/manifests/alias_callers.pp:(17|20)\) -> before\[Exec\[subscribe alias target\]\]})
242242
end
243243
# rubocop:enable Metrics/LineLength
244244
end

spec/octocatalog-diff/tests/catalog_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,11 @@
473473
catalog = OctocatalogDiff::Catalog.new(opts)
474474
catalog.compilation_dir = '/var/folders/dw/5ftmkqk972j_kw2fdjyzdqdw0000gn/T/d20161223-46780-x10xaf/environments/production'
475475
error_str = [
476-
'Catalog has broken references: exec[subscribe caller 1](/modules/test/manifests/subscribe_callers.pp:2)' \
476+
'Catalog has broken references: exec[subscribe caller 1](modules/test/manifests/subscribe_callers.pp:2)' \
477477
' -> 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]]'
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]]'
481481
].join('; ')
482482
expect { catalog.validate_references }.to raise_error(OctocatalogDiff::Errors::ReferenceValidationError, error_str)
483483
end
@@ -518,7 +518,7 @@
518518
target_value: 'bar'
519519
}
520520
result = @test_obj.send(:format_missing_references, [obj])
521-
expect(result).to eq('baz[buzz](/modules/foo/manifests/bar.pp:23) -> foo[bar]')
521+
expect(result).to eq('baz[buzz](modules/foo/manifests/bar.pp:23) -> foo[bar]')
522522
end
523523
end
524524

0 commit comments

Comments
 (0)