Skip to content

Commit 02954fa

Browse files
author
Kevin Paulisse
committed
Make catalog-diff return API objects
1 parent 8f54ac7 commit 02954fa

5 files changed

Lines changed: 35 additions & 20 deletions

File tree

lib/octocatalog-diff/api/v1/catalog-diff.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

3+
require_relative 'catalog'
34
require_relative 'common'
5+
require_relative 'diff'
46
require_relative '../../util/catalogs'
57
require_relative '../../catalog-util/cached_master_directory'
68

@@ -18,7 +20,6 @@ class CatalogDiff
1820
# @param :logger [Logger] Logger object (be sure to configure log level)
1921
# Other catalog-diff parameters are required
2022
# @return [OpenStruct] { :diffs (Array); :from (OctocatalogDiff::Catalog), :to (OctocatalogDiff::Catalog) }
21-
2223
def self.catalog_diff(options = nil)
2324
# Validate the required options.
2425
unless options.is_a?(Hash)
@@ -56,9 +57,9 @@ def self.catalog_diff(options = nil)
5657

5758
# Return diffs and catalogs in expected format
5859
OpenStruct.new(
59-
diffs: diffs,
60-
from: catalogs[:from],
61-
to: catalogs[:to]
60+
diffs: diffs.map { |x| OctocatalogDiff::API::V1::Diff.new(x) },
61+
from: OctocatalogDiff::API::V1::Catalog.new(catalogs[:from]),
62+
to: OctocatalogDiff::API::V1::Catalog.new(catalogs[:to])
6263
)
6364
end
6465
end

lib/octocatalog-diff/api/v1/diff.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ module V1
1414
# [4] File and line of the "old" catalog
1515
# [5] File and line of the "new" catalog
1616
# This object seeks to preserve this traditional structure, while providing methods to make it
17-
# easier to deal with.
17+
# easier to deal with. We recommend using the named options, rather than #raw or the indexed array,
18+
# as the raw object and indexed array are not guaranteed to be stable.
1819
class Diff
1920
attr_reader :raw
2021

@@ -48,12 +49,18 @@ def change_type_word
4849
raise ArgumentError, "No change type corresponds to #{@raw[0].inspect}"
4950
end
5051

51-
# Public: Get the type_title string
52+
# Public: Get the type_title_structure string
5253
# @return [?] Type_title_structure
53-
def type_title
54+
def type_title_structure
5455
@raw[1]
5556
end
5657

58+
# Public: For shortening, `tts` = `Type Title Structure`
59+
# @return [?] Type_title_structure
60+
def tts
61+
type_title_structure
62+
end
63+
5764
# Public: Get the resource type
5865
# @return [String] Resource type
5966
def type

lib/octocatalog-diff/cli.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,15 @@ def self.cli(argv = ARGV, logger = Logger.new(STDERR), opts = {})
112112

113113
# Compile catalogs and do catalog-diff
114114
catalog_diff = OctocatalogDiff::API::V1::CatalogDiff.catalog_diff(options.merge(logger: logger))
115+
diffs = catalog_diff.diffs.map(&:raw)
115116

116117
# Display diffs
117118
printer_obj = OctocatalogDiff::Cli::Printer.new(options, logger)
118-
printer_obj.printer(catalog_diff.diffs, catalog_diff.from.compilation_dir, catalog_diff.to.compilation_dir)
119+
printer_obj.printer(diffs, catalog_diff.from.compilation_dir, catalog_diff.to.compilation_dir)
119120

120121
# Return the diff object if requested (generally for testing) or otherwise return exit code
121-
return catalog_diff.diffs if opts[:RETURN_DIFFS]
122-
catalog_diff.diffs.any? ? EXITCODE_SUCCESS_WITH_DIFFS : EXITCODE_SUCCESS_NO_DIFFS
122+
return diffs if opts[:RETURN_DIFFS]
123+
diffs.any? ? EXITCODE_SUCCESS_WITH_DIFFS : EXITCODE_SUCCESS_NO_DIFFS
123124
end
124125

