Skip to content

Commit 3873e0e

Browse files
author
Kevin Paulisse
committed
Add legacy JSON display format
1 parent 0bab80d commit 3873e0e

4 files changed

Lines changed: 75 additions & 5 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# frozen_string_literal: true
22

3+
require_relative '../api/v1/diff'
34
require_relative 'differ'
45
require_relative 'display/json'
6+
require_relative 'display/legacy_json'
57
require_relative 'display/text'
68

79
module OctocatalogDiff
@@ -21,8 +23,9 @@ class Display
2123
# @param logger [Logger] Logger object
2224
# @return [String] Text output for provided diff
2325
def self.output(diff_in, options = {}, logger = nil)
24-
diff = diff_in.is_a?(OctocatalogDiff::CatalogDiff::Differ) ? diff_in.diff : diff_in
25-
raise ArgumentError, "text_output requires Array<Diff results>; passed in #{diff_in.class}" unless diff.is_a?(Array)
26+
diff_x = diff_in.is_a?(OctocatalogDiff::CatalogDiff::Differ) ? diff_in.diff : diff_in
27+
raise ArgumentError, "text_output requires Array<Diff results>; passed in #{diff_in.class}" unless diff_x.is_a?(Array)
28+
diff = diff_x.map { |x| OctocatalogDiff::API::V1::Diff.new(x) }
2629

2730
# req_format means 'requested format' because 'format' has a built-in meaning to Ruby
2831
req_format = options.fetch(:format, :color_text)
@@ -41,6 +44,9 @@ def self.output(diff_in, options = {}, logger = nil)
4144
when :json
4245
logger.debug 'Generating JSON output' if logger
4346
OctocatalogDiff::CatalogDiff::Display::Json.generate(diff, opts, logger)
47+
when :legacy_json
48+
logger.debug 'Generating Legacy JSON output' if logger
49+
OctocatalogDiff::CatalogDiff::Display::LegacyJson.generate(diff, opts, logger)
4450
when :text
4551
logger.debug 'Generating non-colored text output' if logger
4652
OctocatalogDiff::CatalogDiff::Display::Text.generate(diff, opts.merge(color: false), logger)

lib/octocatalog-diff/catalog-diff/display/json.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
module OctocatalogDiff
88
module CatalogDiff
99
class Display
10-
# Display the output from a diff in JSON format.
10+
# Display the output from a diff in JSON format. This is the new format, used in octocatalog-diff
11+
# 1.x, where each diff is represented by an hash with named keys.
1112
class Json < OctocatalogDiff::CatalogDiff::Display
1213
# Generate JSON representation of the 'diff' suitable for further analysis.
1314
# @param diff [Array<Diff results>] The diff which *must* be in this format
@@ -16,7 +17,7 @@ class Json < OctocatalogDiff::CatalogDiff::Display
1617
# @param _logger [Logger] Not used here
1718
def self.generate(diff, options = {}, _logger = nil)
1819
result = {
19-
'diff' => diff
20+
'diff' => diff.map(&:to_h_with_string_keys)
2021
}
2122
result['header'] = options[:header] unless options[:header].nil?
2223
result.to_json
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../display'
4+
5+
require 'json'
6+
7+
module OctocatalogDiff
8+
module CatalogDiff
9+
class Display
10+
# Display the output from a diff in JSON format. This is the legacy format, used in octocatalog-diff
11+
# 0.x, where each diff is represented by an array.
12+
class LegacyJson < OctocatalogDiff::CatalogDiff::Display
13+
# Generate JSON representation of the 'diff' suitable for further analysis.
14+
# @param diff [Array<Diff results>] The diff which *must* be in this format
15+
# @param options [Hash] Options which are:
16+
# - :header => [String] Header to print; no header is printed if not specified
17+
# @param _logger [Logger] Not used here
18+
def self.generate(diff, options = {}, _logger = nil)
19+
result = {
20+
'diff' => diff.map(&:raw)
21+
}
22+
result['header'] = options[:header] unless options[:header].nil?
23+
result.to_json
24+
end
25+
end
26+
end
27+
end
28+
end

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,49 @@
4141
expect(result.first).to eq('+ Class[Fizzbuzz]')
4242
end
4343

44+
it 'should call OctocatalogDiff::CatalogDiff::Display::LegacyJson' do
45+
opts = {
46+
format: :legacy_json,
47+
header: 'My awesome header'
48+
}
49+
answer = [
50+
[
51+
'+',
52+
"Class\fFizzbuzz",
53+
{ 'type' => 'Class', 'title' => 'Fizzbuzz', 'tags' => %w(class fizzbuzz), 'exported' => false },
54+
{ 'file' => nil, 'line' => nil }
55+
]
56+
]
57+
result = OctocatalogDiff::CatalogDiff::Display.output(differ, opts)
58+
parse_result = JSON.parse(result)
59+
expect(parse_result['diff']).to eq(answer)
60+
expect(parse_result['header']).to eq('My awesome header')
61+
end
62+
4463
it 'should call OctocatalogDiff::CatalogDiff::Display::Json' do
4564
opts = {
4665
format: :json,
4766
header: 'My awesome header'
4867
}
68+
answer = [
69+
{
70+
'diff_type' => '+',
71+
'type' => 'Class',
72+
'title' => 'Fizzbuzz',
73+
'structure' => [],
74+
'old_value' => nil,
75+
'new_value' => { 'type' => 'Class', 'title' => 'Fizzbuzz', 'tags' => %w(class fizzbuzz), 'exported' => false },
76+
'old_file' => nil,
77+
'old_line' => nil,
78+
'new_file' => nil,
79+
'new_line' => nil,
80+
'old_location' => nil,
81+
'new_location' => { 'file' => nil, 'line' => nil }
82+
}
83+
]
4984
result = OctocatalogDiff::CatalogDiff::Display.output(differ, opts)
5085
parse_result = JSON.parse(result)
51-
expect(parse_result['diff']).to eq(differ.diff)
86+
expect(parse_result['diff']).to eq(answer)
5287
expect(parse_result['header']).to eq('My awesome header')
5388
end
5489

0 commit comments

Comments
 (0)