Skip to content

Commit

Permalink
Merge pull request #100 from luisdelarosa/check_for_merge_commits_in_…
Browse files Browse the repository at this point in the history
…yaml_squashed

Show informative error message when a merge conflict is detected in a YAML file.
  • Loading branch information
fabiopelosin committed Apr 7, 2014
2 parents 85b4d64 + 883de73 commit 0f24d8f
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 38 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,11 @@

##### Enhancements

* Show informative error message when a merge conflict is detected in a YAML file.
[Luis de la Rosa](https://github.com/luisdelarosa)
[#69](https://github.com/CocoaPods/Core/issues/69)
[#100](https://github.com/CocoaPods/Core/pull/100)

* Added a check to the linter to ensure that the `social_media_url` has
been changed from the example value.
[Richard Lee](https://github.com/dlackty)
Expand Down
2 changes: 1 addition & 1 deletion lib/cocoapods-core.rb
Expand Up @@ -28,7 +28,7 @@ class Informative < PlainInformative; end
autoload :Source, 'cocoapods-core/source'
autoload :Specification, 'cocoapods-core/specification'
autoload :StandardError, 'cocoapods-core/standard_error'
autoload :YAMLConverter, 'cocoapods-core/yaml_converter'
autoload :YAMLHelper, 'cocoapods-core/yaml_helper'

# TODO: Fix
#
Expand Down
4 changes: 2 additions & 2 deletions lib/cocoapods-core/lockfile.rb
Expand Up @@ -40,7 +40,7 @@ def initialize(hash)
def self.from_file(path)
return nil unless path.exist?
require 'yaml'
hash = File.open(path) { |f| YAML.load(f) }
hash = File.open(path) { |f| YAMLHelper.load(f) }
unless hash && hash.is_a?(Hash)
raise Informative, "Invalid Lockfile in `#{path}`"
end
Expand Down Expand Up @@ -312,7 +312,7 @@ def to_yaml
'SPEC CHECKSUMS',
'COCOAPODS',
]
YAMLConverter.convert_hash(to_hash, keys_hint, "\n\n")
YAMLHelper.convert_hash(to_hash, keys_hint, "\n\n")
end

#-------------------------------------------------------------------------#
Expand Down
2 changes: 1 addition & 1 deletion lib/cocoapods-core/podfile.rb
Expand Up @@ -266,7 +266,7 @@ def self.from_yaml(path)
if string.respond_to?(:encoding) && string.encoding.name != 'UTF-8'
string.encode!('UTF-8')
end
hash = YAML.load(string)
hash = YAMLHelper.load(string)
from_hash(hash, path)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/cocoapods-core/specification/set/statistics.rb
Expand Up @@ -155,7 +155,7 @@ def github_pushed_at(set)
def cache
unless @cache
if cache_file && cache_file.exist?
@cache = YAML.load(cache_file.read)
@cache = YAMLHelper.load(cache_file.read)
else
@cache = {}
end
Expand Down
Expand Up @@ -15,7 +15,7 @@ module Pod
# The missing features include:
# - Strings are never quoted even when ambiguous.
#
class YAMLConverter
class YAMLHelper

class << self

Expand All @@ -41,6 +41,18 @@ def convert_hash(value, hash_keys_hint, line_separator = "\n")
result << "\n"
end

# Load a YAML file and provide more informative error messages in special cases like merge conflict.
# @param A YAML string.
def load(yaml_string)
YAML.load(yaml_string)
rescue Exception => exception
if yaml_has_merge_error(yaml_string)
raise Informative, 'Merge conflict(s) detected'
else
raise exception
end
end

#-----------------------------------------------------------------------#

private
Expand Down Expand Up @@ -120,6 +132,13 @@ def process_hash(hash, hash_keys_hint = nil, line_separator = "\n")
key_lines * line_separator
end

# Check for merge errors in a YAML string.
# @param A YAML string.
# @return If a merge error was detected or not.
def yaml_has_merge_error(yaml_string)
yaml_string.include?('<<<<<<< HEAD')
end

#-----------------------------------------------------------------------#

private
Expand Down
10 changes: 5 additions & 5 deletions spec/lockfile_spec.rb
Expand Up @@ -65,14 +65,14 @@ def self.specs
end

it 'stores the initialization hash' do
lockfile = Lockfile.new(YAML.load(Sample.yaml))
lockfile.internal_data.should == YAML.load(Sample.yaml)
lockfile = Lockfile.new(YAMLHelper.load(Sample.yaml))
lockfile.internal_data.should == YAMLHelper.load(Sample.yaml)
end

it 'loads from a file' do
File.open(@tmp_path, 'w') { |f| f.write(Sample.yaml) }
lockfile = Lockfile.from_file(@tmp_path)
lockfile.internal_data.should == YAML.load(Sample.yaml)
lockfile.internal_data.should == YAMLHelper.load(Sample.yaml)
end

it "returns nil if it can't find the initialization file" do
Expand Down Expand Up @@ -331,7 +331,7 @@ def self.specs
end

it 'generates a valid YAML representation' do
YAML.load(@lockfile.to_yaml).should == YAML.load(Sample.yaml)
YAMLHelper.load(@lockfile.to_yaml).should == YAMLHelper.load(Sample.yaml)
end

it "serializes correctly `:head' dependencies" do
Expand Down Expand Up @@ -414,7 +414,7 @@ def self.specs
end

it 'it includes all the information that it is expected to store' do
@lockfile.internal_data.should == YAML.load(Sample.yaml)
@lockfile.internal_data.should == YAMLHelper.load(Sample.yaml)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/podfile_spec.rb
Expand Up @@ -196,7 +196,7 @@ module Pod
generate_bridge_support: true
set_arc_compatibility_flag: true
EOF
YAML.load(podfile.to_yaml).should == YAML.load(expected)
YAMLHelper.load(podfile.to_yaml).should == YAMLHelper.load(expected)
end

it 'includes inhibit warnings per pod' do
Expand Down
8 changes: 4 additions & 4 deletions spec/specification/set/statistics_spec.rb
Expand Up @@ -95,14 +95,14 @@ module Pod

it 'saves the cache after computing the creation date of a set' do
@stats.creation_date(@set)
cache_hash = YAML.load(@cache_file.read)
cache_hash = YAMLHelper.load(@cache_file.read)
cache_hash['JSONKit'][:creation_date].should == Time.parse('2011-09-12 10:49:04 +0200')
end

it 'saves the cache after computing the creation date of many sets' do
sets = [@set, @source.search_by_name('libPusher').first]
@stats.creation_dates(sets)
cache_hash = YAML.load(@cache_file.read)
cache_hash = YAMLHelper.load(@cache_file.read)
cache_hash['JSONKit'][:creation_date].should == Time.parse('2011-09-12 10:49:04 +0200')
cache_hash['libPusher'][:creation_date].should == Time.parse('2012-02-01 17:05:58 +0100')
end
Expand All @@ -128,7 +128,7 @@ module Pod

it 'saves the cache after retrieving GitHub information' do
@stats.github_watchers(@set)
saved_cache = YAML.load(@cache_file.read)
saved_cache = YAMLHelper.load(@cache_file.read)
saved_cache['JSONKit'][:gh_date] = nil
@cache_hash['JSONKit'][:gh_date] = nil
saved_cache.should == @cache_hash
Expand All @@ -141,7 +141,7 @@ module Pod

it 'stores in the cache time of the last access to the GitHub API' do
@stats.github_watchers(@set)
saved_cache = YAML.load(@cache_file.read)
saved_cache = YAMLHelper.load(@cache_file.read)
time_delta = (Time.now - saved_cache['JSONKit'][:gh_date])
time_delta.should < 60
end
Expand Down

0 comments on commit 0f24d8f

Please sign in to comment.