diff --git a/src/Microsoft.ComponentDetection.Orchestrator/Commands/ScanSettings.cs b/src/Microsoft.ComponentDetection.Orchestrator/Commands/ScanSettings.cs index 89f9e7cc6..edacac5ba 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/Commands/ScanSettings.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/Commands/ScanSettings.cs @@ -4,6 +4,7 @@ namespace Microsoft.ComponentDetection.Orchestrator.Commands; using System.ComponentModel; using System.IO; using Microsoft.ComponentDetection.Orchestrator.Extensions; +using Spectre.Console; using Spectre.Console.Cli; /// @@ -60,4 +61,14 @@ public class ScanSettings : BaseSettings "Comma separated list of docker image names or hashes to execute container scanning on, ex: ubuntu:16.04, 56bab49eef2ef07505f6a1b0d5bd3a601dfc3c76ad4460f24c91d6fa298369ab")] [TypeConverter(typeof(CommaDelimitedConverter))] public IEnumerable DockerImagesToScan { get; set; } + + public override ValidationResult Validate() + { + if (this.SourceDirectory is null) + { + return ValidationResult.Error($"{nameof(this.SourceDirectory)} is required"); + } + + return !this.SourceDirectory.Exists ? ValidationResult.Error($"The {nameof(this.SourceDirectory)} {this.SourceDirectory} does not exist") : base.Validate(); + } } diff --git a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/ScanSettingsTests.cs b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/ScanSettingsTests.cs new file mode 100644 index 000000000..ec87df249 --- /dev/null +++ b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/ScanSettingsTests.cs @@ -0,0 +1,48 @@ +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 ScanSettingsTests +{ + [TestMethod] + public void Validate_ChecksNullSourceDirectory() + { + var settings = new ScanSettings(); + + var result = settings.Validate(); + + result.Successful.Should().BeFalse(); + } + + [TestMethod] + public void Validate_ChecksSourceDirectoryExists() + { + var settings = new ScanSettings + { + SourceDirectory = new DirectoryInfo(Path.GetTempPath()), + }; + + var result = settings.Validate(); + + result.Successful.Should().BeTrue(); + } + + [TestMethod] + public void Validate_FailIfSourceDirectoryDoesntExist() + { + var settings = new ScanSettings + { + SourceDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())), + }; + + var result = settings.Validate(); + + result.Successful.Should().BeFalse(); + } +}