Skip to content

Commit

Permalink
Fixed IndexOutOfRangeException on lock file microsoft#1676 (microsoft…
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Nov 22, 2023
1 parent aa0eb94 commit 0704792
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ What's changed since pre-release v3.0.0-B0084:
[#1656](https://github.com/microsoft/PSRule/pull/1656)
- Bump System.Drawing.Common to v8.0.0.
[#1669](https://github.com/microsoft/PSRule/pull/1669)
- Bug fixes:
- Fixed CLI IndexOutOfRangeException with lock file by @BernieWhite.
[#1676](https://github.com/microsoft/PSRule/issues/1676)

## v3.0.0-B0084 (pre-release)

Expand Down
4 changes: 2 additions & 2 deletions src/PSRule.Tool/ClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal sealed class ClientHelper
private const int ERROR_MODULE_FAILEDTOFIND = 502;

private const int ERROR_MODULE_ADD_VIOLATES_CONSTRAINT = 503;


/// <summary>
/// One or more failures occurred.
Expand All @@ -60,7 +60,7 @@ public static int RunAnalyze(AnalyzerOptions operationOptions, ClientContext cli
option.Include.Path = operationOptions.Path;

// Build command
var builder = CommandLineBuilder.Assert(operationOptions.Module, file, option, host);
var builder = CommandLineBuilder.Assert(operationOptions.Module, option, host, file);
builder.Baseline(BaselineOption.FromString(operationOptions.Baseline));
builder.InputPath(inputPath);
builder.UnblockPublisher(PUBLISHER);
Expand Down
46 changes: 26 additions & 20 deletions src/PSRule/Pipeline/CommandLineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using PSRule.Configuration;
using PSRule.Pipeline.Dependencies;

#nullable enable

namespace PSRule.Pipeline
{
/// <summary>
Expand All @@ -15,20 +17,17 @@ public static class CommandLineBuilder
/// Create a builder for an Invoke pipeline.
/// </summary>
/// <remarks>
/// Invoke piplines process objects and produce records indicating the outcome of each rule.
/// Invoke pipelines process objects and produce records indicating the outcome of each rule.
/// </remarks>
/// <param name="module">The name of modules containing rules to process.</param>
/// <param name="option">Options that configure PSRule.</param>
/// <param name="hostContext">An implementation of a host context that will recieve output and results.</param>
/// <param name="hostContext">An implementation of a host context that will receive output and results.</param>
/// <param name="file">An optional lock file.</param>
/// <returns>A builder object to configure the pipeline.</returns>
public static IInvokePipelineBuilder Invoke(string[] module, PSRuleOption option, IHostContext hostContext)
public static IInvokePipelineBuilder Invoke(string[] module, PSRuleOption option, IHostContext hostContext, LockFile? file = null)
{
var sourcePipeline = new SourcePipelineBuilder(hostContext, option, GetLocalPath());
for (var i = 0; i < module.Length; i++)
sourcePipeline.ModuleByName(module[i]);

for (var i = 0; option.Include.Module != null && i < option.Include.Module.Length; i++)
sourcePipeline.ModuleByName(option.Include.Module[i]);
LoadModules(sourcePipeline, module, option, file);

var source = sourcePipeline.Build();
var pipeline = new InvokeRulePipelineBuilder(source, hostContext);
Expand All @@ -43,34 +42,41 @@ public static IInvokePipelineBuilder Invoke(string[] module, PSRuleOption option
/// Assert pipelines process objects with rules and produce text-based output suitable for output to a CI pipeline.
/// </remarks>
/// <param name="module">The name of modules containing rules to process.</param>
/// <param name="file">An optional lock file.</param>
/// <param name="option">Options that configure PSRule.</param>
/// <param name="hostContext">An implementation of a host context that will recieve output and results.</param>
/// <param name="hostContext">An implementation of a host context that will receive output and results.</param>
/// <param name="file">An optional lock file.</param>
/// <returns>A builder object to configure the pipeline.</returns>
public static IInvokePipelineBuilder Assert(string[] module, LockFile file, PSRuleOption option, IHostContext hostContext)
public static IInvokePipelineBuilder Assert(string[] module, PSRuleOption option, IHostContext hostContext, LockFile? file = null)
{
var sourcePipeline = new SourcePipelineBuilder(hostContext, option, GetLocalPath());
LoadModules(sourcePipeline, module, option, file);

var source = sourcePipeline.Build();
var pipeline = new AssertPipelineBuilder(source, hostContext);
pipeline.Configure(option);
return pipeline;
}

private static void LoadModules(SourcePipelineBuilder builder, string[] module, PSRuleOption option, LockFile? file)
{
for (var i = 0; module != null && i < module.Length; i++)
{
var version = file != null && file.Modules.TryGetValue(module[i], out var entry) ? entry.Version.ToString() : null;
sourcePipeline.ModuleByName(module[i], version);
builder.ModuleByName(module[i], version);
}

for (var i = 0; option.Include.Module != null && i < option.Include.Module.Length; i++)
{
var version = file != null && file.Modules.TryGetValue(module[i], out var entry) ? entry.Version.ToString() : null;
sourcePipeline.ModuleByName(option.Include.Module[i], version);
var version = file != null && file.Modules.TryGetValue(option.Include.Module[i], out var entry) ? entry.Version.ToString() : null;
builder.ModuleByName(option.Include.Module[i], version);
}

var source = sourcePipeline.Build();
var pipeline = new AssertPipelineBuilder(source, hostContext);
pipeline.Configure(option);
return pipeline;
}

internal static string GetLocalPath()
private static string? GetLocalPath()
{
return string.IsNullOrEmpty(AppContext.BaseDirectory) ? null : Environment.GetRootedBasePath(AppContext.BaseDirectory);
}
}
}

#nullable restore

0 comments on commit 0704792

Please sign in to comment.