Fix several rubocop issues with "rubocop -a"

This commit is contained in:
Nicolas McCurdy
2014-01-04 21:57:14 -05:00
parent f03e7670cf
commit fc2bbb1d6e
5 changed files with 50 additions and 82 deletions

View File

@@ -31,22 +31,22 @@ class Homesick < Thor
destination = nil destination = nil
if File.exist?(uri) if File.exist?(uri)
uri = Pathname.new(uri).expand_path uri = Pathname.new(uri).expand_path
raise "Castle already cloned to #{uri}" if uri.to_s.start_with?(repos_dir.to_s) fail "Castle already cloned to #{uri}" if uri.to_s.start_with?(repos_dir.to_s)
destination = uri.basename destination = uri.basename
ln_s uri, destination ln_s uri, destination
elsif uri =~ GITHUB_NAME_REPO_PATTERN elsif uri =~ GITHUB_NAME_REPO_PATTERN
destination = Pathname.new(uri).basename destination = Pathname.new(uri).basename
git_clone "https://github.com/#{$1}.git", :destination => destination git_clone "https://github.com/#{Regexp.last_match[1]}.git", destination: destination
elsif uri =~ /%r([^%r]*?)(\.git)?\Z/ elsif uri =~ /%r([^%r]*?)(\.git)?\Z/
destination = Pathname.new($1) destination = Pathname.new(Regexp.last_match[1])
git_clone uri git_clone uri
elsif uri =~ /[^:]+:([^:]+)(\.git)?\Z/ elsif uri =~ /[^:]+:([^:]+)(\.git)?\Z/
destination = Pathname.new($1) destination = Pathname.new(Regexp.last_match[1])
git_clone uri git_clone uri
else else
raise "Unknown URI format: #{uri}" fail "Unknown URI format: #{uri}"
end end
if destination.join('.gitmodules').exist? if destination.join('.gitmodules').exist?
@@ -80,7 +80,7 @@ class Homesick < Thor
end end
desc 'pull CASTLE', 'Update the specified castle' desc 'pull CASTLE', 'Update the specified castle'
method_option :all, :type => :boolean, :default => false, :required => false, :desc => 'Update all cloned castles' method_option :all, type: :boolean, default: false, required: false, desc: 'Update all cloned castles'
def pull(name = DEFAULT_CASTLE_NAME) def pull(name = DEFAULT_CASTLE_NAME)
if options[:all] if options[:all]
inside_each_castle do |castle| inside_each_castle do |castle|
@@ -90,13 +90,11 @@ class Homesick < Thor
else else
update_castle name update_castle name
end end
end end
desc 'commit CASTLE MESSAGE', "Commit the specified castle's changes" desc 'commit CASTLE MESSAGE', "Commit the specified castle's changes"
def commit(name = DEFAULT_CASTLE_NAME, message = nil) def commit(name = DEFAULT_CASTLE_NAME, message = nil)
commit_castle name, message commit_castle name, message
end end
desc 'push CASTLE', 'Push the specified castle' desc 'push CASTLE', 'Push the specified castle'
@@ -122,7 +120,7 @@ class Homesick < Thor
end end
desc 'symlink CASTLE', 'Symlinks all dotfiles from the specified castle' desc 'symlink CASTLE', 'Symlinks all dotfiles from the specified castle'
method_option :force, :default => false, :desc => 'Overwrite existing conflicting symlinks without prompting.' method_option :force, default: false, desc: 'Overwrite existing conflicting symlinks without prompting.'
def symlink(name = DEFAULT_CASTLE_NAME) def symlink(name = DEFAULT_CASTLE_NAME)
check_castle_existance(name, 'symlink') check_castle_existance(name, 'symlink')
@@ -231,20 +229,19 @@ class Homesick < Thor
end end
end end
desc "destroy CASTLE", "Delete all symlinks and remove the cloned repository" desc 'destroy CASTLE', 'Delete all symlinks and remove the cloned repository'
def destroy(name) def destroy(name)
check_castle_existance name, "destroy" check_castle_existance name, 'destroy'
if shell.yes?("This will destroy your castle irreversible! Are you sure?") if shell.yes?('This will destroy your castle irreversible! Are you sure?')
unlink(name) unlink(name)
rm_rf repos_dir.join(name) rm_rf repos_dir.join(name)
end end
end end
desc "cd CASTLE", "Open a new shell in the root of the given castle" desc 'cd CASTLE', 'Open a new shell in the root of the given castle'
def cd(castle = DEFAULT_CASTLE_NAME) def cd(castle = DEFAULT_CASTLE_NAME)
check_castle_existance castle, "cd" check_castle_existance castle, 'cd'
castle_dir = repos_dir.join(castle) castle_dir = repos_dir.join(castle)
say_status "cd #{castle_dir.realpath}", "Opening a new shell in castle '#{castle}'. To return to the original one exit from the new shell.", :green say_status "cd #{castle_dir.realpath}", "Opening a new shell in castle '#{castle}'. To return to the original one exit from the new shell.", :green
inside castle_dir do inside castle_dir do
@@ -252,14 +249,14 @@ class Homesick < Thor
end end
end end
desc "open CASTLE", "Open your default editor in the root of the given castle" desc 'open CASTLE', 'Open your default editor in the root of the given castle'
def open(castle = DEFAULT_CASTLE_NAME) def open(castle = DEFAULT_CASTLE_NAME)
if ! ENV['EDITOR'] if !ENV['EDITOR']
say_status :error,"The $EDITOR environment variable must be set to use this command", :red say_status :error, 'The $EDITOR environment variable must be set to use this command', :red
exit(1) exit(1)
end end
check_castle_existance castle, "open" check_castle_existance castle, 'open'
castle_dir = repos_dir.join(castle) castle_dir = repos_dir.join(castle)
say_status "#{ENV['EDITOR']} #{castle_dir.realpath}", "Opening the root directory of castle '#{castle}' in editor '#{ENV['EDITOR']}'.", :green say_status "#{ENV['EDITOR']} #{castle_dir.realpath}", "Opening the root directory of castle '#{castle}' in editor '#{ENV['EDITOR']}'.", :green
inside castle_dir do inside castle_dir do
@@ -297,7 +294,7 @@ class Homesick < Thor
def all_castles def all_castles
dirs = Pathname.glob("#{repos_dir}/**/.git", File::FNM_DOTMATCH) dirs = Pathname.glob("#{repos_dir}/**/.git", File::FNM_DOTMATCH)
# reject paths that lie inside another castle, like git submodules # reject paths that lie inside another castle, like git submodules
return dirs.reject do |dir| dirs.reject do |dir|
dirs.any? do |other| dirs.any? do |other|
dir != other && dir.fnmatch(other.parent.join('*').to_s) dir != other && dir.fnmatch(other.parent.join('*').to_s)
end end
@@ -325,7 +322,7 @@ class Homesick < Thor
def commit_castle(castle, message) def commit_castle(castle, message)
check_castle_existance(castle, 'commit') check_castle_existance(castle, 'commit')
inside repos_dir.join(castle) do inside repos_dir.join(castle) do
git_commit_all :message => message git_commit_all message: message
end end
end end
@@ -402,7 +399,7 @@ class Homesick < Thor
def each_file(castle, basedir, subdirs) def each_file(castle, basedir, subdirs)
absolute_basedir = Pathname.new(basedir).expand_path absolute_basedir = Pathname.new(basedir).expand_path
inside basedir do inside basedir do
files = Pathname.glob('{.*,*}').reject{ |a| ['.', '..'].include?(a.to_s) } files = Pathname.glob('{.*,*}').reject { |a| ['.', '..'].include?(a.to_s) }
files.each do |path| files.each do |path|
absolute_path = path.expand_path absolute_path = path.expand_path
castle_home = castle_dir(castle) castle_home = castle_dir(castle)

