Skip to content

Commit

Permalink
Merge pull request #906 from nanoc/stop-start-exceptions
Browse files Browse the repository at this point in the history
Do not call Listener#stop when #start failed
  • Loading branch information
denisdefreyne committed Jul 10, 2016
2 parents 0660600 + e6a4b82 commit 4977fd3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/nanoc/cli/commands/compile.rb
Expand Up @@ -49,6 +49,18 @@ def start
# @return [void]
def stop
end

# @api private
def start_safely
start
@_started = true
end

# @api private
def stop_safely
stop if @_started
@_started = false
end
end

# Generates diffs for every output file written
Expand Down Expand Up @@ -401,7 +413,7 @@ def setup_listeners
.select { |klass| klass.enable_for?(self) }
.map { |klass| klass.new(reps: reps) }

@listeners.each(&:start)
@listeners.each(&:start_safely)
end

def listeners
Expand All @@ -416,7 +428,7 @@ def run_listeners_while
end

def teardown_listeners
@listeners.each(&:stop)
@listeners.each(&:stop_safely)
end

def reps
Expand Down
64 changes: 64 additions & 0 deletions spec/nanoc/cli/commands/compile_spec.rb
@@ -0,0 +1,64 @@
describe Nanoc::CLI::Commands::Compile::Listener do
let(:klass) do
Class.new(described_class) do
attr_reader :started
attr_reader :stopped

def initialize
@started = false
@stopped = false
end

def start
@started = true
end

def stop
@stopped = true
end
end
end

subject { klass.new }

it 'starts' do
subject.start
expect(subject.started).to be
end

it 'stops' do
subject.start
subject.stop
expect(subject.stopped).to be
end

it 'starts safely' do
subject.start_safely
expect(subject.started).to be
end

it 'stops safely' do
subject.start_safely
subject.stop_safely
expect(subject.stopped).to be
end

context 'listener that does not start or stop properly' do
let(:klass) do
Class.new(described_class) do
def start
raise 'boom'
end

def stop
raise 'boom'
end
end
end

it 'raises on start, but not stop' do
expect { subject.start_safely }.to raise_error(RuntimeError)
expect { subject.stop_safely }.not_to raise_error
end
end
end

0 comments on commit 4977fd3

Please sign in to comment.