Skip to content

Commit

Permalink
Add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danmoseley committed Apr 30, 2024
1 parent 7d42fec commit 9d15d5d
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/tasks/tests/ConvertDllsToWebCilTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Moq;
using System.Collections.Generic;
using System.IO;
using Xunit;

namespace Microsoft.NET.Sdk.WebAssembly.Tests
{
public class ConvertDllsToWebCilTests
{
private ConvertDllsToWebCil task = new ConvertDllsToWebCil();
private List<BuildErrorEventArgs> errors = new List<BuildErrorEventArgs>();

public ConvertDllsToWebCilTests()
{
var buildEngine = new Mock<IBuildEngine>();
buildEngine.Setup(x => x.LogErrorEvent(It.IsAny<BuildErrorEventArgs>())).Callback<BuildErrorEventArgs>(e => errors.Add(e));
task.BuildEngine = buildEngine.Object;
}

[Fact]
public void TestEmptyInput()
{
string input = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".dll");

try
{
File.Create(input).Dispose();

task.Candidates = new ITaskItem[] { new TaskItem(input) };
task.IsEnabled = true;
task.OutputPath = task.IntermediateOutputPath = Path.GetTempPath();

bool result = task.Execute();

Assert.False(result);
Assert.Single(errors);
Assert.Contains(input, errors[0].Message);
}
finally
{
File.Delete(input);
}
}

[Fact]
public void TestInvalidDirectoryInput()
{
string input = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".dll");

try
{
Directory.CreateDirectory(input);

task.Candidates = new ITaskItem[] { new TaskItem(input) };
task.IsEnabled = true;
task.OutputPath = task.IntermediateOutputPath = Path.GetTempPath();

bool result = task.Execute();

Assert.False(result);
Assert.Single(errors);
Assert.Contains(input, errors[0].Message);
}
finally
{
Directory.Delete(input);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
<Nullable>enable</Nullable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="$(MicrosoftBuildFrameworkVersion)" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions src/tasks/tests/tests.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.34815.271
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests", "Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj", "{035240F3-31CE-409E-8D55-2A89ADE522D6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.WebAssembly.Pack.Tasks", "..\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.csproj", "{4C68406D-BBFC-4637-A634-917C0621A883}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{035240F3-31CE-409E-8D55-2A89ADE522D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{035240F3-31CE-409E-8D55-2A89ADE522D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{035240F3-31CE-409E-8D55-2A89ADE522D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{035240F3-31CE-409E-8D55-2A89ADE522D6}.Release|Any CPU.Build.0 = Release|Any CPU
{4C68406D-BBFC-4637-A634-917C0621A883}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C68406D-BBFC-4637-A634-917C0621A883}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C68406D-BBFC-4637-A634-917C0621A883}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C68406D-BBFC-4637-A634-917C0621A883}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {15EFCF6A-C86C-474F-BBAD-F2DB4B3DBAA3}
EndGlobalSection
EndGlobal

0 comments on commit 9d15d5d

Please sign in to comment.