From ef7ad072b4ae3b0dedcc82ab8d9f17d3ff7683f9 Mon Sep 17 00:00:00 2001 From: schneems Date: Fri, 11 Dec 2020 10:27:00 -0600 Subject: [PATCH 1/6] Allow DeadEnd.call to output to stringio object --- lib/dead_end/internals.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/dead_end/internals.rb b/lib/dead_end/internals.rb index c4bb39f..1612a87 100644 --- a/lib/dead_end/internals.rb +++ b/lib/dead_end/internals.rb @@ -37,7 +37,7 @@ 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 search = CodeSearch.new(source, record_dir: record_dir).call @@ -50,10 +50,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 From d054348118fafc2a2336f9d929e435cb6d33cf94 Mon Sep 17 00:00:00 2001 From: schneems Date: Fri, 11 Dec 2020 10:30:48 -0600 Subject: [PATCH 2/6] Move integration specs to integration folder --- spec/integration/exe_cli_spec.rb | 70 +++++++++++++++++++++ spec/integration/ruby_command_line_spec.rb | 73 ++++++++++++++++++++++ spec/unit/dead_end_spec.rb | 69 -------------------- 3 files changed, 143 insertions(+), 69 deletions(-) create mode 100644 spec/integration/exe_cli_spec.rb create mode 100644 spec/integration/ruby_command_line_spec.rb diff --git a/spec/integration/exe_cli_spec.rb b/spec/integration/exe_cli_spec.rb new file mode 100644 index 0000000..120d015 --- /dev/null +++ b/spec/integration/exe_cli_spec.rb @@ -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 diff --git a/spec/integration/ruby_command_line_spec.rb b/spec/integration/ruby_command_line_spec.rb new file mode 100644 index 0000000..ba71d02 --- /dev/null +++ b/spec/integration/ruby_command_line_spec.rb @@ -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 diff --git a/spec/unit/dead_end_spec.rb b/spec/unit/dead_end_spec.rb index 7786214..56e2561 100644 --- a/spec/unit/dead_end_spec.rb +++ b/spec/unit/dead_end_spec.rb @@ -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 From 44be659cda89cf6b8b730fd096fa09d05288a2cb Mon Sep 17 00:00:00 2001 From: schneems Date: Fri, 11 Dec 2020 10:42:19 -0600 Subject: [PATCH 3/6] Fix AroundBlockScan bug Previously when the indexes were being set, the `before_lines` and `after_lines` were not respecting the index moving. Now they are. --- lib/dead_end/around_block_scan.rb | 4 ++-- spec/unit/display_invalid_blocks_spec.rb | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/dead_end/around_block_scan.rb b/lib/dead_end/around_block_scan.rb index 8aea9df..c943a72 100644 --- a/lib/dead_end/around_block_scan.rb +++ b/lib/dead_end/around_block_scan.rb @@ -183,11 +183,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 diff --git a/spec/unit/display_invalid_blocks_spec.rb b/spec/unit/display_invalid_blocks_spec.rb index 9f9f3c4..6f108ec 100644 --- a/spec/unit/display_invalid_blocks_spec.rb +++ b/spec/unit/display_invalid_blocks_spec.rb @@ -206,6 +206,7 @@ def hai code_lines = code_line_array(<<~EOM) class OH def hello + def hai end end @@ -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 From c9d7198adf84453a4b37e5b1f784eed46f3a1379 Mon Sep 17 00:00:00 2001 From: schneems Date: Fri, 11 Dec 2020 10:49:51 -0600 Subject: [PATCH 4/6] Allow debugging env to enable logging --- lib/dead_end/internals.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/dead_end/internals.rb b/lib/dead_end/internals.rb index 1612a87..92afbcc 100644 --- a/lib/dead_end/internals.rb +++ b/lib/dead_end/internals.rb @@ -40,6 +40,7 @@ def self.handle_error(e, search_source_on_error: SEARCH_SOURCE_ON_ERROR_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 From 1c9ffe9806dfec95f81e7d6da16f342f69bb37cf Mon Sep 17 00:00:00 2001 From: schneems Date: Fri, 11 Dec 2020 10:50:10 -0600 Subject: [PATCH 5/6] Don't manually construct arrays when not required --- lib/dead_end/parse_blocks_from_indent_line.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dead_end/parse_blocks_from_indent_line.rb b/lib/dead_end/parse_blocks_from_indent_line.rb index b2cb436..9c5a4df 100644 --- a/lib/dead_end/parse_blocks_from_indent_line.rb +++ b/lib/dead_end/parse_blocks_from_indent_line.rb @@ -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] From d773844e8e61d3681f74968967df2994964b7289 Mon Sep 17 00:00:00 2001 From: schneems Date: Fri, 11 Dec 2020 12:05:42 -0600 Subject: [PATCH 6/6] Refactor `scan_adjacent_indent` for clarity --- lib/dead_end/around_block_scan.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/dead_end/around_block_scan.rb b/lib/dead_end/around_block_scan.rb index c943a72..b13ed72 100644 --- a/lib/dead_end/around_block_scan.rb +++ b/lib/dead_end/around_block_scan.rb @@ -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