diff --git a/DevProxy.Tests/DevProxyCommandTests.cs b/DevProxy.Tests/DevProxyCommandTests.cs new file mode 100644 index 00000000..ffcba30f --- /dev/null +++ b/DevProxy.Tests/DevProxyCommandTests.cs @@ -0,0 +1,54 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using DevProxy.Commands; + +using Xunit; + +namespace DevProxy.Tests; + +public class DevProxyCommandTests +{ + [Fact] + public void ApplyDiscoverOption_True_EnablesRecording() + { + var config = new FakeProxyConfiguration { Record = false }; + + DevProxyCommand.ApplyDiscoverOption(config, true); + + Assert.True(config.Record); + } + + [Fact] + public void ApplyDiscoverOption_False_DoesNotEnableRecording() + { + var config = new FakeProxyConfiguration { Record = false }; + + DevProxyCommand.ApplyDiscoverOption(config, false); + + Assert.False(config.Record); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void ApplyDiscoverOption_NotSpecified_LeavesRecordingUnchanged(bool record) + { + var config = new FakeProxyConfiguration { Record = record }; + + DevProxyCommand.ApplyDiscoverOption(config, null); + + Assert.Equal(record, config.Record); + } + + [Fact] + public void ApplyDiscoverOption_False_KeepsRecordingEnabledFromOtherSources() + { + var config = new FakeProxyConfiguration { Record = true }; + + DevProxyCommand.ApplyDiscoverOption(config, false); + + Assert.True(config.Record); + } +} diff --git a/DevProxy/Commands/DevProxyCommand.cs b/DevProxy/Commands/DevProxyCommand.cs index dfc9b763..cd582468 100644 --- a/DevProxy/Commands/DevProxyCommand.cs +++ b/DevProxy/Commands/DevProxyCommand.cs @@ -674,6 +674,15 @@ private void CustomizeHelp() } } + internal static void ApplyDiscoverOption(IProxyConfiguration proxyConfiguration, bool? isDiscover) + { + // Discovery mode requires recording, but --discover false must not enable it + if (isDiscover == true) + { + proxyConfiguration.Record = true; + } + } + private void ConfigureFromOptions(ParseResult parseResult) { var port = parseResult.GetValueOrDefault(PortOptionName); @@ -742,11 +751,7 @@ private void ConfigureFromOptions(ParseResult parseResult) { _proxyConfiguration.TimeoutSeconds = timeout.Value; } - var isDiscover = parseResult.GetValueOrDefault(DiscoverOptionName); - if (isDiscover is not null) - { - _proxyConfiguration.Record = true; - } + ApplyDiscoverOption(_proxyConfiguration, parseResult.GetValueOrDefault(DiscoverOptionName)); var env = parseResult.GetValueOrDefault(EnvOptionName); if (env is not null) {