Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philasmar committed Oct 24, 2024
1 parent fb2bcc9 commit 2ae19d2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/AutoVer/Services/GitHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ public TagCollection GetGitTags(UserConfiguration userConfiguration, string curr

public void StageChanges(UserConfiguration userConfiguration, string currentPath)
{
using var gitRepository = new Repository(userConfiguration.GitRoot);

LibGit2Sharp.Commands.Stage(gitRepository, currentPath);
var relativePath = Path.IsPathFullyQualified(currentPath) ? Path.GetRelativePath(userConfiguration.GitRoot, currentPath) : currentPath;
using (var gitRepository = new Repository(userConfiguration.GitRoot))
{
string fullPath = !currentPath.Equals("*") ? Path.Combine(gitRepository.Info.WorkingDirectory, relativePath) : "*";
LibGit2Sharp.Commands.Stage(gitRepository, fullPath);
}
}

public void CommitChanges(UserConfiguration userConfiguration, string commitMessage)
Expand Down
4 changes: 3 additions & 1 deletion test/AutoVer.UnitTests/Utilities/GitUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ public static string GetLastCommitMessage(string gitRepositoryPath)

public static void StageChanges(string gitRepositoryPath, string currentPath)
{
var relativePath = Path.IsPathFullyQualified(currentPath) ? Path.GetRelativePath(gitRepositoryPath, currentPath) : currentPath;
using (var gitRepository = new Repository(gitRepositoryPath))
{
LibGit2Sharp.Commands.Stage(gitRepository, currentPath);
string fullPath = !currentPath.Equals("*") ? Path.Combine(gitRepository.Info.WorkingDirectory, relativePath) : "*";
LibGit2Sharp.Commands.Stage(gitRepository, fullPath);
}
}

Expand Down
54 changes: 44 additions & 10 deletions test/AutoVer.UnitTests/Utilities/IOUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using AutoVer.Constants;
using AutoVer.Models;
using LibGit2Sharp;
using System.Diagnostics;
using System.IO;
using System.Xml;

namespace AutoVer.UnitTests.Utilities;
Expand Down Expand Up @@ -86,11 +84,6 @@ public static async Task<string> GetProjectVersion(string projectPath)
var xmlProjectFile = new XmlDocument { PreserveWhitespace = true };
xmlProjectFile.LoadXml(await File.ReadAllTextAsync(projectPath));

var projectDefinition = new ProjectDefinition(
xmlProjectFile,
projectPath
);

var versionTag = ProjectConstants.VersionTag;
if (string.Equals(extension, ".nuspec"))
versionTag = ProjectConstants.NuspecVersionTag;
Expand Down Expand Up @@ -191,17 +184,58 @@ public static async Task<bool> CreateProject(params string[] path)
Console.WriteLine(error);
}

if (process.ExitCode == 0)
return process.ExitCode == 0;
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while running dotnet new: {ex.Message}");
return false;
}
}

public static bool AddGitignore(params string[] path)
{
var outputDir = Path.Combine(path);
if (!Directory.Exists(outputDir))
Directory.CreateDirectory(outputDir);
try
{
// Build the dotnet new command
var processStartInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"new gitignore",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = outputDir
};

using (var process = new Process())
{
process.StartInfo = processStartInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();

Console.WriteLine("Output:");
Console.WriteLine(output);

if (!string.IsNullOrEmpty(error))
{
var gitRepositoryPath = Repository.Init(path[0]);
Console.WriteLine("Error:");
Console.WriteLine(error);
}

return process.ExitCode == 0;
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while running dotnet new: {ex.Message}");
Console.WriteLine($"An error occurred while running dotnet new gitignore: {ex.Message}");
return false;
}
}
Expand Down
9 changes: 8 additions & 1 deletion test/AutoVer.UnitTests/VersionTest.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
using AutoVer.Models;
using AutoVer.UnitTests.Utilities;
using LibGit2Sharp;

namespace AutoVer.UnitTests;

[Retry(3)]
public class VersionTest
{
[Before(Test)]
public void Before(TestContext context)
{
var tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDir);
context.ObjectBag["tempDir"] = tempDir;
Repository.Init(tempDir);
using (var repo = new Repository(tempDir))
{
context.ObjectBag["tempDir"] = repo.Info.WorkingDirectory;
IOUtilities.AddGitignore(repo.Info.WorkingDirectory);
}
}

[Test]
Expand Down

0 comments on commit 2ae19d2

Please sign in to comment.