Skip to content

Commit

Permalink
Implement middleware and version commands in the rage/cli.rb file.
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneyter committed Aug 18, 2024
1 parent 7858743 commit c489f7d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/rage/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "thor"
require "rack"
require "rage/version"

module Rage
class CLI < Thor
Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions spec/rage/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c489f7d

Please sign in to comment.