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
19 changes: 14 additions & 5 deletions lib/dead_end/around_block_scan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,20 @@ def scan_neighbors
self.scan_while {|line| line.not_empty? && line.indent >= @orig_indent }
end

def next_up
@code_lines[before_index.pred]
end

def next_down
@code_lines[after_index.next]
end

def scan_adjacent_indent
before_indent = @code_lines[@orig_before_index.pred]&.indent || 0
after_indent = @code_lines[@orig_after_index.next]&.indent || 0
before_after_indent = []
before_after_indent << next_up&.indent || 0
before_after_indent << next_down&.indent || 0

indent = [before_indent, after_indent].min
indent = before_after_indent.min
self.scan_while {|line| line.not_empty? && line.indent >= indent }

self
Expand All @@ -183,11 +192,11 @@ def after_index
end

private def before_lines
@code_lines[0...@orig_before_index]
@code_lines[0...@orig_before_index] || []
end

private def after_lines
@code_lines[@orig_after_index.next..-1]
@code_lines[after_index.next..-1] || []
end
end
end
7 changes: 4 additions & 3 deletions lib/dead_end/internals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ def self.handle_error(e, search_source_on_error: SEARCH_SOURCE_ON_ERROR_DEFAULT)
raise e
end

def self.call(source: , filename: , terminal: false, record_dir: nil, timeout: TIMEOUT_DEFAULT)
def self.call(source: , filename: , terminal: false, record_dir: nil, timeout: TIMEOUT_DEFAULT, io: $stderr)
search = nil
Timeout.timeout(timeout) do
record_dir ||= ENV["DEBUG"] ? "tmp" : nil
search = CodeSearch.new(source, record_dir: record_dir).call
end

Expand All @@ -50,10 +51,10 @@ def self.call(source: , filename: , terminal: false, record_dir: nil, timeout: T
terminal: terminal,
code_lines: search.code_lines,
invalid_obj: invalid_type(source),
io: $stderr
io: io
).call
rescue Timeout::Error
$stderr.puts "Search timed out DEAD_END_TIMEOUT=#{timeout}, run with DEBUG=1 for more info"
io.puts "Search timed out DEAD_END_TIMEOUT=#{timeout}, run with DEBUG=1 for more info"
end

# Used for counting spaces
Expand Down
2 changes: 1 addition & 1 deletion lib/dead_end/parse_blocks_from_indent_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def each_neighbor_block(target_line)
.skip(:hidden?)
.scan_while {|line| line.indent >= target_line.indent }

neighbors = @code_lines[scan.before_index..scan.after_index]
neighbors = scan.code_block.lines

until neighbors.empty?
lines = [neighbors.pop]
Expand Down
70 changes: 70 additions & 0 deletions spec/integration/exe_cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# frozen_string_literal: true

require_relative "../spec_helper.rb"

module DeadEnd
RSpec.describe "exe" do
def exe_path
root_dir.join("exe").join("dead_end")
end

def exe(cmd)
out = run!("#{exe_path} #{cmd}")
puts out if ENV["DEBUG"]
out
end

it "parses valid code" do
ruby_file = exe_path
out = exe(ruby_file)
expect(out.strip).to include("Syntax OK")
end

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

expect(out.strip).to include("Missing `end` detected")
expect(out.strip).to include("❯ 36 def filename")
end

it "handles heredocs" do
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")

