-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #995 from Shopify/dont-load-engines-in-app
Filter engines in application directory so they are not loaded
- Loading branch information
Showing
5 changed files
with
108 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
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)) | ||
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) | ||
end | ||
|
||
sig { params(path: T.any(String, Pathname)).returns(String) } | ||
def to_realpath(path) | ||
path_string = path.to_s | ||
path_string = File.realpath(path_string) if File.exist?(path_string) | ||
path_string | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1073,6 +1073,80 @@ def foo(a, b, c, d, e, f, g, h); end | |
assert_empty_stderr(result) | ||
assert_success_status(result) | ||
end | ||
|
||
it "must not load engines in the application" do | ||
@project.write("config/application.rb", <<~RB) | ||
require "rails" | ||
module ModuleTest | ||
class Application < Rails::Application | ||
attr_reader :config | ||
def initialize | ||
super | ||
root = Pathname.new("#{@project.path}") | ||
@config = Rails::Application::Configuration.new(root) | ||
end | ||
end | ||
def self.application | ||
Application.new | ||
end | ||
end | ||
lib_dir = File.expand_path("../lib/", __dir__) | ||
# Add lib directory to load path | ||
$LOAD_PATH << lib_dir | ||
# Require files from lib directory | ||
Dir.glob("**/*.rb", base: lib_dir).sort.each do |file| | ||
require(file) | ||
end | ||
RB | ||
|
||
@project.write("config/environment.rb", <<~RB) | ||
require_relative "application.rb" | ||
RB | ||
|
||
@project.write("foo/app/models/foo.rb", <<~RB) | ||
raise NotImplementedError, "This file should not be loaded" | ||
RB | ||
|
||
@project.write("foo/lib/foo.rb", <<~RB) | ||
module Foo | ||
class Engine < ::Rails::Engine | ||
isolate_namespace Foo | ||
end | ||
end | ||
RB | ||
|
||
@project.write("foo/foo.gemspec", <<~GEMSPEC) | ||
Gem::Specification.new do |spec| | ||
spec.name = "foo" | ||
spec.version = "0.0.1" | ||
spec.authors = ["Maple Ong"] | ||
spec.email = ["[email protected]"] | ||
spec.summary = "Summary of Foo." | ||
spec.description = "Description of Foo." | ||
spec.files = Dir.chdir(File.expand_path(__dir__)) do | ||
Dir["{app,lib}/**/*"] | ||
end | ||
spec.add_dependency "rails", ">= 7.0.3" | ||
end | ||
GEMSPEC | ||
|
||
@project.require_real_gem("rails") | ||
@project.gemfile(<<~GEMFILE, append: true) | ||
gem 'foo', path: 'foo' | ||
GEMFILE | ||
|
||
@project.bundle_install | ||
res = @project.tapioca("gem activesupport") | ||
|
||
refute_includes(res.err, "This file should not be loaded") | ||
assert_success_status(res) | ||
end | ||
end | ||
|
||
describe "sync" do | ||
|