summaryrefslogtreecommitdiff
path: root/spec/syntax_suggest/integration
diff options
context:
space:
mode:
Diffstat (limited to 'spec/syntax_suggest/integration')
-rw-r--r--spec/syntax_suggest/integration/exe_cli_spec.rb3
-rw-r--r--spec/syntax_suggest/integration/ruby_command_line_spec.rb71
-rw-r--r--spec/syntax_suggest/integration/syntax_suggest_spec.rb135
3 files changed, 147 insertions, 62 deletions
diff --git a/spec/syntax_suggest/integration/exe_cli_spec.rb b/spec/syntax_suggest/integration/exe_cli_spec.rb
index f0b49b4386..b9a3173715 100644
--- a/spec/syntax_suggest/integration/exe_cli_spec.rb
+++ b/spec/syntax_suggest/integration/exe_cli_spec.rb
@@ -13,7 +13,8 @@ module SyntaxSuggest
end
def exe(cmd)
- out = run!("#{exe_path} #{cmd}", raise_on_nonzero_exit: false)
+ ruby = ENV.fetch("RUBY", "ruby")
+ out = run!("#{ruby} #{exe_path} #{cmd}", raise_on_nonzero_exit: false)
puts out if ENV["SYNTAX_SUGGEST_DEBUG"]
out
end
diff --git a/spec/syntax_suggest/integration/ruby_command_line_spec.rb b/spec/syntax_suggest/integration/ruby_command_line_spec.rb
index 6ed1bf0bf7..02354ceff0 100644
--- a/spec/syntax_suggest/integration/ruby_command_line_spec.rb
+++ b/spec/syntax_suggest/integration/ruby_command_line_spec.rb
@@ -3,12 +3,13 @@
require_relative "../spec_helper"
module SyntaxSuggest
+ ruby = ENV.fetch("RUBY", "ruby")
RSpec.describe "Requires with ruby cli" do
it "namespaces all monkeypatched methods" do
Dir.mktmpdir do |dir|
tmpdir = Pathname(dir)
script = tmpdir.join("script.rb")
- script.write <<~'EOM'
+ script.write <<~EOM
puts Kernel.private_methods
EOM
@@ -16,9 +17,9 @@ module SyntaxSuggest
api_only_methods_file = tmpdir.join("api_only_methods.txt")
kernel_methods_file = tmpdir.join("kernel_methods.txt")
- d_pid = Process.spawn("ruby -I#{lib_dir} -rsyntax_suggest #{script} 2>&1 > #{syntax_suggest_methods_file}")
- k_pid = Process.spawn("ruby #{script} 2>&1 >> #{kernel_methods_file}")
- r_pid = Process.spawn("ruby -I#{lib_dir} -rsyntax_suggest/api #{script} 2>&1 > #{api_only_methods_file}")
+ d_pid = Process.spawn("#{ruby} -I#{lib_dir} -rsyntax_suggest #{script} 2>&1 > #{syntax_suggest_methods_file}")
+ k_pid = Process.spawn("#{ruby} #{script} 2>&1 >> #{kernel_methods_file}")
+ r_pid = Process.spawn("#{ruby} -I#{lib_dir} -rsyntax_suggest/api #{script} 2>&1 > #{api_only_methods_file}")
Process.wait(k_pid)
Process.wait(d_pid)
@@ -45,9 +46,25 @@ module SyntaxSuggest
end
end
- it "detects require error and adds a message with auto mode" do
- skip if ruby_core?
+ # Since Ruby 3.2 includes syntax_suggest as a default gem, we might accidentally
+ # be requiring the default gem instead of this library under test. Assert that's
+ # not the case
+ it "tests current version of syntax_suggest" do
+ Dir.mktmpdir do |dir|
+ tmpdir = Pathname(dir)
+ script = tmpdir.join("script.rb")
+ contents = <<~'EOM'
+ puts "suggest_version is #{SyntaxSuggest::VERSION}"
+ EOM
+ script.write(contents)
+
+ out = `#{ruby} -I#{lib_dir} -rsyntax_suggest/version #{script} 2>&1`
+ expect(out).to include("suggest_version is #{SyntaxSuggest::VERSION}").once
+ end
+ 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")
@@ -69,19 +86,21 @@ module SyntaxSuggest
load "#{script.expand_path}"
EOM
- out = `ruby -I#{lib_dir} -rsyntax_suggest #{require_rb} 2>&1`
+ out = `#{ruby} -I#{lib_dir} -rsyntax_suggest #{require_rb} 2>&1`
expect($?.success?).to be_falsey
- expect(out).to include('❯ 5 it "flerg"').once
+ expect(out).to include('> 5 it "flerg"').once
end
end
- it "annotates a syntax error in Ruby 3.2+ when require is not used" do
- pending("Support for SyntaxError#detailed_message monkeypatch needed https://gist.github.com/schneems/09f45cc23b9a8c46e9af6acbb6e6840d?permalink_comment_id=4172585#gistcomment-4172585")
+ it "gem can be tested when executing on Ruby with default gem included" do
+ out = `#{ruby} -I#{lib_dir} -rsyntax_suggest -e "puts SyntaxError.instance_method(:detailed_message).source_location" 2>&1`
- skip if ruby_core?
- skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2")
+ expect($?.success?).to be_truthy
+ expect(out).to include(lib_dir.join("syntax_suggest").join("core_ext.rb").to_s).once
+ end
+ it "annotates a syntax error in Ruby 3.2+ when require is not used" do
Dir.mktmpdir do |dir|
tmpdir = Pathname(dir)
script = tmpdir.join("script.rb")
@@ -98,10 +117,10 @@ module SyntaxSuggest
end
EOM
- out = `ruby -I#{lib_dir} -rsyntax_suggest #{script} 2>&1`
+ out = `#{ruby} -I#{lib_dir} -rsyntax_suggest #{script} 2>&1`
expect($?.success?).to be_falsey
- expect(out).to include('❯ 5 it "flerg"').once
+ expect(out).to include('> 5 it "flerg"').once
end
end
@@ -125,7 +144,7 @@ module SyntaxSuggest
load "#{script.expand_path}"
EOM
- out = `ruby -I#{lib_dir} -rsyntax_suggest #{require_rb} 2>&1`
+ out = `#{ruby} -I#{lib_dir} -rsyntax_suggest #{require_rb} 2>&1`
expect($?.success?).to be_truthy
expect(out).to include("SyntaxSuggest is NOT loaded").once
@@ -136,19 +155,35 @@ module SyntaxSuggest
Dir.mktmpdir do |dir|
tmpdir = Pathname(dir)
script = tmpdir.join("script.rb")
- script.write <<~'EOM'
+ script.write <<~EOM
$stderr = STDOUT
eval("def lol")
EOM
- out = `ruby -I#{lib_dir} -rsyntax_suggest #{script} 2>&1`
+ out = `#{ruby} -I#{lib_dir} -rsyntax_suggest #{script} 2>&1`
expect($?.success?).to be_falsey
- expect(out).to include("(eval):1")
+ expect(out).to match(/\(eval.*\):1/)
expect(out).to_not include("SyntaxSuggest")
expect(out).to_not include("Could not find filename")
end
end
+
+ it "does not say 'syntax ok' when a syntax error fires" do
+ Dir.mktmpdir do |dir|
+ tmpdir = Pathname(dir)
+ script = tmpdir.join("script.rb")
+ script.write <<~EOM
+ break
+ EOM
+
+ out = `#{ruby} -I#{lib_dir} -rsyntax_suggest -e "require_relative '#{script}'" 2>&1`
+
+ expect($?.success?).to be_falsey
+ expect(out.downcase).to_not include("syntax ok")
+ expect(out).to include("Invalid break")
+ end
+ end
end
end
diff --git a/spec/syntax_suggest/integration/syntax_suggest_spec.rb b/spec/syntax_suggest/integration/syntax_suggest_spec.rb
index a7287ff64e..de9bc5d38e 100644
--- a/spec/syntax_suggest/integration/syntax_suggest_spec.rb
+++ b/spec/syntax_suggest/integration/syntax_suggest_spec.rb
@@ -13,7 +13,7 @@ module SyntaxSuggest
io = StringIO.new
- benchmark = Benchmark.measure do
+ benchmark_measure do
debug_perf do
SyntaxSuggest.call(
io: io,
@@ -21,17 +21,17 @@ module SyntaxSuggest
filename: file
)
end
- debug_display(io.string)
- debug_display(benchmark)
end
- expect(io.string).to include(<<~'EOM')
+ debug_display(io.string)
+
+ expect(io.string).to include(<<~EOM)
6 class SyntaxTree < Ripper
170 def self.parse(source)
174 end
- ❯ 754 def on_args_add(arguments, argument)
- ❯ 776 class ArgsAddBlock
- ❯ 810 end
+ > 754 def on_args_add(arguments, argument)
+ > 776 class ArgsAddBlock
+ > 810 end
9233 end
EOM
end
@@ -41,7 +41,7 @@ module SyntaxSuggest
io = StringIO.new
debug_perf do
- benchmark = Benchmark.measure do
+ benchmark_measure do
SyntaxSuggest.call(
io: io,
source: file.read,
@@ -49,15 +49,14 @@ module SyntaxSuggest
)
end
debug_display(io.string)
- debug_display(benchmark)
end
expect(io.string).to_not include("def ruby_install_binstub_path")
- expect(io.string).to include(<<~'EOM')
- ❯ 1067 def add_yarn_binary
- ❯ 1068 return [] if yarn_preinstalled?
- ❯ 1069 |
- ❯ 1075 end
+ expect(io.string).to include(<<~EOM)
+ > 1067 def add_yarn_binary
+ > 1068 return [] if yarn_preinstalled?
+ > 1069 |
+ > 1075 end
EOM
end
@@ -71,11 +70,11 @@ module SyntaxSuggest
)
debug_display(io.string)
- expect(io.string).to include(<<~'EOM')
+ expect(io.string).to include(<<~EOM)
1 Rails.application.routes.draw do
- ❯ 113 namespace :admin do
- ❯ 116 match "/foobar(*path)", via: :all, to: redirect { |_params, req|
- ❯ 120 }
+ > 113 namespace :admin do
+ > 116 match "/foobar(*path)", via: :all, to: redirect { |_params, req|
+ > 120 }
121 end
EOM
end
@@ -90,12 +89,12 @@ module SyntaxSuggest
)
debug_display(io.string)
- expect(io.string).to include(<<~'EOM')
+ expect(io.string).to include(<<~EOM)
1 describe "webmock tests" do
22 it "body" do
27 query = Cutlass::FunctionQuery.new(
- ❯ 28 port: port
- ❯ 29 body: body
+ > 28 port: port
+ > 29 body: body
30 ).call
34 end
35 end
@@ -112,15 +111,12 @@ module SyntaxSuggest
)
debug_display(io.string)
- expect(io.string).to include(<<~'EOM')
+ expect(io.string).to include(<<~EOM)
5 module DerailedBenchmarks
6 class RequireTree
- 7 REQUIRED_BY = {}
- 9 attr_reader :name
- 10 attr_writer :cost
- ❯ 13 def initialize(name)
- ❯ 18 def self.reset!
- ❯ 25 end
+ > 13 def initialize(name)
+ > 18 def self.reset!
+ > 25 end
73 end
74 end
EOM
@@ -140,9 +136,9 @@ module SyntaxSuggest
expect(out).to include(<<~EOM)
16 class Rexe
- ❯ 77 class Lookups
- ❯ 78 def input_modes
- ❯ 148 end
+ > 77 class Lookups
+ > 78 def input_modes
+ > 148 end
551 end
EOM
end
@@ -160,16 +156,15 @@ module SyntaxSuggest
out = io.string
expect(out).to include(<<~EOM)
16 class Rexe
- 18 VERSION = '1.5.1'
- ❯ 77 class Lookups
- ❯ 140 def format_requires
- ❯ 148 end
+ > 77 class Lookups
+ > 140 def format_requires
+ > 148 end
551 end
EOM
end
it "ambiguous end" do
- source = <<~'EOM'
+ source = <<~EOM
def call # 0
print "lol" # 1
end # one # 2
@@ -182,14 +177,14 @@ module SyntaxSuggest
)
out = io.string
expect(out).to include(<<~EOM)
- ❯ 1 def call # 0
- ❯ 3 end # one # 2
- ❯ 4 end # two # 3
+ > 1 def call # 0
+ > 3 end # one # 2
+ > 4 end # two # 3
EOM
end
it "simple regression" do
- source = <<~'EOM'
+ source = <<~EOM
class Dog
def bark
puts "woof"
@@ -202,9 +197,63 @@ module SyntaxSuggest
)
out = io.string
expect(out).to include(<<~EOM)
- ❯ 1 class Dog
- ❯ 2 def bark
- ❯ 4 end
+ > 1 class Dog
+ > 2 def bark
+ > 4 end
+ EOM
+ end
+
+ it "multi-line chain with missing paren" do
+ source = <<~EOM
+ class Dog
+ def bark
+ User
+ .where(name: "schneems"
+ .first
+ end
+ end
+ EOM
+ io = StringIO.new
+ SyntaxSuggest.call(
+ io: io,
+ source: source
+ )
+ out = io.string
+ expect(out).to include(<<~EOM)
+ > 3 User
+ > 4 .where(name: "schneems"
+ > 5 .first
+ EOM
+ end
+
+ it "empty else" do
+ source = <<~EOM
+ class Foo
+ def foo
+ if cond?
+ foo
+ else
+
+ end
+ end
+
+ # ...
+
+ def bar
+ if @recv
+ end_is_missing_here
+ end
+ end
+ EOM
+
+ io = StringIO.new
+ SyntaxSuggest.call(
+ io: io,
+ source: source
+ )
+ out = io.string
+ expect(out).to include(<<~EOM)
+ end_is_missing_here
EOM
end
end