Skip to content

Commit

Permalink
Merge pull request #1175 from Shopify/uk-fix-directory-comparison
Browse files Browse the repository at this point in the history
Start matching pathname directory prefixes more rigorously
  • Loading branch information
paracycle authored Sep 18, 2022
2 parents 703ce22 + 1a68dcc commit 95d76a4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/tapioca/gemfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def contains_path?(path)
if default_gem?
files.any? { |file| file.to_s == to_realpath(path) }
else
to_realpath(path).start_with?(full_gem_path) || has_parent_gemspec?(path)
path_in_dir?(to_realpath(path), full_gem_path) || has_parent_gemspec?(path)
end
end

Expand Down
22 changes: 17 additions & 5 deletions lib/tapioca/helpers/gem_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ module Tapioca
module GemHelper
extend T::Sig

sig { params(gemfile_dir: String, full_gem_path: String).returns(T::Boolean) }
def gem_in_app_dir?(gemfile_dir, full_gem_path)
!gem_in_bundle_path?(to_realpath(full_gem_path)) &&
full_gem_path.start_with?(to_realpath(gemfile_dir))
sig { params(app_dir: String, full_gem_path: String).returns(T::Boolean) }
def gem_in_app_dir?(app_dir, full_gem_path)
app_dir = to_realpath(app_dir)
full_gem_path = to_realpath(full_gem_path)

!gem_in_bundle_path?(full_gem_path) && path_in_dir?(full_gem_path, app_dir)
end

sig { params(full_gem_path: String).returns(T::Boolean) }
def gem_in_bundle_path?(full_gem_path)
full_gem_path.start_with?(Bundler.bundle_path.to_s, Bundler.app_cache.to_s)
path_in_dir?(full_gem_path, Bundler.bundle_path) || path_in_dir?(full_gem_path, Bundler.app_cache)
end

sig { params(path: T.any(String, Pathname)).returns(String) }
Expand All @@ -22,5 +24,15 @@ def to_realpath(path)
path_string = File.realpath(path_string) if File.exist?(path_string)
path_string
end

private

sig { params(path: T.any(Pathname, String), dir: T.any(Pathname, String)).returns(T::Boolean) }
def path_in_dir?(path, dir)
dir = Pathname.new(dir)
path = Pathname.new(path)

path.ascend.any?(dir)
end
end
end
10 changes: 8 additions & 2 deletions spec/spec_with_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ def mock_project(sorbet_dependency: true, &block)
name: String,
version: String,
dependencies: T::Array[String],
path: String,
block: T.nilable(T.proc.params(gem: MockGem).bind(MockGem).void),
).returns(MockGem)
end
def mock_gem(name, version, dependencies: [], &block)
gem = MockGem.new("#{TEST_TMP_PATH}/#{spec_name}/gems/#{name}", name, version, dependencies)
def mock_gem(name, version, dependencies: [], path: default_gem_path(name), &block)
gem = MockGem.new(path, name, version, dependencies)
gem.gemspec(gem.default_gemspec_contents)
gem.instance_exec(gem, &block) if block
gem
Expand Down Expand Up @@ -139,6 +140,11 @@ def assert_stderr_includes(result, snippet)

private

sig { params(name: String).returns(String) }
def default_gem_path(name)
"#{TEST_TMP_PATH}/#{spec_name}/gems/#{name}"
end

sig { returns(String) }
def spec_name
spec_class = T.unsafe(self).class
Expand Down
14 changes: 14 additions & 0 deletions spec/tapioca/cli/gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,20 @@ class Foo::Secret; end
assert_success_status(result)
end

it "must generate RBIs for gems whose folder location starts with the same prefix as project folder" do
# Set the gem path so that the project folder is a prefix of the gem folder
gem_path = @project.path + "-gem/foo"
gem = mock_gem("foo", "0.0.1", path: gem_path)

@project.require_mock_gem(gem)

result = @project.tapioca("gem foo")

assert_stdout_includes(result, "Compiled foo")
assert_empty_stderr(result)
assert_success_status(result)
end

it "must respect exclude option" do
@project.require_mock_gem(mock_gem("foo", "0.0.1"))
@project.require_mock_gem(mock_gem("bar", "0.3.0"))
Expand Down

0 comments on commit 95d76a4

Please sign in to comment.