-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Git checkout option for GitRepoClone (#414)
* Updated Functional Tests Signed-off-by: Rakesh <[email protected]> * Added Git Checkout * Update Docs * commit changes * commit changes * commit changes * Rename Commit * Commit changes --------- Signed-off-by: Rakesh <[email protected]>
- Loading branch information
1 parent
8fc3478
commit 81f7761
Showing
3 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
src/VirtualClient/VirtualClient.Dependencies.UnitTests/Packaging/GitRepoCloneTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace VirtualClient.Dependencies | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using NUnit.Framework; | ||
using VirtualClient.Common; | ||
using VirtualClient.Common.Telemetry; | ||
|
||
[TestFixture] | ||
[Category("Unit")] | ||
public class GitRepoCloneTests | ||
{ | ||
private MockFixture mockFixture; | ||
|
||
[Test] | ||
public async Task GitRepoCloneRunsTheExpectedCommand() | ||
{ | ||
this.mockFixture = new MockFixture(); | ||
this.mockFixture.File.Reset(); | ||
this.mockFixture.Setup(PlatformID.Unix); | ||
this.mockFixture.File.Setup(f => f.Exists(It.IsAny<string>())) | ||
.Returns(true); | ||
this.mockFixture.Parameters = new Dictionary<string, IConvertible>() | ||
{ | ||
{ nameof(GitRepoClone.PackageName), "coremark" }, | ||
{ nameof(GitRepoClone.RepoUri), "https://github.com/eembc/coremark.git" } | ||
}; | ||
|
||
ProcessStartInfo expectedInfo = new ProcessStartInfo(); | ||
List<string> expectedCommands = new List<string>() | ||
{ | ||
$@"git clone {this.mockFixture.Parameters[$"{nameof(GitRepoClone.RepoUri)}"]} {this.mockFixture.GetPackagePath()}/{this.mockFixture.Parameters[$"{nameof(GitRepoClone.PackageName)}"]}" | ||
}; | ||
|
||
int commandExecuted = 0; | ||
this.mockFixture.ProcessManager.OnCreateProcess = (exe, arguments, workingDir) => | ||
{ | ||
if (expectedCommands.Any(c => c == $"{exe} {arguments}")) | ||
{ | ||
commandExecuted++; | ||
} | ||
|
||
IProcessProxy process = new InMemoryProcess() | ||
{ | ||
ExitCode = 0, | ||
OnStart = () => true, | ||
OnHasExited = () => true | ||
}; | ||
return process; | ||
}; | ||
|
||
using (TestGitRepoClone installation = new TestGitRepoClone(this.mockFixture.Dependencies, this.mockFixture.Parameters)) | ||
{ | ||
await installation.ExecuteAsync(CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
Assert.AreEqual(1, commandExecuted); | ||
} | ||
|
||
[Test] | ||
public async Task GitRepoCloneRunsTheExpectedCommandWithCheckout() | ||
{ | ||
this.mockFixture = new MockFixture(); | ||
this.mockFixture.Setup(PlatformID.Unix); | ||
this.mockFixture.File.Reset(); | ||
this.mockFixture.File.Setup(f => f.Exists(It.IsAny<string>())) | ||
.Returns(true); | ||
|
||
// The parameter Commit can be a branch-name, tag or a commit id. | ||
this.mockFixture.Parameters = new Dictionary<string, IConvertible>() | ||
{ | ||
{ nameof(GitRepoClone.PackageName), "coremark" }, | ||
{ nameof(GitRepoClone.RepoUri), "https://github.com/eembc/coremark.git" }, | ||
{ nameof(GitRepoClone.Commit), "Checkout-string" } | ||
}; | ||
|
||
ProcessStartInfo expectedInfo = new ProcessStartInfo(); | ||
List<string> expectedCommands = new List<string>() | ||
{ | ||
$@"git clone {this.mockFixture.Parameters[$"{nameof(GitRepoClone.RepoUri)}"]} {this.mockFixture.GetPackagePath()}/{this.mockFixture.Parameters[$"{nameof(GitRepoClone.PackageName)}"]}", | ||
$@"git -C {this.mockFixture.GetPackagePath()}/{this.mockFixture.Parameters[$"{nameof(GitRepoClone.PackageName)}"]} checkout {this.mockFixture.Parameters[$"{nameof(GitRepoClone.Commit)}"]}", | ||
}; | ||
|
||
int commandExecuted = 0; | ||
this.mockFixture.ProcessManager.OnCreateProcess = (exe, arguments, workingDir) => | ||
{ | ||
if (expectedCommands.Any(c => c == $"{exe} {arguments}")) | ||
{ | ||
commandExecuted++; | ||
} | ||
|
||
IProcessProxy process = new InMemoryProcess() | ||
{ | ||
ExitCode = 0, | ||
OnStart = () => true, | ||
OnHasExited = () => true | ||
}; | ||
return process; | ||
}; | ||
|
||
using (TestGitRepoClone installation = new TestGitRepoClone(this.mockFixture.Dependencies, this.mockFixture.Parameters)) | ||
{ | ||
await installation.ExecuteAsync(CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
Assert.AreEqual(2, commandExecuted); | ||
} | ||
|
||
private class TestGitRepoClone : GitRepoClone | ||
{ | ||
public TestGitRepoClone(IServiceCollection dependencies, IDictionary<string, IConvertible> parameters) | ||
: base(dependencies, parameters) | ||
{ | ||
} | ||
|
||
public new Task ExecuteAsync(EventContext context, CancellationToken cancellationToken) | ||
{ | ||
return base.ExecuteAsync(context, cancellationToken); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters