Skip to content

Commit f70192d

Browse files
author
Kevin Paulisse
committed
Add spec test coverage for YAML ignorer
1 parent 2eccbab commit f70192d

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

lib/octocatalog-diff/catalog-diff/differ.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def equivalent_yaml_files?(diff)
187187
return false unless diff[0] == '~' || diff[0] == '!'
188188

189189
# Make sure we are comparing file content for a file ending in .yaml or .yml extension
190-
return false unless diff[1] =~ /^File\f.+\.ya?ml\fparameters\fcontent$/
190+
return false unless diff[1] =~ /^File\f([^\f]+)\.ya?ml\fparameters\fcontent$/
191191

192192
# Attempt to convert the old (diff[2]) and new (diff[3]) into YAML objects. Assuming
193193
# that doesn't error out, the return value is whether or not they're equal.
@@ -208,7 +208,7 @@ def filter_diffs_for_absent_files(result)
208208
absent_files = Set.new
209209
result.each do |diff|
210210
next unless diff[0] == '~' || diff[0] == '!'
211-
next unless diff[1] =~ /^File\f(.+)\fparameters\fensure$/
211+
next unless diff[1] =~ /^File\f([^\f]+)\fparameters\fensure$/
212212
next unless ['absent', 'false', false].include?(diff[3])
213213
absent_files.add Regexp.last_match(1)
214214
end

spec/octocatalog-diff/tests/catalog-diff/differ_spec.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,9 @@
532532
end
533533
end
534534
end
535+
end
535536

537+
describe OctocatalogDiff::CatalogDiff::Differ do
536538
context 'ignoring only adds / removes / changes' do
537539
describe '#ignore' do
538540
before(:all) do
@@ -1279,4 +1281,73 @@
12791281
expect(@logger_str.string).to match(/Exiting filter_diffs_for_absent_files with 7 diffs/)
12801282
end
12811283
end
1284+
1285+
describe '#filter_diffs_for_equivalent_yaml_files' do
1286+
let(:obj) { described_class.allocate }
1287+
let(:str1a) { "---\n foo: bar" }
1288+
let(:str1b) { "---\nfoo: bar" }
1289+
let(:str2) { "---\n foo: baz" }
1290+
1291+
it 'should not filter out an added resource' do
1292+
result = [['+', "File\ffoobar.yaml", { 'parameters' => { 'content' => str1a } }]]
1293+
obj.send(:filter_diffs_for_equivalent_yaml_files, result)
1294+
expect(result.size).to eq(1)
1295+
end
1296+
1297+
it 'should not filter out a removed resource' do
1298+
result = [['-', "File\ffoobar.yaml", { 'parameters' => { 'content' => str1a } }]]
1299+
obj.send(:filter_diffs_for_equivalent_yaml_files, result)
1300+
expect(result.size).to eq(1)
1301+
end
1302+
1303+
it 'should not filter out a non-file resource' do
1304+
result = [['~', "Exec\ffoobar.yaml\fparameters\fcontent", str1a, str1b]]
1305+
obj.send(:filter_diffs_for_equivalent_yaml_files, result)
1306+
expect(result.size).to eq(1)
1307+
end
1308+
1309+
it 'should not filter out a file whose extension is not .yaml / .yml' do
1310+
result = [['~', "File\ffoobar.json\fparameters\fcontent", str1a, str1b]]
1311+
obj.send(:filter_diffs_for_equivalent_yaml_files, result)
1312+
expect(result.size).to eq(1)
1313+
end
1314+
1315+
it 'should not filter out a change with no content change' do
1316+
result = [['~', "File\ffoobar.json\fparameters\fowner", 'root', 'nobody']]
1317+
obj.send(:filter_diffs_for_equivalent_yaml_files, result)
1318+
expect(result.size).to eq(1)
1319+
end
1320+
1321+
it 'should not filter out a change where YAML objects are dissimilar' do
1322+
result = [['~', "File\ffoobar.yaml\fparameters\fcontent", str1a, str2]]
1323+
obj.send(:filter_diffs_for_equivalent_yaml_files, result)
1324+
expect(result.size).to eq(1)
1325+
end
1326+
1327+
it 'should not filter out a change where YAML is invalid' do
1328+
x_str = '---{ "blah": "foo" }'
1329+
result = [['~', "File\ffoobar.yaml\fparameters\fcontent", x_str, x_str]]
1330+
obj.send(:filter_diffs_for_equivalent_yaml_files, result)
1331+
expect(result.size).to eq(1)
1332+
end
1333+
1334+
it 'should not filter out a change where YAML is unparseable' do
1335+
x_str = "--- !ruby/object:This::Does::Not::Exist\n foo: bar"
1336+
result = [['~', "File\ffoobar.yaml\fparameters\fcontent", x_str, x_str]]
1337+
obj.send(:filter_diffs_for_equivalent_yaml_files, result)
1338+
expect(result.size).to eq(1)
1339+
end
1340+
1341+
it 'should filter out a whitespace-only change to a .yaml file' do
1342+
result = [['~', "File\ffoobar.yaml\fparameters\fcontent", str1a, str1b]]
1343+
obj.send(:filter_diffs_for_equivalent_yaml_files, result)
1344+
expect(result.size).to eq(0)
1345+
end
1346+
1347+
it 'should filter out a whitespace-only change to a .yml file' do
1348+
result = [['~', "File\ffoobar.yml\fparameters\fcontent", str1a, str1b]]
1349+
obj.send(:filter_diffs_for_equivalent_yaml_files, result)
1350+
expect(result.size).to eq(0)
1351+
end
1352+
end
12821353
end

0 commit comments

Comments
 (0)