# frozen_string_literal: true
require "tmpdir"
require "fileutils"
require_relative "../test_helper"
require_relative "../../lib/herb/dev/runner"
require_relative "../../lib/herb/dev"
module Dev
class RunnerTest < Minitest::Spec
class FakeWebSocket
attr_reader :messages #: Array[Hash[Symbol, untyped]]
def initialize
@messages = []
end
def client_count
1
end
def broadcast(message, **)
@messages << message
end
end
BROKEN = "
\n
\n" #: String
def broadcast_for(current_content, previous_content)
websocket = FakeWebSocket.new
pipeline = Herb::Dev::Pipeline.new(server: websocket, compiler: -> {})
event = Herb::Dev::Watcher::Event.new(
kind: :changed,
path: "/app/views/posts/index.html.erb",
relative_path: "app/views/posts/index.html.erb",
previous: previous_content,
current: current_content
)
capture_io { pipeline.handle_event(event) }
websocket.messages.first
end
test "names the parser as what found the error, not itself as what delivered it" do
error = broadcast_for(BROKEN, "")[:errors].first
parsed = Herb.parse(BROKEN, strict: true, analyze: true).errors.first.to_diagnostic(template: "app/views/posts/index.html.erb")
assert_equal "Herb Parser", error[:origin]
assert_equal parsed.origin, error[:origin]
end
test "sends the code and suggestion the same error would carry off the page" do
error = broadcast_for(BROKEN, "")[:errors].first
parsed = Herb.parse(BROKEN, strict: true, analyze: true).errors.first.to_diagnostic(template: "app/views/posts/index.html.erb")
assert_equal parsed.code, error[:code]
assert_equal parsed.suggestion, error[:suggestion]
assert_equal parsed.message, error[:message]
end
test "sends the whole file with the errors, so the panel can highlight it" do
assert_equal BROKEN, broadcast_for(BROKEN, "")[:source]
end
test "sends the content it just read, not the content it replaced" do
assert_equal BROKEN, broadcast_for(BROKEN, "\n
\n")[:source]
end
test "a fixed file broadcasts a clearing schema and never a fixed message" do
websocket = FakeWebSocket.new
pipeline = Herb::Dev::Pipeline.new(server: websocket, compiler: -> {})
broken = Herb::Dev::Watcher::Event.new(kind: :changed, path: "/a", relative_path: "a.html.erb", previous: "", current: BROKEN)
fixed = Herb::Dev::Watcher::Event.new(kind: :changed, path: "/a", relative_path: "a.html.erb", previous: BROKEN, current: "\n \n
\n")
capture_io do
pipeline.handle_event(broken)
pipeline.handle_event(fixed)
end
types = websocket.messages.map { |message| message[:type] }
assert_equal ["error", "schema", "invalidate"], types
schema = websocket.messages.find { |message| message[:type] == "schema" }
assert_equal [], schema[:diagnostics]
end
test "text_changed is patchable" do
diff_result = Herb.diff("Hello
", "World
")
assert Herb::Dev::Runner.can_patch?(diff_result.operations)
assert_equal 1, diff_result.operation_count
assert_equal :text_changed, diff_result.operations[0].type
end
test "attribute_value_changed is patchable" do
diff_result = Herb.diff('Content
', 'Content
')
assert Herb::Dev::Runner.can_patch?(diff_result.operations)
assert_equal 1, diff_result.operation_count
assert_equal :attribute_value_changed, diff_result.operations[0].type
end
test "attribute_added is patchable" do
diff_result = Herb.diff("Content
", 'Content
')
assert Herb::Dev::Runner.can_patch?(diff_result.operations)
assert_equal 1, diff_result.operation_count
assert_equal :attribute_added, diff_result.operations[0].type
end
test "attribute_removed is patchable" do
diff_result = Herb.diff('Content
', "Content
")
assert Herb::Dev::Runner.can_patch?(diff_result.operations)
assert_equal 1, diff_result.operation_count
assert_equal :attribute_removed, diff_result.operations[0].type
end
test "node_inserted triggers reload" do
diff_result = Herb.diff("", "New
")
refute Herb::Dev::Runner.can_patch?(diff_result.operations)
end
test "node_removed triggers reload" do
diff_result = Herb.diff("Old
", "")
refute Herb::Dev::Runner.can_patch?(diff_result.operations)
end
test "erb_content_changed triggers reload" do
diff_result = Herb.diff("<%= foo %>", "<%= bar %>")
refute Herb::Dev::Runner.can_patch?(diff_result.operations)
end
test "inserting ERB node triggers reload" do
diff_result = Herb.diff("Hello
", "<%= name %>
")
refute Herb::Dev::Runner.can_patch?(diff_result.operations)
end
test "identical templates produce no operations" do
diff_result = Herb.diff("Hello
", "Hello
")
assert diff_result.identical?
assert_equal 0, diff_result.operation_count
end
test "multiple patchable operations are all patchable" do
diff_result = Herb.diff('Hello
', 'World
')
assert Herb::Dev::Runner.can_patch?(diff_result.operations)
assert_equal 2, diff_result.operation_count
end
test "mixed patchable and non-patchable triggers reload" do
diff_result = Herb.diff('Hello
', 'Hello
New')
refute Herb::Dev::Runner.can_patch?(diff_result.operations)
end
test "indexing writes no cursor escapes when stdout is not a terminal" do
directory = Dir.mktmpdir("herb_dev_runner_test")
File.write(File.join(directory, "index.html.erb"), "Hello
\n")
config = Herb::Configuration.load(directory)
watcher = Herb::Dev::Watcher.new(config: config, root: directory) { |event| event }
pipeline = Herb::Dev::Pipeline.new(server: FakeWebSocket.new, compiler: -> {})
output, = capture_io do
Herb::Dev::Runner.new(path: directory).send(:index_files, watcher, pipeline)
end
assert_equal " Files: 1 template indexed\n", output
ensure
FileUtils.rm_rf(directory)
Herb.reset_configuration!
end
class TerminalOutput < StringIO
def tty?
true
end
end
def index_on_terminal(process_name)
directory = Dir.mktmpdir("herb_dev_runner_test")
File.write(File.join(directory, "index.html.erb"), "Hello
\n")
config = Herb::Configuration.load(directory)
watcher = Herb::Dev::Watcher.new(config: config, root: directory) { |event| event }
pipeline = Herb::Dev::Pipeline.new(server: FakeWebSocket.new, compiler: -> {})
with_process_name(process_name) do
on_terminal { Herb::Dev::Runner.new(path: directory).send(:index_files, watcher, pipeline) }
end
ensure
FileUtils.rm_rf(directory)
Herb.reset_configuration!
end
def on_terminal
previous_stdout = $stdout
$stdout = TerminalOutput.new
yield
$stdout.string
ensure
$stdout = previous_stdout
end
def with_process_name(process_name)
previous = ENV.fetch("PS", nil)
process_name ? ENV["PS"] = process_name : ENV.delete("PS")
yield
ensure
previous ? ENV["PS"] = previous : ENV.delete("PS")
end
test "indexing rewrites its placeholder line on a terminal of its own" do
assert_equal " Indexing files...\n\e[1A\e[2K Files: 1 template indexed\n", index_on_terminal(nil)
end
test "indexing writes no cursor escapes on a terminal shared through a process manager" do
assert_equal " Files: 1 template indexed\n", index_on_terminal("herb")
end
def paint_error(previous, current, broken: [])
runner = Herb::Dev::Runner.new
runner.instance_variable_set(:@broken_files, Set.new(broken))
classification = Herb::Dev::Classifier.new.call(previous, current)
event = Herb::Dev::Watcher::Event.new(
kind: :changed,
path: "/app/views/posts/index.html.erb",
relative_path: "app/views/posts/index.html.erb",
previous: previous,
current: current
)
output, = capture_io do
runner.send(:paint_change, event, classification, "12:00:00", "index.html.erb")
end
output
end
test "prints the diagnostic with its source context and suggestion" do
output = paint_error("", BROKEN)
expected = [
" 12:00:00 \u2717 error \u2718 [MissingClosingTagError] Opening tag `` in the same scope.",
"",
" app/views/posts/index.html.erb:2:3:",
" 2 \u2502