Skip to content

Commit

Permalink
Merge pull request #30 from DannyBen/add/error-capture-on-watch
Browse files Browse the repository at this point in the history
Capture error when running render --watch
  • Loading branch information
DannyBen authored Oct 23, 2022
2 parents 39b1a38 + 5b7f776 commit cfc70f2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/victor/cli/commands/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ class Render < Base
param "SVG_FILE", "Output SVG file. Leave empty to write to stdout"

example "victor render input.rb output.svg"
example "victor render input.rb output.svg --watch"
example "victor render input.rb --template minimal"

def run
if args['--watch']
watch { generate }
watch_and_generate
else
generate
end
Expand Down Expand Up @@ -54,6 +55,14 @@ def watch
end
end

def watch_and_generate
watch do
generate
rescue Exception => e
say! "!undred!#{e.class}!txtrst!\n#{e.message}"
end
end

def file_watcher
@file_watcher ||= Filewatcher.new(ruby_file, immediate: true)
end
Expand Down
1 change: 1 addition & 0 deletions spec/approvals/cli/render/help
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Parameters:

Examples:
victor render input.rb output.svg
victor render input.rb output.svg --watch
victor render input.rb --template minimal
2 changes: 2 additions & 0 deletions spec/approvals/cli/render/watch-error
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Exception
Intentional error
16 changes: 16 additions & 0 deletions spec/victor-cli/commands/render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@
subject.run %W[render #{ruby_file} --watch]
end.to output_approval('cli/render/watch')
end

context "when the script contains an error" do
it "shows it gracefully and continues to watch" do
command = Victor::CLI::Commands::Render
expect do
expect_any_instance_of(command).to receive(:watch) do |watcher, &block|
expect_any_instance_of(command).to receive(:generate) do
raise Exception, "Intentional error"
end
changes = { "some-path" => :updated }
block.call changes
end
subject.run %W[render #{ruby_file} --watch]
end.to output_approval('cli/render/watch-error').to_stderr
end
end
end

context "with RUBY_FILE SVG_FILE" do
Expand Down

0 comments on commit cfc70f2

Please sign in to comment.