Skip to content

Commit

Permalink
fix: validate --SourceDirectory (#805)
Browse files Browse the repository at this point in the history
  • Loading branch information
melotic authored Sep 25, 2023
1 parent f5db568 commit 4a6b01d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -60,4 +61,15 @@ 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<string> DockerImagesToScan { get; set; }

/// <inheritdoc />
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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 4a6b01d

Please sign in to comment.