< Summary - SonghayCore

Information
Class: Songhay.Extensions.EnvironmentExtensions
Assembly: SonghayCore
File(s): /home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/EnvironmentExtensions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 145
Coverable lines: 145
Total lines: 183
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.cctor()100%10%
GetConventionalValue(...)100%10%
ListEnvironmentVariables()0%240%

File(s)

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/EnvironmentExtensions.cs

#LineLine coverage
 1using System.Security;
 2
 3namespace Songhay.Extensions;
 4
 5/// <summary>
 6/// Extensions of <see cref="Environment"/>.
 7/// </summary>
 8public static class EnvironmentExtensions
 9{
 10    /// <summary>
 11    /// The conventional <see cref="ICollection{T}"/>
 12    /// of <see cref="SystemVariable"/>.
 13    /// </summary>
 014    public static readonly Lazy<ICollection<SystemVariable>> EnvironmentVariables =
 015        new(ListEnvironmentVariables, LazyThreadSafetyMode.PublicationOnly);
 16
 17    /// <summary>
 18    /// Gets the value from the conventional <see cref="ICollection{T}"/>
 19    /// of <see cref="SystemVariable"/> based on the specified variable name.
 20    /// </summary>
 21    /// <param name="conventionalName">The <see cref="SystemVariable.VariableName"/>.</param>
 22    /// <returns>Returns <see cref="SystemVariable.VariableValue"/>.</returns>
 23    public static string? GetConventionalValue(string? conventionalName)
 024    {
 025        return EnvironmentVariables.Value
 026            .Where(e => e.VariableName.EqualsInvariant(conventionalName))
 027            .Select(e => e.VariableValue).First();
 028    }
 29
 30    static ICollection<SystemVariable> ListEnvironmentVariables()
 031    {
 032        var list = new List<SystemVariable>
 033        {
 034            new()
 035            {
 036                VariableName = nameof(Environment.MachineName),
 037                VariableDescription = "Network Identification",
 038                VariableValue = Environment.MachineName
 039            },
 040            new()
 041            {
 042                VariableName = $"{nameof(Environment.OSVersion)}.{nameof(Environment.OSVersion.Platform)}",
 043                VariableDescription = "Operating System Platform",
 044                VariableValue = Environment.OSVersion.Platform.ToString()
 045            },
 046            new()
 047            {
 048                VariableName = $"{nameof(Environment.OSVersion)}.{nameof(Environment.OSVersion.ServicePack)}",
 049                VariableDescription = "Operating System Service Pack",
 050                VariableValue = Environment.OSVersion.ServicePack
 051            },
 052            new()
 053            {
 054                VariableName = $"{nameof(Environment.OSVersion)}.{nameof(Environment.OSVersion.VersionString)}",
 055                VariableDescription = "Operating System Version Summary",
 056                VariableValue = Environment.OSVersion.VersionString
 057            },
 058            new()
 059            {
 060                VariableName = nameof(Environment.UserDomainName),
 061                VariableDescription = "User Domain Name",
 062                VariableValue = Environment.UserDomainName
 063            },
 064            new()
 065            {
 066                VariableName = nameof(Environment.UserName),
 067                VariableDescription = "User Name",
 068                VariableValue = Environment.UserName
 069            },
 070            new()
 071            {
 072                VariableName = nameof(Environment.Version.Major),
 073                VariableDescription = "CLR Major Version",
 074                VariableValue = Environment.Version.Major.ToString(CultureInfo.InvariantCulture)
 075            },
 076            new()
 077            {
 078                VariableName = $"{nameof(Environment.Version)}.{nameof(Environment.Version.MajorRevision)}",
 079                VariableDescription = "CLR Major Revision",
 080                VariableValue = Environment.Version.MajorRevision.ToString(CultureInfo.InvariantCulture)
 081            },
 082            new()
 083            {
 084                VariableName = $"{nameof(Environment.Version)}.{nameof(Environment.Version.Minor)}",
 085                VariableDescription = "CLR Minor Version",
 086                VariableValue = Environment.Version.Minor.ToString(CultureInfo.InvariantCulture)
 087            },
 088            new()
 089            {
 090                VariableName = $"{nameof(Environment.Version)}.{nameof(Environment.Version.MinorRevision)}",
 091                VariableDescription = "CLR Minor Revision",
 092                VariableValue = Environment.Version.MinorRevision.ToString(CultureInfo.InvariantCulture)
 093            },
 094            new()
 095            {
 096                VariableName = $"{nameof(Environment.Version)}.{nameof(Environment.Version.Revision)}",
 097                VariableDescription = "CLR Revision",
 098                VariableValue = Environment.Version.Revision.ToString(CultureInfo.InvariantCulture)
 099            }
 0100        };
 101
 102        #region Insert data into list:
 103
 104        try
 0105        {
 0106            foreach (DictionaryEntry environment in Environment.GetEnvironmentVariables(
 0107                         EnvironmentVariableTarget.Machine))
 0108            {
 0109                if (string.IsNullOrWhiteSpace(environment.Value?.ToString())) continue;
 110
 0111                list.Add(new SystemVariable
 0112                {
 0113                    VariableName = $"{nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.Machine)} [ke
 0114                    VariableDescription = "Machine Environment Variables",
 0115                    VariableValue = environment.Value?.ToString()
 0116                });
 0117            }
 0118        }
 0119        catch (SecurityException ex)
 0120        {
 0121            list.Add(new SystemVariable
 0122            {
 0123                VariableName = $"EXCEPTION! {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.Machin
 0124                VariableDescription = $"EXCEPTION for {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTar
 0125                VariableValue = $"Message: {ex.Message}\nGranted Set: {ex.GrantedSet}\nPermission State: {ex.PermissionS
 0126            });
 0127        }
 128
 129        try
 0130        {
 0131            foreach (DictionaryEntry environment in Environment.GetEnvironmentVariables(
 0132                         EnvironmentVariableTarget.Process))
 0133            {
 0134                if (string.IsNullOrWhiteSpace(environment.Value?.ToString())) continue;
 135
 0136                list.Add(new SystemVariable
 0137                {
 0138                    VariableName = $"{nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.Process)} [ke
 0139                    VariableDescription = "Process Environment Variables",
 0140                    VariableValue = environment.Value?.ToString()
 0141                });
 0142            }
 0143        }
 0144        catch (SecurityException ex)
 0145        {
 0146            list.Add(new SystemVariable
 0147            {
 0148                VariableName = $"EXCEPTION! {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.Proces
 0149                VariableDescription = $"EXCEPTION for {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTar
 0150                VariableValue = $"Message: {ex.Message}\nGranted Set: {ex.GrantedSet}\nPermission State: {ex.PermissionS
 0151            });
 0152        }
 153
 154        try
 0155        {
 0156            foreach (DictionaryEntry environment in Environment.GetEnvironmentVariables(
 0157                         EnvironmentVariableTarget.User))
 0158            {
 0159                if (string.IsNullOrWhiteSpace(environment.Value?.ToString())) continue;
 160
 0161                list.Add(new SystemVariable
 0162                {
 0163                    VariableName = $"{nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.User)} [key: 
 0164                    VariableDescription = "User Environment Variables",
 0165                    VariableValue = environment.Value?.ToString()
 0166                });
 0167            }
 0168        }
 0169        catch (SecurityException ex)
 0170        {
 0171            list.Add(new SystemVariable
 0172            {
 0173                VariableName = $"EXCEPTION! {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTarget.User)}
 0174                VariableDescription = $"EXCEPTION for {nameof(EnvironmentVariableTarget)}.{nameof(EnvironmentVariableTar
 0175                VariableValue = $"Message: {ex.Message}\nGranted Set: {ex.GrantedSet}\nPermission State: {ex.PermissionS
 0176            });
 0177        }
 178
 179        #endregion
 180
 0181        return list;
 0182    }
 183}