From aa6517250a36c33dbecab864567fa9e4c28ed958 Mon Sep 17 00:00:00 2001 From: Paul Higinbotham Date: Mon, 3 May 2021 16:56:00 -0700 Subject: [PATCH 1/3] Move the PSResourceInfo class to its own source file, and rename test --- src/code/PSResourceInfo.cs | 413 ++++++++++++++++++ src/code/Utils.cs | 388 ---------------- ...nfo.Tests.ps1 => PSResourceInfo.Tests.ps1} | 0 3 files changed, 413 insertions(+), 388 deletions(-) create mode 100644 src/code/PSResourceInfo.cs rename test/{PSGetResourceInfo.Tests.ps1 => PSResourceInfo.Tests.ps1} (100%) diff --git a/src/code/PSResourceInfo.cs b/src/code/PSResourceInfo.cs new file mode 100644 index 000000000..86225a77e --- /dev/null +++ b/src/code/PSResourceInfo.cs @@ -0,0 +1,413 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.Management.Automation; + +namespace Microsoft.PowerShell.PowerShellGet.UtilClasses +{ + #region Enums + + public enum ResourceType + { + Module, + Script + } + + public enum VersionType + { + Unknown, + MinimumVersion, + RequiredVersion, + MaximumVersion + } + + #endregion + + #region VersionInfo + + public struct VersionInfo + { + public VersionInfo( + VersionType versionType, + Version versionNum) + { + VersionType = versionType; + VersionNum = versionNum; + } + + public VersionType VersionType { get; } + public Version VersionNum { get; } + + public override string ToString() => $"{VersionType}: {VersionNum}"; + } + + #endregion + + #region ResourceIncludes + + public sealed class ResourceIncludes + { + #region Properties + + public string[] Cmdlet { get; } + + public string[] Command { get; } + + public string[] DscResource { get; } + + public string[] Function { get; } + + public string[] RoleCapability { get; } + + public string[] Workflow { get; } + + #endregion + + #region Constructor + + /// + /// Constructor + /// + /// Provided hashtable has form: + /// Key: Cmdlet + /// Value: ArrayList of Cmdlet name strings + /// Key: Command + /// Value: ArrayList of Command name strings + /// Key: DscResource + /// Value: ArrayList of DscResource name strings + /// Key: Function + /// Value: ArrayList of Function name strings + /// Key: RoleCapability (deprecated for PSGetV3) + /// Value: ArrayList of RoleCapability name strings + /// Key: Workflow (deprecated for PSGetV3) + /// Value: ArrayList of Workflow name strings + /// + /// Hashtable of PSGet includes + public ResourceIncludes(Hashtable includes) + { + if (includes == null) { return; } + + Cmdlet = GetHashTableItem(includes, nameof(Cmdlet)); + Command = GetHashTableItem(includes, nameof(Command)); + DscResource = GetHashTableItem(includes, nameof(DscResource)); + Function = GetHashTableItem(includes, nameof(Function)); + RoleCapability = GetHashTableItem(includes, nameof(RoleCapability)); + Workflow = GetHashTableItem(includes, nameof(Workflow)); + } + + #endregion + + #region Public methods + + public Hashtable ConvertToHashtable() + { + var hashtable = new Hashtable + { + { nameof(Cmdlet), Cmdlet }, + { nameof(Command), Command }, + { nameof(DscResource), DscResource }, + { nameof(Function), Function }, + { nameof(RoleCapability), RoleCapability }, + { nameof(Workflow), Workflow } + }; + + return hashtable; + } + + #endregion + + #region Private methods + + private string[] GetHashTableItem( + Hashtable table, + string name) + { + if (table.ContainsKey(name) && + table[name] is PSObject psObjectItem) + { + return Utils.GetStringArray(psObjectItem.BaseObject as ArrayList); + } + + return null; + } + + #endregion + } + + #endregion + + #region PSResourceInfo + + public sealed class PSResourceInfo + { + #region Properties + + public Dictionary AdditionalMetadata { get; set; } + public string Author { get; set; } + public string CompanyName { get; set; } + public string Copyright { get; set; } + public string[] Dependencies { get; set; } + public string Description { get; set; } + public Uri IconUri { get; set; } + public ResourceIncludes Includes { get; set; } + public DateTime? InstalledDate { get; set; } + public string InstalledLocation { get; set; } + public Uri LicenseUri { get; set; } + public string Name { get; set; } + public string PackageManagementProvider { get; set; } + public string PowerShellGetFormatVersion { get; set; } + public Uri ProjectUri { get; set; } + public DateTime? PublishedDate { get; set; } + public string ReleaseNotes { get; set; } + public string Repository { get; set; } + public string RepositorySourceLocation { get; set; } + public string[] Tags { get; set; } + public string Type { get; set; } + public DateTime? UpdatedDate { get; set; } + public Version Version { get; set; } + + #endregion + + #region Public static methods + + /// + /// Writes the PSGetResourceInfo properties to the specified file path as a + /// PowerShell serialized xml file, maintaining compatibility with + /// PowerShellGet v2 file format. + /// + public bool TryWrite( + string filePath, + out string errorMsg) + { + errorMsg = string.Empty; + + if (string.IsNullOrWhiteSpace(filePath)) + { + errorMsg = "TryWritePSGetInfo: Invalid file path. Filepath cannot be empty or whitespace."; + return false; + } + + try + { + var infoXml = PSSerializer.Serialize( + source: ConvertToCustomObject(), + depth: 5); + + System.IO.File.WriteAllText( + path: filePath, + contents: infoXml); + + return true; + } + catch(Exception ex) + { + errorMsg = string.Format( + CultureInfo.InvariantCulture, + @"TryWritePSGetInfo: Cannot convert and write the PowerShellGet information to file, with error: {0}", + ex.Message); + + return false; + } + } + + /// + /// Reads a PSGet resource xml (PowerShell serialized) file and returns + /// a PSGetResourceInfo object containing the file contents. + /// + public static bool TryRead( + string filePath, + out PSResourceInfo psGetInfo, + out string errorMsg) + { + psGetInfo = null; + errorMsg = string.Empty; + + if (string.IsNullOrWhiteSpace(filePath)) + { + errorMsg = "TryReadPSGetInfo: Invalid file path. Filepath cannot be empty or whitespace."; + return false; + } + + try + { + // Read and deserialize information xml file. + var psObjectInfo = (PSObject) PSSerializer.Deserialize( + System.IO.File.ReadAllText( + filePath)); + + psGetInfo = new PSResourceInfo + { + AdditionalMetadata = GetProperty>(nameof(PSResourceInfo.AdditionalMetadata), psObjectInfo), + Author = GetProperty(nameof(PSResourceInfo.Author), psObjectInfo), + CompanyName = GetProperty(nameof(PSResourceInfo.CompanyName), psObjectInfo), + Copyright = GetProperty(nameof(PSResourceInfo.Copyright), psObjectInfo), + Dependencies = Utils.GetStringArray(GetProperty(nameof(PSResourceInfo.Dependencies), psObjectInfo)), + Description = GetProperty(nameof(PSResourceInfo.Description), psObjectInfo), + IconUri = GetProperty(nameof(PSResourceInfo.IconUri), psObjectInfo), + Includes = new ResourceIncludes(GetProperty(nameof(PSResourceInfo.Includes), psObjectInfo)), + InstalledDate = GetProperty(nameof(PSResourceInfo.InstalledDate), psObjectInfo), + InstalledLocation = GetProperty(nameof(PSResourceInfo.InstalledLocation), psObjectInfo), + LicenseUri = GetProperty(nameof(PSResourceInfo.LicenseUri), psObjectInfo), + Name = GetProperty(nameof(PSResourceInfo.Name), psObjectInfo), + PackageManagementProvider = GetProperty(nameof(PSResourceInfo.PackageManagementProvider), psObjectInfo), + PowerShellGetFormatVersion = GetProperty(nameof(PSResourceInfo.PowerShellGetFormatVersion), psObjectInfo), + ProjectUri = GetProperty(nameof(PSResourceInfo.ProjectUri), psObjectInfo), + PublishedDate = GetProperty(nameof(PSResourceInfo.PublishedDate), psObjectInfo), + ReleaseNotes = GetProperty(nameof(PSResourceInfo.ReleaseNotes), psObjectInfo), + Repository = GetProperty(nameof(PSResourceInfo.Repository), psObjectInfo), + RepositorySourceLocation = GetProperty(nameof(PSResourceInfo.RepositorySourceLocation), psObjectInfo), + Tags = Utils.GetStringArray(GetProperty(nameof(PSResourceInfo.Tags), psObjectInfo)), + Type = GetProperty(nameof(PSResourceInfo.Type), psObjectInfo), + UpdatedDate = GetProperty(nameof(PSResourceInfo.UpdatedDate), psObjectInfo), + Version = GetProperty(nameof(PSResourceInfo.Version), psObjectInfo) + }; + + return true; + } + catch(Exception ex) + { + errorMsg = string.Format( + CultureInfo.InvariantCulture, + @"TryReadPSGetInfo: Cannot read the PowerShellGet information file with error: {0}", + ex.Message); + + return false; + } + } + + #endregion + + #region Private static methods + + private static T ConvertToType(PSObject psObject) + { + // We only convert Dictionary types. + if (typeof(T) != typeof(Dictionary)) + { + return default(T); + } + + var dict = new Dictionary(); + foreach (var prop in psObject.Properties) + { + dict.Add(prop.Name, prop.Value.ToString()); + } + + return (T)Convert.ChangeType(dict, typeof(T)); + } + + private static T GetProperty( + string Name, + PSObject psObjectInfo) + { + var val = psObjectInfo.Properties[Name]?.Value; + if (val == null) + { + return default(T); + } + + switch (val) + { + case T valType: + return valType; + + case PSObject valPSObject: + switch (valPSObject.BaseObject) + { + case T valBase: + return valBase; + + case PSCustomObject _: + // A base object of PSCustomObject means this is additional metadata + // and type T should be Dictionary. + return ConvertToType(valPSObject); + + default: + return default(T); + } + + default: + return default(T); + } + } + + #endregion + + #region Private methods + + private PSObject ConvertToCustomObject() + { + var additionalMetadata = new PSObject(); + foreach (var item in AdditionalMetadata) + { + additionalMetadata.Properties.Add(new PSNoteProperty(item.Key, item.Value)); + } + + var psObject = new PSObject(); + psObject.Properties.Add(new PSNoteProperty(nameof(AdditionalMetadata), additionalMetadata)); + psObject.Properties.Add(new PSNoteProperty(nameof(Author), Author)); + psObject.Properties.Add(new PSNoteProperty(nameof(CompanyName), CompanyName)); + psObject.Properties.Add(new PSNoteProperty(nameof(Copyright), Copyright)); + psObject.Properties.Add(new PSNoteProperty(nameof(Dependencies), Dependencies)); + psObject.Properties.Add(new PSNoteProperty(nameof(Description), Description)); + psObject.Properties.Add(new PSNoteProperty(nameof(IconUri), IconUri)); + psObject.Properties.Add(new PSNoteProperty(nameof(Includes), Includes.ConvertToHashtable())); + psObject.Properties.Add(new PSNoteProperty(nameof(InstalledDate), InstalledDate)); + psObject.Properties.Add(new PSNoteProperty(nameof(InstalledLocation), InstalledLocation)); + psObject.Properties.Add(new PSNoteProperty(nameof(LicenseUri), LicenseUri)); + psObject.Properties.Add(new PSNoteProperty(nameof(Name), Name)); + psObject.Properties.Add(new PSNoteProperty(nameof(PackageManagementProvider), PackageManagementProvider)); + psObject.Properties.Add(new PSNoteProperty(nameof(PowerShellGetFormatVersion), PowerShellGetFormatVersion)); + psObject.Properties.Add(new PSNoteProperty(nameof(ProjectUri), ProjectUri)); + psObject.Properties.Add(new PSNoteProperty(nameof(PublishedDate), PublishedDate)); + psObject.Properties.Add(new PSNoteProperty(nameof(ReleaseNotes), ReleaseNotes)); + psObject.Properties.Add(new PSNoteProperty(nameof(Repository), Repository)); + psObject.Properties.Add(new PSNoteProperty(nameof(RepositorySourceLocation), RepositorySourceLocation)); + psObject.Properties.Add(new PSNoteProperty(nameof(Tags), Tags)); + psObject.Properties.Add(new PSNoteProperty(nameof(Type), Type)); + psObject.Properties.Add(new PSNoteProperty(nameof(UpdatedDate), UpdatedDate)); + psObject.Properties.Add(new PSNoteProperty(nameof(Version), Version)); + + return psObject; + } + + #endregion + } + + #endregion + + #region Test Hooks + + public static class TestHooks + { + public static PSObject ReadPSGetResourceInfo(string filePath) + { + if (PSResourceInfo.TryRead(filePath, out PSResourceInfo psGetInfo, out string errorMsg)) + { + return PSObject.AsPSObject(psGetInfo); + } + + throw new PSInvalidOperationException(errorMsg); + } + + public static void WritePSGetResourceInfo( + string filePath, + PSObject psObjectGetInfo) + { + if (psObjectGetInfo.BaseObject is PSResourceInfo psGetInfo) + { + if (! psGetInfo.TryWrite(filePath, out string errorMsg)) + { + throw new PSInvalidOperationException(errorMsg); + } + + return; + } + + throw new PSArgumentException("psObjectGetInfo argument is not a PSGetResourceInfo type."); + } + } + + #endregion +} diff --git a/src/code/Utils.cs b/src/code/Utils.cs index 440a18d86..02d13e40d 100644 --- a/src/code/Utils.cs +++ b/src/code/Utils.cs @@ -3,9 +3,6 @@ using System; using System.Collections; -using System.Collections.Generic; -using System.Globalization; -using System.Management.Automation; using System.Management.Automation.Language; namespace Microsoft.PowerShell.PowerShellGet.UtilClasses @@ -57,389 +54,4 @@ public static string[] GetStringArray(ArrayList list) #endregion } - - #region PSGetResourceInfo classes - - internal sealed class PSGetIncludes - { - #region Properties - - public string[] Cmdlet { get; } - - public string[] Command { get; } - - public string[] DscResource { get; } - - public string[] Function { get; } - - public string[] RoleCapability { get; } - - public string[] Workflow { get; } - - #endregion - - #region Constructor - - /// - /// Constructor - /// - /// Provided hashtable has form: - /// Key: Cmdlet - /// Value: ArrayList of Cmdlet name strings - /// Key: Command - /// Value: ArrayList of Command name strings - /// Key: DscResource - /// Value: ArrayList of DscResource name strings - /// Key: Function - /// Value: ArrayList of Function name strings - /// Key: RoleCapability (deprecated for PSGetV3) - /// Value: ArrayList of RoleCapability name strings - /// Key: Workflow (deprecated for PSGetV3) - /// Value: ArrayList of Workflow name strings - /// - /// Hashtable of PSGet includes - public PSGetIncludes(Hashtable includes) - { - if (includes == null) { return; } - - Cmdlet = GetHashTableItem(includes, nameof(Cmdlet)); - Command = GetHashTableItem(includes, nameof(Command)); - DscResource = GetHashTableItem(includes, nameof(DscResource)); - Function = GetHashTableItem(includes, nameof(Function)); - RoleCapability = GetHashTableItem(includes, nameof(RoleCapability)); - Workflow = GetHashTableItem(includes, nameof(Workflow)); - } - - #endregion - - #region Public methods - - public Hashtable ConvertToHashtable() - { - var hashtable = new Hashtable - { - { nameof(Cmdlet), Cmdlet }, - { nameof(Command), Command }, - { nameof(DscResource), DscResource }, - { nameof(Function), Function }, - { nameof(RoleCapability), RoleCapability }, - { nameof(Workflow), Workflow } - }; - - return hashtable; - } - - #endregion - - #region Private methods - - private string[] GetHashTableItem( - Hashtable table, - string name) - { - if (table.ContainsKey(name) && - table[name] is PSObject psObjectItem) - { - return Utils.GetStringArray(psObjectItem.BaseObject as ArrayList); - } - - return null; - } - - #endregion - } - - internal sealed class PSGetResourceInfo - { - #region Properties - - public Dictionary AdditionalMetadata { get; set; } - - public string Author { get; set; } - - public string CompanyName { get; set; } - - public string Copyright { get; set; } - - public string[] Dependencies { get; set; } - - public string Description { get; set; } - - public Uri IconUri { get; set; } - - public PSGetIncludes Includes { get; set; } - - public DateTime InstalledDate { get; set; } - - public string InstalledLocation { get; set; } - - public Uri LicenseUri { get; set; } - - public string Name { get; set; } - - public string PackageManagementProvider { get; set; } - - public string PowerShellGetFormatVersion { get; set; } - - public Uri ProjectUri { get; set; } - - public DateTime PublishedDate { get; set; } - - public string ReleaseNotes { get; set; } - - public string Repository { get; set; } - - public string RepositorySourceLocation { get; set; } - - public string[] Tags { get; set; } - - public string Type { get; set; } - - public DateTime UpdatedDate { get; set; } - - public Version Version { get; set; } - - #endregion - - #region Public static methods - - /// - /// Writes the PSGetResourceInfo properties to the specified file path as a - /// PowerShell serialized xml file, maintaining compatibility with - /// PowerShellGet v2 file format. - /// - public bool TryWrite( - string filePath, - out string errorMsg) - { - errorMsg = string.Empty; - - if (string.IsNullOrWhiteSpace(filePath)) - { - errorMsg = "TryWritePSGetInfo: Invalid file path. Filepath cannot be empty or whitespace."; - return false; - } - - try - { - var infoXml = PSSerializer.Serialize( - source: ConvertToCustomObject(), - depth: 5); - - System.IO.File.WriteAllText( - path: filePath, - contents: infoXml); - - return true; - } - catch(Exception ex) - { - errorMsg = string.Format( - CultureInfo.InvariantCulture, - @"TryWritePSGetInfo: Cannot convert and write the PowerShellGet information to file, with error: {0}", - ex.Message); - - return false; - } - } - - /// - /// Reads a PSGet resource xml (PowerShell serialized) file and returns - /// a PSGetResourceInfo object containing the file contents. - /// - public static bool TryRead( - string filePath, - out PSGetResourceInfo psGetInfo, - out string errorMsg) - { - psGetInfo = null; - errorMsg = string.Empty; - - if (string.IsNullOrWhiteSpace(filePath)) - { - errorMsg = "TryReadPSGetInfo: Invalid file path. Filepath cannot be empty or whitespace."; - return false; - } - - try - { - // Read and deserialize information xml file. - var psObjectInfo = (PSObject) PSSerializer.Deserialize( - System.IO.File.ReadAllText( - filePath)); - - psGetInfo = new PSGetResourceInfo - { - AdditionalMetadata = GetProperty>(nameof(PSGetResourceInfo.AdditionalMetadata), psObjectInfo), - Author = GetProperty(nameof(PSGetResourceInfo.Author), psObjectInfo), - CompanyName = GetProperty(nameof(PSGetResourceInfo.CompanyName), psObjectInfo), - Copyright = GetProperty(nameof(PSGetResourceInfo.Copyright), psObjectInfo), - Dependencies = Utils.GetStringArray(GetProperty(nameof(PSGetResourceInfo.Dependencies), psObjectInfo)), - Description = GetProperty(nameof(PSGetResourceInfo.Description), psObjectInfo), - IconUri = GetProperty(nameof(PSGetResourceInfo.IconUri), psObjectInfo), - Includes = new PSGetIncludes(GetProperty(nameof(PSGetResourceInfo.Includes), psObjectInfo)), - InstalledDate = GetProperty(nameof(PSGetResourceInfo.InstalledDate), psObjectInfo), - InstalledLocation = GetProperty(nameof(PSGetResourceInfo.InstalledLocation), psObjectInfo), - LicenseUri = GetProperty(nameof(PSGetResourceInfo.LicenseUri), psObjectInfo), - Name = GetProperty(nameof(PSGetResourceInfo.Name), psObjectInfo), - PackageManagementProvider = GetProperty(nameof(PSGetResourceInfo.PackageManagementProvider), psObjectInfo), - PowerShellGetFormatVersion = GetProperty(nameof(PSGetResourceInfo.PowerShellGetFormatVersion), psObjectInfo), - ProjectUri = GetProperty(nameof(PSGetResourceInfo.ProjectUri), psObjectInfo), - PublishedDate = GetProperty(nameof(PSGetResourceInfo.PublishedDate), psObjectInfo), - ReleaseNotes = GetProperty(nameof(PSGetResourceInfo.ReleaseNotes), psObjectInfo), - Repository = GetProperty(nameof(PSGetResourceInfo.Repository), psObjectInfo), - RepositorySourceLocation = GetProperty(nameof(PSGetResourceInfo.RepositorySourceLocation), psObjectInfo), - Tags = Utils.GetStringArray(GetProperty(nameof(PSGetResourceInfo.Tags), psObjectInfo)), - Type = GetProperty(nameof(PSGetResourceInfo.Type), psObjectInfo), - UpdatedDate = GetProperty(nameof(PSGetResourceInfo.UpdatedDate), psObjectInfo), - Version = GetProperty(nameof(PSGetResourceInfo.Version), psObjectInfo) - }; - - return true; - } - catch(Exception ex) - { - errorMsg = string.Format( - CultureInfo.InvariantCulture, - @"TryReadPSGetInfo: Cannot read the PowerShellGet information file with error: {0}", - ex.Message); - - return false; - } - } - - #endregion - - #region Private static methods - - private static T ConvertToType(PSObject psObject) - { - // We only convert Dictionary types. - if (typeof(T) != typeof(Dictionary)) - { - return default(T); - } - - var dict = new Dictionary(); - foreach (var prop in psObject.Properties) - { - dict.Add(prop.Name, prop.Value.ToString()); - } - - return (T)Convert.ChangeType(dict, typeof(T)); - } - - private static T GetProperty( - string Name, - PSObject psObjectInfo) - { - var val = psObjectInfo.Properties[Name]?.Value; - if (val == null) - { - return default(T); - } - - switch (val) - { - case T valType: - return valType; - - case PSObject valPSObject: - switch (valPSObject.BaseObject) - { - case T valBase: - return valBase; - - case PSCustomObject _: - // A base object of PSCustomObject means this is additional metadata - // and type T should be Dictionary. - return ConvertToType(valPSObject); - - default: - return default(T); - } - - default: - return default(T); - } - } - - #endregion - - #region Private methods - - private PSObject ConvertToCustomObject() - { - var additionalMetadata = new PSObject(); - foreach (var item in AdditionalMetadata) - { - additionalMetadata.Properties.Add(new PSNoteProperty(item.Key, item.Value)); - } - - var psObject = new PSObject(); - psObject.Properties.Add(new PSNoteProperty(nameof(AdditionalMetadata), additionalMetadata)); - psObject.Properties.Add(new PSNoteProperty(nameof(Author), Author)); - psObject.Properties.Add(new PSNoteProperty(nameof(CompanyName), CompanyName)); - psObject.Properties.Add(new PSNoteProperty(nameof(Copyright), Copyright)); - psObject.Properties.Add(new PSNoteProperty(nameof(Dependencies), Dependencies)); - psObject.Properties.Add(new PSNoteProperty(nameof(Description), Description)); - psObject.Properties.Add(new PSNoteProperty(nameof(IconUri), IconUri)); - psObject.Properties.Add(new PSNoteProperty(nameof(Includes), Includes.ConvertToHashtable())); - psObject.Properties.Add(new PSNoteProperty(nameof(InstalledDate), InstalledDate)); - psObject.Properties.Add(new PSNoteProperty(nameof(InstalledLocation), InstalledLocation)); - psObject.Properties.Add(new PSNoteProperty(nameof(LicenseUri), LicenseUri)); - psObject.Properties.Add(new PSNoteProperty(nameof(Name), Name)); - psObject.Properties.Add(new PSNoteProperty(nameof(PackageManagementProvider), PackageManagementProvider)); - psObject.Properties.Add(new PSNoteProperty(nameof(PowerShellGetFormatVersion), PowerShellGetFormatVersion)); - psObject.Properties.Add(new PSNoteProperty(nameof(ProjectUri), ProjectUri)); - psObject.Properties.Add(new PSNoteProperty(nameof(PublishedDate), PublishedDate)); - psObject.Properties.Add(new PSNoteProperty(nameof(ReleaseNotes), ReleaseNotes)); - psObject.Properties.Add(new PSNoteProperty(nameof(Repository), Repository)); - psObject.Properties.Add(new PSNoteProperty(nameof(RepositorySourceLocation), RepositorySourceLocation)); - psObject.Properties.Add(new PSNoteProperty(nameof(Tags), Tags)); - psObject.Properties.Add(new PSNoteProperty(nameof(Type), Type)); - psObject.Properties.Add(new PSNoteProperty(nameof(UpdatedDate), UpdatedDate)); - psObject.Properties.Add(new PSNoteProperty(nameof(Version), Version)); - - return psObject; - } - - #endregion - } - - #endregion - - #region Test Hooks - - public static class TestHooks - { - public static PSObject ReadPSGetResourceInfo(string filePath) - { - if (PSGetResourceInfo.TryRead(filePath, out PSGetResourceInfo psGetInfo, out string errorMsg)) - { - return PSObject.AsPSObject(psGetInfo); - } - - throw new PSInvalidOperationException(errorMsg); - } - - public static void WritePSGetResourceInfo( - string filePath, - PSObject psObjectGetInfo) - { - if (psObjectGetInfo.BaseObject is PSGetResourceInfo psGetInfo) - { - if (! psGetInfo.TryWrite(filePath, out string errorMsg)) - { - throw new PSInvalidOperationException(errorMsg); - } - - return; - } - - throw new PSArgumentException("psObjectGetInfo argument is not a PSGetResourceInfo type."); - } - } - - #endregion } diff --git a/test/PSGetResourceInfo.Tests.ps1 b/test/PSResourceInfo.Tests.ps1 similarity index 100% rename from test/PSGetResourceInfo.Tests.ps1 rename to test/PSResourceInfo.Tests.ps1 From 2dbb68c0a29e116e88cf85917e1e01663a536888 Mon Sep 17 00:00:00 2001 From: Paul Higinbotham Date: Mon, 3 May 2021 17:03:26 -0700 Subject: [PATCH 2/3] Add missing copyright to new source file --- src/code/PSResourceInfo.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/code/PSResourceInfo.cs b/src/code/PSResourceInfo.cs index 86225a77e..0ba826baa 100644 --- a/src/code/PSResourceInfo.cs +++ b/src/code/PSResourceInfo.cs @@ -1,3 +1,6 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + using System; using System.Collections; using System.Collections.Generic; From 0e8a4c9a80c8003acc230770c9b777ac572e073b Mon Sep 17 00:00:00 2001 From: Paul Higinbotham Date: Mon, 3 May 2021 17:21:52 -0700 Subject: [PATCH 3/3] Fix Codeacy warning --- src/code/PSResourceInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/code/PSResourceInfo.cs b/src/code/PSResourceInfo.cs index 0ba826baa..cdac5664a 100644 --- a/src/code/PSResourceInfo.cs +++ b/src/code/PSResourceInfo.cs @@ -29,7 +29,7 @@ public enum VersionType #region VersionInfo - public struct VersionInfo + public sealed class VersionInfo { public VersionInfo( VersionType versionType,