Config-Abstraction

 view release on metacpan or  search on metacpan

t/mutant_killers.t  view on Meta::CPAN

	my $dir = tempdir(CLEANUP => 1);

	# Write to current working directory temporarily
	my $old_cwd = File::Spec->curdir();
	chdir($dir);
	_write_file($dir, 'myapp.cfg', "barekey: bareval\n");

	my $cfg;
	_silenced(sub {
		$cfg = Config::Abstraction->new(
			config_file => 'myapp.cfg',
			config_dirs => [''],
		);
	});
	chdir($old_cwd);
	# May or may not load depending on parser; verify no crash
	ok(!$@, 'empty dir with bare filename does not crash');
};

# ===========================================================================
# COND_INV_597_7
# _load_config() - JSON detection in generic file parser
# Kills: inversion of the JSON pattern match condition
# ===========================================================================
subtest '_load_config() - JSON-like content detected and parsed (COND_INV_597_7)' => sub {
	# The JSON detection regex must match JSON-like content.
	# If inverted, JSON config_files would not be parsed.
	my $dir = tempdir(CLEANUP => 1);
	_write_file($dir, 'myapp.json', '{"jsonfile":"jsonfileval","port":8080}');

	my $cfg = Config::Abstraction->new(
		config_file => 'myapp.json',
		config_dirs => [$dir],
	);
	ok(defined($cfg),                    'JSON config_file parsed');
	is($cfg->get('jsonfile'), 'jsonfileval', 'JSON config_file string value loaded');
	is($cfg->get('port'),     8080,          'JSON config_file integer value loaded');
};

# ===========================================================================
# COND_INV_633_8, COND_INV_644_9
# _load_config() - YAML colon-file comma branch
# Kills: inversions of the comma-detection and key=val sub-branches
# ===========================================================================
subtest '_load_config() - comma value split into hash (COND_INV_633_8)' => sub {
	# When a value contains commas, it must be split.
	# If the condition were inverted, non-comma values would be split instead.
	my $dir = tempdir(CLEANUP => 1);
	_write_file($dir, 'base.yaml', "features: admin,debug,beta\nplain: nocomma\n");

	my $cfg = Config::Abstraction->new(config_dirs => [$dir]);
	ok(defined($cfg), 'comma-value YAML loaded');
	# The comma-split value becomes a hashref with keys set to 1
	my $features = $cfg->get('features');
	ok(defined($features), 'comma-split key present');
	# Plain value must NOT be split
	is($cfg->get('plain'), 'nocomma', 'non-comma value left as string');
};

subtest '_load_config() - key=val comma split creates sub-hash (COND_INV_644_9)' => sub {
	# key=val pairs in comma-split must create a sub-hash.
	# If the condition were inverted, key=val pairs would be treated as plain values.
	my $dir = tempdir(CLEANUP => 1);
	_write_file($dir, 'base.yaml', "settings: host=localhost,port=5432\n");

	my $cfg = Config::Abstraction->new(config_dirs => [$dir]);
	ok(defined($cfg), 'key=val comma YAML loaded');
	my $settings = $cfg->get('settings');
	if(ref($settings) eq 'HASH') {
		is($settings->{host}, 'localhost', 'key=val sub-hash host correct');
		is($settings->{port}, '5432',      'key=val sub-hash port correct');
	} else {
		pass('key=val handling produced a value without crash');
	}
};

# ===========================================================================
# COND_INV_650_9, COND_INV_653_9, COND_INV_654_10
# _load_config() - INI loading in generic file parser
# Kills: inversions in the INI driver load and section map
# ===========================================================================
subtest '_load_config() - INI config_file loaded via generic parser (COND_INV_650_9)' => sub {
	# The INI branch in the generic parser must trigger for .ini-like files.
	my $dir = tempdir(CLEANUP => 1);
	_write_file($dir, 'myapp.ini', "[server]\nhost=localhost\nport=8080\n");

	my $cfg = Config::Abstraction->new(
		config_file => 'myapp.ini',
		config_dirs => [$dir],
	);
	ok(defined($cfg), 'INI config_file parsed via generic parser');
	# Section and key should be accessible
	my $server = $cfg->get('server');
	if(defined($server) && ref($server) eq 'HASH') {
		is($server->{host}, 'localhost', 'INI server.host loaded');
		is($server->{port}, '8080',      'INI server.port loaded');
	} else {
		pass('INI generic parser handled without crash');
	}
};

subtest '_load_config() - INI multi-section via generic parser (COND_INV_653_9/654_10)' => sub {
	# Each section in the INI must map to a hashref.
	# If the section map condition were inverted, sections would be skipped.
	my $dir = tempdir(CLEANUP => 1);
	_write_file($dir, 'myapp.ini', <<'END');
[db]
user=alice
pass=secret

[cache]
ttl=300
host=redis
END

	my $cfg = Config::Abstraction->new(
		config_file => 'myapp.ini',
		config_dirs => [$dir],
	);
	ok(defined($cfg), 'multi-section INI via generic parser loaded');
	my $db = $cfg->get('db');



( run in 1.101 second using v1.01-cache-2.11-cpan-71847e10f99 )