diff --git a/gems/instruments.gemfile b/gems/instruments.gemfile index 679c7f69..1c2e5386 100644 --- a/gems/instruments.gemfile +++ b/gems/instruments.gemfile @@ -4,3 +4,5 @@ gem 'httpclient' gem 'http' gem 'redis' gem 'moped' +gem 'actionpack' +gem 'actionview' diff --git a/test/test_helper.rb b/test/test_helper.rb index dca6abc3..9cc3b3d6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -65,7 +65,13 @@ def method_missing(sym) end end +def remove_rails_namespace + Object.send(:remove_const, "Rails") if defined?(Rails) +end + def fake_rails(version) + remove_rails_namespace if (ENV["SCOUT_TEST_FEATURES"] || "").include?("instruments") + Kernel.const_set("Rails", Module.new) Kernel.const_set("ActionController", Module.new) r = Kernel.const_get("Rails") diff --git a/test/unit/instruments/action_view_test.rb b/test/unit/instruments/action_view_test.rb new file mode 100644 index 00000000..2a9b3795 --- /dev/null +++ b/test/unit/instruments/action_view_test.rb @@ -0,0 +1,102 @@ +# Most of this was taken from Rails: +# https://github.com/rails/rails/blob/v7.1.3/actionview/test/actionpack/controller/render_test.rb +# https://github.com/rails/rails/blob/v7.1.3/actionview/test/abstract_unit.rb + +if (ENV["SCOUT_TEST_FEATURES"] || "").include?("instruments") + require 'test_helper' + require 'action_view' + require 'action_pack' + require 'action_controller' + + FIXTURE_LOAD_PATH = File.expand_path("fixtures", __dir__) + + include ActionView::Context + include ActionView::Helpers::TagHelper + include ActionView::Helpers::TextHelper + + module ActionController + + class Base + self.view_paths = FIXTURE_LOAD_PATH + + def self.test_routes(&block) + routes = ActionDispatch::Routing::RouteSet.new + routes.draw(&block) + include routes.url_helpers + routes + end + end + + class TestCase + include ActionDispatch::TestProcess + + def self.with_routes(&block) + routes = ActionDispatch::Routing::RouteSet.new + routes.draw(&block) + include Module.new { + define_method(:setup) do + super() + @routes = routes + @controller.singleton_class.include @routes.url_helpers if @controller + end + } + routes + end + end + end + + class TestController < ActionController::Base + + def render_test_view + render template: "test_view" + end + end + + class RenderTest < ActionController::TestCase + + tests TestController + + with_routes do + get :render_test_view, to: "test#render_test_view" + end + + def setup + super + @controller.logger = ActiveSupport::Logger.new(nil) + ActionView::Base.logger = ActiveSupport::Logger.new(nil) + + @request.host = "www.scoutapm.com" + + @old_view_paths = ActionController::Base.view_paths + ActionController::Base.view_paths = FIXTURE_LOAD_PATH + end + + def teardown + ActionView::Base.logger = nil + + ActionController::Base.view_paths = @old_view_paths + end + + def test_partial_instrumentation + recorder = FakeRecorder.new + agent_context.recorder = recorder + + instrument = ScoutApm::Instruments::ActionView.new(agent_context) + instrument.install(prepend: true) + + get :render_test_view + assert_response :success + + root_layer = recorder.requests.first.root_layer + children = root_layer.children.to_a + assert_equal 2, children.size + + partial_layer = children[0] + collection_layer = children[1] + + assert_equal "test_view/Rendering", root_layer.name + assert_equal "test/_test_partial/Rendering", partial_layer.name + assert_equal "test/_test_partial_collection/Rendering", collection_layer.name + end + end +end diff --git a/test/unit/instruments/fixtures/test/_test_partial.html.erb b/test/unit/instruments/fixtures/test/_test_partial.html.erb new file mode 100644 index 00000000..ac1a569c --- /dev/null +++ b/test/unit/instruments/fixtures/test/_test_partial.html.erb @@ -0,0 +1,3 @@ +
+

<%= message %>

+
diff --git a/test/unit/instruments/fixtures/test/_test_partial_collection.html.erb b/test/unit/instruments/fixtures/test/_test_partial_collection.html.erb new file mode 100644 index 00000000..c148e6f2 --- /dev/null +++ b/test/unit/instruments/fixtures/test/_test_partial_collection.html.erb @@ -0,0 +1,3 @@ +
+

<%= index %>

+
diff --git a/test/unit/instruments/fixtures/test_view.html.erb b/test/unit/instruments/fixtures/test_view.html.erb new file mode 100644 index 00000000..913e28d7 --- /dev/null +++ b/test/unit/instruments/fixtures/test_view.html.erb @@ -0,0 +1,10 @@ + + + View + + +

View

+ <%= render partial: "test_partial", locals: { message: 'Partial' } %> + <%= render partial: "test_partial_collection", collection: [1, 2, 3], as: :index %> + +