Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Git checkout option for GitRepoClone #414

Merged
merged 22 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0f3d501
Updated Functional Tests
RakeshwarK May 13, 2024
b984341
Merge branch 'microsoft:main' into main
RakeshwarK May 20, 2024
8d36530
Merge branch 'main' of https://github.com/RakeshwarK/VirtualClient
RakeshwarK Jun 14, 2024
6629bc9
Merge branch 'main' of https://github.com/RakeshwarK/VirtualClient
RakeshwarK Jun 18, 2024
56fe32c
Merge branch 'main' of https://github.com/RakeshwarK/VirtualClient
RakeshwarK Jun 26, 2024
e97f648
Merge branch 'main' of https://github.com/RakeshwarK/VirtualClient
RakeshwarK Jul 11, 2024
869694b
Merge branch 'main' of https://github.com/RakeshwarK/VirtualClient
RakeshwarK Jul 25, 2024
75b1e5c
Merge branch 'main' of https://github.com/RakeshwarK/VirtualClient
RakeshwarK Jul 30, 2024
c38abb0
Merge branch 'main' of https://github.com/RakeshwarK/VirtualClient
RakeshwarK Aug 2, 2024
8d8831c
Merge branch 'main' of https://github.com/RakeshwarK/VirtualClient
RakeshwarK Sep 3, 2024
47ee79d
Merge branch 'main' of https://github.com/RakeshwarK/VirtualClient
RakeshwarK Nov 12, 2024
9d6b169
Added Git Checkout
RakeshwarK Dec 16, 2024
e50a400
Merge branch 'main' into v-rkambaiahg/GitCheckout
RakeshwarK Dec 16, 2024
d6f0a46
Update Docs
RakeshwarK Dec 16, 2024
1193747
Merge branch 'v-rkambaiahg/GitCheckout' of https://github.com/Rakeshw…
RakeshwarK Dec 16, 2024
df28835
Merge branch 'main' into v-rkambaiahg/GitCheckout
RakeshwarK Dec 16, 2024
49c5b73
commit changes
RakeshwarK Dec 17, 2024
a320313
commit changes
RakeshwarK Dec 17, 2024
2e324b4
Merge branch 'v-rkambaiahg/GitCheckout' of https://github.com/Rakeshw…
RakeshwarK Dec 17, 2024
75b6b8e
commit changes
RakeshwarK Dec 17, 2024
f241ab0
Rename Commit
RakeshwarK Dec 18, 2024
a311eb9
Commit changes
RakeshwarK Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace VirtualClient.Dependencies
{
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -47,13 +48,30 @@ public Uri RepoUri
}
}

/// <summary>
/// Parameter to checkout to a specific branch, commit or tag in the repository
/// </summary>
public string Commit
{
get
{
return this.Parameters.GetValue<string>(nameof(GitRepoClone.Commit), string.Empty);
}

set
{
this.Parameters[nameof(GitRepoClone.Commit)] = value;
}
}

/// <summary>
/// Executes the git clone operation.
/// </summary>
protected override async Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
{
telemetryContext.AddContext("repoUri", this.RepoUri);
telemetryContext.AddContext("packagesDirectory", this.PlatformSpecifics.PackagesDirectory);
telemetryContext.AddContext("Commit", this.Commit);

ISystemManagement systemManagement = this.Dependencies.GetService<ISystemManagement>();
ProcessManager processManager = systemManagement.ProcessManager;
Expand Down Expand Up @@ -100,6 +118,11 @@ await systemManagement.PackageManager.RegisterPackageAsync(
await systemManagement.PackageManager.RegisterPackageAsync(package, cancellationToken)
.ConfigureAwait(false);
}

if (!string.IsNullOrEmpty(this.Commit))
{
await this.ExecuteCommandAsync("git", $"-C {cloneDirectory} checkout {this.Commit}", this.PlatformSpecifics.PackagesDirectory, telemetryContext, cancellationToken);
}
}
}
}
2 changes: 2 additions & 0 deletions website/docs/dependencies/0040-install-git-repo.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The following section describes the parameters used by the individual component
|---------------|--------------|-----------------------------------------------------------------------------------------------------------------|
| PackageName | Yes | The logical name of the that will be registered with the Virtual Client runtime to represent the packages directory into which the repo was cloned. Other profile components can use this name to reference/discover the repo and its location. |
| RepoUri | Yes | The full URI to the Git repository to download/clone into the packages directory. |
| Commit | No | The branch-name or commit-hash or tag to checkout to a specific branch, commit, tag in the repository. |
| Scenario | No | A name/identifier for the specific component in the profile. This is used for telemetry purposes only with components in dependency sections of the profile (i.e. cannot be used with --scenarios option on the command line). |


Expand All @@ -36,6 +37,7 @@ In this example, VC clones https://github.com/eembc/coremark.git into the runtim
"Type": "GitRepoClone",
"Parameters": {
"RepoUri": "https://github.com/eembc/coremark.git",
"Commit": "v1.01"
"PackageName": "coremark"
}
}
Expand Down
Loading