Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Spec and command for DsclStore#delete

  • Loading branch information...
commit f5bd6d0661ed07f9a00c655f424e03ab923e5e1a 1 parent 58dff56
@bjeanes authored
View
21 lib/ghost/store/dscl_store.rb
@@ -1,16 +1,8 @@
require 'ghost/host'
+require 'set'
module Ghost
module Store
- # TODO: How to write these tests without actually
- # destroying local hosts or needing sudo?
- #
- # How to run off OS X?
- #
- # Setting expectations for the calling the
- # dscl commands is basically just testing
- # the implementation instead of the side
- # effect. Useless tests?
class DsclStore
class Dscl
class << self
@@ -58,6 +50,17 @@ def find(regex)
end
def delete(host)
+ result = SortedSet.new
+
+ all.each do |existing_host|
+ next unless host.match(existing_host.name)
+ next if host.respond_to?(:ip) && host.ip != existing_host.ip
+
+ Dscl.delete(domain, existing_host.name)
+ result << existing_host
+ end
+
+ result.to_a
end
def empty
View
77 spec/ghost/store/dscl_store_spec.rb
@@ -1,8 +1,9 @@
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper.rb")
require 'ghost/store/dscl_store'
+# TODO: Raise exception error when `dscl` doesn't have sufficient privileges
describe Ghost::Store::DsclStore do
- let(:store) { Ghost.store = Ghost::Store::DsclStore.new }
+ let(:store) { Ghost::Store::DsclStore.new }
let(:cmd) { Ghost::Store::DsclStore::Dscl }
before do
@@ -69,7 +70,77 @@
end
end
- describe "#delete"
- describe "#delete"
+ describe "#delete" do
+ before do
+ store.stub(:all).and_return [
+ Ghost::Host.new('foo.com', '127.0.0.1'),
+ Ghost::Host.new('fo.com', '127.0.0.1'),
+ Ghost::Host.new('fooo.com', '127.0.0.2')
+ ]
+ end
+
+ context 'using a Ghost::Host to identify host' do
+ context 'and the IP does not match an entry' do
+ let(:host) { Ghost::Host.new("foo.com", "127.0.0.2") }
+
+ it 'returns empty array' do
+ store.delete(host).should == []
+ end
+
+ it 'has no effect' do
+ store.delete(host)
+ cmd.should_not_receive(:delete)
+ end
+ end
+
+ context 'and the IP matches an entry' do
+ let(:host) { Ghost::Host.new("foo.com", "127.0.0.1") }
+
+ it 'returns array of deleted hosts' do
+ store.delete(host).should == [host]
+ end
+
+ # In order to make this run off OS X and without root, have to use an
+ # expectation... I think?
+ it 'deletes the host' do
+ cmd.should_receive(:delete).with('localhost', 'foo.com')
+ store.delete(host)
+ end
+ end
+ end
+
+ context 'using a regex to identify hosts' do
+ let(:host) { /fo*\.com/ }
+
+ it 'returns array of removed hosts' do
+ store.delete(host).should == [
+ Ghost::Host.new('fo.com', '127.0.0.1'),
+ Ghost::Host.new('foo.com', '127.0.0.1'),
+ Ghost::Host.new('fooo.com', '127.0.0.2')
+ ]
+ end
+
+ it 'deletes the hosts' do
+ cmd.should_receive(:delete).with('localhost', 'fo.com')
+ cmd.should_receive(:delete).with('localhost', 'foo.com')
+ cmd.should_receive(:delete).with('localhost', 'fooo.com')
+ store.delete(host)
+ end
+ end
+
+ context 'using a string to identify host' do
+ let(:host) { "foo.com" }
+
+ it 'returns array of removed hosts' do
+ store.delete(host).should == [Ghost::Host.new('foo.com', '127.0.0.1')]
+ end
+
+ it 'removes the host from the file' do
+ cmd.should_receive(:delete).with('localhost', 'foo.com')
+ store.delete(host)
+ end
+ end
+ end
+
describe "#empty"
end
View
1  spec/ghost/store/hosts_file_store_spec.rb
@@ -187,7 +187,6 @@ def no_write
context 'when deleting one of the ghost entries' do
context 'using a Ghost::Host to identify host' do
-
context 'and the IP does not match an entry' do
let(:host) { Ghost::Host.new("google.com", "127.0.0.2") }
Please sign in to comment.
Something went wrong with that request. Please try again.