Skip to content

Commit 9ff8074

Browse files
authored
Migrate tests off sorbet-runtime and replace type-erasure casts with total methods (#71)
* Migrate tests off sorbet-runtime and trim gemspec sorbet deps The migration in #68 left the test suite using Sorbet's runtime DSL (extend T::Sig, sig, T.must, T.unsafe, T.bind), which only worked because packwerk transitively loaded sorbet-runtime. packwerk 3.3.0 drops sorbet-runtime, so the suite fails with 'uninitialized constant T'. Rather than re-add a sorbet-runtime dependency, finish the migration in test/ the same way as lib/: - Translate sig blocks to #: RBS comments and drop extend T::Sig - Replace requires_ancestor/T.bind with a Minitest::Runnable @requires_ancestor annotation in the fixture helper - Convert T.must to '#: as !nil' and T.unsafe to '#: as untyped' casts Also drop the redundant 'sorbet-static' dev dependency (the 'sorbet' gem already depends on it). * Avoid untyped cast in integration specs Use Enumerable#any? with the class as a === pattern instead of casting the abstract Packwerk::Checker element to untyped to call is_a?. This keeps the check fully typed (no type erasure). * Use Array#fetch(-1) instead of last with a non-nil cast fetch(-1) returns a non-nil element type, so no '#: as !nil' cast is needed (and it raises IndexError rather than silently returning nil). * Use total methods instead of non-nil casts in privacy checker - publicized_locations.fetch(location) instead of [] with '#: as !nil' (the key is guaranteed present by the preceding key? guard) - lines.first(5) instead of lines[0..4] with '#: as !nil' (first(n) returns a non-nil Array)
1 parent dc6689f commit 9ff8074

18 files changed

Lines changed: 19 additions & 49 deletions

Gemfile.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ DEPENDENCIES
268268
rubocop-gusto
269269
rubocop-sorbet!
270270
sorbet
271-
sorbet-static
272271
tapioca
273272

274273
BUNDLED WITH

lib/packwerk/privacy/checker.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def publicized_location?(location)
2525
publicized_locations[location] = check_for_publicized_sigil(location)
2626
end
2727

28-
publicized_locations[location] #: as !nil
28+
publicized_locations.fetch(location)
2929
end
3030

3131
#: (String location) -> bool
@@ -35,8 +35,7 @@ def check_for_publicized_sigil(location)
3535

3636
#: (Array[String] lines) -> bool
3737
def content_contains_sigil?(lines)
38-
first_lines = lines[0..4] #: as !nil
39-
first_lines.any? { |l| l =~ PUBLICIZED_SIGIL_REGEX }
38+
lines.first(5).any? { |l| l =~ PUBLICIZED_SIGIL_REGEX }
4039
end
4140
end
4241

packwerk-extensions.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,5 @@ Gem::Specification.new do |spec|
3434
spec.add_development_dependency 'rubocop'
3535
spec.add_development_dependency 'rubocop-gusto'
3636
spec.add_development_dependency 'sorbet'
37-
spec.add_development_dependency 'sorbet-static'
3837
spec.add_development_dependency 'tapioca'
3938
end

test/integration/extension_test.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
module Packwerk
77
module Privacy
88
class ExtensionTest < Minitest::Test
9-
extend T::Sig
10-
119
include ApplicationFixtureHelper
1210

1311
setup do
@@ -22,10 +20,7 @@ class ExtensionTest < Minitest::Test
2220
use_template(:extended)
2321
Packwerk::Checker.all
2422
assert_equal(Packwerk::Checker.all.count, 5)
25-
found_checker = Packwerk::Checker.all.any? do |checker|
26-
T.unsafe(checker).is_a?(Packwerk::Privacy::Checker)
27-
end
28-
assert found_checker
23+
assert(Packwerk::Checker.all.any?(Packwerk::Privacy::Checker))
2924
end
3025
end
3126
end

test/integration/extension_with_unrecognized_config_test.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
module Packwerk
77
module Privacy
88
class ExtensionWithUnrecognizedConfigTest < Minitest::Test
9-
extend T::Sig
10-
119
include ApplicationFixtureHelper
1210

1311
setup do
@@ -22,10 +20,7 @@ class ExtensionWithUnrecognizedConfigTest < Minitest::Test
2220
use_template(:with_unrecognized_config)
2321
Packwerk::Checker.all
2422
assert_equal(Packwerk::Checker.all.count, 5)
25-
found_checker = Packwerk::Checker.all.any? do |checker|
26-
T.unsafe(checker).is_a?(Packwerk::Privacy::Checker)
27-
end
28-
assert found_checker
23+
assert(Packwerk::Checker.all.any?(Packwerk::Privacy::Checker))
2924
end
3025
end
3126
end

test/support/application_fixture_helper.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
# typed: true
22
# frozen_string_literal: true
33

4+
# @requires_ancestor: Minitest::Runnable
45
module ApplicationFixtureHelper
56
TEMP_FIXTURE_DIR = ROOT.join('tmp', 'fixtures').to_s
67
DEFAULT_TEMPLATE = :minimal
78

8-
extend T::Helpers
9-
10-
requires_ancestor { Kernel }
11-
129
def setup_application_fixture
1310
@old_working_dir = Dir.pwd
1411
end
@@ -80,13 +77,12 @@ def using_template?
8077
end
8178

8279
def copy_dir(path)
83-
root = FileUtils.mkdir_p(fixture_path).last
84-
FileUtils.cp_r("#{path}/.", T.must(root))
80+
root = FileUtils.mkdir_p(fixture_path).fetch(-1)
81+
FileUtils.cp_r("#{path}/.", root)
8582
@app_dir = root
8683
end
8784

8885
def fixture_path
89-
T.bind(self, Minitest::Runnable)
9086
File.join(TEMP_FIXTURE_DIR, "#{name}-#{Time.now.strftime('%Y_%m_%d_%H_%M_%S')}")
9187
end
9288
end

test/unit/folder_privacy/checker_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
module Packwerk
77
module FolderPrivacy
88
class CheckerTest < Minitest::Test
9-
extend T::Sig
109
include FactoryHelper
1110
include RailsApplicationFixtureHelper
1211

@@ -61,7 +60,7 @@ class CheckerTest < Minitest::Test
6160

6261
private
6362

64-
sig { returns(Checker) }
63+
#: -> Checker
6564
def folder_privacy_checker
6665
FolderPrivacy::Checker.new
6766
end

test/unit/folder_privacy/validator_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
module Packwerk
1010
module FolderPrivacy
1111
class ValidatorTest < Minitest::Test
12-
extend T::Sig
1312
include ApplicationFixtureHelper
1413
include RailsApplicationFixtureHelper
1514

@@ -40,7 +39,7 @@ class ValidatorTest < Minitest::Test
4039
assert result.ok?
4140
end
4241

43-
sig { returns(Packwerk::FolderPrivacy::Validator) }
42+
#: -> Packwerk::FolderPrivacy::Validator
4443
def validator
4544
@validator ||= Packwerk::FolderPrivacy::Validator.new
4645
end

test/unit/layer/checker_architecture_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
module Packwerk
77
module Layer
88
class CheckerTest < Minitest::Test
9-
extend T::Sig
109
include FactoryHelper
1110
include RailsApplicationFixtureHelper
1211

@@ -120,7 +119,7 @@ def utility_pack(enforce: false)
120119

121120
private
122121

123-
sig { returns(Checker) }
122+
#: -> Checker
124123
def layer_checker
125124
Packwerk::Layer::Checker.new
126125
end

test/unit/layer/checker_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
module Packwerk
77
module Layer
88
class CheckerTest < Minitest::Test
9-
extend T::Sig
109
include FactoryHelper
1110
include RailsApplicationFixtureHelper
1211

@@ -124,7 +123,7 @@ def utility_pack(enforce: false)
124123

125124
private
126125

127-
sig { returns(Checker) }
126+
#: -> Checker
128127
def layer_checker
129128
Packwerk::Layer::Checker.new
130129
end

0 commit comments

Comments
 (0)