Skip to content

Commit

Permalink
fix: validate --Output (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
melotic authored Sep 26, 2023
1 parent 641c827 commit 3ccbe42
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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
{
}
}

0 comments on commit 3ccbe42

Please sign in to comment.