Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee committed Jun 21, 2023
1 parent e28cb50 commit f7852d8
Show file tree
Hide file tree
Showing 171 changed files with 560 additions and 315 deletions.
27 changes: 25 additions & 2 deletions src/Microsoft.ComponentDetection.Common/AsyncExecution.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
namespace Microsoft.ComponentDetection.Common;

using System;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// Provides methods for executing code asynchronously with a timeout.
/// </summary>
public static class AsyncExecution
{
/// <summary>
/// Executes the provided function asynchronously with a timeout.
/// </summary>
/// <param name="toExecute">The function to execute.</param>
/// <param name="timeout">The timeout.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <typeparam name="T">The return type of the function.</typeparam>
/// <returns>The result of the function.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="toExecute"/> is null.</exception>
/// <exception cref="TimeoutException">Thrown when the execution does not complete within the timeout.</exception>
public static async Task<T> ExecuteWithTimeoutAsync<T>(Func<Task<T>> toExecute, TimeSpan timeout, CancellationToken cancellationToken)
{
if (toExecute == null)
Expand All @@ -17,12 +31,21 @@ public static async Task<T> ExecuteWithTimeoutAsync<T>(Func<Task<T>> toExecute,
var completedInTime = await Task.Run(() => work.Wait(timeout));
if (!completedInTime)
{
throw new TimeoutException($"The execution did not complete in the alotted time ({timeout.TotalSeconds} seconds) and has been terminated prior to completion");
throw new TimeoutException($"The execution did not complete in the allotted time ({timeout.TotalSeconds} seconds) and has been terminated prior to completion");
}

return await work;
}

/// <summary>
/// Executes the provided function asynchronously with a timeout.
/// </summary>
/// <param name="toExecute">The function to execute.</param>
/// <param name="timeout">The timeout.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="toExecute"/> is null.</exception>
/// <exception cref="TimeoutException">Thrown when the execution does not complete within the timeout.</exception>
public static async Task ExecuteVoidWithTimeoutAsync(Action toExecute, TimeSpan timeout, CancellationToken cancellationToken)
{
if (toExecute == null)
Expand All @@ -34,7 +57,7 @@ public static async Task ExecuteVoidWithTimeoutAsync(Action toExecute, TimeSpan
var completedInTime = await Task.Run(() => work.Wait(timeout));
if (!completedInTime)
{
throw new TimeoutException($"The execution did not complete in the alotted time ({timeout.TotalSeconds} seconds) and has been terminated prior to completion");
throw new TimeoutException($"The execution did not complete in the allotted time ({timeout.TotalSeconds} seconds) and has been terminated prior to completion");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -10,10 +11,12 @@
using Microsoft.ComponentDetection.Common.Telemetry.Records;
using Microsoft.ComponentDetection.Contracts;

/// <inheritdoc/>
public class CommandLineInvocationService : ICommandLineInvocationService
{
private readonly IDictionary<string, string> commandLocatableCache = new ConcurrentDictionary<string, string>();

/// <inheritdoc/>
public async Task<bool> CanCommandBeLocatedAsync(string command, IEnumerable<string> additionalCandidateCommands = null, DirectoryInfo workingDirectory = null, params string[] parameters)
{
additionalCandidateCommands ??= Enumerable.Empty<string>();
Expand Down Expand Up @@ -48,6 +51,7 @@ public async Task<bool> CanCommandBeLocatedAsync(string command, IEnumerable<str
return !string.IsNullOrWhiteSpace(validCommand);
}

/// <inheritdoc/>
public async Task<CommandLineExecutionResult> ExecuteCommandAsync(string command, IEnumerable<string> additionalCandidateCommands = null, DirectoryInfo workingDirectory = null, params string[] parameters)
{
var isCommandLocatable = await this.CanCommandBeLocatedAsync(command, additionalCandidateCommands);
Expand Down Expand Up @@ -80,16 +84,19 @@ public async Task<CommandLineExecutionResult> ExecuteCommandAsync(string command
}
}

/// <inheritdoc/>
public bool IsCommandLineExecution()
{
return true;
}

/// <inheritdoc/>
public async Task<bool> CanCommandBeLocatedAsync(string command, IEnumerable<string> additionalCandidateCommands = null, params string[] parameters)
{
return await this.CanCommandBeLocatedAsync(command, additionalCandidateCommands, workingDirectory: null, parameters);
}

/// <inheritdoc/>
public async Task<CommandLineExecutionResult> ExecuteCommandAsync(string command, IEnumerable<string> additionalCandidateCommands = null, params string[] parameters)
{
return await this.ExecuteCommandAsync(command, additionalCandidateCommands, workingDirectory: null, parameters);
Expand Down
15 changes: 15 additions & 0 deletions src/Microsoft.ComponentDetection.Common/ComponentComparer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
namespace Microsoft.ComponentDetection.Common;

using System.Collections.Generic;
using Microsoft.ComponentDetection.Contracts.TypedComponent;

/// <summary>
/// Compares two <see cref="TypedComponent"/>s by their <see cref="TypedComponent.Id"/>.
/// </summary>
public class ComponentComparer : EqualityComparer<TypedComponent>
{
/// <summary>
/// Determines whether the specified objects are equal.
/// </summary>
/// <param name="x">The first object of type <see cref="TypedComponent"/> to compare.</param>
/// <param name="y">The second object of type <see cref="TypedComponent"/> to compare.</param>
/// <returns>true if the specified objects are equal; otherwise, false.</returns>
public override bool Equals(TypedComponent x, TypedComponent y)
{
return x.Id.Equals(y.Id);
}

/// <summary>
/// Returns a hash code for the specified object.
/// </summary>
/// <param name="obj">The <see cref="TypedComponent"/> for which a hash code is to be returned.</param>
/// <returns>A hash code for the specified object.</returns>
public override int GetHashCode(TypedComponent obj)
{
return obj.Id.GetHashCode();
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.ComponentDetection.Common/ComponentStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System.IO;
using Microsoft.ComponentDetection.Contracts;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System;

public class ConsoleWritingService : IConsoleWritingService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using Microsoft.ComponentDetection.Contracts.BcdeModels;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.ComponentDetection.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System.Collections.Generic;

public static class DigestUtility
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma warning disable SA1402
namespace Microsoft.ComponentDetection.Common;

using System;

public class DockerReferenceException : Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// identifier := /[a-f0-9]{64}/
// short-identifier := /[a-f0-9]{6,64}/
namespace Microsoft.ComponentDetection.Common;

using Microsoft.ComponentDetection.Contracts;

public static class DockerReferenceUtility
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System.Linq;
using System.Text.RegularExpressions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System;
using System.Linq;
using Microsoft.ComponentDetection.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
namespace Microsoft.ComponentDetection.Common.Exceptions;

using System;

/// <summary>
/// Exception thrown when the user input is invalid.
/// </summary>
public class InvalidUserInputException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="InvalidUserInputException"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="innerException">The exception that is the cause of the current exception.</param>
public InvalidUserInputException(string message, Exception innerException)
: base(message, innerException)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="InvalidUserInputException"/> class.
/// </summary>
public InvalidUserInputException()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="InvalidUserInputException"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public InvalidUserInputException(string message)
: base(message)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System.IO;
using Microsoft.ComponentDetection.Contracts;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System.Collections.Generic;
using System.IO;
using Microsoft.ComponentDetection.Contracts;
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.ComponentDetection.Common/MatchedFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System.IO;

public class MatchedFile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common.Telemetry.Attributes;

using System;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common.Telemetry;

using Microsoft.ComponentDetection.Common.Telemetry.Records;

public interface ITelemetryService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common.Telemetry.Records;

using System;
using System.Diagnostics;
using Microsoft.ComponentDetection.Common.Telemetry.Attributes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common.Telemetry.Records;

using System;

using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common.Telemetry.Records;

using System;
using Microsoft.ComponentDetection.Contracts;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common.Telemetry.Records;

using System.Runtime.CompilerServices;

public class DetectedComponentScopeRecord : BaseDetectionTelemetryRecord
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common.Telemetry.Records;

using System;

public interface IDetectionTelemetryRecord : IDisposable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common.Telemetry.Records;

using System;

public class NuGetProjectAssetsTelemetryRecord : IDetectionTelemetryRecord, IDisposable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common.Telemetry.Records;

using System.Net;

public class PypiFailureTelemetryRecord : BaseDetectionTelemetryRecord
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common.Telemetry.Records;

using System.Net;

public class PypiRetryTelemetryRecord : BaseDetectionTelemetryRecord
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts.BcdeModels;

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts.BcdeModels;

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts.BcdeModels;

using System.Collections.Generic;

public class DependencyGraph : Dictionary<string, HashSet<string>>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts.BcdeModels;

using System.Collections.Generic;

public class DependencyGraphCollection : Dictionary<string, DependencyGraphWithMetadata>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts.BcdeModels;

using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts.BcdeModels;

using System.Collections.Generic;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Newtonsoft.Json;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts.BcdeModels;

using System.Collections.Generic;
using Microsoft.ComponentDetection.Contracts.TypedComponent;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts.BcdeModels;

using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts.BcdeModels;

using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts.BcdeModels;

using System;
using System.Collections.Generic;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts;

using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Contracts;

using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
Expand Down Expand Up @@ -54,6 +55,9 @@ public interface ICommandLineInvocationService
Task<CommandLineExecutionResult> ExecuteCommandAsync(string command, IEnumerable<string> additionalCandidateCommands = null, params string[] parameters);
}

/// <summary>
/// Represents the result of executing a command line command.
/// </summary>
public class CommandLineExecutionResult
{
/// <summary>
Expand Down
Loading

0 comments on commit f7852d8

Please sign in to comment.