Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD (unreleased)

- Don't print terminal color codes when output is not tty (https://github.com/zombocom/dead_end/pull/91)

## 2.0.1

- Reintroduce Ruby 2.5 support (https://github.com/zombocom/dead_end/pull/90)
Expand Down
5 changes: 4 additions & 1 deletion exe/dead_end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ require "optparse"
require_relative "../lib/dead_end"

options = {}
options[:terminal] = true
options[:record_dir] = ENV["DEAD_END_RECORD_DIR"]

parser = OptionParser.new do |opts|
Expand Down Expand Up @@ -46,6 +45,10 @@ parser = OptionParser.new do |opts|
options[:record_dir] = v
end

opts.on("--terminal", "Enable terminal highlighting") do |v|
options[:terminal] = true
end

opts.on("--no-terminal", "Disable terminal highlighting") do |v|
options[:terminal] = false
end
Expand Down
4 changes: 2 additions & 2 deletions lib/dead_end/display_invalid_blocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module DeadEnd
class DisplayInvalidBlocks
attr_reader :filename

def initialize(code_lines:, blocks:, io: $stderr, filename: nil, terminal: false, invalid_obj: WhoDisSyntaxError::Null.new)
@terminal = terminal
def initialize(code_lines:, blocks:, io: $stderr, filename: nil, terminal: nil, invalid_obj: WhoDisSyntaxError::Null.new)
@terminal = terminal.nil? ? io.isatty : terminal
@filename = filename
@io = io

Expand Down
3 changes: 1 addition & 2 deletions lib/dead_end/internals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ def self.handle_error(e, search_source_on_error: SEARCH_SOURCE_ON_ERROR_DEFAULT)
call(
source: Pathname(filename).read,
filename: filename,
terminal: true
)
end

raise e
end

def self.call(source:, filename:, terminal: false, record_dir: nil, timeout: TIMEOUT_DEFAULT, io: $stderr)
def self.call(source:, filename:, terminal: nil, record_dir: nil, timeout: TIMEOUT_DEFAULT, io: $stderr)
search = nil
Timeout.timeout(timeout) do
record_dir ||= ENV["DEBUG"] ? "tmp" : nil
Expand Down
19 changes: 16 additions & 3 deletions spec/integration/exe_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def exe(cmd)

it "parses invalid code" do
ruby_file = fixtures_dir.join("this_project_extra_def.rb.txt")
out = exe("#{ruby_file} --no-terminal")
out = exe(ruby_file)

expect(out.strip).to include("Missing `end` detected")
expect(out.strip).to include("❯ 36 def filename")
Expand All @@ -32,13 +32,13 @@ def exe(cmd)
end

it "handles heredocs" do
lines = fixtures_dir.join("rexe.rb.txt").read.lines
Tempfile.create do |file|
lines = fixtures_dir.join("rexe.rb.txt").read.lines
lines.delete_at(85 - 1)

Pathname(file.path).write(lines.join)

out = exe("#{file.path} --no-terminal")
out = exe(file.path)

expect(out).to include(<<~EOM.indent(4))
16 class Rexe
Expand All @@ -50,6 +50,19 @@ def exe(cmd)
end
end

describe "terminal coloring" do
# When ruby sub shells it is not a interactive shell and dead_end will
# default to no coloring.

it "passing --terminal will force color codes" do
ruby_file = fixtures_dir.join("this_project_extra_def.rb.txt")
out = exe("#{ruby_file} --terminal")

expect(out.strip).to include("Missing `end` detected")
expect(out.strip).to include("\e[0m❯ 36 \e[1;3m def filename")
end
end

it "records search" do
Dir.mktmpdir do |dir|
dir = Pathname(dir)
Expand Down