View File

@@ -9,7 +9,7 @@ class Homesick
destination = Pathname.new(destination) unless destination.kind_of?(Pathname) destination = Pathname.new(destination) unless destination.kind_of?(Pathname)
FileUtils.mkdir_p destination.dirname FileUtils.mkdir_p destination.dirname
if ! destination.directory? if !destination.directory?
say_status 'git clone', "#{repo} to #{destination.expand_path}", :green unless options[:quiet] say_status 'git clone', "#{repo} to #{destination.expand_path}", :green unless options[:quiet]
system "git clone -q --config push.default=upstream --recursive #{repo} #{destination}" unless options[:pretend] system "git clone -q --config push.default=upstream --recursive #{repo} #{destination}" unless options[:pretend]
else else
@@ -78,12 +78,12 @@ class Homesick
def git_status(config = {}) def git_status(config = {})
say_status 'git status', '', :green unless options[:quiet] say_status 'git status', '', :green unless options[:quiet]
system "git status" unless options[:pretend] system 'git status' unless options[:pretend]
end end
def git_diff(config = {}) def git_diff(config = {})
say_status 'git diff', '', :green unless options[:quiet] say_status 'git diff', '', :green unless options[:quiet]
system "git diff" unless options[:pretend] system 'git diff' unless options[:pretend]
end end
def mv(source, destination, config = {}) def mv(source, destination, config = {})

