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: 1 addition & 1 deletion exe/dead_end
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ warn "Record dir: #{options[:record_dir]}" if options[:record_dir]
display = DeadEnd.call(
source: file.read,
filename: file.expand_path,
terminal: options[:terminal],
terminal: options.fetch(:terminal, DeadEnd::DEFAULT_VALUE),
record_dir: options[:record_dir]
)

Expand Down
5 changes: 3 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,9 @@ module DeadEnd
class DisplayInvalidBlocks
attr_reader :filename

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

@filename = filename
@io = io

Expand Down
8 changes: 6 additions & 2 deletions lib/dead_end/internals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
require "timeout"

module DeadEnd
# Used to indicate a default value that cannot
# be confused with another input
DEFAULT_VALUE = Object.new.freeze
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea. I was thinking of merging the IO object and terminal into one argument, but didn't since I wasn't sure it is a good idea. Using nil/false/true might be a bit fragile.


class Error < StandardError; end
SEARCH_SOURCE_ON_ERROR_DEFAULT = true
TIMEOUT_DEFAULT = ENV.fetch("DEAD_END_TIMEOUT", 1).to_i
Expand All @@ -27,14 +31,14 @@ def self.handle_error(e, search_source_on_error: SEARCH_SOURCE_ON_ERROR_DEFAULT)
if search_source_on_error
call(
source: Pathname(filename).read,
filename: filename,
filename: filename
)
end

raise e
end

def self.call(source:, filename:, terminal: nil, record_dir: nil, timeout: TIMEOUT_DEFAULT, io: $stderr)
def self.call(source:, filename:, terminal: DEFAULT_VALUE, record_dir: nil, timeout: TIMEOUT_DEFAULT, io: $stderr)
search = nil
Timeout.timeout(timeout) do
record_dir ||= ENV["DEBUG"] ? "tmp" : nil
Expand Down
18 changes: 8 additions & 10 deletions spec/integration/exe_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,15 @@ def exe(cmd)
end
end

describe "terminal coloring" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to write some specs here for testing tty, but came to the same conclusion as you did. It's hard.

# 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")
# When ruby sub shells it is not a interactive shell and dead_end will
# default to no coloring. Colors/bold can be forced with `--terminal`
# flag
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
expect(out.strip).to include("Missing `end` detected")
expect(out.strip).to include("\e[0m❯ 36 \e[1;3m def filename")
end

it "records search" do
Expand Down
44 changes: 44 additions & 0 deletions spec/unit/display_invalid_blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,50 @@ def hai
expect(io.string).to include("Syntax OK")
end

it "selectively prints to terminal if input is a tty by default" do
source = <<~EOM
class OH
def hello
def hai
end
end
EOM

code_lines = CleanDocument.new(source: source).call.lines

io = StringIO.new
def io.isatty
true
end

block = CodeBlock.new(lines: code_lines[1])
display = DisplayInvalidBlocks.new(
io: io,
blocks: block,
code_lines: code_lines
)
display.call
expect(io.string).to include([
"❯ 2 ",
DisplayCodeWithLineNumbers::TERMINAL_HIGHLIGHT,
" def hello"
].join)

io = StringIO.new
def io.isatty
false
end

block = CodeBlock.new(lines: code_lines[1])
display = DisplayInvalidBlocks.new(
io: io,
blocks: block,
code_lines: code_lines
)
display.call
expect(io.string).to include("❯ 2 def hello")
end

it "outputs to io when using `call`" do
source = <<~EOM
class OH
Expand Down