Skip to content

Commit 62a7eb2

Browse files
committed
made colorizer filter fail if necessary executables are not present
--HG-- branch : 3.1.x
1 parent 8cb5a7a commit 62a7eb2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/nanoc3/filters/colorize_syntax.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# encoding: utf-8
22

3+
require 'open3'
4+
35
module Nanoc3::Filters
46
class ColorizeSyntax < Nanoc3::Filter
57

@@ -93,6 +95,8 @@ def dummy(code, language, params={})
9395
end
9496

9597
def pygmentize(code, language, params={})
98+
check_availability('pygmentize --V')
99+
96100
IO.popen("pygmentize -l #{language} -f html", "r+") do |io|
97101
io.write(code)
98102
io.close_write
@@ -103,5 +107,10 @@ def pygmentize(code, language, params={})
103107
end
104108
end
105109

110+
def check_availability(cmd)
111+
# Will raise on error
112+
Open3.popen3('highlight --version') { |stdin, stdout, stderr| }
113+
end
114+
106115
end
107116
end

test/filters/test_colorize_syntax.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,32 @@ def test_pygmentize
5353
assert_match(expected_output, actual_output)
5454
end
5555

56+
def test_colorize_syntax_with_missing_executables
57+
if_have 'nokogiri' do
58+
begin
59+
original_path = ENV['PATH']
60+
ENV['PATH'] = './blooblooblah'
61+
62+
# Create filter
63+
filter = ::Nanoc3::Filters::ColorizeSyntax.new
64+
65+
# Get input and expected output
66+
input = '<pre><code class="language-ruby">puts "foo"</code></pre>'
67+
68+
# Run filter
69+
begin
70+
input = '<pre><code class="language-ruby">puts "foo"</code></pre>'
71+
actual_output = filter.run(
72+
input,
73+
:colorizers => { :ruby => :pygmentize })
74+
flunk "expected colorizer to raise if no executable is available"
75+
rescue => e
76+
assert_match /No such file or directory/, e.message
77+
end
78+
ensure
79+
ENV['PATH'] = original_path
80+
end
81+
end
82+
end
83+
5684
end

0 commit comments

Comments
 (0)