Skip to content

Commit

Permalink
improved coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
hennadiilu committed Jan 28, 2024
1 parent b94d451 commit fcecf16
Show file tree
Hide file tree
Showing 17 changed files with 729 additions and 30 deletions.
7 changes: 7 additions & 0 deletions Heleonix.Build.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
.github\workflows\hx-net-nuget.yml = .github\workflows\hx-net-nuget.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Heleonix.Build.Tests.ExeMock", "test\Heleonix.Build.Tests.ExeMock\Heleonix.Build.Tests.ExeMock.csproj", "{3D5694BD-41EC-4E7B-8775-2757EA4059CA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -41,6 +43,10 @@ Global
{F4CE21CC-39D4-43C0-81F2-BFED6436DC5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F4CE21CC-39D4-43C0-81F2-BFED6436DC5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F4CE21CC-39D4-43C0-81F2-BFED6436DC5C}.Release|Any CPU.Build.0 = Release|Any CPU
{3D5694BD-41EC-4E7B-8775-2757EA4059CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3D5694BD-41EC-4E7B-8775-2757EA4059CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D5694BD-41EC-4E7B-8775-2757EA4059CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D5694BD-41EC-4E7B-8775-2757EA4059CA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -49,6 +55,7 @@ Global
{44202B1B-5C68-40CE-B2C2-1FEB9AEC48E3} = {86666126-26CC-48EE-A647-28E94D1AE95A}
{F4CE21CC-39D4-43C0-81F2-BFED6436DC5C} = {A3674ABC-7370-429D-8AE6-9AA33F4DE8F9}
{34F2A7E3-FDE8-4425-BD9C-323F304F8EEE} = {3499A7EA-88BC-48DF-8E9B-1EB80C052AD0}
{3D5694BD-41EC-4E7B-8775-2757EA4059CA} = {A3674ABC-7370-429D-8AE6-9AA33F4DE8F9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DADF4FE8-717D-467C-948E-0F170CDA33E6}
Expand Down
7 changes: 4 additions & 3 deletions src/Heleonix.Build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public static int Main(string[] args)
|| "-h".Equals(args[0], StringComparison.OrdinalIgnoreCase)))
{
Console.WriteLine("Description:");
Console.WriteLine(" The MSBuild-based build framework to use in CI/CD systems" +
" Version: " + Assembly.GetExecutingAssembly().GetName().Version);
Console.WriteLine(" The MSBuild-based build framework to use in CI/CD systems");
Console.WriteLine(" Version: {0}", Assembly.GetExecutingAssembly().GetName().Version);
Console.WriteLine(" More: https://heleonix.github.io/docs/api/Heleonix.Build/intro.html");
Console.WriteLine();
Console.WriteLine("Options:");
Expand All @@ -54,7 +54,8 @@ public static int Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Red;

Console.Error.WriteLine("The dotnet executable path is not defined for the specified argument '-exe'.");
Console.Error.WriteLine(
"The dotnet executable path is not defined for the specified argument '--exe'.");

return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Heleonix.Build/Tasks/Hx_MetadataToCmdArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected override void ExecuteInternal()

foreach (DictionaryEntry pair in metadata)
{
var key = Regex.Replace(Regex.Replace(pair.Key.ToString(), "^_", "-"), "^_", "-");
var key = Regex.Replace(Regex.Replace(pair.Key.ToString(), "^__", "--"), "^_", "-");

key = this.DottedKeys ? key.Replace("_", ".") : key;

Expand Down
4 changes: 2 additions & 2 deletions src/Heleonix.Build/Tasks/Hx_NetFindProjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Hx_NetFindProjects : BaseTask
/// Projects files found in the specified solution file [Output].
/// </summary>
[Output]
public ITaskItem[] ProjectFiles { get; set; }
public string[] ProjectFiles { get; set; }

/// <summary>
/// Executes the implementation of the task.
Expand All @@ -33,6 +33,6 @@ protected override void ExecuteInternal()
slnContent,
"(?<=Project.+=\\s*\".+\"\\s*,\\s*\")(.+proj)(?=\"\\s*,\\s*\".+\"\\s*EndProject)");

this.ProjectFiles = matches.Select(m => new TaskItem(Path.Combine(slnDir, m.Value))).ToArray();
this.ProjectFiles = matches.Select(m => Path.Combine(slnDir, m.Value)).ToArray();
}
}
2 changes: 1 addition & 1 deletion src/Heleonix.Build/Tasks/Hx_NetFindSln.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public class Hx_NetFindSln : BaseTask
/// </summary>
protected override void ExecuteInternal()
{
this.SlnFile = Directory.GetFiles(this.StartDir, "*.sln").Single();
this.SlnFile = Directory.GetFiles(this.StartDir, "*.sln").SingleOrDefault();
}
}
32 changes: 10 additions & 22 deletions src/Heleonix.Build/Tasks/Hx_NetSetupTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

