diff --git a/Duplicati/Library/Utility/Utility.cs b/Duplicati/Library/Utility/Utility.cs index 7b021c433..9f0036eaa 100644 --- a/Duplicati/Library/Utility/Utility.cs +++ b/Duplicati/Library/Utility/Utility.cs @@ -637,15 +637,17 @@ namespace Duplicati.Library.Utility } /// - /// Parses a string into a boolean value + /// Parses a string into a boolean value. /// - /// The value to parse - /// The default value, in case the string is not a valid boolean value - /// The parsed value or the default value - public static bool ParseBool(string value, bool @default) + /// The value to parse. + /// A delegate that returns the default value if is not a valid boolean value. + /// The parsed value, or the value returned by . + public static bool ParseBool(string value, Func defaultFunc) { - if (value == null) - value = ""; + if (String.IsNullOrWhiteSpace(value)) + { + return defaultFunc(); + } switch (value.Trim().ToLower()) { @@ -660,10 +662,21 @@ namespace Duplicati.Library.Utility case "no": return false; default: - return @default; + return defaultFunc(); } } + /// + /// Parses a string into a boolean value. + /// + /// The value to parse. + /// The default value, in case is not a valid boolean value. + /// The parsed value, or the default value. + public static bool ParseBool(string value, bool @default) + { + return Utility.ParseBool(value, () => @default); + } + /// /// Parses an option from the option set, using the convention that if the option is set, it is true unless it parses to false, and false otherwise /// @@ -826,18 +839,11 @@ namespace Duplicati.Library.Utility { var str = Environment.GetEnvironmentVariable("FILESYSTEM_CASE_SENSITIVE"); - if (!string.IsNullOrWhiteSpace(str)) - { - str = str.Trim(); - if (new[] { "yes", "1", "on", "true" }.Contains(str, StringComparer.OrdinalIgnoreCase)) - return true; - if (new[] { "no", "0", "off", "false" }.Contains(str, StringComparer.OrdinalIgnoreCase)) - return false; - } - - //TODO: This should probably be determined by filesystem rather than OS, + // TODO: This should probably be determined by filesystem rather than OS, // OSX can actually have the disks formated as Case Sensitive, but insensitive is default - return IsClientLinux && !IsClientOSX; + Func defaultReply = () => Utility.IsClientLinux && !Utility.IsClientOSX; + + return Utility.ParseBool(str, defaultReply); } } diff --git a/Duplicati/UnitTest/BasicSetupHelper.cs b/Duplicati/UnitTest/BasicSetupHelper.cs index f039f2cb8..8636df956 100644 --- a/Duplicati/UnitTest/BasicSetupHelper.cs +++ b/Duplicati/UnitTest/BasicSetupHelper.cs @@ -60,9 +60,7 @@ namespace Duplicati.UnitTest /// this can be used to diagnose errors on a CI build instance by setting /// the environment variable DEBUG_OUTPUT=1 and running the job /// - public static readonly bool DEBUG_OUTPUT = - new[] { "1", "true", "on", "yes" } - .Contains(Environment.GetEnvironmentVariable("DEBUG_OUTPUT") ?? "", StringComparer.OrdinalIgnoreCase); + public static readonly bool DEBUG_OUTPUT = Library.Utility.Utility.ParseBool(Environment.GetEnvironmentVariable("DEBUG_OUTPUT"), false); /// /// Writes a message to TestContext.Progress and Console.Out diff --git a/Duplicati/UnitTest/UtilityTests.cs b/Duplicati/UnitTest/UtilityTests.cs index 18ddbd7ee..208ee933f 100644 --- a/Duplicati/UnitTest/UtilityTests.cs +++ b/Duplicati/UnitTest/UtilityTests.cs @@ -57,5 +57,42 @@ namespace Duplicati.UnitTest Assert.IsNotNull(actualUniqueItems); Assert.IsNotNull(actualDuplicateItems); } + + [Test] + [Category("Utility")] + public void ParseBool() + { + string[] expectTrue = { "1", "on", "true", "yes" }; + string[] expectFalse = { "0", "off", "false", "no" }; + string[] expectDefault = { null, "", "maybe" }; + Func returnsTrue = () => true; + Func returnsFalse = () => false; + + foreach (string value in expectTrue) + { + string message = $"{value} should be parsed to true."; + + Assert.IsTrue(Utility.ParseBool(value, false), message); + Assert.IsTrue(Utility.ParseBool(value.ToUpper(), false), message); + Assert.IsTrue(Utility.ParseBool($" {value} ", false), message); + } + + foreach (string value in expectFalse) + { + string message = $"{value} should be parsed to false."; + + Assert.IsFalse(Utility.ParseBool(value, true), message); + Assert.IsFalse(Utility.ParseBool(value.ToUpper(), true), message); + Assert.IsFalse(Utility.ParseBool($" {value} ", true), message); + } + + foreach (string value in expectDefault) + { + Assert.IsTrue(Utility.ParseBool(value, true)); + Assert.IsTrue(Utility.ParseBool(value, returnsTrue)); + Assert.IsFalse(Utility.ParseBool(value, false)); + Assert.IsFalse(Utility.ParseBool(value, returnsFalse)); + } + } } }