diff --git a/CHANGELOG.md b/CHANGELOG.md index db44e5034..57e1a4be3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,8 @@ ##### Bug Fixes -* None. +* Ensure that specs can round-trip to JSON byte-for-byte identical. + [Samuel Giddins](https://github.com/segiddins) ## 1.8.1 (2019-09-27) diff --git a/lib/cocoapods-core/specification/json.rb b/lib/cocoapods-core/specification/json.rb index 7230908d8..a6ac3c32d 100644 --- a/lib/cocoapods-core/specification/json.rb +++ b/lib/cocoapods-core/specification/json.rb @@ -22,6 +22,7 @@ def to_pretty_json(*a) # def to_hash hash = attributes_hash.dup + hash.delete('platforms') if root? || available_platforms != parent.available_platforms platforms = Hash[available_platforms.map { |p| [p.name.to_s, p.deployment_target && p.deployment_target.to_s] }] hash['platforms'] = platforms @@ -31,13 +32,17 @@ def to_hash all_testspecs = specs_by_type[:test] || [] all_subspecs = specs_by_type[:library] || [] + hash.delete('testspecs') hash['testspecs'] = all_testspecs.map(&:to_hash) unless all_testspecs.empty? + hash.delete('appspecs') hash['appspecs'] = all_appspecs.map(&:to_hash) unless all_appspecs.empty? + hash.delete('subspecs') hash['subspecs'] = all_subspecs.map(&:to_hash) unless all_subspecs.empty? # Since CocoaPods 1.7 version the DSL has changed to be pluralized. When we serialize a podspec to JSON with # 1.7, ensure that we also include the singular version in the hash to maintain backwards compatibility with # < 1.7 versions. + hash.delete('swift_version') hash['swift_version'] = swift_version.to_s unless swift_version.nil? hash diff --git a/spec/specification/json_spec.rb b/spec/specification/json_spec.rb index 70309330b..ed96ebd9f 100644 --- a/spec/specification/json_spec.rb +++ b/spec/specification/json_spec.rb @@ -63,6 +63,23 @@ module Pod spec = Specification.new(nil, 'BananaLib') spec.to_pretty_json.should.end_with "\n" end + + it 'can round-trip' do + spec = Specification.new do |s| + s.name = 'BananaLib' + s.app_spec 'App' + s.test_spec 'Tests' + s.version = '17.0' + s.swift_versions = %w(5.0) + end + + json = spec.to_pretty_json + + loaded_spec = Specification.from_json(json) + new_json = loaded_spec.to_pretty_json + + new_json.should.equal json + end end #-------------------------------------------------------------------------#