namespace Heleonix.Build.Tasks;

using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections;
using System.IO;
using System.Reflection;

Expand Down Expand Up @@ -78,16 +76,16 @@ protected override void ExecuteInternal()
this.Version = "2.74.1";
}

if ("NunitXml.TestLogger".Equals(this.Name, StringComparison.OrdinalIgnoreCase))
if ("extent".Equals(this.Name, StringComparison.OrdinalIgnoreCase))
{
this.IsPackage = true;
this.Version = "3.1.15";
this.Version = "0.0.3";
}

if ("extent".Equals(this.Name, StringComparison.OrdinalIgnoreCase))
if ("NunitXml.TestLogger".Equals(this.Name, StringComparison.OrdinalIgnoreCase))
{
this.IsPackage = true;
this.Version = "0.0.3";
this.Version = "3.1.15";
}

if (this.IsPackage)
Expand Down Expand Up @@ -154,8 +152,6 @@ protected override void ExecuteInternal()
this.Log.LogMessage(MessageImportance.High, args);

this.Log.LogError(result.Error);

this.Log.LogMessage(MessageImportance.High, result.Output);
}

if (!this.IsPackage)
Expand All @@ -165,17 +161,14 @@ protected override void ExecuteInternal()
return;
}

if (this.IsPackage && !string.IsNullOrEmpty(this.Version))
if (string.IsNullOrEmpty(this.Version))
{
this.ToolPath = Path.Combine(toolsDir, this.PackageName, this.Version);
this.ToolPath = Hx_NetSetupTool.GetLatestVersionPath(Path.Combine(toolsDir, this.PackageName));

return;
}

if (this.IsPackage && string.IsNullOrEmpty(this.Version))
{
this.ToolPath = Hx_NetSetupTool.GetLatestVersionPath(Path.Combine(toolsDir, this.PackageName));
}
this.ToolPath = Path.Combine(toolsDir, this.PackageName, this.Version);
}

