Skip to content

Commit

Permalink
Add backwards-compatibility test
Browse files Browse the repository at this point in the history
Co-authored-by: Alexandre Terrasa <[email protected]>
  • Loading branch information
dirceu and Morriar committed Nov 24, 2022
1 parent e21a55c commit 03be53d
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 19 deletions.
28 changes: 15 additions & 13 deletions exe/tapioca
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@

require "sorbet-runtime"

begin
T::Configuration.default_checked_level = :never
# Suppresses call validation errors
T::Configuration.call_validation_error_handler = ->(*) {}
# Suppresses errors caused by T.cast, T.let, T.must, etc.
T::Configuration.inline_type_error_handler = ->(*) {}
# Suppresses errors caused by incorrect parameter ordering
T::Configuration.sig_validation_error_handler = ->(*) {}
rescue
# Need this rescue so that if another gem has
# already set the checked level by the time we
# get to it, we don't fail outright.
nil
unless ENV["ENFORCE_TYPECHECKING"] == "1"
begin
T::Configuration.default_checked_level = :never
# Suppresses call validation errors
T::Configuration.call_validation_error_handler = ->(*) {}
# Suppresses errors caused by T.cast, T.let, T.must, etc.
T::Configuration.inline_type_error_handler = ->(*) {}
# Suppresses errors caused by incorrect parameter ordering
T::Configuration.sig_validation_error_handler = ->(*) {}
rescue
# Need this rescue so that if another gem has
# already set the checked level by the time we
# get to it, we don't fail outright.
nil
end
end

require_relative "../lib/tapioca/internal"
Expand Down
14 changes: 8 additions & 6 deletions spec/helpers/mock_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,19 @@ def bundle_install(version: nil)
end

# Run a `command` with `bundle exec` in this project context (unbundled env)
sig { params(command: String).returns(ExecResult) }
def bundle_exec(command)
sig { params(command: String, env: T::Hash[String, String]).returns(ExecResult) }
def bundle_exec(command, env = {})
opts = {}
opts[:chdir] = path
Bundler.with_unbundled_env do
out, err, status = Open3.capture3(["bundle", "_#{bundler_version}_", "exec", command].join(" "), opts)
out, err, status = Open3.capture3(env, ["bundle", "_#{bundler_version}_", "exec", command].join(" "), opts)
ExecResult.new(out: out, err: err, status: T.must(status.success?))
end
end

# Run a Tapioca `command` with `bundle exec` in this project context (unbundled env)
sig { params(command: String).returns(ExecResult) }
def tapioca(command)
sig { params(command: String, enforce_typechecking: T::Boolean).returns(ExecResult) }
def tapioca(command, enforce_typechecking = false)
exec_command = ["tapioca", command]
if command.start_with?(/gem/)
exec_command << "--workers=1" unless command.match?("--workers")
Expand All @@ -130,7 +130,9 @@ def tapioca(command)
elsif command.start_with?(/dsl/)
exec_command << "--workers=1" unless command.match?("--workers")
end
bundle_exec(exec_command.join(" "))
env = {}
env["ENFORCE_TYPECHECKING"] = "1" if enforce_typechecking
bundle_exec(exec_command.join(" "), env)
end
end
end
97 changes: 97 additions & 0 deletions spec/tapioca/cli/backwards_compatibility_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# typed: true
# frozen_string_literal: true

require "spec_helper"
require "tapioca/helpers/test/template"

module Tapioca
class BackwardsCompatibilitySpec < SpecWithProject
GENERIC_INTERFACE_RB = <<~RB
module GenericInterface
extend T::Generic
interface!
Parameter = type_member
end
class Concrete
extend T::Generic
include GenericInterface
Parameter = type_member
end
GENERIC_CONSTANT = T.let(
Concrete.new,
GenericInterface[Numeric]
)
RB

describe "compilation of constants of generic types" do
before do
@expected_out = <<~OUT
Compiled generic_interface
create sorbet/rbi/gems/[email protected]
OUT

@expected_rbi = <<~RBI
# typed: true
# DO NOT EDIT MANUALLY
# This is an autogenerated file for types exported from the `generic_interface` gem.
# Please instead update this file by running `bin/tapioca gem generic_interface`.
class Concrete
extend T::Generic
include ::GenericInterface
Parameter = type_member
end
GENERIC_CONSTANT = T.let(T.unsafe(nil), Concrete[T.untyped])
module GenericInterface
extend T::Generic
interface!
Parameter = type_member
end
RBI

@generic_interface = mock_gem("generic_interface", "0.0.1") do
write("lib/generic_interface.rb", GENERIC_INTERFACE_RB)
end
@project = mock_project(sorbet_dependency: false)
@project.require_mock_gem(@generic_interface)
end

after do
@project.destroy
end

it "must succeed on sorbet-runtime < 0.5.10554" do
@project.require_real_gem("sorbet-static-and-runtime", "=0.5.10539")
@project.bundle_install
result = @project.tapioca("gem", true)

assert_stdout_includes(result, @expected_out)
assert_project_file_equal("sorbet/rbi/gems/[email protected]", @expected_rbi)
assert_empty_stderr(result)
assert_success_status(result)
end

it "must succeed on sorbet-runtime >= 0.5.10554" do
@project.require_real_gem("sorbet-static-and-runtime", ">=0.5.10554")
@project.bundle_install
result = @project.tapioca("gem", true)

assert_stdout_includes(result, @expected_out)
assert_project_file_equal("sorbet/rbi/gems/[email protected]", @expected_rbi)
assert_empty_stderr(result)
assert_success_status(result)
end
end
end
end

0 comments on commit 03be53d

Please sign in to comment.