1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/optparse] Add tests for OptionParser#load

https://github.com/ruby/optparse/commit/fb34a1d3a3
This commit is contained in:
Nobuyoshi Nakada 2022-10-29 22:23:14 +09:00 committed by git
parent bc28acc347
commit 4dac53f0a4

139
test/optparse/test_load.rb Normal file
View file

@ -0,0 +1,139 @@
# frozen_string_literal: false
require 'test/unit'
require 'optparse'
require 'tmpdir'
class TestOptionParserLoad < Test::Unit::TestCase
def setup
@tmpdir = Dir.mktmpdir("optparse_test-")
@basename = File.basename($0, '.*')
@envs = %w[HOME XDG_CONFIG_HOME XDG_CONFIG_DIRS].each_with_object({}) do |v, h|
h[v] = ENV.delete(v)
end
end
def teardown
ENV.update(@envs)
FileUtils.rm_rf(@tmpdir)
end
def new_parser
@result = nil
OptionParser.new do |opt|
opt.on("--test=arg") {|v| @result = v}
end
end
def assert_load(result)
assert new_parser.load
assert_equal(result, @result)
end
def setup_options(env, dir, suffix = nil)
optdir = File.join(@tmpdir, dir)
FileUtils.mkdir_p(optdir)
file = File.join(optdir, [@basename, suffix].join(""))
File.write(file, "--test=#{dir}")
ENV.update(env)
if block_given?
begin
yield dir, optdir
ensure
File.unlink(file)
Dir.rmdir(optdir) rescue nil
end
else
return dir, optdir
end
end
def setup_options_home(&block)
setup_options({'HOME'=>@tmpdir}, ".options", &block)
end
def setup_options_xdg_config_home(&block)
setup_options({'XDG_CONFIG_HOME'=>@tmpdir+"/xdg"}, "xdg", ".options", &block)
end
def setup_options_home_config(&block)
setup_options({'HOME'=>@tmpdir}, ".config", ".options", &block)
end
def setup_options_xdg_config_dirs(&block)
setup_options({'XDG_CONFIG_DIRS'=>@tmpdir+"/xdgconf"}, "xdgconf", ".options", &block)
end
def setup_options_home_config_settings(&block)
setup_options({'HOME'=>@tmpdir}, "config/settings", ".options", &block)
end
def test_load_home_options
result, = setup_options_home
assert_load(result)
setup_options_xdg_config_home do
assert_load(result)
end
setup_options_home_config do
assert_load(result)
end
setup_options_xdg_config_dirs do
assert_load(result)
end
setup_options_home_config_settings do
assert_load(result)
end
end
def test_load_xdg_config_home
result, = setup_options_xdg_config_home
assert_load(result)
setup_options_home_config do
assert_load(result)
end
setup_options_xdg_config_dirs do
assert_load(result)
end
setup_options_home_config_settings do
assert_load(result)
end
end
def test_load_home_config
result, = setup_options_home_config
assert_load(result)
setup_options_xdg_config_dirs do
assert_load(result)
end
setup_options_home_config_settings do
assert_load(result)
end
end
def test_load_xdg_config_dirs
result, = setup_options_xdg_config_dirs
assert_load(result)
setup_options_home_config_settings do
assert_load(result)
end
end
def test_load_home_config_settings
result, = setup_options_home_config_settings
assert_load(result)
end
def test_load_nothing
assert !new_parser.load
assert_nil @result
end
end