test: add CLI test coverage
Cover version, help, unknown command, parse_serve_options defaults and overrides, prepare_config template generation and no-op.
This commit is contained in:
@@ -0,0 +1,63 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "test_helper"
|
||||||
|
require "tmpdir"
|
||||||
|
require "volumen/cli"
|
||||||
|
|
||||||
|
class CLITest < Minitest::Test
|
||||||
|
def test_version
|
||||||
|
assert_output("#{Volumen::VERSION}\n") do
|
||||||
|
Volumen::CLI.run(["version"])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_help
|
||||||
|
output, = capture_io { Volumen::CLI.run(["help"]) }
|
||||||
|
assert_includes output, "volumen #{Volumen::VERSION}"
|
||||||
|
assert_includes output, "Usage:"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_unknown_command
|
||||||
|
_output, err = capture_io do
|
||||||
|
assert_raises(SystemExit) { Volumen::CLI.run(["bogus"]) }
|
||||||
|
end
|
||||||
|
assert_includes err, "unknown command"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_parse_serve_defaults
|
||||||
|
cli = Volumen::CLI.new(["serve"])
|
||||||
|
options = cli.__send__(:parse_serve_options)
|
||||||
|
assert_equal Volumen::Config::DEFAULT_PATH, options[:config]
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_parse_serve_overrides
|
||||||
|
cli = Volumen::CLI.new([
|
||||||
|
"serve", "--config", "/t.toml", "--host", "127.0.0.1", "--port", "9000"
|
||||||
|
])
|
||||||
|
options = cli.__send__(:parse_serve_options)
|
||||||
|
assert_equal "/t.toml", options[:config]
|
||||||
|
assert_equal "127.0.0.1", options[:host]
|
||||||
|
assert_equal 9000, options[:port]
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_prepare_config_writes_template
|
||||||
|
Dir.mktmpdir do |dir|
|
||||||
|
path = File.join(dir, "config.toml")
|
||||||
|
cli = Volumen::CLI.new([])
|
||||||
|
_output, err = capture_io { cli.__send__(:prepare_config, path) }
|
||||||
|
assert_includes err, "wrote a default config"
|
||||||
|
assert_path_exists path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_prepare_config_noops_when_present
|
||||||
|
Dir.mktmpdir do |dir|
|
||||||
|
path = File.join(dir, "config.toml")
|
||||||
|
File.write(path, "[server]\nport = 1\n")
|
||||||
|
cli = Volumen::CLI.new([])
|
||||||
|
out, err = capture_io { cli.__send__(:prepare_config, path) }
|
||||||
|
assert_empty err
|
||||||
|
assert_empty out
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user