зеркало из https://github.com/microsoft/PSRule.git
Родитель
cf3574978a
Коммит
7092853420
|
@ -22,6 +22,7 @@ indent_size = 2
|
|||
|
||||
# Code style defaults
|
||||
csharp_using_directive_placement = outside_namespace:suggestion
|
||||
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
|
||||
dotnet_sort_system_directives_first = true
|
||||
dotnet_style_readonly_field = true:suggestion
|
||||
|
||||
|
@ -42,9 +43,17 @@ dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
|
|||
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
|
||||
csharp_prefer_simple_default_expression = true:suggestion
|
||||
|
||||
# Pattern matching
|
||||
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
|
||||
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
|
||||
csharp_style_prefer_not_pattern = true:suggestion
|
||||
csharp_style_prefer_switch_expression = false:none
|
||||
csharp_style_prefer_pattern_matching = false:none
|
||||
|
||||
# Prefer "var" everywhere
|
||||
csharp_style_var_for_built_in_types = true:suggestion
|
||||
csharp_style_var_when_type_is_apparent = true:suggestion
|
||||
csharp_style_inlined_variable_declaration = true:suggestion
|
||||
csharp_style_var_elsewhere = true:suggestion
|
||||
|
||||
# Define the 'private_fields' symbol group:
|
||||
|
|
12
PSRule.sln
12
PSRule.sln
|
@ -26,6 +26,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.Types", "src\PSRule.
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.BuildTask", "src\PSRule.BuildTask\PSRule.BuildTask.csproj", "{872D2648-2F00-475E-84B5-F08BE07385B7}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{E0EA0CBA-96C5-4447-8B69-BC13EF0D7A4A}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.Tool.Tests", "tests\PSRule.Tool.Tests\PSRule.Tool.Tests.csproj", "{DA46C891-08F1-4D01-9F98-1F8BB10CAFEC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -68,10 +72,18 @@ Global
|
|||
{872D2648-2F00-475E-84B5-F08BE07385B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{872D2648-2F00-475E-84B5-F08BE07385B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{872D2648-2F00-475E-84B5-F08BE07385B7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{DA46C891-08F1-4D01-9F98-1F8BB10CAFEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{DA46C891-08F1-4D01-9F98-1F8BB10CAFEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DA46C891-08F1-4D01-9F98-1F8BB10CAFEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DA46C891-08F1-4D01-9F98-1F8BB10CAFEC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{D3488CE2-779F-4474-B38A-F894A4B689F7} = {E0EA0CBA-96C5-4447-8B69-BC13EF0D7A4A}
|
||||
{DA46C891-08F1-4D01-9F98-1F8BB10CAFEC} = {E0EA0CBA-96C5-4447-8B69-BC13EF0D7A4A}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {533491EB-BAE9-472E-B57F-A675ECD335B5}
|
||||
EndGlobalSection
|
||||
|
|
|
@ -44,6 +44,9 @@ What's changed since pre-release v2.9.0-B0033:
|
|||
- Update the baseline group to point to a new baseline.
|
||||
- Currently only a single baseline can be referenced by a baseline group.
|
||||
- See [baselines][6] for more information.
|
||||
- General improvements:
|
||||
- Added style and improved handling for restore command by @BernieWhite.
|
||||
[#1152](https://github.com/microsoft/PSRule/issues/1152)
|
||||
- Engineering:
|
||||
- Bump Microsoft.NET.Test.Sdk to v17.6.1.
|
||||
[#1540](https://github.com/microsoft/PSRule/pull/1540)
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.5" />
|
||||
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.6" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<AssemblyTitle>PSRule.BuildTask</AssemblyTitle>
|
||||
<EnableNuget>false</EnableNuget>
|
||||
<IsPackable>false</IsPackable>
|
||||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -3,15 +3,19 @@
|
|||
|
||||
using System.CommandLine;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using PSRule.Tool.Resources;
|
||||
|
||||
namespace PSRule.Tool
|
||||
{
|
||||
internal sealed class ClientBuilder
|
||||
{
|
||||
private static readonly string _Version = (Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
|
||||
|
||||
private readonly Option<string> _Option;
|
||||
private readonly Option<bool> _Verbose;
|
||||
private readonly Option<bool> _Debug;
|
||||
private readonly Option<bool> _RestoreForce;
|
||||
private readonly Option<string[]> _Path;
|
||||
private readonly Option<DirectoryInfo> _OutputPath;
|
||||
private readonly Option<string> _OutputFormat;
|
||||
|
@ -52,6 +56,10 @@ namespace PSRule.Tool
|
|||
_Baseline = new Option<string>(
|
||||
new string[] { "--baseline" }
|
||||
);
|
||||
_RestoreForce = new Option<bool>(
|
||||
new string[] { "--force" },
|
||||
CmdStrings.Restore_Force_Description
|
||||
);
|
||||
|
||||
cmd.AddGlobalOption(_Option);
|
||||
cmd.AddGlobalOption(_Verbose);
|
||||
|
@ -62,7 +70,7 @@ namespace PSRule.Tool
|
|||
|
||||
public static Command New()
|
||||
{
|
||||
var cmd = new RootCommand(CmdStrings.Cmd_Description)
|
||||
var cmd = new RootCommand(string.Concat(CmdStrings.Cmd_Description, " v", _Version))
|
||||
{
|
||||
Name = "ps-rule"
|
||||
};
|
||||
|
@ -103,6 +111,7 @@ namespace PSRule.Tool
|
|||
{
|
||||
var cmd = new Command("restore", CmdStrings.Restore_Description);
|
||||
cmd.AddOption(_Path);
|
||||
cmd.AddOption(_RestoreForce);
|
||||
cmd.SetHandler((invocation) =>
|
||||
{
|
||||
var option = new RestoreOptions
|
||||
|
@ -111,6 +120,7 @@ namespace PSRule.Tool
|
|||
Option = invocation.ParseResult.GetValueForOption(_Option),
|
||||
Verbose = invocation.ParseResult.GetValueForOption(_Verbose),
|
||||
Debug = invocation.ParseResult.GetValueForOption(_Debug),
|
||||
Force = invocation.ParseResult.GetValueForOption(_RestoreForce),
|
||||
};
|
||||
var client = new ClientContext();
|
||||
invocation.ExitCode = ClientHelper.RunRestore(option, client, invocation);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System.Collections;
|
||||
using System.CommandLine;
|
||||
using System.CommandLine.Invocation;
|
||||
using System.Management.Automation;
|
||||
|
@ -30,6 +31,13 @@ namespace PSRule.Tool
|
|||
/// </summary>
|
||||
private const int ERROR_BREAK_ON_FAILURE = 100;
|
||||
|
||||
private const string PARAM_NAME = "Name";
|
||||
private const string PARAM_VERSION = "Version";
|
||||
|
||||
private const string FIELD_PRERELEASE = "Prerelease";
|
||||
private const string FIELD_PSDATA = "PSData";
|
||||
private const string PRERELEASE_SEPARATOR = "-";
|
||||
|
||||
public static int RunAnalyze(AnalyzerOptions operationOptions, ClientContext clientContext, InvocationContext invocation)
|
||||
{
|
||||
var exitCode = 0;
|
||||
|
@ -69,13 +77,22 @@ namespace PSRule.Tool
|
|||
using var pwsh = PowerShell.Create();
|
||||
for (var i = 0; i < requires.Length; i++)
|
||||
{
|
||||
if (string.Equals(requires[i].Module, "PSRule", System.StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
|
||||
invocation.Console.WriteLine($"Getting {requires[i].Module}.");
|
||||
var version = GetVersion(pwsh, requires[i]);
|
||||
if (IsInstalled(pwsh, requires[i], out var installedVersion) && !operationOptions.Force)
|
||||
continue;
|
||||
|
||||
invocation.Console.WriteLine($"Installing {requires[i].Module} v{version}.");
|
||||
InstallVersion(pwsh, requires[i].Module, version);
|
||||
var idealVersion = FindVersion(pwsh, requires[i], installedVersion);
|
||||
if (idealVersion != null)
|
||||
{
|
||||
var version = idealVersion.ToString();
|
||||
invocation.Console.WriteLine($"Installing {requires[i].Module} v{version}.");
|
||||
InstallVersion(pwsh, requires[i].Module, version);
|
||||
}
|
||||
|
||||
if (pwsh.HadErrors)
|
||||
if (pwsh.HadErrors || (idealVersion == null && installedVersion == null))
|
||||
{
|
||||
exitCode = ERROR_MODUILE_FAILEDTOINSTALL;
|
||||
invocation.Console.Error.Write($"Failed to install {requires[i].Module}.");
|
||||
|
@ -88,25 +105,71 @@ namespace PSRule.Tool
|
|||
return exitCode;
|
||||
}
|
||||
|
||||
private static string GetVersion(PowerShell pwsh, ModuleConstraint constraint)
|
||||
private static bool IsInstalled(PowerShell pwsh, ModuleConstraint constraint, out SemanticVersion.Version installedVersion)
|
||||
{
|
||||
pwsh.Commands.Clear();
|
||||
pwsh.Streams.ClearStreams();
|
||||
pwsh.AddCommand("Get-Module")
|
||||
.AddParameter(PARAM_NAME, constraint.Module)
|
||||
.AddParameter("ListAvailable");
|
||||
|
||||
var versions = pwsh.Invoke();
|
||||
installedVersion = null;
|
||||
foreach (var version in versions)
|
||||
{
|
||||
if (TryModuleInfo(version, out var versionString) &&
|
||||
SemanticVersion.TryParseVersion(versionString, out var v) &&
|
||||
constraint.Constraint.Equals(v) &&
|
||||
v.CompareTo(installedVersion) > 0)
|
||||
installedVersion = v;
|
||||
}
|
||||
return installedVersion != null;
|
||||
}
|
||||
|
||||
private static bool TryModuleInfo(PSObject value, out string version)
|
||||
{
|
||||
version = null;
|
||||
if (value?.BaseObject is not PSModuleInfo info)
|
||||
return false;
|
||||
|
||||
version = info.Version?.ToString();
|
||||
if (TryPrivateData(info, FIELD_PSDATA, out var psData) && psData.ContainsKey(FIELD_PRERELEASE))
|
||||
version = string.Concat(version, PRERELEASE_SEPARATOR, psData[FIELD_PRERELEASE].ToString());
|
||||
|
||||
return version != null;
|
||||
}
|
||||
|
||||
private static bool TryPrivateData(PSModuleInfo info, string propertyName, out Hashtable value)
|
||||
{
|
||||
value = null;
|
||||
if (info.PrivateData is Hashtable privateData && privateData.ContainsKey(propertyName) && privateData[propertyName] is Hashtable data)
|
||||
{
|
||||
value = data;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static SemanticVersion.Version FindVersion(PowerShell pwsh, ModuleConstraint constraint, SemanticVersion.Version installedVersion)
|
||||
{
|
||||
pwsh.Commands.Clear();
|
||||
pwsh.Streams.ClearStreams();
|
||||
pwsh.AddCommand("Find-Module")
|
||||
.AddParameter("Name", constraint.Module)
|
||||
.AddParameter(PARAM_NAME, constraint.Module)
|
||||
.AddParameter("AllVersions");
|
||||
|
||||
var versions = pwsh.Invoke();
|
||||
SemanticVersion.Version result = null;
|
||||
foreach (var version in versions)
|
||||
{
|
||||
if (version.Properties["Version"].Value is string versionString &&
|
||||
if (version.Properties[PARAM_VERSION].Value is string versionString &&
|
||||
SemanticVersion.TryParseVersion(versionString, out var v) &&
|
||||
constraint.Constraint.Equals(v) &&
|
||||
v.CompareTo(result) > 0)
|
||||
v.CompareTo(result) > 0 &&
|
||||
v.CompareTo(installedVersion) > 0)
|
||||
result = v;
|
||||
}
|
||||
return result?.ToString();
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void InstallVersion(PowerShell pwsh, string name, string version)
|
||||
|
@ -114,8 +177,10 @@ namespace PSRule.Tool
|
|||
pwsh.Commands.Clear();
|
||||
pwsh.Streams.ClearStreams();
|
||||
pwsh.AddCommand("Install-Module")
|
||||
.AddParameter("Name", name)
|
||||
.AddParameter(PARAM_NAME, name)
|
||||
.AddParameter("RequiredVersion", version)
|
||||
.AddParameter("Scope", "CurrentUser")
|
||||
.AddParameter("AllowPrerelease")
|
||||
.AddParameter("Force");
|
||||
|
||||
pwsh.Invoke();
|
||||
|
@ -127,7 +192,7 @@ namespace PSRule.Tool
|
|||
var option = PSRuleOption.FromFileOrEmpty();
|
||||
option.Execution.InitialSessionState = Configuration.SessionState.Minimal;
|
||||
option.Input.Format = InputFormat.File;
|
||||
option.Output.Style = OutputStyle.Client;
|
||||
option.Output.Style ??= OutputStyle.Client;
|
||||
return option;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.CommandLine;
|
||||
using System.CommandLine.Invocation;
|
||||
using System.CommandLine.IO;
|
||||
|
@ -15,12 +16,16 @@ namespace PSRule.Tool
|
|||
private readonly InvocationContext _Invocation;
|
||||
private readonly bool _Verbose;
|
||||
private readonly bool _Debug;
|
||||
private readonly ConsoleColor _BackgroundColor;
|
||||
private readonly ConsoleColor _ForegroundColor;
|
||||
|
||||
public ClientHost(InvocationContext invocation, bool verbose, bool debug)
|
||||
{
|
||||
_Invocation = invocation;
|
||||
_Verbose = verbose;
|
||||
_Debug = debug;
|
||||
_BackgroundColor = Console.BackgroundColor;
|
||||
_ForegroundColor = Console.ForegroundColor;
|
||||
|
||||
Verbose($"Using working path: {Directory.GetCurrentDirectory()}");
|
||||
}
|
||||
|
@ -55,7 +60,27 @@ namespace PSRule.Tool
|
|||
public override void Information(InformationRecord informationRecord)
|
||||
{
|
||||
if (informationRecord?.MessageData is HostInformationMessage info)
|
||||
_Invocation.Console.WriteLine(info.Message);
|
||||
{
|
||||
SetConsole(info);
|
||||
if (info.NoNewLine.GetValueOrDefault(false))
|
||||
_Invocation.Console.Write(info.Message);
|
||||
else
|
||||
_Invocation.Console.WriteLine(info.Message);
|
||||
|
||||
RevertConsole();
|
||||
}
|
||||
}
|
||||
|
||||
private void SetConsole(HostInformationMessage info)
|
||||
{
|
||||
Console.BackgroundColor = info.BackgroundColor.GetValueOrDefault(_BackgroundColor);
|
||||
Console.ForegroundColor = info.ForegroundColor.GetValueOrDefault(_ForegroundColor);
|
||||
}
|
||||
|
||||
private void RevertConsole()
|
||||
{
|
||||
Console.BackgroundColor = _BackgroundColor;
|
||||
Console.ForegroundColor = _ForegroundColor;
|
||||
}
|
||||
|
||||
public override void Verbose(string text)
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("PSRule.Tool.Tests")]
|
|
@ -1,8 +1,14 @@
|
|||
{
|
||||
"profiles": {
|
||||
"PSRule.Tool": {
|
||||
"ps-rule analyze": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "analyze"
|
||||
"commandLineArgs": "analyze",
|
||||
"workingDirectory": "../../"
|
||||
},
|
||||
"ps-rule restore": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "restore",
|
||||
"workingDirectory": "../../"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ namespace PSRule.Tool.Resources {
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Run rule analysis..
|
||||
/// Looks up a localized string similar to Run rule analysis.
|
||||
/// </summary>
|
||||
internal static string Analyze_Description {
|
||||
get {
|
||||
|
@ -115,12 +115,21 @@ namespace PSRule.Tool.Resources {
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Restore PSRule modules..
|
||||
/// Looks up a localized string similar to Restore PSRule modules.
|
||||
/// </summary>
|
||||
internal static string Restore_Description {
|
||||
get {
|
||||
return ResourceManager.GetString("Restore_Description", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Force restore of modules.
|
||||
/// </summary>
|
||||
internal static string Restore_Force_Description {
|
||||
get {
|
||||
return ResourceManager.GetString("Restore_Force_Description", resourceCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -118,7 +118,7 @@
|
|||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<data name="Analyze_Description" xml:space="preserve">
|
||||
<value>Run rule analysis.</value>
|
||||
<value>Run rule analysis</value>
|
||||
</data>
|
||||
<data name="Cmd_Description" xml:space="preserve">
|
||||
<value>PSRule CLI</value>
|
||||
|
@ -136,6 +136,9 @@
|
|||
<value>Return verbose output</value>
|
||||
</data>
|
||||
<data name="Restore_Description" xml:space="preserve">
|
||||
<value>Restore PSRule modules.</value>
|
||||
<value>Restore PSRule modules</value>
|
||||
</data>
|
||||
<data name="Restore_Force_Description" xml:space="preserve">
|
||||
<value>Force restore of modules</value>
|
||||
</data>
|
||||
</root>
|
|
@ -12,5 +12,7 @@ namespace PSRule.Tool
|
|||
public bool Verbose { get; set; }
|
||||
|
||||
public bool Debug { get; set; }
|
||||
|
||||
public bool Force { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -762,8 +762,7 @@ namespace PSRule
|
|||
_FunctionBuilder.Push();
|
||||
while (reader.TokenType != JsonToken.EndObject)
|
||||
{
|
||||
var name = reader.Value as string;
|
||||
if (name != null)
|
||||
if (reader.Value is string name)
|
||||
{
|
||||
reader.Consume(JsonToken.PropertyName);
|
||||
if (reader.TryConsume(JsonToken.StartObject))
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace PSRule
|
|||
internal static bool TryValidateResourceAnnotation(this IResource resource, out ValidateResourceAnnotation value)
|
||||
{
|
||||
value = null;
|
||||
if (!(resource is IAnnotated<ResourceAnnotation> annotated))
|
||||
if (resource is not IAnnotated<ResourceAnnotation> annotated)
|
||||
return false;
|
||||
|
||||
value = annotated.GetAnnotation<ValidateResourceAnnotation>();
|
||||
|
|
|
@ -23,7 +23,7 @@ namespace PSRule.Configuration
|
|||
private const bool DEFAULT_SARIF_PROBLEMS_ONLY = true;
|
||||
private const OutputStyle DEFAULT_STYLE = OutputStyle.Detect;
|
||||
|
||||
internal static readonly OutputOption Default = new OutputOption
|
||||
internal static readonly OutputOption Default = new()
|
||||
{
|
||||
As = DEFAULT_AS,
|
||||
Banner = DEFAULT_BANNER,
|
||||
|
|
|
@ -241,7 +241,7 @@ namespace PSRule.Host
|
|||
private static ScriptBlockAst GetParentBlock(Ast ast)
|
||||
{
|
||||
var block = ast;
|
||||
while (block != null && !(block is ScriptBlockAst))
|
||||
while (block != null && block is not ScriptBlockAst)
|
||||
block = block.Parent;
|
||||
|
||||
return (ScriptBlockAst)block;
|
||||
|
|
|
@ -97,7 +97,7 @@ namespace PSRule.Pipeline
|
|||
|
||||
public override void WriteObject(object sendToPipeline, bool enumerateCollection)
|
||||
{
|
||||
if (!(sendToPipeline is RuleHelpInfo[] result))
|
||||
if (sendToPipeline is not RuleHelpInfo[] result)
|
||||
{
|
||||
base.WriteObject(sendToPipeline, enumerateCollection);
|
||||
return;
|
||||
|
|
|
@ -27,7 +27,7 @@ namespace PSRule.Pipeline.Output
|
|||
|
||||
public override void WriteObject(object sendToPipeline, bool enumerateCollection)
|
||||
{
|
||||
if (!(sendToPipeline is InvokeResult result))
|
||||
if (sendToPipeline is not InvokeResult result)
|
||||
return;
|
||||
|
||||
Add(result);
|
||||
|
|
|
@ -349,7 +349,7 @@ namespace PSRule.Pipeline.Output
|
|||
|
||||
public override void WriteObject(object sendToPipeline, bool enumerateCollection)
|
||||
{
|
||||
if (!(sendToPipeline is InvokeResult result))
|
||||
if (sendToPipeline is not InvokeResult result)
|
||||
return;
|
||||
|
||||
Add(result);
|
||||
|
|
|
@ -243,7 +243,7 @@ namespace PSRule.Pipeline
|
|||
private static RepositoryInfo GetRepositoryInfo(TargetObject targetObject, out TargetSourceInfo sourceInfo)
|
||||
{
|
||||
sourceInfo = null;
|
||||
if (!(targetObject.Value.BaseObject is InputFileInfo inputFileInfo))
|
||||
if (targetObject.Value.BaseObject is not InputFileInfo inputFileInfo)
|
||||
return null;
|
||||
|
||||
sourceInfo = new TargetSourceInfo(inputFileInfo);
|
||||
|
|
|
@ -22,7 +22,7 @@ namespace PSRule.Pipeline
|
|||
|
||||
public override void WriteObject(object sendToPipeline, bool enumerateCollection)
|
||||
{
|
||||
if (!(sendToPipeline is InvokeResult result) || !ShouldOutput(result.Outcome))
|
||||
if (sendToPipeline is not InvokeResult result || !ShouldOutput(result.Outcome))
|
||||
return;
|
||||
|
||||
base.WriteObject(result.IsSuccess(), false);
|
||||
|
|
|
@ -1586,7 +1586,7 @@ namespace PSRule.Runtime
|
|||
|
||||
private static bool TryTypeName(object fieldValue, string typeName)
|
||||
{
|
||||
if (!(fieldValue is PSObject pso))
|
||||
if (fieldValue is not PSObject pso)
|
||||
return false;
|
||||
|
||||
for (var i = 0; i < pso.TypeNames.Count; i++)
|
||||
|
|
|
@ -104,8 +104,8 @@ namespace PSRule.Runtime.ObjectPath
|
|||
[DebuggerDisplay("Type = {Type}, Arg = {Arg}")]
|
||||
internal sealed class PathToken : IPathToken
|
||||
{
|
||||
public readonly static PathToken RootRef = new PathToken(PathTokenType.RootRef);
|
||||
public readonly static PathToken CurrentRef = new PathToken(PathTokenType.CurrentRef);
|
||||
public readonly static PathToken RootRef = new(PathTokenType.RootRef);
|
||||
public readonly static PathToken CurrentRef = new(PathTokenType.CurrentRef);
|
||||
|
||||
public PathTokenType Type { get; }
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ namespace PSRule.Runtime
|
|||
{
|
||||
internal static class RuleConditionHelper
|
||||
{
|
||||
private readonly static RuleConditionResult Empty = new RuleConditionResult(pass: 0, count: 0, hadErrors: false);
|
||||
private readonly static RuleConditionResult Empty = new(pass: 0, count: 0, hadErrors: false);
|
||||
|
||||
internal static RuleConditionResult Create(IEnumerable<object> value)
|
||||
{
|
||||
|
@ -41,7 +41,7 @@ namespace PSRule.Runtime
|
|||
private static bool TryBoolean(object o, out bool result)
|
||||
{
|
||||
result = false;
|
||||
if (!(o is bool bresult))
|
||||
if (o is not bool bresult)
|
||||
return false;
|
||||
|
||||
result = bresult;
|
||||
|
@ -51,7 +51,7 @@ namespace PSRule.Runtime
|
|||
private static bool TryAssertResult(object o, out bool result)
|
||||
{
|
||||
result = false;
|
||||
if (!(o is AssertResult assert))
|
||||
if (o is not AssertResult assert)
|
||||
return false;
|
||||
|
||||
result = assert.Result;
|
||||
|
|
|
@ -8,7 +8,6 @@ using PSRule.Definitions.Rules;
|
|||
using PSRule.Pipeline;
|
||||
using PSRule.Pipeline.Formatters;
|
||||
using PSRule.Rules;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -9,7 +9,6 @@ using Newtonsoft.Json.Linq;
|
|||
using PSRule.Data;
|
||||
using PSRule.Pipeline;
|
||||
using PSRule.Runtime;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
// Licensed under the MIT License.
|
||||
|
||||
using PSRule.Badges;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -13,7 +13,6 @@ using PSRule.Host;
|
|||
using PSRule.Pipeline;
|
||||
using PSRule.Pipeline.Output;
|
||||
using PSRule.Runtime;
|
||||
using Xunit;
|
||||
using YamlDotNet.Serialization;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ using System.IO;
|
|||
using System.Management.Automation;
|
||||
using PSRule.Configuration;
|
||||
using PSRule.Pipeline;
|
||||
using Xunit;
|
||||
using static PSRule.PipelineTests;
|
||||
|
||||
namespace PSRule
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
// Licensed under the MIT License.
|
||||
|
||||
using PSRule.Data;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
public sealed class ExpressionHelpersTests
|
||||
|
|
|
@ -9,7 +9,6 @@ using PSRule.Definitions.Selectors;
|
|||
using PSRule.Host;
|
||||
using PSRule.Pipeline;
|
||||
using PSRule.Runtime;
|
||||
using Xunit;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
namespace PSRule
|
||||
|
|
|
@ -7,7 +7,6 @@ using System.Management.Automation;
|
|||
using PSRule.Configuration;
|
||||
using PSRule.Definitions.Expressions;
|
||||
using PSRule.Pipeline;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -6,7 +6,6 @@ using System.IO;
|
|||
using System.Linq;
|
||||
using System.Management.Automation;
|
||||
using PSRule.Pipeline;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
@ -22,7 +21,7 @@ namespace PSRule
|
|||
Assert.Equal("Test", actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<string>("kind"));
|
||||
Assert.Equal(2, actual[1].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<int>("value2"));
|
||||
Assert.Equal(2, actual[1].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<PSObject[]>("array").Length);
|
||||
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out string path));
|
||||
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out var path));
|
||||
Assert.Null(path);
|
||||
|
||||
// Array item
|
||||
|
@ -44,7 +43,7 @@ namespace PSRule
|
|||
Assert.Equal("Test", actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<string>("kind"));
|
||||
Assert.Equal(2, actual[1].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<int>("value2"));
|
||||
Assert.Equal(3, actual[1].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<PSObject[]>("array").Length);
|
||||
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out string path));
|
||||
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out var path));
|
||||
Assert.Null(path);
|
||||
actual[0].Value.TryTargetInfo(out var info1);
|
||||
actual[1].Value.TryTargetInfo(out var info2);
|
||||
|
@ -74,7 +73,7 @@ namespace PSRule
|
|||
Assert.Equal("Test", actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<string>("kind"));
|
||||
Assert.Equal(1, actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<int>("value1"));
|
||||
Assert.Equal(2, actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<PSObject[]>("array").Length);
|
||||
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out string path));
|
||||
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out var path));
|
||||
Assert.Null(path);
|
||||
}
|
||||
|
||||
|
@ -88,7 +87,7 @@ namespace PSRule
|
|||
Assert.Equal("Test", actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<string>("kind"));
|
||||
Assert.Equal(1, actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<int>("value1"));
|
||||
Assert.Equal(2, actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<Array>("array").Length);
|
||||
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out string path));
|
||||
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out var path));
|
||||
Assert.Null(path);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@ using System;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using PSRule.Pipeline;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
// Licensed under the MIT License.
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
using System.Management.Automation;
|
||||
using PSRule.Host;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -8,7 +8,6 @@ using PSRule.Configuration;
|
|||
using PSRule.Host;
|
||||
using PSRule.Pipeline;
|
||||
using PSRule.Runtime;
|
||||
using Xunit;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
namespace PSRule
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
using System;
|
||||
using PSRule.Pipeline;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using PSRule.Definitions;
|
||||
using Xunit;
|
||||
using ObjectHelper = PSRule.Runtime.ObjectHelper;
|
||||
|
||||
namespace PSRule
|
||||
|
|
|
@ -6,7 +6,6 @@ using System.IO;
|
|||
using System.Linq;
|
||||
using System.Management.Automation;
|
||||
using PSRule.Pipeline;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
using PSRule.Configuration;
|
||||
using PSRule.Pipeline;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -14,7 +14,6 @@ using PSRule.Definitions.Rules;
|
|||
using PSRule.Pipeline;
|
||||
using PSRule.Pipeline.Output;
|
||||
using PSRule.Rules;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -7,13 +7,11 @@
|
|||
<IsPackable>false</IsPackable>
|
||||
<RootNamespace>PSRule</RootNamespace>
|
||||
<DebugType>Full</DebugType>
|
||||
<!--<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>-->
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.1" />
|
||||
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.5" />
|
||||
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.6" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
|
|
@ -6,7 +6,6 @@ using System.IO;
|
|||
using System.Management.Automation;
|
||||
using PSRule.Configuration;
|
||||
using PSRule.Pipeline;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -7,7 +7,6 @@ using System.Management.Automation;
|
|||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using PSRule.Runtime.ObjectPath;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
using System;
|
||||
using PSRule.Pipeline;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
// Licensed under the MIT License.
|
||||
|
||||
using PSRule.Runtime.ObjectPath;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -12,7 +12,6 @@ using PSRule.Configuration;
|
|||
using PSRule.Pipeline;
|
||||
using PSRule.Resources;
|
||||
using PSRule.Rules;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
// Licensed under the MIT License.
|
||||
|
||||
using PSRule.Definitions;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -8,7 +8,6 @@ using PSRule.Definitions;
|
|||
using PSRule.Host;
|
||||
using PSRule.Pipeline;
|
||||
using PSRule.Runtime;
|
||||
using Xunit;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
namespace PSRule
|
||||
|
|
|
@ -7,7 +7,6 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using PSRule.Definitions;
|
||||
using PSRule.Help;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
using System.Collections;
|
||||
using PSRule.Definitions;
|
||||
using PSRule.Definitions.Rules;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using PSRule.Host;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -11,7 +11,6 @@ using PSRule.Host;
|
|||
using PSRule.Pipeline;
|
||||
using PSRule.Rules;
|
||||
using PSRule.Runtime;
|
||||
using Xunit;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
namespace PSRule
|
||||
|
|
|
@ -11,7 +11,6 @@ using PSRule.Definitions.Selectors;
|
|||
using PSRule.Host;
|
||||
using PSRule.Pipeline;
|
||||
using PSRule.Runtime;
|
||||
using Xunit;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
namespace PSRule
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
public sealed class SemanticBreakTests
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
// Licensed under the MIT License.
|
||||
|
||||
using PSRule.Data;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
public sealed class StringExtensionsTests
|
||||
|
|
|
@ -9,7 +9,6 @@ using PSRule.Host;
|
|||
using PSRule.Pipeline;
|
||||
using PSRule.Rules;
|
||||
using PSRule.Runtime;
|
||||
using Xunit;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
namespace PSRule
|
||||
|
|
|
@ -9,7 +9,6 @@ using PSRule.Configuration;
|
|||
using PSRule.Host;
|
||||
using PSRule.Pipeline;
|
||||
using PSRule.Runtime;
|
||||
using Xunit;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
namespace PSRule
|
||||
|
|
|
@ -6,7 +6,6 @@ using Newtonsoft.Json.Linq;
|
|||
using PSRule.Configuration;
|
||||
using PSRule.Definitions.Baselines;
|
||||
using PSRule.Pipeline;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
// Licensed under the MIT License.
|
||||
|
||||
using System.Management.Automation;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
|
|
@ -5,7 +5,6 @@ using System.Management.Automation;
|
|||
using PSRule.Configuration;
|
||||
using PSRule.Data;
|
||||
using PSRule.Pipeline;
|
||||
using Xunit;
|
||||
|
||||
namespace PSRule
|
||||
{
|
||||
|
@ -56,7 +55,7 @@ namespace PSRule
|
|||
|
||||
PipelineContext.CurrentThread = PipelineContext.New(GetOption(), null, null, null, null, null, null, null);
|
||||
|
||||
var actual = PipelineHookActions.BindTargetName(new string[] { "Name" }, false, false, pso1, out string path);
|
||||
var actual = PipelineHookActions.BindTargetName(new string[] { "Name" }, false, false, pso1, out var path);
|
||||
Assert.Equal("OtherName", actual);
|
||||
Assert.Equal("Name", path);
|
||||
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
global using Xunit;
|
|
@ -0,0 +1,39 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System.CommandLine;
|
||||
using System.CommandLine.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PSRule.Tool;
|
||||
|
||||
public sealed class CommandTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Analyze()
|
||||
{
|
||||
var console = new TestConsole();
|
||||
var builder = ClientBuilder.New();
|
||||
Assert.NotNull(builder.Subcommands.FirstOrDefault(c => c.Name == "analyze"));
|
||||
|
||||
await builder.InvokeAsync("analyze", console);
|
||||
var output = console.Out.ToString();
|
||||
|
||||
Assert.NotNull(output);
|
||||
Assert.Contains($"Using PSRule v0.0.1{System.Environment.NewLine}", output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Restore()
|
||||
{
|
||||
var console = new TestConsole();
|
||||
var builder = ClientBuilder.New();
|
||||
Assert.NotNull(builder.Subcommands.FirstOrDefault(c => c.Name == "restore"));
|
||||
|
||||
await builder.InvokeAsync("restore", console);
|
||||
|
||||
var output = console.Out.ToString();
|
||||
Assert.NotNull(output);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ProjectGuid>{d3488ce2-779f-4474-b38a-f894a4b689f7}</ProjectGuid>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
<IsPackable>false</IsPackable>
|
||||
<RootNamespace>PSRule.Tool</RootNamespace>
|
||||
<DebugType>Full</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\PSRule.Tool\PSRule.Tool.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,4 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
global using Xunit;
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
|
||||
"diagnosticMessages": true
|
||||
}
|
Загрузка…
Ссылка в новой задаче