Skip to content

Commit 75fc2d5

Browse files
committed
Additional refactoring to address inconsistencies with command and working directories with .NET Process instances.
1 parent eb2a42d commit 75fc2d5

14 files changed

Lines changed: 321 additions & 137 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.20
1+
2.1.21

src/VirtualClient/VirtualClient.Common.UnitTests/UnixProcessManagerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void UnixProcessManagerCreatesTheExpectedProcess_1()
2424
Assert.IsNotNull(process.StartInfo);
2525
Assert.AreEqual(command, process.StartInfo.FileName);
2626
Assert.IsEmpty(process.StartInfo.Arguments);
27-
Assert.AreEqual("/home", process.StartInfo.WorkingDirectory);
27+
Assert.IsEmpty(process.StartInfo.WorkingDirectory);
2828
Assert.IsTrue(process.StartInfo.RedirectStandardOutput);
2929
Assert.IsTrue(process.StartInfo.RedirectStandardError);
3030
Assert.IsFalse(process.StartInfo.RedirectStandardInput);
@@ -43,7 +43,7 @@ public void UnixProcessManagerCreatesTheExpectedProcess_2()
4343
Assert.IsNotNull(process.StartInfo);
4444
Assert.AreEqual(command, process.StartInfo.FileName);
4545
Assert.AreEqual(commandArguments, process.StartInfo.Arguments);
46-
Assert.AreEqual("/home", process.StartInfo.WorkingDirectory);
46+
Assert.IsEmpty(process.StartInfo.WorkingDirectory);
4747
Assert.IsTrue(process.StartInfo.RedirectStandardOutput);
4848
Assert.IsTrue(process.StartInfo.RedirectStandardError);
4949
Assert.IsFalse(process.StartInfo.RedirectStandardInput);

src/VirtualClient/VirtualClient.Common.UnitTests/WindowsProcessManagerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void WindowsProcessManagerCreatesTheExpectedProcess_1()
2525
Assert.IsNotNull(process.StartInfo);
2626
Assert.AreEqual(command, process.StartInfo.FileName);
2727
Assert.IsEmpty(process.StartInfo.Arguments);
28-
Assert.AreEqual(@"C:\users\any\temp", process.StartInfo.WorkingDirectory);
28+
Assert.IsEmpty(process.StartInfo.WorkingDirectory);
2929
Assert.IsTrue(process.StartInfo.RedirectStandardOutput);
3030
Assert.IsTrue(process.StartInfo.RedirectStandardError);
3131
Assert.IsFalse(process.StartInfo.UseShellExecute);
@@ -44,7 +44,7 @@ public void WindowsProcessManagerCreatesTheExpectedProcess_2()
4444
Assert.IsNotNull(process.StartInfo);
4545
Assert.AreEqual(command, process.StartInfo.FileName);
4646
Assert.AreEqual(commandArguments, process.StartInfo.Arguments);
47-
Assert.AreEqual(@"C:\users\any\temp", process.StartInfo.WorkingDirectory);
47+
Assert.IsEmpty(process.StartInfo.WorkingDirectory);
4848
Assert.IsTrue(process.StartInfo.RedirectStandardOutput);
4949
Assert.IsTrue(process.StartInfo.RedirectStandardError);
5050
Assert.IsFalse(process.StartInfo.UseShellExecute);

