Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that specs can safely round-trip through JSON #595

Merged
merged 1 commit into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions lib/cocoapods-core/specification/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/specification/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#-------------------------------------------------------------------------#
Expand Down