From 3ccbe42c894ff0d387b8f06ecb5b6d68e58e9f26 Mon Sep 17 00:00:00 2001 From: Justin Perez Date: Tue, 26 Sep 2023 11:24:29 -0700 Subject: [PATCH] fix: validate `--Output` (#808) --- .../Commands/BaseSettings.cs | 6 ++ .../Commands/BaseSettingsTests.cs | 55 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/BaseSettingsTests.cs diff --git a/src/Microsoft.ComponentDetection.Orchestrator/Commands/BaseSettings.cs b/src/Microsoft.ComponentDetection.Orchestrator/Commands/BaseSettings.cs index 72eba5ccf..eec348e92 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/Commands/BaseSettings.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/Commands/BaseSettings.cs @@ -2,6 +2,7 @@ namespace Microsoft.ComponentDetection.Orchestrator.Commands; using System; using System.ComponentModel; +using System.IO; using Serilog.Events; using Spectre.Console; using Spectre.Console.Cli; @@ -44,6 +45,11 @@ public override ValidationResult Validate() return ValidationResult.Error($"{nameof(this.Timeout)} must be a positive integer"); } + if (!string.IsNullOrEmpty(this.Output) && !Directory.Exists(this.Output)) + { + return ValidationResult.Error($"{nameof(this.Output)} must be a valid path"); + } + return base.Validate(); } } diff --git a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/BaseSettingsTests.cs b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/BaseSettingsTests.cs new file mode 100644 index 000000000..0c129fe0a --- /dev/null +++ b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/BaseSettingsTests.cs @@ -0,0 +1,55 @@ +namespace Microsoft.ComponentDetection.Orchestrator.Tests.Commands; + +using System.IO; +using FluentAssertions; +using Microsoft.ComponentDetection.Orchestrator.Commands; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +[TestClass] +[TestCategory("Governance/All")] +[TestCategory("Governance/ComponentDetection")] +public class BaseSettingsTests +{ + [TestMethod] + public void Validate_FailsNegativeTimeout() + { + var settings = new TestBaseSettings + { + Timeout = -1, + }; + + var result = settings.Validate(); + + result.Successful.Should().BeFalse(); + } + + [TestMethod] + public void Validate_Success_Empty_Output() + { + var settings = new TestBaseSettings + { + Output = string.Empty, + }; + + var result = settings.Validate(); + + result.Successful.Should().BeTrue(); + } + + [TestMethod] + public void Validate_Fails_Output_NotExists() + { + var setting = new TestBaseSettings + { + Output = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()), + }; + + var result = setting.Validate(); + + result.Successful.Should().BeFalse(); + } + + private class TestBaseSettings : BaseSettings + { + } +}