From de18c777f02f1d729f4b62e48d049f3e247284fa Mon Sep 17 00:00:00 2001 From: Jannick Johnsen Date: Mon, 5 Oct 2020 20:19:17 +0200 Subject: [PATCH] add test for env parsed args --- .../Fakes/Simple_Options_With_Env.cs | 28 ++++++++++++++++ tests/CommandLine.Tests/Unit/ParserTests.cs | 33 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tests/CommandLine.Tests/Fakes/Simple_Options_With_Env.cs diff --git a/tests/CommandLine.Tests/Fakes/Simple_Options_With_Env.cs b/tests/CommandLine.Tests/Fakes/Simple_Options_With_Env.cs new file mode 100644 index 00000000..9218b2e0 --- /dev/null +++ b/tests/CommandLine.Tests/Fakes/Simple_Options_With_Env.cs @@ -0,0 +1,28 @@ +// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. + +using System.Collections.Generic; + +namespace CommandLine.Tests.Fakes +{ + class Simple_Options_With_Env + { + [Option('s', Default = "", Env = "StringValue")] + public string StringValue { get; set; } + + [Option("bvff", Env = "BoolValueFullFalse", HelpText = "Define a boolean or switch value here.")] + public bool BoolValueFullFalse { get; set; } + [Option("bvft", Env = "BoolValueFullTrue", HelpText = "Define a boolean or switch value here.")] + public bool BoolValueFullTrue { get; set; } + [Option("bvst", Env = "BoolValueShortTrue", HelpText = "Define a boolean or switch value here.")] + public bool BoolValueShortTrue { get; set; } + [Option("bvsf", Env = "BoolValueShortFalse", HelpText = "Define a boolean or switch value here.")] + public bool BoolValueShortFalse { get; set; } + + + [Option('l', Default = 1, Env = "LongValue")] + public long LongValue { get; set; } + + [Option('i', Default = 2, Env = "IntValue")] + public long IntValue { get; set; } + } +} diff --git a/tests/CommandLine.Tests/Unit/ParserTests.cs b/tests/CommandLine.Tests/Unit/ParserTests.cs index bc6d77a8..e5f1e0ff 100644 --- a/tests/CommandLine.Tests/Unit/ParserTests.cs +++ b/tests/CommandLine.Tests/Unit/ParserTests.cs @@ -132,6 +132,39 @@ public void Parse_options_with_double_dash() // Teardown } + [Fact] + public void Parse_spec_with_enviroment_variables() + { + // Fixture setup + var expectedOptions = new Simple_Options_With_Env + { + StringValue = "astring", + LongValue = 20L, + IntValue = 2, + BoolValueFullFalse = false, + BoolValueFullTrue = true, + BoolValueShortTrue = true, + BoolValueShortFalse = false, + }; + var sut = new Parser(with => with.EnableDashDash = true); + + // Exercize system + Environment.SetEnvironmentVariable("StringValue", "astring"); + Environment.SetEnvironmentVariable("LongValue", "20"); + Environment.SetEnvironmentVariable("IntValue", null); + Environment.SetEnvironmentVariable("BoolValueFullFalse", "false"); + Environment.SetEnvironmentVariable("BoolValueFullTrue", "true"); + Environment.SetEnvironmentVariable("BoolValueShortTrue", "1"); + Environment.SetEnvironmentVariable("BoolValueShortFalse", "0"); + var result = + sut.ParseArguments( + new string[0]); + + // Verify outcome + ((Parsed)result).Value.Should().BeEquivalentTo(expectedOptions); + // Teardown + } + [Fact] public void Parse_options_with_double_dash_and_option_sequence() {