diff --git a/lib/homesick.rb b/lib/homesick.rb index d458686..d156226 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -21,7 +21,12 @@ class Homesick < Thor inside repos_dir do destination = nil if File.exist?(uri) - destination = Pathname.new(uri).basename + uri = Pathname.new(uri).expand_path + if uri.to_s.start_with?(repos_dir) + raise "Castle already cloned to #{uri}" + end + + destination = uri.basename ln_s uri, destination elsif uri =~ GITHUB_NAME_REPO_PATTERN diff --git a/spec/homesick_spec.rb b/spec/homesick_spec.rb index 2d880af..222f7de 100644 --- a/spec/homesick_spec.rb +++ b/spec/homesick_spec.rb @@ -6,14 +6,37 @@ describe "homesick" do end describe "clone" do - it "should symlink existing directories" do - somewhere = create_construct - somewhere.directory('wtf') - wtf = somewhere + 'wtf' + context "of a file" do + it "should symlink existing directories" do + somewhere = create_construct + somewhere.directory('wtf') + wtf = somewhere + 'wtf' - @homesick.should_receive(:ln_s).with(wtf.to_s, wtf.basename) + @homesick.should_receive(:ln_s).with(wtf, wtf.basename) - @homesick.clone wtf.to_s + @homesick.clone wtf + end + + context "when it exists in a repo directory" do + before do + @repos_dir = create_construct + @existing_dir = @repos_dir.directory('existing_castle') + @homesick.stub!(:repos_dir).and_return(@repos_dir) + end + + it "should not symlink" do + @homesick.should_not_receive(:git_clone) + + @homesick.clone @existing_dir.to_s rescue nil + end + + it "should raise an error" do + @existing_castle = @homesick.send(:repos_dir) + 'existing_castle' + lambda { + @homesick.clone @existing_castle.to_s + }.should raise_error(/already cloned/i) + end + end end it "should clone git repo like git://host/path/to.git" do