Skip to content

Commit

Permalink
trace2: add process wrapper
Browse files Browse the repository at this point in the history
Wrap the Process class to allow finer-grained control for writing future
process-related TRACE2 events.

Pull process-relevant information out of IEnvironment/Environment and into
its own process-oriented ProcessManager class, interface, and child
classes. This allows us to  maintain a functional, non-circular order
of dependencies with our process wrapper.

Add a ChildProcess class to wrap certain functionality of Process.cs.
This gives us the level of control required to write child process
information to TRACE2 in the final patch in this series. Update all
current invocations of Process to instead use this wrapper.
  • Loading branch information
Lessley Dennington authored and ldennington committed Mar 9, 2023
1 parent 85d1675 commit 975070d
Show file tree
Hide file tree
Showing 40 changed files with 338 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public TesterWindow()
#if DEBUG
this.AttachDevTools();
#endif

if (PlatformUtils.IsWindows())
{
_environment = new WindowsEnvironment(new WindowsFileSystem());
Expand Down
55 changes: 37 additions & 18 deletions src/shared/Core.Tests/GitConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void GitProcess_GetConfiguration_ReturnsConfiguration()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath);
var config = git.GetConfiguration();
Assert.NotNull(config);
}
Expand All @@ -71,7 +72,8 @@ public void GitConfiguration_Enumerate_CallbackReturnsTrue_InvokesCallbackForEac
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

var actualVisitedEntries = new List<(string name, string value)>();
Expand Down Expand Up @@ -109,7 +111,8 @@ public void GitConfiguration_Enumerate_CallbackReturnsFalse_InvokesCallbackForEa
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

var actualVisitedEntries = new List<(string name, string value)>();
Expand Down Expand Up @@ -139,7 +142,9 @@ public void GitConfiguration_TryGet_Name_Exists_ReturnsTrueOutString()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();

var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

bool result = config.TryGet("user.name", false, out string value);
Expand All @@ -156,7 +161,8 @@ public void GitConfiguration_TryGet_Name_DoesNotExists_ReturnsFalse()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

string randomName = $"{Guid.NewGuid():N}.{Guid.NewGuid():N}";
Expand All @@ -175,7 +181,8 @@ public void GitConfiguration_TryGet_IsPath_True_ReturnsCanonicalPath()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

bool result = config.TryGet("example.path", true, out string value);
Expand All @@ -193,7 +200,8 @@ public void GitConfiguration_TryGet_IsPath_False_ReturnsRawConfig()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

bool result = config.TryGet("example.path", false, out string value);
Expand All @@ -211,7 +219,8 @@ public void GitConfiguration_TryGet_BoolType_ReturnsCanonicalBool()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

bool result = config.TryGet(GitConfigurationLevel.Local, GitConfigurationType.Bool,
Expand All @@ -230,7 +239,8 @@ public void GitConfiguration_TryGet_BoolWithoutType_ReturnsRawConfig()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

bool result = config.TryGet(GitConfigurationLevel.Local, GitConfigurationType.Raw,
Expand All @@ -249,7 +259,8 @@ public void GitConfiguration_Get_Name_Exists_ReturnsString()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

string value = config.Get("user.name");
Expand All @@ -265,7 +276,8 @@ public void GitConfiguration_Get_Name_DoesNotExists_ThrowsException()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

string randomName = $"{Guid.NewGuid():N}.{Guid.NewGuid():N}";
Expand All @@ -280,7 +292,8 @@ public void GitConfiguration_Set_Local_SetsLocalConfig()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

config.Set(GitConfigurationLevel.Local, "core.foobar", "foo123");
Expand All @@ -298,7 +311,8 @@ public void GitConfiguration_Set_All_ThrowsException()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

Assert.Throws<InvalidOperationException>(() => config.Set(GitConfigurationLevel.All, "core.foobar", "test123"));
Expand All @@ -317,7 +331,8 @@ public void GitConfiguration_Unset_Global_UnsetsGlobalConfig()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

config.Unset(GitConfigurationLevel.Global, "core.foobar");
Expand Down Expand Up @@ -348,7 +363,8 @@ public void GitConfiguration_Unset_Local_UnsetsLocalConfig()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

config.Unset(GitConfigurationLevel.Local, "core.foobar");
Expand All @@ -374,7 +390,8 @@ public void GitConfiguration_Unset_All_ThrowsException()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

Assert.Throws<InvalidOperationException>(() => config.Unset(GitConfigurationLevel.All, "core.foobar"));
Expand All @@ -391,7 +408,8 @@ public void GitConfiguration_UnsetAll_UnsetsAllConfig()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

config.UnsetAll(GitConfigurationLevel.Local, "core.foobar", "foo*");
Expand All @@ -409,7 +427,8 @@ public void GitConfiguration_UnsetAll_All_ThrowsException()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, repoPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

Assert.Throws<InvalidOperationException>(() => config.UnsetAll(GitConfigurationLevel.All, "core.foobar", Constants.RegexPatterns.Any));
Expand Down
30 changes: 20 additions & 10 deletions src/shared/Core.Tests/GitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public void Git_GetCurrentRepository_NoLocalRepo_ReturnsNull()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, Path.GetTempPath());
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, Path.GetTempPath());

