Skip to content

Commit 557c88f

Browse files
author
Kevin Paulisse
committed
Move the YAML parsing option to --filters
1 parent fbeb740 commit 557c88f

10 files changed

Lines changed: 77 additions & 68 deletions

File tree

doc/advanced-filter.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Filters
2+
3+
It is possible to enable additional filters for output results via the `--filters` command line option. This command line option accepts a comma-separated list of additional filters, and applies them to the results in the order you specify. The default behavior is not to use any of these filters.
4+
5+
Please note that there are other options to ignore specified diffs, including:
6+
7+
- [Ignoring by type, title, attribute, value, ...](/doc/advanced-ignores.md)
8+
9+
Here is the list of available filters and an explanation of each:
10+
11+
## YAML
12+
13+
#### Usage
14+
15+
```
16+
--filters YAML
17+
```
18+
19+
#### Description
20+
21+
If a file resource has extension `.yml` or `.yaml` and a difference in its content is observed, YAML objects are constructed from the previous and new values. If these YAML objects are identical, the difference is ignored.
22+
23+
This allows you to ignore changes in whitespace, comments, etc., that are not meaningful to a machine parsing the file. Please note that by filtering these changes, you are ignoring changes to comments, which may be meaningful to humans.

doc/optionsref.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ Usage: octocatalog-diff [command line options]
4646
--no-hiera-path-strip Do not use any default hiera path strip settings
4747
--ignore-attr "attr1,attr2,..."
4848
Attributes to ignore
49-
--[no-]ignore-equivalent-yaml-files
50-
Ignore YAML files differing only by whitespace
49+
--filters FILTER1[,FILTER2[,...]]
50+
Filters to apply
5151
--[no-]display-source Show source file and line for each difference
5252
--[no-]validate-references "before,require,subscribe,notify"
5353
References to validate
@@ -426,6 +426,20 @@ on which this is running. (<a href="../lib/octocatalog-diff/catalog-diff/cli/opt
426426
</td>
427427
</tr>
428428

429+
<tr>
430+
<td valign=top>
431+
<pre><code>--filters FILTER1[,FILTER2[,...]]</code></pre>
432+
</td>
433+
<td valign=top>
434+
Filters to apply
435+
</td>
436+
<td valign=top>
437+
Specify one or more filters to apply to the results of the catalog difference.
438+
For a list of available filters and further explanation, please refer to
439+
[Filtering results](/doc/advanced-filter.md). (<a href="../lib/octocatalog-diff/catalog-diff/cli/options/filters.rb">filters.rb</a>)
440+
</td>
441+
</tr>
442+
429443
<tr>
430444
<td valign=top>
431445
<pre><code>-f FROM_BRANCH
@@ -564,19 +578,6 @@ Puppet control repo template, the value of this should be 'hieradata', which is
564578
</td>
565579
</tr>
566580

567-
<tr>
568-
<td valign=top>
569-
<pre><code>--ignore-equivalent-yaml-files
570-
--no-ignore-equivalent-yaml-files </code></pre>
571-
</td>
572-
<td valign=top>
573-
Ignore YAML files differing only by whitespace
574-
</td>
575-
<td valign=top>
576-
Ignore difference between YAML files if they contain the same content differing only by whitespace. (<a href="../lib/octocatalog-diff/catalog-diff/cli/options/ignore_equivalent_yaml_files.rb">ignore_equivalent_yaml_files.rb</a>)
577-
</td>
578-
</tr>
579-
580581
<tr>
581582
<td valign=top>
582583
<pre><code>--ignore-tags STRING1[,STRING2[,...]]</code></pre>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
# Specify one or more filters to apply to the results of the catalog difference.
4+
# For a list of available filters and further explanation, please refer to
5+
# [Filtering results](/doc/advanced-filter.md).
6+
# @param parser [OptionParser object] The OptionParser argument
7+
# @param options [Hash] Options hash being constructed; this is modified in this method.
8+
OctocatalogDiff::CatalogDiff::Cli::Options::Option.newoption(:filters) do
9+
has_weight 199
10+
11+
def parse(parser, options)
12+
parser.on('--filters FILTER1[,FILTER2[,...]]', Array, 'Filters to apply') do |x|
13+
options[:filters] ||= []
14+
options[:filters].concat x
15+
end
16+
end
17+
end

lib/octocatalog-diff/catalog-diff/cli/options/ignore_equivalent_yaml_files.rb

Lines changed: 0 additions & 14 deletions
This file was deleted.

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,8 @@ def catdiff
154154
# out any such parameters from the result array.
155155
filter_diffs_for_absent_files(result) if @opts[:suppress_absent_file_details]
156156

157-
# If --ignore-equivalent-yaml-files is specified, then for any YAML file that is changed, parse the YAML
158-
# to construct the object. If the objects are identical this means the YAML file differs only in whitespace
159-
# or comments but not in function. Remove these.
160-
if @opts[:ignore_equivalent_yaml_files]
161-
OctocatalogDiff::CatalogDiff::Filter.filter(result, 'YAML')
162-
end
157+
# Apply any additional pluggable filters.
158+
OctocatalogDiff::CatalogDiff::Filter.apply_filters(result, @opts[:filters])
163159

164160
# That's it!
165161
@logger.debug "Exiting catdiff; change count: #{result.size}"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Filter
1111
# @param filter_names [Array] Filters to run
1212
# @param options [Hash] Options for each filter (hashed by name)
1313
def self.apply_filters(result, filter_names, options = {})
14+
return unless filter_names.is_a?(Array)
1415
filter_names.each { |x| filter(result, x, options[x] || {}) }
1516
end
1617

spec/octocatalog-diff/integration/yaml_diff_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
context 'with YAML diff suppression enabled' do
4949
before(:all) do
50-
argv = ['-n', 'rspec-node.github.net', '--to-fact-override', 'role=bar', '--ignore-equivalent-yaml-files']
50+
argv = ['-n', 'rspec-node.github.net', '--to-fact-override', 'role=bar', '--filters', 'YAML']
5151
hash = { hiera_config: 'hiera.yaml', spec_fact_file: 'facts.yaml', spec_repo: 'yaml-diff' }
5252
@result = OctocatalogDiff::Integration.integration(hash.merge(argv: argv))
5353
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../options_helper'
4+
5+
describe OctocatalogDiff::CatalogDiff::Cli::Options do
6+
describe '#opt_ignore_equivalent_yaml_files' do
7+
it 'should accept comma delimited parameters for --filters' do
8+
result = run_optparse(['--filters', 'fizzbuzz,barbuzz'])
9+
expect(result[:filters]).to eq(%w(fizzbuzz barbuzz))
10+
end
11+
12+
it 'should accept multiple parameters for --filters' do
13+
result = run_optparse(['--filters', 'fizzbuzz', '--filters', 'barbuzz'])
14+
expect(result[:filters]).to eq(%w(fizzbuzz barbuzz))
15+
end
16+
end
17+
end

spec/octocatalog-diff/tests/catalog-diff/cli/options/ignore_equivalent_yaml_files_spec.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -662,29 +662,6 @@
662662
end
663663
end
664664

665-
context 'additional pluggable filters' do
666-
context 'equivalent YAML files with different text representations' do
667-
before(:all) do
668-
@c1 = OctocatalogDiff::Catalog.new(json: OctocatalogDiff::Spec.fixture_read('catalogs/ignore-equivalent-yaml-1.json'))
669-
@c2 = OctocatalogDiff::Catalog.new(json: OctocatalogDiff::Spec.fixture_read('catalogs/ignore-equivalent-yaml-2.json'))
670-
end
671-
672-
it 'should filter when filter is enabled' do
673-
opts = { ignore_equivalent_yaml_files: true }
674-
testobj = OctocatalogDiff::CatalogDiff::Differ.new(opts, @c1, @c2)
675-
result = testobj.diff
676-
expect(result.size).to eq(0)
677-
end
678-
679-
it 'should not filter when filter is disabled' do
680-
opts = {}
681-
testobj = OctocatalogDiff::CatalogDiff::Differ.new(opts, @c1, @c2)
682-
result = testobj.diff
683-
expect(result.size).to eq(1)
684-
end
685-
end
686-
end
687-
688665
context 'ignoring specific changes in attributes' do
689666
before(:all) do
690667
@c1 = OctocatalogDiff::Catalog.new(json: OctocatalogDiff::Spec.fixture_read('catalogs/ignore-enhanced-changes-1.json'))

0 commit comments

Comments
 (0)