Skip to content

Commit 2b25b2d

Browse files
author
Andy Waite
authored
Test server.rb with better isolation (#585)
* Test `server.rb` with better isolation * Ensure sorbet not loaded * Explain shim * Explain the separate Rake tasks
1 parent 3d1ab4e commit 2b25b2d

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

Rakefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@ load "rails/tasks/statistics.rake"
1010
require "bundler/gem_tasks"
1111
require "rake/testtask"
1212

13+
# Since `server.rb` runs within the host Rails application, we want to ensure
14+
# we don't accidentally depend on sorbet-runtime. `server_test` intentionally doesn't use `test_helper`.
15+
# We run `server_test` in a seperate Rake task.
1316
Rake::TestTask.new(:test) do |t|
1417
t.libs << "test"
1518
t.libs << "lib"
16-
t.test_files = FileList["test/**/*_test.rb"]
19+
t.test_files = FileList["test/**/*_test.rb"] - ["test/ruby_lsp_rails/server_test.rb"]
1720
end
1821

19-
task default: [:"db:setup", :test]
22+
Rake::TestTask.new(:server_test) do |t|
23+
t.libs << "test"
24+
t.libs << "lib"
25+
t.test_files = ["test/ruby_lsp_rails/server_test.rb"]
26+
end
27+
28+
task default: [:"db:setup", :test, :server_test]

sorbet/rbi/shims/server_test.rbi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
module RubyLsp
5+
module Rails
6+
class Server
7+
# We need this since RBS doesn't yet have a replacement for T.unsafe()
8+
def print_it!; end
9+
end
10+
end
11+
end

test/ruby_lsp_rails/server_test.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
# typed: true
22
# frozen_string_literal: true
33

4-
require "test_helper"
4+
# Configure Rails Environment
5+
ENV["RAILS_ENV"] = "test"
6+
require_relative "../dummy/config/environment"
7+
8+
require "minitest/autorun"
9+
require "mocha/minitest"
510
require "ruby_lsp/ruby_lsp_rails/server"
611

12+
if defined?(T)
13+
puts "T is defined, indicating that sorbet-runtime was loaded"
14+
puts "This test should run without sorbet-runtime"
15+
exit(1)
16+
end
17+
718
class ServerTest < ActiveSupport::TestCase
819
setup do
920
@stdout = StringIO.new
@@ -246,7 +257,7 @@ def print_it!
246257
end
247258
end
248259

249-
T.unsafe(server).print_it!
260+
server.print_it!
250261

251262
assert_match("Content-Length: 70\r\n\r\n", stderr.string)
252263
assert_match(

0 commit comments

Comments
 (0)