string actual = git.GetCurrentRepository();

Expand All @@ -29,7 +30,8 @@ public void Git_GetCurrentRepository_LocalRepo_ReturnsNotNull()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, workDirPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, workDirPath);

string actual = git.GetCurrentRepository();

Expand All @@ -42,7 +44,8 @@ public void Git_GetRemotes_NoLocalRepo_ReturnsEmpty()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, Path.GetTempPath());
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, Path.GetTempPath());

GitRemote[] remotes = git.GetRemotes().ToArray();

Expand All @@ -57,7 +60,8 @@ public void Git_GetRemotes_NoRemotes_ReturnsEmpty()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var git = new GitProcess(trace, env, gitPath, workDirPath);
var processManager = new TestProcessManager();
var git = new GitProcess(trace, processManager, gitPath, workDirPath);

GitRemote[] remotes = git.GetRemotes().ToArray();

Expand All @@ -75,8 +79,9 @@ public void Git_GetRemotes_OneRemote_ReturnsRemote()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var processManager = new TestProcessManager();

var git = new GitProcess(trace, env, gitPath, workDirPath);
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
GitRemote[] remotes = git.GetRemotes().ToArray();

Assert.Single(remotes);
Expand All @@ -96,8 +101,9 @@ public void Git_GetRemotes_OneRemoteFetchAndPull_ReturnsRemote()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var processManager = new TestProcessManager();

var git = new GitProcess(trace, env, gitPath, workDirPath);
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
GitRemote[] remotes = git.GetRemotes().ToArray();

Assert.Single(remotes);
Expand All @@ -119,8 +125,9 @@ public void Git_GetRemotes_NonHttpRemote_ReturnsRemote(string url)
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var processManager = new TestProcessManager();

var git = new GitProcess(trace, env, gitPath, workDirPath);
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
GitRemote[] remotes = git.GetRemotes().ToArray();

Assert.Single(remotes);
Expand All @@ -144,8 +151,9 @@ public void Git_GetRemotes_MultipleRemotes_ReturnsAllRemotes()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var processManager = new TestProcessManager();

var git = new GitProcess(trace, env, gitPath, workDirPath);
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
GitRemote[] remotes = git.GetRemotes().ToArray();

Assert.Equal(3, remotes.Length);
Expand All @@ -168,8 +176,9 @@ public void Git_GetRemotes_RemoteNoFetchOnlyPull_ReturnsRemote()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var processManager = new TestProcessManager();

var git = new GitProcess(trace, env, gitPath, workDirPath);
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
GitRemote[] remotes = git.GetRemotes().ToArray();

Assert.Single(remotes);
Expand All @@ -183,8 +192,9 @@ public void Git_Version_ReturnsVersion()
string gitPath = GetGitPath();
var trace = new NullTrace();
var env = new TestEnvironment();
var processManager = new TestProcessManager();

var git = new GitProcess(trace, env, gitPath, Path.GetTempPath());
var git = new GitProcess(trace, processManager, gitPath, Path.GetTempPath());
GitVersion version = git.Version;

Assert.NotEqual(new GitVersion(), version);
Expand Down
Loading

0 comments on commit 975070d

Please sign in to comment.