View File

@@ -1,7 +1,6 @@
class Homesick class Homesick
# Hack in support for diffing symlinks # Hack in support for diffing symlinks
class Shell < Thor::Shell::Color class Shell < Thor::Shell::Color
def show_diff(destination, content) def show_diff(destination, content)
destination = Pathname.new(destination) destination = Pathname.new(destination)
@@ -12,6 +11,5 @@ class Homesick
super super
end end
end end
end end
end end

View File

@@ -17,18 +17,12 @@ CyclomaticComplexity:
Documentation: Documentation:
Enabled: false Enabled: false
EmptyLinesAroundBody:
Enabled: false
Eval: Eval:
Enabled: false Enabled: false
FavorUnlessOverNegatedIf: FavorUnlessOverNegatedIf:
Enabled: false Enabled: false
HashSyntax:
Enabled: false
IndentationWidth: IndentationWidth:
Enabled: false Enabled: false
@@ -38,26 +32,5 @@ LineLength:
MethodLength: MethodLength:
Max: 31 Max: 31
PerlBackrefs:
Enabled: false
RedundantReturn:
Enabled: false
RegexpLiteral: RegexpLiteral:
Enabled: false Enabled: false
SignalException:
Enabled: false
SpaceAfterComma:
Enabled: false
SpaceAfterNot:
Enabled: false
SpaceAroundBlockBraces:
Enabled: false
StringLiterals:
Enabled: false

View File