/// <summary>
Expand All @@ -185,15 +178,10 @@ protected override void ExecuteInternal()
/// <returns>The path to the latest version folder of a given package path.</returns>
private static string GetLatestVersionPath(string packagePath)
{
var dirs = Directory.GetDirectories(packagePath);

if (dirs.Length > 0)
{
Array.Sort(dirs, StringComparer.OrdinalIgnoreCase);
var dirs = Directory.Exists(packagePath) ? Directory.GetDirectories(packagePath) : Array.Empty<string>();

return dirs.Last();
}
Array.Sort(dirs, StringComparer.OrdinalIgnoreCase);

return null;
return dirs.LastOrDefault();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
12 changes: 12 additions & 0 deletions test/Heleonix.Build.Tests.ExeMock/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if(args.Any(a => a.Contains("ERROR")))
{
Console.Error.Write("ERROR details");

Console.Write("ERROR simulated");

return 1;
}

Console.Write(string.Join(' ', args));

return 0;
5 changes: 5 additions & 0 deletions test/Heleonix.Build.Tests/Common/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public static class PathHelper
/// </summary>
public static string SnkPairFile { get; } = Path.Combine(CurrentDir, "SnkPair.snk");

/// <summary>
/// Gets the path to the ExeMoc file at runtime.
/// </summary>
public static string ExeMockFile { get; } = Path.Combine(CurrentDir, "Heleonix.Build.Tests.ExeMock.exe");

/// <summary>
/// Gets the random file name in current directory.
/// </summary>
Expand Down
10 changes: 9 additions & 1 deletion test/Heleonix.Build.Tests/Common/TestBuildEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class TestBuildEngine : IBuildEngine
/// </summary>
public List<string> ErrorMessages { get; } = new List<string>();

/// <summary>
/// Gets logged messages.
/// </summary>
public List<string> Messages { get; } = new List<string>();

/// <summary>
/// Gets a value indicating whether the ContinueOnError flag was set to true
/// for this particular task in the project file.
Expand Down Expand Up @@ -66,7 +71,10 @@ public void LogWarningEvent(BuildWarningEventArgs e)
/// <param name="e">The event data.</param>
public void LogMessageEvent(BuildMessageEventArgs e)
{
// Dummy implementation.
if (!string.IsNullOrEmpty(e.Message))
{
this.Messages.Add(e.Message);
}
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions test/Heleonix.Build.Tests/Heleonix.Build.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Heleonix.Build\Heleonix.Build.csproj" />
<ProjectReference Include="..\Heleonix.Build.Tests.ExeMock\Heleonix.Build.Tests.ExeMock.csproj" />
</ItemGroup>

<Target Name="Hx_InstallTool" AfterTargets="Build">
Expand Down
109 changes: 109 additions & 0 deletions test/Heleonix.Build.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// <copyright file="ProgramTests.cs" company="Heleonix - Hennadii Lutsyshyn">
// Copyright (c) Heleonix - Hennadii Lutsyshyn. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the repository root for full license information.
// </copyright>

namespace Heleonix.Build.Tests;

/// <summary>
/// Tests the <see cref="Program"/>.
/// </summary>
[ComponentTest(Type = typeof(Program))]

public static class ProgramTests
{
/// <summary>
/// Tests the <see cref="Program.Main"/>.
/// </summary>
[MemberTest(Name = nameof(Program.Main))]

public static void MainTests()
{
string[] args = null;
string outptut = null;
string errors = null;
var returnValue = 0;

Act(() =>
{
using var outWriter = new StringWriter();
using var errorWriter = new StringWriter();

Console.SetOut(outWriter);
Console.SetError(errorWriter);

returnValue = Program.Main(args);

outptut = outWriter.ToString();
errors = errorWriter.ToString();
});

When("the help command is called", () =>
{
And("the short form is specified", () =>
{
args = new string[] { "-h" };

Should("display information about the tool", () =>
{
Assert.That(returnValue, Is.Zero);
Assert.That(outptut, Contains.Substring("Description:"));
Assert.That(outptut, Contains.Substring("Version:"));
Assert.That(outptut, Contains.Substring("More:"));
Assert.That(outptut, Contains.Substring("Options:"));
Assert.That(outptut, Contains.Substring("Examples:"));
});
});

And("the long form is specified", () =>
{
args = new string[] { "--help" };

Should("display information about the tool", () =>
{
Assert.That(returnValue, Is.Zero);
Assert.That(outptut, Contains.Substring("Description:"));
Assert.That(outptut, Contains.Substring("Version:"));
Assert.That(outptut, Contains.Substring("More:"));
Assert.That(outptut, Contains.Substring("Options:"));
Assert.That(outptut, Contains.Substring("Examples:"));
});
});
});

When("the '--exe' value is not provided", () =>
{
args = new string[] { "--exe" };

Should("retun the error code: 1", () =>
{
Assert.That(returnValue, Is.EqualTo(1));
Assert.That(
errors,
Contains.Substring("The dotnet executable path is not defined for the specified argument '--exe'."));
});
});

When("the --exe value is provided", () =>
{
args = new string[] { "--exe", "dotnet.exe", "-p:Hx_WS_RepositoryUrl=https://example.com" };

Should("run the provided dotnet executable with passed command-line arguments", () =>
{
Assert.That(returnValue, Is.Zero);
Assert.That(outptut, Contains.Substring("https://example.com"));
});
});

When("the default dotnet.exe is used", () =>
{
args = new string[] { "-p:Hx_WS_RepositoryUrl=https://example.com" };

Should("run the provided dotnet executable with passed command-line arguments", () =>
{
Assert.That(returnValue, Is.Zero);
Assert.That(outptut, Contains.Substring("https://example.com"));
});
});
}
}
Loading

0 comments on commit fcecf16

Please sign in to comment.