Skip to content

Commit

Permalink
Additional fixes for #1746 (#1759)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Feb 14, 2024
1 parent 5ae1555 commit 1d89752
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
6 changes: 6 additions & 0 deletions docs/CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers

## Unreleased

What's changed since pre-release v3.0.0-B0151:

- Bug fixes:
- Fixes null references for CLI module handling by @BernieWhite.
[#1746](https://github.com/microsoft/PSRule/issues/1746)

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

What's changed since pre-release v3.0.0-B0141:
Expand Down
31 changes: 17 additions & 14 deletions src/PSRule.CommandLine/Commands/ModuleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static int ModuleRestore(RestoreOptions operationOptions, ClientContext c
}

// Restore from included modules.
if (clientContext.Option.Include?.Module != null && clientContext.Option.Include.Module.Length > 0)
if (clientContext.Option?.Include?.Module != null && clientContext.Option.Include.Module.Length > 0)
{
foreach (var includeModule in clientContext.Option.Include.Module)
{
Expand Down Expand Up @@ -140,7 +140,7 @@ public static int ModuleInit(ModuleOptions operationOptions, ClientContext clien
using var pwsh = PowerShell.Create();

// Add for any included modules.
if (clientContext.Option.Include?.Module != null && clientContext.Option.Include.Module.Length > 0)
if (clientContext.Option?.Include?.Module != null && clientContext.Option.Include.Module.Length > 0)
{
foreach (var includeModule in clientContext.Option.Include.Module)
{
Expand Down Expand Up @@ -335,7 +335,7 @@ public static int ModuleUpgrade(ModuleOptions operationOptions, ClientContext cl

#region Helper methods

private static IEnumerable<ModuleRecord> GetModules(PowerShell pwsh, LockFile file, PSRuleOption option)
private static IEnumerable<ModuleRecord> GetModules(PowerShell pwsh, LockFile file, PSRuleOption? option)
{
var results = new List<ModuleRecord>();

Expand All @@ -352,19 +352,22 @@ private static IEnumerable<ModuleRecord> GetModules(PowerShell pwsh, LockFile fi
}

// Process included modules from options.
foreach (var includeModule in option.Include.Module)
if (option?.Include?.Module != null && option.Include.Module.Length > 0)
{
// Skip modules already in the lock.
if (file.Modules.ContainsKey(includeModule))
continue;
foreach (var includeModule in option.Include.Module)
{
// Skip modules already in the lock.
if (file.Modules.ContainsKey(includeModule))
continue;

var installed = IsInstalled(pwsh, includeModule, null, out var installedVersion);
results.Add(new ModuleRecord(
Name: includeModule,
Version: installedVersion?.ToString() ?? "latest",
Installed: installed,
Locked: false
));
var installed = IsInstalled(pwsh, includeModule, null, out var installedVersion);
results.Add(new ModuleRecord(
Name: includeModule,
Version: installedVersion?.ToString() ?? "latest",
Installed: installed,
Locked: false
));
}
}

return results;
Expand Down
8 changes: 6 additions & 2 deletions src/PSRule/Pipeline/Dependencies/LockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,22 @@ public static LockFile Read(string path)
{
path = Environment.GetRootedPath(path);
path = Path.GetExtension(path) == ".json" ? path : Path.Combine(path, DEFAULT_FILE);
LockFile result = null;
if (File.Exists(path))
{
var json = File.ReadAllText(path, Encoding.UTF8);
return JsonConvert.DeserializeObject<LockFile>(json, new JsonSerializerSettings
result = JsonConvert.DeserializeObject<LockFile>(json, new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
new SemanticVersionConverter()
},
});
return result;
}
return new LockFile();
result ??= new LockFile();
result.Modules ??= new Dictionary<string, LockEntry>(StringComparer.OrdinalIgnoreCase);
return result;
}

/// <summary>
Expand Down

0 comments on commit 1d89752

Please sign in to comment.