Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Added a validate cli command to load an test config for scripting.
Browse files Browse the repository at this point in the history
This will add a special command that will do most of the boot-up process
except it will not "run", but exit back to the shell.

If there are any issues with your configuration, it will fail exactly
like when you execute with `lita start`.
  • Loading branch information
brodock committed Jul 22, 2016
1 parent fda3a80 commit 63008b4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
11 changes: 9 additions & 2 deletions lib/lita.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,23 @@ class << self
# environment variable LITA_GLOBAL_LOG_LEVEL set to one of the standard log level names.
attr_accessor :logger

# Loads user configuration and starts the robot.
# Loads user configuration.
# @param config_path [String] The path to the user configuration file.
# @return [void]
def run(config_path = nil)
def load_config(config_path = nil)
hooks[:before_run].each { |hook| hook.call(config_path: config_path) }
ConfigurationBuilder.load_user_config(config_path)
ConfigurationBuilder.freeze_config(config)
ConfigurationValidator.new(self).call
hooks[:config_finalized].each { |hook| hook.call(config_path: config_path) }
self.locale = config.robot.locale
end

# Loads user configuration and starts the robot.
# @param config_path [String] The path to the user configuration file.
# @return [void]
def run(config_path = nil)
load_config(config_path)
Robot.new.run
end

Expand Down
23 changes: 23 additions & 0 deletions lib/lita/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ def version
end
map %w(-v --version) => :version

desc "validate", "Verifies if lita is correctly configured"
option :config,
aliases: "-c",
banner: "PATH",
default: File.expand_path("lita_config.rb", Dir.pwd),
desc: "Path to the configuration file to use"
# Outputs detailed stacktrace when there is a problem or exit 0 when OK.
# You can use this as a pre-check script for any automation
# @return [void]
def validate
check_ruby_verison
check_default_handlers

begin
Bundler.require
rescue Bundler::GemfileNotFound
say I18n.t("lita.cli.no_gemfile_warning"), :red
abort
end

Lita.load_config(options[:config])
end

private

def check_ruby_verison
Expand Down
39 changes: 28 additions & 11 deletions spec/lita_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,41 +153,58 @@
end
end

describe ".run" do
describe ".load_config" do
let(:hook) { double("Hook") }
let(:robot) { double("Lita::Robot", run: nil) }
let(:validator) { instance_double("Lita::ConfigurationValidator", call: nil) }

before do
allow(Lita::Robot).to receive(:new).and_return(robot)
allow(
Lita::ConfigurationValidator
).to receive(:new).with(described_class).and_return(validator)
end

after { described_class.reset }

it "runs a new Robot" do
expect(robot).to receive(:run)
described_class.run
end

it "calls before_run hooks" do
described_class.register_hook(:before_run, hook)
expect(hook).to receive(:call).with(config_path: "path/to/config")
described_class.run("path/to/config")
described_class.load_config("path/to/config")
end

it "calls config_finalized hooks" do
described_class.register_hook(:config_finalized, hook)
expect(hook).to receive(:call).with(config_path: "path/to/config")
described_class.run("path/to/config")
described_class.load_config("path/to/config")
end

it "raises if the configuration is not valid" do
allow(validator).to receive(:call).and_raise(SystemExit)

expect { described_class.run }.to raise_error(SystemExit)
expect { described_class.load_config }.to raise_error(SystemExit)
end
end

describe ".run" do
let(:validator) { instance_double("Lita::ConfigurationValidator", call: nil) }
let(:robot) { double("Lita::Robot", run: nil) }

before do
allow(Lita::Robot).to receive(:new).and_return(robot)
allow(
Lita::ConfigurationValidator
).to receive(:new).with(described_class).and_return(validator)
end

after { described_class.reset }

it "runs a new Robot" do
expect(robot).to receive(:run)
described_class.run
end

it "loads configuration from a config file" do
expect(described_class).to receive(:load_config).with("path/to/config")
described_class.run("path/to/config")
end
end
end

0 comments on commit 63008b4

Please sign in to comment.