125126
# Parse command line options with 'optparse'. Returns a hash with the parsed arguments.

spec/octocatalog-diff/integration/api_catalog_compile_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
end
2424

2525
it 'should return a catalog object' do
26-
expect(@result).to be_a_kind_of(OctocatalogDiff::Catalog)
26+
expect(@result).to be_a_kind_of(OctocatalogDiff::API::V1::Catalog)
27+
expect(@result.raw).to be_a_kind_of(OctocatalogDiff::Catalog)
2728
end
2829

2930
it 'should be a valid catalog' do

spec/octocatalog-diff/tests/api/v1/catalog-diff_spec.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
describe OctocatalogDiff::API::V1::CatalogDiff do
1212
describe '#catalog_diff' do
13+
before(:each) do
14+
@from_catalog = OctocatalogDiff::Catalog.new(backend: :noop)
15+
@to_catalog = OctocatalogDiff::Catalog.new(backend: :noop)
16+
end
17+
1318
it 'should raise error if no options are passed' do
1419
expect { described_class.catalog_diff }.to raise_error(ArgumentError)
1520
end
@@ -20,21 +25,22 @@
2025

2126
context 'with :cached_master_dir undefined' do
2227
before(:each) do
23-
catalog_obj = OpenStruct.new(catalogs: { from: 'from-catalog', to: 'to-catalog' })
28+
catalog_obj = OpenStruct.new(catalogs: { from: @from_catalog, to: @to_catalog })
2429
expect(OctocatalogDiff::Util::Catalogs).to receive(:new).and_return(catalog_obj)
2530

2631
diffs_obj = double
27-
allow(diffs_obj).to receive(:diffs).and_return(['diff-1', 'diff-2'])
32+
allow(diffs_obj).to receive(:diffs).and_return([['diff-1'], ['diff-2']])
2833
expect(OctocatalogDiff::Cli::Diffs).to receive(:new).and_return(diffs_obj)
2934

3035
logger, @logger_str = OctocatalogDiff::Spec.setup_logger
3136
@result = described_class.catalog_diff(logger: logger, node: 'foo')
3237
end
3338

3439
it 'should return the expected data structure' do
35-
expect(@result.diffs).to eq(['diff-1', 'diff-2'])
36-
expect(@result.from).to eq('from-catalog')
37-
expect(@result.to).to eq('to-catalog')
40+
expect(@result.diffs[0].raw).to eq(['diff-1'])
41+
expect(@result.diffs[1].raw).to eq(['diff-2'])
42+
expect(@result.from.raw).to eq(@from_catalog)
43+
expect(@result.to.raw).to eq(@to_catalog)
3844
end
3945

4046
it 'should log the expected messages' do
@@ -48,10 +54,9 @@
4854

4955
context 'with :cached_master_dir defined' do
5056
before(:each) do
51-
@from_catalog = double
5257
expect(@from_catalog).to receive(:"compilation_dir=").with('foo')
5358

54-
catalog_obj = OpenStruct.new(catalogs: { from: @from_catalog, to: 'to-catalog' })
59+
catalog_obj = OpenStruct.new(catalogs: { from: @from_catalog, to: @to_catalog })
5560
expect(OctocatalogDiff::Util::Catalogs).to receive(:new).and_return(catalog_obj)
5661

5762
expect(OctocatalogDiff::CatalogUtil::CachedMasterDirectory).to receive(:save_catalog_in_cache_dir).and_return('yes')
@@ -66,8 +71,8 @@
6671

6772
it 'should return the expected data structure' do
6873
expect(@result.diffs).to eq([])
69-
expect(@result.from).to eq(@from_catalog)
70-
expect(@result.to).to eq('to-catalog')
74+
expect(@result.from.raw).to eq(@from_catalog)
75+
expect(@result.to.raw).to eq(@to_catalog)
7176
end
7277

7378
it 'should log the expected messages' do

0 commit comments

Comments
 (0)