@@ -99,7 +99,7 @@ describe 'homesick' do
end end
it 'should clone a github repo' do it 'should clone a github repo' do
homesick.should_receive(:git_clone).with('https://github.com/wfarr/dotfiles.git', :destination => Pathname.new('dotfiles')) homesick.should_receive(:git_clone).with('https://github.com/wfarr/dotfiles.git', destination: Pathname.new('dotfiles'))
homesick.clone 'wfarr/dotfiles' homesick.clone 'wfarr/dotfiles'
end end
@@ -165,7 +165,7 @@ describe 'homesick' do
end end
context 'when forced' do context 'when forced' do
let(:homesick) { Homesick.new [], :force => true } let(:homesick) { Homesick.new [], force: true }
it 'can override symlinks to directories' do it 'can override symlinks to directories' do
somewhere_else = create_construct somewhere_else = create_construct
@@ -237,7 +237,7 @@ describe 'homesick' do
end end
end end
context "when call with no castle name" do context 'when call with no castle name' do
let(:castle) { given_castle('dotfiles') } let(:castle) { given_castle('dotfiles') }
it 'using default castle name: "dotfiles"' do it 'using default castle name: "dotfiles"' do
dotfile = castle.file('.some_dotfile') dotfile = castle.file('.some_dotfile')
@@ -320,7 +320,7 @@ describe 'homesick' do
end end
end end
context "when call with no castle name" do context 'when call with no castle name' do
let(:castle) { given_castle('dotfiles') } let(:castle) { given_castle('dotfiles') }
it 'using default castle name: "dotfiles"' do it 'using default castle name: "dotfiles"' do
@@ -484,7 +484,7 @@ describe 'homesick' do
some_nested_dir.realpath.should == tracked_file.realpath some_nested_dir.realpath.should == tracked_file.realpath
end end
context "when call with no castle name" do context 'when call with no castle name' do
it 'using default castle name: "dotfiles"' do it 'using default castle name: "dotfiles"' do
castle = given_castle('dotfiles') castle = given_castle('dotfiles')
@@ -553,62 +553,62 @@ describe 'homesick' do
end end
end end
describe "destroy" do describe 'destroy' do
it "removes the symlink files" do it 'removes the symlink files' do
expect_any_instance_of(Thor::Shell::Basic).to receive(:yes?).and_return('y') expect_any_instance_of(Thor::Shell::Basic).to receive(:yes?).and_return('y')
given_castle("stronghold") given_castle('stronghold')
some_rc_file = home.file '.some_rc_file' some_rc_file = home.file '.some_rc_file'
homesick.track(some_rc_file.to_s, "stronghold") homesick.track(some_rc_file.to_s, 'stronghold')
homesick.destroy('stronghold') homesick.destroy('stronghold')
some_rc_file.should_not be_exist some_rc_file.should_not be_exist
end end
it "deletes the cloned repository" do it 'deletes the cloned repository' do
expect_any_instance_of(Thor::Shell::Basic).to receive(:yes?).and_return('y') expect_any_instance_of(Thor::Shell::Basic).to receive(:yes?).and_return('y')
castle = given_castle("stronghold") castle = given_castle('stronghold')
some_rc_file = home.file '.some_rc_file' some_rc_file = home.file '.some_rc_file'
homesick.track(some_rc_file.to_s, "stronghold") homesick.track(some_rc_file.to_s, 'stronghold')
homesick.destroy('stronghold') homesick.destroy('stronghold')
castle.should_not be_exist castle.should_not be_exist
end end
end end
describe "cd" do describe 'cd' do
it "cd's to the root directory of the given castle" do it "cd's to the root directory of the given castle" do
given_castle('castle_repo') given_castle('castle_repo')
homesick.should_receive("inside").once.with(kind_of(Pathname)).and_yield homesick.should_receive('inside').once.with(kind_of(Pathname)).and_yield
homesick.should_receive("system").once.with(ENV["SHELL"]) homesick.should_receive('system').once.with(ENV['SHELL'])
Capture.stdout { homesick.cd 'castle_repo' } Capture.stdout { homesick.cd 'castle_repo' }
end end
it "returns an error message when the given castle does not exist" do it 'returns an error message when the given castle does not exist' do
homesick.should_receive("say_status").once.with(:error, /Could not cd castle_repo, expected \/tmp\/construct_container.* exist and contain dotfiles/, :red) homesick.should_receive('say_status').once.with(:error, /Could not cd castle_repo, expected \/tmp\/construct_container.* exist and contain dotfiles/, :red)
expect { homesick.cd "castle_repo" }.to raise_error(SystemExit) expect { homesick.cd 'castle_repo' }.to raise_error(SystemExit)
end end
end end
describe "open" do describe 'open' do
it "opens the system default editor in the root of the given castle" do it 'opens the system default editor in the root of the given castle' do
ENV.stub(:[]).and_call_original # Make sure calls to ENV use default values for most things... ENV.stub(:[]).and_call_original # Make sure calls to ENV use default values for most things...
ENV.stub(:[]).with('EDITOR').and_return('vim') # Set a default value for 'EDITOR' just in case none is set ENV.stub(:[]).with('EDITOR').and_return('vim') # Set a default value for 'EDITOR' just in case none is set
given_castle 'castle_repo' given_castle 'castle_repo'
homesick.should_receive("inside").once.with(kind_of(Pathname)).and_yield homesick.should_receive('inside').once.with(kind_of(Pathname)).and_yield
homesick.should_receive("system").once.with('vim') homesick.should_receive('system').once.with('vim')
Capture.stdout { homesick.open 'castle_repo' } Capture.stdout { homesick.open 'castle_repo' }
end end
it "returns an error message when the $EDITOR environment variable is not set" do it 'returns an error message when the $EDITOR environment variable is not set' do
ENV.stub(:[]).with('EDITOR').and_return(nil) # Set the default editor to make sure it fails. ENV.stub(:[]).with('EDITOR').and_return(nil) # Set the default editor to make sure it fails.
homesick.should_receive("say_status").once.with(:error, "The $EDITOR environment variable must be set to use this command", :red) homesick.should_receive('say_status').once.with(:error, 'The $EDITOR environment variable must be set to use this command', :red)
expect { homesick.open "castle_repo" }.to raise_error(SystemExit) expect { homesick.open 'castle_repo' }.to raise_error(SystemExit)
end end
it "returns an error message when the given castle does not exist" do it 'returns an error message when the given castle does not exist' do
ENV.stub(:[]).with('EDITOR').and_return('vim') # Set a default just in case none is set ENV.stub(:[]).with('EDITOR').and_return('vim') # Set a default just in case none is set
homesick.should_receive("say_status").once.with(:error, /Could not open castle_repo, expected \/tmp\/construct_container.* exist and contain dotfiles/, :red) homesick.should_receive('say_status').once.with(:error, /Could not open castle_repo, expected \/tmp\/construct_container.* exist and contain dotfiles/, :red)
expect { homesick.open "castle_repo" }.to raise_error(SystemExit) expect { homesick.open 'castle_repo' }.to raise_error(SystemExit)
end end
end end