Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
Refactor read_author_info_from_config
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-rushakoff committed Mar 14, 2013
1 parent 2d8f286 commit 34d2822
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/pivotal_git_scripts/git_pair.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def self.main(argv)
runner.main(argv)
end

class GitPairException < Exception; end

class Runner
def main(argv)
git_dir = `git rev-parse --git-dir`.chomp
Expand All @@ -33,6 +35,9 @@ def main(argv)
[:name, :email, :initials].each do |key|
report_git_settings(git_dir, key)
end
rescue GitPairException => e
puts e.message
exit 1
end

def parse_cli_options(argv)
Expand Down Expand Up @@ -92,7 +97,7 @@ def read_pairs_config
end

unless pairs_file_path
puts <<-INSTRUCTIONS
raise GitPairException, <<-INSTRUCTIONS
Could not find a .pairs file. Create a YAML file in your project or home directory.
Format: <initials>: <name>[; <email>]
Example:
Expand All @@ -106,19 +111,15 @@ def read_pairs_config
prefix: pair
domain: pivotallabs.com
INSTRUCTIONS
exit(1)
end
pairs_file_path ? YAML.load_file(pairs_file_path) : {}

YAML.load_file(pairs_file_path)
end

def read_author_info_from_config(config, initials_ary)
initials_ary.map do |initials|
if full_name = config['pairs'][initials.downcase]
full_name
else
puts "Couldn't find author name for initials: #{initials}. Add this person to the .pairs file in your project or home directory."
exit 1
end
config['pairs'][initials.downcase] or
raise GitPairException, "Couldn't find author name for initials: #{initials}. Add this person to the .pairs file in your project or home directory."
end
end

Expand Down
20 changes: 20 additions & 0 deletions spec/git_pair_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,24 @@
runner.set_git_config(true, 'foo' => 'bar baz')
end
end

describe 'read_author_info_from_config' do
it 'maps from the initials to the full name' do
config = {
'pairs' => {
'aa' => 'An Aardvark',
'tt' => 'The Turtle'
}
}

names = runner.read_author_info_from_config(config, ['aa', 'tt'])
names.should =~ ['An Aardvark', 'The Turtle']
end

it 'exits when initials cannot be found' do
expect {
runner.read_author_info_from_config({"pairs" => {}}, ['aa'])
}.to raise_error(PivotalGitScripts::GitPair::GitPairException)
end
end
end

0 comments on commit 34d2822

Please sign in to comment.