From 15dcb222f7d7dd2395a5b12639d53736596d7e8a Mon Sep 17 00:00:00 2001 From: Daniel Pepper Date: Tue, 19 Nov 2024 06:33:56 +0800 Subject: [PATCH 1/4] rack cleanup --- Gemfile | 1 + Gemfile.lock | 1 + lib/singed/rack_middleware.rb | 8 ++----- spec/singed/middleware_spec.rb | 41 ++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 spec/singed/middleware_spec.rb diff --git a/Gemfile b/Gemfile index 97879fc..b83ed2e 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ source "https://rubygems.org" gemspec gem "activejob" +gem "rack-test" gem "rake", "~> 13.0" gem "rspec" gem "rubyzip" diff --git a/Gemfile.lock b/Gemfile.lock index f246c40..98084c1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,6 +118,7 @@ PLATFORMS DEPENDENCIES activejob + rack-test rake (~> 13.0) rspec rubyzip diff --git a/lib/singed/rack_middleware.rb b/lib/singed/rack_middleware.rb index bce1e2f..f267974 100644 --- a/lib/singed/rack_middleware.rb +++ b/lib/singed/rack_middleware.rb @@ -9,15 +9,11 @@ def initialize(app) end def call(env) - status, headers, body = if capture_flamegraph?(env) - flamegraph do - @app.call(env) - end + if capture_flamegraph?(env) + flamegraph { @app.call(env) } else @app.call(env) end - - [status, headers, body] end def capture_flamegraph?(env) diff --git a/spec/singed/middleware_spec.rb b/spec/singed/middleware_spec.rb new file mode 100644 index 0000000..93abbcc --- /dev/null +++ b/spec/singed/middleware_spec.rb @@ -0,0 +1,41 @@ +describe Singed::RackMiddleware do + subject do + instance.call(env) + end + + let(:app) { ->(*) { [200, {'Content-Type' => 'text/plain'}, ['OK']] } } + let(:instance) { described_class.new(app) } + let(:env) { Rack::MockRequest.env_for('/', headers) } + let(:headers) { {} } + + it 'returns a proper rack response' do + status, _ = subject + expect(status).to eq 200 + end + + it 'does not capture a flamegraph by default' do + expect(instance).not_to receive(:flamegraph) + subject + end + + context 'when enabled' do + before { allow(instance).to receive(:capture_flamegraph?).and_return(true) } + + it 'captures a flamegraph' do + expect(instance).to receive(:flamegraph) + subject + end + end + + describe '.capture_flamegraph?' do + subject { instance.capture_flamegraph?(env) } + + it { is_expected.to be false } + + context 'when HTTP_X_SINGED is true' do + let(:headers) { { 'HTTP_X_SINGED' => 'true' } } + + it { is_expected.to be true } + end + end +end From 10c24ccc360cbd879e4bc842c9af7aa9a51f22d3 Mon Sep 17 00:00:00 2001 From: Daniel Pepper Date: Mon, 9 Mar 2026 06:11:06 -0800 Subject: [PATCH 2/4] Improve middleware spec with Rack::Lint and cleanup Co-Authored-By: Claude Opus 4.5 --- spec/singed/middleware_spec.rb | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/spec/singed/middleware_spec.rb b/spec/singed/middleware_spec.rb index 93abbcc..0fa2089 100644 --- a/spec/singed/middleware_spec.rb +++ b/spec/singed/middleware_spec.rb @@ -1,39 +1,48 @@ +# frozen_string_literal: true + +require "tmpdir" + describe Singed::RackMiddleware do subject do instance.call(env) end - let(:app) { ->(*) { [200, {'Content-Type' => 'text/plain'}, ['OK']] } } + let(:app) { ->(*) { [200, {"content-type" => "text/plain"}, ["OK"]] } } let(:instance) { described_class.new(app) } - let(:env) { Rack::MockRequest.env_for('/', headers) } + let(:env) { Rack::MockRequest.env_for("/", headers) } let(:headers) { {} } - it 'returns a proper rack response' do - status, _ = subject - expect(status).to eq 200 + before do + allow_any_instance_of(Singed::Flamegraph).to receive(:open) + Singed.output_directory = Dir.mktmpdir("singed-spec") + end + + it "returns a proper rack response" do + linted_app = Rack::Lint.new(instance) + expect { linted_app.call(env) }.not_to raise_error end - it 'does not capture a flamegraph by default' do + it "does not capture a flamegraph by default" do expect(instance).not_to receive(:flamegraph) subject end - context 'when enabled' do + context "when enabled" do before { allow(instance).to receive(:capture_flamegraph?).and_return(true) } - it 'captures a flamegraph' do - expect(instance).to receive(:flamegraph) + it "captures a flamegraph" do + expect(instance).to receive(:flamegraph).and_call_original subject end end - describe '.capture_flamegraph?' do + describe ".capture_flamegraph?" do subject { instance.capture_flamegraph?(env) } it { is_expected.to be false } - context 'when HTTP_X_SINGED is true' do - let(:headers) { { 'HTTP_X_SINGED' => 'true' } } + context "when HTTP_X_SINGED is true" do + let(:headers) { {"HTTP_X_SINGED" => "true"} } it { is_expected.to be true } end From c1d1031bd3207d25967fe9e1b72533cd8c904c80 Mon Sep 17 00:00:00 2001 From: Daniel Pepper Date: Sat, 28 Mar 2026 06:31:45 +0700 Subject: [PATCH 3/4] rack-test not needed --- Gemfile | 1 - Gemfile.lock | 1 - 2 files changed, 2 deletions(-) diff --git a/Gemfile b/Gemfile index b83ed2e..97879fc 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,6 @@ source "https://rubygems.org" gemspec gem "activejob" -gem "rack-test" gem "rake", "~> 13.0" gem "rspec" gem "rubyzip" diff --git a/Gemfile.lock b/Gemfile.lock index 98084c1..f246c40 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,7 +118,6 @@ PLATFORMS DEPENDENCIES activejob - rack-test rake (~> 13.0) rspec rubyzip From 88a14559d0e44b50f5dfc71e6c0e3646a66d5bcf Mon Sep 17 00:00:00 2001 From: Daniel Pepper Date: Sat, 28 Mar 2026 06:45:20 +0700 Subject: [PATCH 4/4] Address PR feedback: sigil fix and cleanup - Change `.capture_flamegraph?` to `#capture_flamegraph?` (instance method) - Remove temp directory setup (now handled globally via spec_helper) Co-Authored-By: Claude Opus 4.5 --- spec/singed/middleware_spec.rb | 60 ++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/spec/singed/middleware_spec.rb b/spec/singed/middleware_spec.rb index 0fa2089..0e54183 100644 --- a/spec/singed/middleware_spec.rb +++ b/spec/singed/middleware_spec.rb @@ -1,42 +1,47 @@ # frozen_string_literal: true -require "tmpdir" - describe Singed::RackMiddleware do subject do instance.call(env) end - let(:app) { ->(*) { [200, {"content-type" => "text/plain"}, ["OK"]] } } + let(:app_response) { [200, {"content-type" => "text/plain"}, ["OK"]] } + let(:app) { ->(*) { app_response } } let(:instance) { described_class.new(app) } let(:env) { Rack::MockRequest.env_for("/", headers) } let(:headers) { {} } - before do - allow_any_instance_of(Singed::Flamegraph).to receive(:open) - Singed.output_directory = Dir.mktmpdir("singed-spec") - end - it "returns a proper rack response" do linted_app = Rack::Lint.new(instance) expect { linted_app.call(env) }.not_to raise_error end - it "does not capture a flamegraph by default" do - expect(instance).not_to receive(:flamegraph) - subject + it "passes through the app response unchanged" do + expect(subject).to eq(app_response) end context "when enabled" do - before { allow(instance).to receive(:capture_flamegraph?).and_return(true) } + before do + allow_any_instance_of(Singed::Flamegraph).to receive(:open) + allow(instance).to receive(:capture_flamegraph?).and_return(true) + end it "captures a flamegraph" do expect(instance).to receive(:flamegraph).and_call_original subject end + + it "returns a proper rack response" do + linted_app = Rack::Lint.new(instance) + expect { linted_app.call(env) }.not_to raise_error + end + + it "passes through the app response unchanged" do + expect(subject).to eq(app_response) + end end - describe ".capture_flamegraph?" do + describe "#capture_flamegraph?" do subject { instance.capture_flamegraph?(env) } it { is_expected.to be false } @@ -46,5 +51,34 @@ it { is_expected.to be true } end + + context "when SINGED_MIDDLEWARE_ALWAYS_CAPTURE env var is set" do + around do |example| + original = ENV["SINGED_MIDDLEWARE_ALWAYS_CAPTURE"] + described_class.remove_instance_variable(:@always_capture) if described_class.instance_variable_defined?(:@always_capture) + example.run + ensure + if original.nil? + ENV.delete("SINGED_MIDDLEWARE_ALWAYS_CAPTURE") + else + ENV["SINGED_MIDDLEWARE_ALWAYS_CAPTURE"] = original + end + described_class.remove_instance_variable(:@always_capture) if described_class.instance_variable_defined?(:@always_capture) + end + + %w[true 1 yes].each do |truthy_value| + context "when SINGED_MIDDLEWARE_ALWAYS_CAPTURE=#{truthy_value}" do + before { ENV["SINGED_MIDDLEWARE_ALWAYS_CAPTURE"] = truthy_value } + + it { is_expected.to be true } + end + end + + context "when SINGED_MIDDLEWARE_ALWAYS_CAPTURE=false" do + before { ENV["SINGED_MIDDLEWARE_ALWAYS_CAPTURE"] = "false" } + + it { is_expected.to be false } + end + end end end