From 51e425e6dd36152f1ff8ab9a818fbecd5e574f5b Mon Sep 17 00:00:00 2001 From: Petr Date: Thu, 25 Jun 2026 21:51:10 +0200 Subject: [PATCH] test: add CLI test coverage Cover version, help, unknown command, parse_serve_options defaults and overrides, prepare_config template generation and no-op. --- test/cli_test.rb | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 test/cli_test.rb diff --git a/test/cli_test.rb b/test/cli_test.rb new file mode 100644 index 0000000..f178f53 --- /dev/null +++ b/test/cli_test.rb @@ -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