expect(out).to include(<<~EOM.indent(4))
16 class Rexe
40 class Options < Struct.new(
71 end
❯ 77 class Lookups
❯ 78 def input_modes
❯ 148 end
152 class CommandLineParser
418 end
551 end
EOM
end
end

it "records search" do
Dir.mktmpdir do |dir|
dir = Pathname(dir)
tmp_dir = dir.join("tmp").tap(&:mkpath)
ruby_file = dir.join("file.rb")
ruby_file.write("def foo\n end\nend")

expect(tmp_dir).to be_empty

out = exe("#{ruby_file} --record #{tmp_dir}")

expect(out.strip).to include("Unmatched `end` detected")
expect(tmp_dir).to_not be_empty
end
end
end
end
73 changes: 73 additions & 0 deletions spec/integration/ruby_command_line_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

require_relative "../spec_helper.rb"

module DeadEnd
RSpec.describe "Requires with ruby cli" do
it "detects require error and adds a message with auto mode" do
Dir.mktmpdir do |dir|
@tmpdir = Pathname(dir)
@script = @tmpdir.join("script.rb")
@script.write <<~EOM
describe "things" do
it "blerg" do
end

it "flerg"
end

it "zlerg" do
end
end
EOM

require_rb = @tmpdir.join("require.rb")
require_rb.write <<~EOM
require_relative "./script.rb"
EOM

out = `ruby -I#{lib_dir} -rdead_end/auto #{require_rb} 2>&1`

expect(out).to include("Unmatched `end` detected")
expect(out).to include("Run `$ dead_end")
expect($?.success?).to be_falsey

out = `ruby -I#{lib_dir} -rdead_end #{require_rb} 2>&1`

expect(out).to include("Unmatched `end` detected")
expect(out).to include("Run `$ dead_end")
expect($?.success?).to be_falsey
end
end

it "detects require error and adds a message with fyi mode" do
Dir.mktmpdir do |dir|
@tmpdir = Pathname(dir)
@script = @tmpdir.join("script.rb")
@script.write <<~EOM
describe "things" do
it "blerg" do
end

it "flerg"
end

it "zlerg" do
end
end
EOM

require_rb = @tmpdir.join("require.rb")
require_rb.write <<~EOM
require_relative "./script.rb"
EOM

out = `ruby -I#{lib_dir} -rdead_end/fyi #{require_rb} 2>&1`

expect(out).to_not include("This code has an unmatched")
expect(out).to include("Run `$ dead_end")
expect($?.success?).to be_falsey
end
end
end
end
69 changes: 0 additions & 69 deletions spec/unit/dead_end_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,5 @@

module DeadEnd
RSpec.describe DeadEnd do
it "has a version number" do
expect(DeadEnd::VERSION).not_to be nil
end

it "detects require error and adds a message with auto mode" do
Dir.mktmpdir do |dir|
@tmpdir = Pathname(dir)
@script = @tmpdir.join("script.rb")
@script.write <<~EOM
describe "things" do
it "blerg" do
end

it "flerg"
end

it "zlerg" do
end
end
EOM

require_rb = @tmpdir.join("require.rb")
require_rb.write <<~EOM
require_relative "./script.rb"
EOM

out = `ruby -I#{lib_dir} -rdead_end/auto #{require_rb} 2>&1`

expect(out).to include("Unmatched `end` detected")
expect(out).to include("Run `$ dead_end")
expect($?.success?).to be_falsey

out = `ruby -I#{lib_dir} -rdead_end #{require_rb} 2>&1`

expect(out).to include("Unmatched `end` detected")
expect(out).to include("Run `$ dead_end")
expect($?.success?).to be_falsey
end
end

it "detects require error and adds a message with fyi mode" do
Dir.mktmpdir do |dir|
@tmpdir = Pathname(dir)
@script = @tmpdir.join("script.rb")
@script.write <<~EOM
describe "things" do
it "blerg" do
end

it "flerg"
end

it "zlerg" do
end
end
EOM

require_rb = @tmpdir.join("require.rb")
require_rb.write <<~EOM
require_relative "./script.rb"
EOM

out = `ruby -I#{lib_dir} -rdead_end/fyi #{require_rb} 2>&1`

expect(out).to_not include("This code has an unmatched")
expect(out).to include("Run `$ dead_end")
expect($?.success?).to be_falsey
end
end
end
end
7 changes: 4 additions & 3 deletions spec/unit/display_invalid_blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def hai
code_lines = code_line_array(<<~EOM)
class OH
def hello

def hai
end
end
Expand All @@ -220,9 +221,9 @@ def hai
expect(display.code_block).to eq(<<~EOM)
1 class OH
❯ 2 def hello
3 def hai
4 end
5 end
4 def hai
5 end
6 end
EOM
end

Expand Down