From c489f7d8c2ea79e37227102a0b09821707980e58 Mon Sep 17 00:00:00 2001 From: Cuneyt Ergun Date: Sun, 18 Aug 2024 09:06:07 +0100 Subject: [PATCH 1/2] Implement middleware and version commands in the rage/cli.rb file. --- lib/rage/cli.rb | 24 ++++++++++++++++++++++ spec/rage/cli_spec.rb | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 spec/rage/cli_spec.rb diff --git a/lib/rage/cli.rb b/lib/rage/cli.rb index ec4175a0..ec8c2c2d 100644 --- a/lib/rage/cli.rb +++ b/lib/rage/cli.rb @@ -2,6 +2,7 @@ require "thor" require "rack" +require "rage/version" module Rage class CLI < Thor @@ -112,6 +113,29 @@ def console IRB.start end + desc "middleware", "List Rack middleware stack enabled for the application" + def middleware + app = ::Rack::Builder.parse_file(options[:config] || "config.ru") + app = app[0] if app.is_a?(Array) + middlewares = [] + + while app + if app.instance_variable_defined?(:@app) + middlewares << app.class + app = app.instance_variable_get(:@app) + else + break + end + end + + middlewares.each { |middleware| puts middleware } + end + + desc "version", "Return the current version of the framework" + def version + puts Rage::VERSION + end + private def environment diff --git a/spec/rage/cli_spec.rb b/spec/rage/cli_spec.rb new file mode 100644 index 00000000..3e92f488 --- /dev/null +++ b/spec/rage/cli_spec.rb @@ -0,0 +1,46 @@ +require "rage/cli" + +RSpec.describe Rage::CLI do + subject(:rage_cli) { described_class.new } + + describe "#middleware" do + let(:config_ru) { "spec/rspec/config.ru" } + + before do + allow(rage_cli).to receive(:options).and_return(config: config_ru) + allow(Rack::Builder).to receive(:parse_file).with(config_ru).and_return([app]) + end + + context "when middleware stack is present" do + let(:app) do + Rack::Builder.app do + use Rage::FiberWrapper + use Rage::Reloader + run ->(env) { [200, { "Content-Type" => "text/plain" }, ["OK"]] } + end + end + + it "lists the middleware stack" do + expect { rage_cli.middleware }.to output(/Rage::FiberWrapper\nRage::Reloader/).to_stdout + end + end + + context "when middleware stack is empty" do + let(:app) { ->(env) { [200, { "Content-Type" => "text/plain" }, ["OK"]] } } + + it "does not list any middleware" do + expect { rage_cli.middleware }.to output("").to_stdout + end + end + end + + describe "#version" do + before do + stub_const("Rage::VERSION", "1.0.0") + end + + it "returns the current version of the framework" do + expect { rage_cli.version }.to output("1.0.0\n").to_stdout + end + end +end From 24b291503ec2351f5368c84621ff1cf71eb78b45 Mon Sep 17 00:00:00 2001 From: Cuneyt Ergun Date: Sat, 31 Aug 2024 19:29:07 +0100 Subject: [PATCH 2/2] Refactor implementation of middleware and version commands in the rage/cli.rb file. --- lib/rage/cli.rb | 15 +++------------ spec/rage/cli_spec.rb | 18 +++++------------- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/lib/rage/cli.rb b/lib/rage/cli.rb index ec8c2c2d..bd640bfe 100644 --- a/lib/rage/cli.rb +++ b/lib/rage/cli.rb @@ -115,20 +115,11 @@ def console desc "middleware", "List Rack middleware stack enabled for the application" def middleware - app = ::Rack::Builder.parse_file(options[:config] || "config.ru") - app = app[0] if app.is_a?(Array) - middlewares = [] + environment - while app - if app.instance_variable_defined?(:@app) - middlewares << app.class - app = app.instance_variable_get(:@app) - else - break - end + Rage.config.middleware.middlewares.each do |middleware| + say "use #{middleware.first.name}" end - - middlewares.each { |middleware| puts middleware } end desc "version", "Return the current version of the framework" diff --git a/spec/rage/cli_spec.rb b/spec/rage/cli_spec.rb index 3e92f488..3fa0836a 100644 --- a/spec/rage/cli_spec.rb +++ b/spec/rage/cli_spec.rb @@ -4,29 +4,21 @@ subject(:rage_cli) { described_class.new } describe "#middleware" do - let(:config_ru) { "spec/rspec/config.ru" } - before do - allow(rage_cli).to receive(:options).and_return(config: config_ru) - allow(Rack::Builder).to receive(:parse_file).with(config_ru).and_return([app]) + allow(rage_cli).to receive(:environment) + allow(Rage.config).to receive_message_chain(:middleware, :middlewares).and_return(middlewares) end context "when middleware stack is present" do - let(:app) do - Rack::Builder.app do - use Rage::FiberWrapper - use Rage::Reloader - run ->(env) { [200, { "Content-Type" => "text/plain" }, ["OK"]] } - end - end + let(:middlewares) { [[Rage::FiberWrapper], [Rage::Reloader, [], nil]] } it "lists the middleware stack" do - expect { rage_cli.middleware }.to output(/Rage::FiberWrapper\nRage::Reloader/).to_stdout + expect { rage_cli.middleware }.to output("use \Rage::FiberWrapper\nuse \Rage::Reloader\n").to_stdout end end context "when middleware stack is empty" do - let(:app) { ->(env) { [200, { "Content-Type" => "text/plain" }, ["OK"]] } } + let(:middlewares) { [] } it "does not list any middleware" do expect { rage_cli.middleware }.to output("").to_stdout