src/VirtualClient/VirtualClient.Common/ProcessExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace VirtualClient.Common
55
{
66
using System;
77
using System.Collections.Generic;
8-
using System.Linq;
98
using System.Text.RegularExpressions;
109
using System.Threading;
1110
using System.Threading.Tasks;
@@ -16,7 +15,7 @@ namespace VirtualClient.Common
1615
/// </summary>
1716
public static class ProcessExtensions
1817
{
19-
/// <summary>
18+
/// <summary>
2019
/// Sets the process for interactive mode (e.g. standard output and input redirected).
2120
/// </summary>
2221
/// <param name="process">Represents a process on the system.</param>

src/VirtualClient/VirtualClient.Common/UnixProcessManager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace VirtualClient.Common
55
{
66
using System.Diagnostics;
7-
using System.IO;
87
using VirtualClient.Common.Extensions;
98

109
/// <summary>
@@ -23,7 +22,7 @@ public override IProcessProxy CreateProcess(string command, string arguments = n
2322
{
2423
FileName = command,
2524
Arguments = arguments,
26-
WorkingDirectory = workingDir ?? Path.GetDirectoryName(command).Replace('\\', '/'),
25+
WorkingDirectory = workingDir,
2726
CreateNoWindow = true,
2827
UseShellExecute = false,
2928
RedirectStandardError = true,

src/VirtualClient/VirtualClient.Common/WindowsProcessManager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace VirtualClient.Common
55
{
66
using System.Diagnostics;
7-
using System.IO;
87
using VirtualClient.Common.Extensions;
98

109
/// <summary>
@@ -23,7 +22,7 @@ public override IProcessProxy CreateProcess(string command, string arguments = n
2322
{
2423
FileName = command,
2524
Arguments = arguments,
26-
WorkingDirectory = workingDir ?? Path.GetDirectoryName(command),
25+
WorkingDirectory = workingDir,
2726
CreateNoWindow = true,
2827
UseShellExecute = false,
2928
RedirectStandardError = true,

src/VirtualClient/VirtualClient.Contracts/ISystemInfo.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ public interface ISystemInfo
5757
/// <returns>True/False is an IP is defined on current system.</returns>
5858
bool IsLocalIPAddress(string ipAddress);
5959

60-
/// <summary>
61-
/// Get the logged In Username i.e, username of the user who invoked a command with elevated privileges using the "sudo" command in Unix operating system.
62-
/// </summary>
63-
/// <returns>Username of the logged In user</returns>
64-
string GetLoggedInUserName();
65-
6660
/// <summary>
6761
/// Returns information about the CPU on the system.
6862
/// </summary>

src/VirtualClient/VirtualClient.Contracts/Parser/TextParsingExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ public static IDictionary<string, string> Sectionize(string text, Regex delimite
109109
}
110110

111111
/// <summary>
112-
/// Sectionize raw text into sections based on regex. First line of each section will become section key!
112+
/// Parses a set of delimited key/value pairs (e.g. key1=value1;key2=value2). Supported delimiters are
113+
/// a comma ',', a semi-colon ';' or a triple-comma ',,,'. Delimiters cannot be mixed (i.e. the same delimiter
114+
/// must be used across the board).
113115
/// </summary>
114116
/// <param name="text">Raw text.</param>
115117
public static IDictionary<string, IConvertible> ParseDelimitedValues(string text)

src/VirtualClient/VirtualClient.Contracts/PlatformSpecifics.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ namespace VirtualClient.Contracts
99
using System.Linq;
1010
using System.Runtime.InteropServices;
1111
using System.Text.RegularExpressions;
12+
using Newtonsoft.Json.Linq;
13+
using VirtualClient.Common;
1214
using VirtualClient.Common.Extensions;
15+
using YamlDotNet.Core.Tokens;
1316

1417
/// <summary>
1518
/// Defines platform-specific information and properties for dependencies and
@@ -605,7 +608,7 @@ public virtual void SetEnvironmentVariable(string name, string value, Environmen
605608
{
606609
commitChange = true;
607610
}
608-
else if (!originalValue.EndsWith(value))
611+
else if (!originalValue.Contains(value, StringComparison.Ordinal))
609612
{
610613
commitChange = true;
611614
newValue = $"{originalValue}{delimiter}{newValue}";
@@ -618,6 +621,51 @@ public virtual void SetEnvironmentVariable(string name, string value, Environmen
618621
}
619622
}
620623

624+
/// <summary>
625+
/// Adds the environment variable to the process variables.
626+
/// </summary>
627+
/// <param name="process">The process to which to add the environment variables.</param>
628+
/// <param name="name">The name of the environment variable to set.</param>
629+
/// <param name="value">The value to which to set the environment variable or append to the end of the existing value.</param>
630+
/// <param name="append">True to append the value to the end of the existing environment variable value. False to replace the existing value.</param>
631+
public void SetEnvironmentVariable(IProcessProxy process, string name, string value, bool append = false)
632+
{
633+
process.ThrowIfNull(nameof(process));
634+
name.ThrowIfNullOrWhiteSpace(nameof(name));
635+
value.ThrowIfNullOrWhiteSpace(nameof(value));
636+
637+
string originalValue = null;
638+
if (process.EnvironmentVariables.ContainsKey(name))
639+
{
640+
originalValue = process.EnvironmentVariables[name];
641+
}
642+
643+
string newValue = value ?? string.Empty;
644+
bool commitChange = true;
645+
646+
if (!string.IsNullOrWhiteSpace(originalValue) && append)
647+
{
648+
commitChange = false;
649+
char delimiter = this.Platform == PlatformID.Unix ? ':' : ';';
650+
651+
originalValue = originalValue?.TrimEnd(delimiter);
652+
if (string.IsNullOrWhiteSpace(originalValue))
653+
{
654+
commitChange = true;
655+
}
656+
else if (!originalValue.Contains(value, StringComparison.Ordinal))
657+
{
658+
commitChange = true;
659+
newValue = $"{originalValue}{delimiter}{newValue}";
660+
}
661+
}
662+
663+
if (commitChange)
664+
{
665+
process.EnvironmentVariables[name] = newValue;
666+
}
667+
}
668+
621669
/// <summary>
622670
/// Standardizes/normalizes the path based upon the platform/OS ensuring
623671
/// a valid path is

src/VirtualClient/VirtualClient.Core/SystemManagement.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public string PlatformArchitectureName
8181
{
8282
get
8383
{
84-
return VirtualClient.Contracts.PlatformSpecifics.GetPlatformArchitectureName(this.Platform, this.CpuArchitecture);
84+
return PlatformSpecifics.GetPlatformArchitectureName(this.Platform, this.CpuArchitecture);
8585
}
8686
}
8787

@@ -125,30 +125,6 @@ public async Task<CpuInfo> GetCpuInfoAsync(CancellationToken cancellationToken)
125125
return info;
126126
}
127127

128-
/// <summary>
129-
/// Get the logged In Username i.e, username of the user who invoked a command with elevated privileges using the "sudo" command in Unix operating system.
130-
/// </summary>
131-
public string GetLoggedInUserName()
132-
{
133-
string loggedInUserName = Environment.UserName;
134-
if (string.Equals(loggedInUserName, "root"))
135-
{
136-
loggedInUserName = Environment.GetEnvironmentVariable("SUDO_USER");
137-
if (string.Equals(loggedInUserName, "root") || string.IsNullOrEmpty(loggedInUserName))
138-
{
139-
loggedInUserName = Environment.GetEnvironmentVariable("VC_SUDO_USER");
140-
if (string.IsNullOrEmpty(loggedInUserName))
141-
{
142-
throw new EnvironmentSetupException($"'USER' Environment variable is set to root and 'SUDO_USER' Environment variable is either root or null." +
143-
"The required environment variable 'VC_SUDO_USER' is expected to be set to a valid non-empty value." +
144-
"Please ensure that the necessary environment variables are configured properly for the execution environment.", ErrorReason.EnvironmentIsInsufficent);
145-
}
146-
}
147-
}
148-
149-
return loggedInUserName;
150-
}
151-
152128
/// <summary>
153129
/// Returns information about the specific Linux distribution (e.g. Ubuntu, CentOS).
154130
/// </summary>

0 commit comments

Comments
 (0)