From cc0fcb245bf87fbf518260db591d139ff218d9af Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sat, 29 Jul 2017 03:31:47 +0200 Subject: Add skip-not-found toggle to .read when called with (Str, Str, Bool) --- META6.json | 2 +- lib/Config.pm6 | 9 ++++++--- t/01-reading.t | 14 +++++++------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/META6.json b/META6.json index ccbda0f..317bdfb 100644 --- a/META6.json +++ b/META6.json @@ -1,7 +1,7 @@ { "perl": "6", "name": "Config", - "version": "1.2.0", + "version": "1.2.1", "auth": "github:scriptkitties", "description": "Extensible library for reading and writing configuration files.", "license": "GPL-3.0", diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 1a1deab..d0c1d93 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -124,11 +124,14 @@ class Config is Associative is export #| Load a configuration file from the given path. Optionally #| set a parser module name to use. If not set, Config will #| attempt to deduce the parser to use. - multi method read(Str $path, Str $parser = "") - { + multi method read( + Str $path, + Str $parser = "", + Bool :$skip-not-found = False + ) { Config::Exception::FileNotFoundException.new( path => $path - ).throw() unless $path.IO.f; + ).throw() unless ($path.IO.f || $skip-not-found); $!parser = self.get-parser($path, $parser); diff --git a/t/01-reading.t b/t/01-reading.t index f7bf7d4..d508b1b 100644 --- a/t/01-reading.t +++ b/t/01-reading.t @@ -4,15 +4,18 @@ use v6.c; use Test; use lib "lib"; -plan 5; +plan 6; use Config; my Config $config = Config.new(); +my Str $null-parser = "Config::Parser::NULL"; throws-like { $config.read("t/files/none") }, Config::Exception::FileNotFoundException, "Reading nonexisting file"; throws-like { $config.read("t/files/config", "Config::Parser:NoSuchParserForTest") }, Config::Exception::MissingParserException, "Using non-existing parser"; +is $config.read("t/files/none", $null-parser, skip-not-found => True), True, ".read allows for non-fatal execution with skip-not-found set"; + my $hash = { "a" => "a", "b" => { @@ -41,10 +44,7 @@ is-deeply $config.get(), { subtest { plan 3; - # Use the NULL parser to mock the parser - my $parser = "Config::Parser::NULL"; - - is $config.read(("t/files/config", "t/files/config.yaml"), $parser, skip-not-found => True), True, "All paths exist"; - is $config.read(("t/files/config", "t/files/none", "t/files/config.yaml"), $parser, skip-not-found => True), True, "At least one path exists"; - is $config.read(("t/files/none", "t/files/none.yaml"), $parser, skip-not-found => True), False, "No paths exist"; + is $config.read(("t/files/config", "t/files/config.yaml"), $null-parser, skip-not-found => True), True, "All paths exist"; + is $config.read(("t/files/config", "t/files/none", "t/files/config.yaml"), $null-parser, skip-not-found => True), True, "At least one path exists"; + is $config.read(("t/files/none", "t/files/none.yaml"), $null-parser, skip-not-found => True), False, "No paths exist"; }, "Read with a List of paths"; -- cgit v1.1