Merge pull request #95 from nicolasmccurdy/use-expect-syntax

Use expect syntax
This commit is contained in:
Nicolas McCurdy
2014-04-04 23:23:42 -04:00
2 changed files with 120 additions and 117 deletions

View File

@@ -10,11 +10,11 @@ describe 'homesick' do
let(:homesick) { Homesick.new }
before { homesick.stub(:repos_dir).and_return(castles) }
before { allow(homesick).to receive(:repos_dir).and_return(castles) }
describe 'clone' do
context 'has a .homesickrc' do
it 'should run the .homesickrc' do
it 'runs the .homesickrc' do
somewhere = create_construct
local_repo = somewhere.directory('some_repo')
local_repo.file('.homesickrc') do |file|
@@ -28,18 +28,18 @@ describe 'homesick' do
expect_any_instance_of(Thor::Shell::Basic).to receive(:say_status).with('eval', kind_of(Pathname))
homesick.clone local_repo
castles.join('some_repo').join('testing').should exist
expect(castles.join('some_repo').join('testing')).to exist
end
end
context 'of a file' do
it 'should symlink existing directories' do
it 'symlinks existing directories' do
somewhere = create_construct
local_repo = somewhere.directory('wtf')
homesick.clone local_repo
castles.join('wtf').readlink.should == local_repo
expect(castles.join('wtf').readlink).to eq(local_repo)
end
context 'when it exists in a repo directory' do
@@ -48,14 +48,14 @@ describe 'homesick' do
@existing_dir = existing_castle.parent
end
it 'should raise an error' do
homesick.should_not_receive(:git_clone)
it 'raises an error' do
expect(homesick).not_to receive(:git_clone)
expect { homesick.clone @existing_dir.to_s }.to raise_error(/already cloned/i)
end
end
end
it 'should clone git repo like file:///path/to.git' do
it 'clones git repo like file:///path/to.git' do
bare_repo = File.join(create_construct.to_s, 'dotfiles.git')
system "git init --bare #{bare_repo} >/dev/null 2>&1"
@@ -63,51 +63,51 @@ describe 'homesick' do
Capture.stderr do
homesick.clone "file://#{bare_repo}"
end
File.directory?(File.join(home.to_s, '.homesick/repos/dotfiles'))
.should be_true
expect(File.directory?(File.join(home.to_s, '.homesick/repos/dotfiles')))
.to be_true
end
it 'should clone git repo like git://host/path/to.git' do
homesick.should_receive(:git_clone)
it 'clones git repo like git://host/path/to.git' do
expect(homesick).to receive(:git_clone)
.with('git://github.com/technicalpickles/pickled-vim.git')
homesick.clone 'git://github.com/technicalpickles/pickled-vim.git'
end
it 'should clone git repo like git@host:path/to.git' do
homesick.should_receive(:git_clone)
it 'clones git repo like git@host:path/to.git' do
expect(homesick).to receive(:git_clone)
.with('git@github.com:technicalpickles/pickled-vim.git')
homesick.clone 'git@github.com:technicalpickles/pickled-vim.git'
end
it 'should clone git repo like http://host/path/to.git' do
homesick.should_receive(:git_clone)
it 'clones git repo like http://host/path/to.git' do
expect(homesick).to receive(:git_clone)
.with('http://github.com/technicalpickles/pickled-vim.git')
homesick.clone 'http://github.com/technicalpickles/pickled-vim.git'
end
it 'should clone git repo like http://host/path/to' do
homesick.should_receive(:git_clone)
it 'clones git repo like http://host/path/to' do
expect(homesick).to receive(:git_clone)
.with('http://github.com/technicalpickles/pickled-vim')
homesick.clone 'http://github.com/technicalpickles/pickled-vim'
end
it 'should clone git repo like host-alias:repos.git' do
homesick.should_receive(:git_clone).with('gitolite:pickled-vim.git')
it 'clones git repo like host-alias:repos.git' do
expect(homesick).to receive(:git_clone).with('gitolite:pickled-vim.git')
homesick.clone 'gitolite:pickled-vim.git'
end
it 'should throw an exception when trying to clone a malformed uri like malformed' do
homesick.should_not_receive(:git_clone)
it 'throws an exception when trying to clone a malformed uri like malformed' do
expect(homesick).not_to receive(:git_clone)
expect { homesick.clone 'malformed' }.to raise_error
end
it 'should clone a github repo' do
homesick.should_receive(:git_clone)
it 'clones a github repo' do
expect(homesick).to receive(:git_clone)
.with('https://github.com/wfarr/dotfiles.git',
destination: Pathname.new('dotfiles'))
@@ -134,7 +134,7 @@ describe 'homesick' do
expect_any_instance_of(Thor::Shell::Basic).to receive(:say_status).with('eval', kind_of(Pathname))
homesick.rc castle
castle.join('testing').should exist
expect(castle.join('testing')).to exist
end
end
@@ -154,7 +154,7 @@ describe 'homesick' do
expect_any_instance_of(Thor::Shell::Basic).to receive(:say_status).with('eval skip', /not evaling.+/, :blue)
homesick.rc castle
castle.join('testing').should_not exist
expect(castle.join('testing')).not_to exist
end
end
end
@@ -167,7 +167,7 @@ describe 'homesick' do
homesick.link('glencairn')
home.join('.some_dotfile').readlink.should == dotfile
expect(home.join('.some_dotfile').readlink).to eq(dotfile)
end
it 'links non-dotfiles from a castle to the home folder' do
@@ -175,7 +175,7 @@ describe 'homesick' do
homesick.link('glencairn')
home.join('bin').readlink.should == dotfile
expect(home.join('bin').readlink).to eq(dotfile)
end
context 'when forced' do
@@ -190,7 +190,7 @@ describe 'homesick' do
homesick.link('glencairn')
existing_dotdir_link.readlink.should == dotdir
expect(existing_dotdir_link.readlink).to eq(dotdir)
end
it 'can override existing directory' do
@@ -200,7 +200,7 @@ describe 'homesick' do
homesick.link('glencairn')
existing_dotdir.readlink.should == dotdir
expect(existing_dotdir.readlink).to eq(dotdir)
end
end
@@ -213,8 +213,8 @@ describe 'homesick' do
homesick.link('glencairn')
home_dotdir = home.join('.config')
home_dotdir.symlink?.should be == false
home_dotdir.join('.some_dotfile').readlink.should == dotfile
expect(home_dotdir.symlink?).to eq(false)
expect(home_dotdir.join('.some_dotfile').readlink).to eq(dotfile)
end
end
@@ -227,8 +227,8 @@ describe 'homesick' do
homesick.link('glencairn')
home_dotdir = home.join('.config').join('appA')
home_dotdir.symlink?.should be == false
home_dotdir.join('.some_dotfile').readlink.should == dotfile
expect(home_dotdir.symlink?).to eq(false)
expect(home_dotdir.join('.some_dotfile').readlink).to eq(dotfile)
end
end
@@ -246,12 +246,12 @@ describe 'homesick' do
home_config_dir = home.join('.config')
home_someapp_dir = home_config_dir.join('someapp')
home_config_dir.symlink?.should be == false
home_config_dir.join('.some_dotfile').readlink
.should be == config_dotfile
home_someapp_dir.symlink?.should be == false
home_someapp_dir.join('.some_appfile').readlink
.should == someapp_dotfile
expect(home_config_dir.symlink?).to eq(false)
expect(home_config_dir.join('.some_dotfile').readlink)
.to eq(config_dotfile)
expect(home_someapp_dir.symlink?).to eq(false)
expect(home_someapp_dir.join('.some_appfile').readlink)
.to eq(someapp_dotfile)
end
end
@@ -262,7 +262,7 @@ describe 'homesick' do
homesick.link
home.join('.some_dotfile').readlink.should == dotfile
expect(home.join('.some_dotfile').readlink).to eq(dotfile)
end
end
end
@@ -276,7 +276,7 @@ describe 'homesick' do
homesick.link('glencairn')
homesick.unlink('glencairn')
home.join('.some_dotfile').should_not exist
expect(home.join('.some_dotfile')).not_to exist
end
it 'unlinks non-dotfiles from the home folder' do
@@ -285,7 +285,7 @@ describe 'homesick' do
homesick.link('glencairn')
homesick.unlink('glencairn')
home.join('bin').should_not exist
expect(home.join('bin')).not_to exist
end
context "with '.config' in .homesick_subdir" do
@@ -298,8 +298,8 @@ describe 'homesick' do
homesick.unlink('glencairn')
home_dotdir = home.join('.config')
home_dotdir.should exist
home_dotdir.join('.some_dotfile').should_not exist
expect(home_dotdir).to exist
expect(home_dotdir.join('.some_dotfile')).not_to exist
end
end
@@ -313,8 +313,8 @@ describe 'homesick' do
homesick.unlink('glencairn')
home_dotdir = home.join('.config').join('appA')
home_dotdir.should exist
home_dotdir.join('.some_dotfile').should_not exist
expect(home_dotdir).to exist
expect(home_dotdir.join('.some_dotfile')).not_to exist
end
end
@@ -333,10 +333,10 @@ describe 'homesick' do
home_config_dir = home.join('.config')
home_someapp_dir = home_config_dir.join('someapp')
home_config_dir.should exist
home_config_dir.join('.some_dotfile').should_not exist
home_someapp_dir.should exist
home_someapp_dir.join('.some_appfile').should_not exist
expect(home_config_dir).to exist
expect(home_config_dir.join('.some_dotfile')).not_to exist
expect(home_someapp_dir).to exist
expect(home_someapp_dir.join('.some_appfile')).not_to exist
end
end
@@ -349,21 +349,21 @@ describe 'homesick' do
homesick.link
homesick.unlink
home.join('.some_dotfile').should_not exist
expect(home.join('.some_dotfile')).not_to exist
end
end
end
describe 'list' do
it 'should say each castle in the castle directory' do
it 'says each castle in the castle directory' do
given_castle('zomg')
given_castle('wtf/zomg')
homesick.should_receive(:say_status)
expect(homesick).to receive(:say_status)
.with('zomg',
'git://github.com/technicalpickles/zomg.git',
:cyan)
homesick.should_receive(:say_status)
expect(homesick).to receive(:say_status)
.with('wtf/zomg',
'git://github.com/technicalpickles/zomg.git',
:cyan)
@@ -373,24 +373,25 @@ describe 'homesick' do
end
describe 'status' do
it 'should say "nothing to commit" when there are no changes' do
it 'says "nothing to commit" when there are no changes' do
given_castle('castle_repo')
text = Capture.stdout { homesick.status('castle_repo') }
text.should =~ /nothing to commit \(create\/copy files and use "git add" to track\)$/
expect(text).to match(/nothing to commit \(create\/copy files and use "git add" to track\)$/)
end
it 'should say "Changes to be committed" when there are changes' do
it 'says "Changes to be committed" when there are changes' do
given_castle('castle_repo')
some_rc_file = home.file '.some_rc_file'
homesick.track(some_rc_file.to_s, 'castle_repo')
text = Capture.stdout { homesick.status('castle_repo') }
text.should =~
expect(text).to match(
/Changes to be committed:.*new file:\s*home\/.some_rc_file/m
)
end
end
describe 'diff' do
it 'should output an empty message when there are no changes to commit' do
it 'outputs an empty message when there are no changes to commit' do
given_castle('castle_repo')
some_rc_file = home.file '.some_rc_file'
homesick.track(some_rc_file.to_s, 'castle_repo')
@@ -398,10 +399,10 @@ describe 'homesick' do
homesick.commit 'castle_repo', 'Adding a file to the test'
end
text = Capture.stdout { homesick.diff('castle_repo') }
text.should eq('')
expect(text).to eq('')
end
it 'should output a diff message when there are changes to commit' do
it 'outputs a diff message when there are changes to commit' do
given_castle('castle_repo')
some_rc_file = home.file '.some_rc_file'
homesick.track(some_rc_file.to_s, 'castle_repo')
@@ -412,31 +413,31 @@ describe 'homesick' do
file.puts 'Some test text'
end
text = Capture.stdout { homesick.diff('castle_repo') }
text.should =~ /diff --git.+Some test text$/m
expect(text).to match(/diff --git.+Some test text$/m)
end
end
describe 'show_path' do
it 'should say the path of a castle' do
it 'says the path of a castle' do
castle = given_castle('castle_repo')
homesick.should_receive(:say).with(castle.dirname)
expect(homesick).to receive(:say).with(castle.dirname)
homesick.show_path('castle_repo')
end
end
describe 'pull' do
it 'should perform a pull, submodule init and update when the given castle exists' do
it 'performs a pull, submodule init and update when the given castle exists' do
given_castle('castle_repo')
homesick.stub(:system).once.with('git pull --quiet')
homesick.stub(:system).once.with('git submodule --quiet init')
homesick.stub(:system).once.with('git submodule --quiet update --init --recursive >/dev/null 2>&1')
allow(homesick).to receive(:system).once.with('git pull --quiet')
allow(homesick).to receive(:system).once.with('git submodule --quiet init')
allow(homesick).to receive(:system).once.with('git submodule --quiet update --init --recursive >/dev/null 2>&1')
homesick.pull 'castle_repo'
end
it 'should print an error message when trying to pull a non-existant castle' do
homesick.should_receive('say_status').once
it 'prints an error message when trying to pull a non-existant castle' do
expect(homesick).to receive('say_status').once
.with(:error,
%r{Could not pull castle_repo, expected .* exist and contain dotfiles},
:red)
@@ -444,13 +445,13 @@ describe 'homesick' do
end
describe '--all' do
it 'should pull each castle when invoked with --all' do
it 'pulls each castle when invoked with --all' do
given_castle('castle_repo')
given_castle('glencairn')
homesick.stub(:system).exactly(2).times.with('git pull --quiet')
homesick.stub(:system).exactly(2).times
allow(homesick).to receive(:system).exactly(2).times.with('git pull --quiet')
allow(homesick).to receive(:system).exactly(2).times
.with('git submodule --quiet init')
homesick.stub(:system).exactly(2).times
allow(homesick).to receive(:system).exactly(2).times
.with('git submodule --quiet update --init --recursive >/dev/null 2>&1')
Capture.stdout do
Capture.stderr { homesick.invoke 'pull', [], all: true }
@@ -461,14 +462,14 @@ describe 'homesick' do
end
describe 'push' do
it 'should perform a git push on the given castle' do
it 'performs a git push on the given castle' do
given_castle('castle_repo')
homesick.stub(:system).once.with('git push')
allow(homesick).to receive(:system).once.with('git push')
homesick.push 'castle_repo'
end
it 'should print an error message when trying to push a non-existant castle' do
homesick.should_receive('say_status').once
it 'prints an error message when trying to push a non-existant castle' do
expect(homesick).to receive('say_status').once
.with(:error,
%r{Could not push castle_repo, expected .* exist and contain dotfiles},
:red)
@@ -477,7 +478,7 @@ describe 'homesick' do
end
describe 'track' do
it 'should move the tracked file into the castle' do
it 'moves the tracked file into the castle' do
castle = given_castle('castle_repo')
some_rc_file = home.file '.some_rc_file'
@@ -485,12 +486,12 @@ describe 'homesick' do
homesick.track(some_rc_file.to_s, 'castle_repo')
tracked_file = castle.join('.some_rc_file')
tracked_file.should exist
expect(tracked_file).to exist
some_rc_file.readlink.should == tracked_file
expect(some_rc_file.readlink).to eq(tracked_file)
end
it 'should handle files with parens' do
it 'handles files with parens' do
castle = given_castle('castle_repo')
some_rc_file = home.file 'Default (Linux).sublime-keymap'
@@ -498,31 +499,31 @@ describe 'homesick' do
homesick.track(some_rc_file.to_s, 'castle_repo')
tracked_file = castle.join('Default (Linux).sublime-keymap')
tracked_file.should exist
expect(tracked_file).to exist
some_rc_file.readlink.should == tracked_file
expect(some_rc_file.readlink).to eq(tracked_file)
end
it 'should track a file in nested folder structure' do
it 'tracks a file in nested folder structure' do
castle = given_castle('castle_repo')
some_nested_file = home.file('some/nested/file.txt')
homesick.track(some_nested_file.to_s, 'castle_repo')
tracked_file = castle.join('some/nested/file.txt')
tracked_file.should exist
some_nested_file.readlink.should == tracked_file
expect(tracked_file).to exist
expect(some_nested_file.readlink).to eq(tracked_file)
end
it 'should track a nested directory' do
it 'tracks a nested directory' do
castle = given_castle('castle_repo')
some_nested_dir = home.directory('some/nested/directory/')
homesick.track(some_nested_dir.to_s, 'castle_repo')
tracked_file = castle.join('some/nested/directory/')
tracked_file.should exist
some_nested_dir.realpath.should == tracked_file.realpath
expect(tracked_file).to exist
expect(some_nested_dir.realpath).to eq(tracked_file.realpath)
end
context 'when call with no castle name' do
@@ -534,21 +535,21 @@ describe 'homesick' do
homesick.track(some_rc_file.to_s)
tracked_file = castle.join('.some_rc_file')
tracked_file.should exist
expect(tracked_file).to exist
some_rc_file.readlink.should == tracked_file
expect(some_rc_file.readlink).to eq(tracked_file)
end
end
describe 'commit' do
it 'should have a commit message when the commit succeeds' do
it 'has a commit message when the commit succeeds' do
given_castle('castle_repo')
some_rc_file = home.file '.a_random_rc_file'
homesick.track(some_rc_file.to_s, 'castle_repo')
text = Capture.stdout do
homesick.commit('castle_repo', 'Test message')
end
text.should =~ /^\[master \(root-commit\) \w+\] Test message/
expect(text).to match(/^\[master \(root-commit\) \w+\] Test message/)
end
end
@@ -556,7 +557,7 @@ describe 'homesick' do
# not for the subdir_file method itself.
describe 'subdir_file' do
it 'should add the nested files parent to the subdir_file' do
it 'adds the nested files parent to the subdir_file' do
castle = given_castle('castle_repo')
some_nested_file = home.file('some/nested/file.txt')
@@ -564,11 +565,11 @@ describe 'homesick' do
subdir_file = castle.parent.join(Homesick::SUBDIR_FILENAME)
File.open(subdir_file, 'r') do |f|
f.readline.should == "some/nested\n"
expect(f.readline).to eq("some/nested\n")
end
end
it 'should NOT add anything if the files parent is already listed' do
it 'does NOT add anything if the files parent is already listed' do
castle = given_castle('castle_repo')
some_nested_file = home.file('some/nested/file.txt')
@@ -578,11 +579,11 @@ describe 'homesick' do
subdir_file = castle.parent.join(Homesick::SUBDIR_FILENAME)
File.open(subdir_file, 'r') do |f|
f.readlines.size.should == 1
expect(f.readlines.size).to eq(1)
end
end
it 'should remove the parent of a tracked file from the subdir_file if the parent itself is tracked' do
it 'removes the parent of a tracked file from the subdir_file if the parent itself is tracked' do
castle = given_castle('castle_repo')
some_nested_file = home.file('some/nested/file.txt')
@@ -592,7 +593,7 @@ describe 'homesick' do
subdir_file = castle.parent.join(Homesick::SUBDIR_FILENAME)
File.open(subdir_file, 'r') do |f|
f.each_line { |line| line.should_not == "some/nested\n" }
f.each_line { |line| expect(line).not_to eq("some/nested\n") }
end
end
end
@@ -606,7 +607,7 @@ describe 'homesick' do
homesick.track(some_rc_file.to_s, 'stronghold')
homesick.destroy('stronghold')
some_rc_file.should_not be_exist
expect(some_rc_file).not_to be_exist
end
it 'deletes the cloned repository' do
@@ -616,20 +617,20 @@ describe 'homesick' do
homesick.track(some_rc_file.to_s, 'stronghold')
homesick.destroy('stronghold')
castle.should_not be_exist
expect(castle).not_to be_exist
end
end
describe 'cd' do
it "cd's to the root directory of the given castle" do
given_castle('castle_repo')
homesick.should_receive('inside').once.with(kind_of(Pathname)).and_yield
homesick.should_receive('system').once.with(ENV['SHELL'])
expect(homesick).to receive('inside').once.with(kind_of(Pathname)).and_yield
expect(homesick).to receive('system').once.with(ENV['SHELL'])
Capture.stdout { homesick.cd 'castle_repo' }
end
it 'returns an error message when the given castle does not exist' do
homesick.should_receive('say_status').once
expect(homesick).to receive('say_status').once
.with(:error,
%r{Could not cd castle_repo, expected .* exist and contain dotfiles},
:red)
@@ -640,19 +641,19 @@ describe 'homesick' do
describe 'open' do
it 'opens the system default editor in the root of the given castle' do
# Make sure calls to ENV use default values for most things...
ENV.stub(:[]).and_call_original
allow(ENV).to receive(:[]).and_call_original
# Set a default value for 'EDITOR' just in case none is set
ENV.stub(:[]).with('EDITOR').and_return('vim')
allow(ENV).to receive(:[]).with('EDITOR').and_return('vim')
given_castle 'castle_repo'
homesick.should_receive('inside').once.with(kind_of(Pathname)).and_yield
homesick.should_receive('system').once.with('vim')
expect(homesick).to receive('inside').once.with(kind_of(Pathname)).and_yield
expect(homesick).to receive('system').once.with('vim')
Capture.stdout { homesick.open 'castle_repo' }
end
it 'returns an error message when the $EDITOR environment variable is not set' do
# Set the default editor to make sure it fails.
ENV.stub(:[]).with('EDITOR').and_return(nil)
homesick.should_receive('say_status').once
allow(ENV).to receive(:[]).with('EDITOR').and_return(nil)
expect(homesick).to receive('say_status').once
.with(:error,
'The $EDITOR environment variable must be set to use this command',
:red)
@@ -661,8 +662,8 @@ describe 'homesick' do
it 'returns an error message when the given castle does not exist' do
# Set a default just in case none is set
ENV.stub(:[]).with('EDITOR').and_return('vim')
homesick.should_receive('say_status').once
allow(ENV).to receive(:[]).with('EDITOR').and_return('vim')
expect(homesick).to receive('say_status').once
.with(:error,
%r{Could not open castle_repo, expected .* exist and contain dotfiles},
:red)
@@ -671,9 +672,9 @@ describe 'homesick' do
end
describe 'version' do
it 'should print the current version of homesick' do
it 'prints the current version of homesick' do
text = Capture.stdout { homesick.version }
text.chomp.should =~ /\d+\.\d+\.\d+/
expect(text.chomp).to match(/\d+\.\d+\.\d+/)
end
end
end

View File

@@ -9,12 +9,14 @@ require 'tempfile'
RSpec.configure do |config|
config.include TestConstruct::Helpers
config.expect_with(:rspec) { |c| c.syntax = :expect }
config.before { ENV['HOME'] = home.to_s }
config.before { silence! }
def silence!
homesick.stub(:say_status)
allow(homesick).to receive(:say_status)
end
def given_castle(path, subdirs = [])