-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement middleware
and version
commands in the rage/cli.rb file.
#99
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make it look a bit closer to the output of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rsamoilov - Thank you for the comment. I think the line will produce a similar output. What do you think?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good. Probably calling
|
||
end | ||
|
||
desc "version", "Return the current version of the framework" | ||
def version | ||
puts Rage::VERSION | ||
end | ||
|
||
private | ||
|
||
def environment | ||
|
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to use
Rage.config.middleware.middlewares
instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rsamoilov - Thank you for your comment.
What do you think about the below approach?
This method gives me the following output in the rage test app
Is it ok to call environment here or is there any better way to initialise the Rage::Configuration so that
Rage.config
is available in the middleware method?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, calling
environment
should be okThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. Thank you.