Skip to content

Commit 0fea3db

Browse files
committed
Create test for running confusing task
Close #32
1 parent 05b89e9 commit 0fea3db

8 files changed

Lines changed: 155 additions & 23 deletions
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using Microsoft.Build.Framework;
6+
using Moq;
7+
using Xunit;
8+
using Task = System.Threading.Tasks.Task;
9+
10+
namespace Confuser.MSBuild.Tasks.Tests {
11+
public class ConfuseTaskTests : VerifyTestBase {
12+
private Mock<IBuildEngine> buildEngine;
13+
private List<BuildErrorEventArgs> errors;
14+
15+
public ConfuseTaskTests() {
16+
this.buildEngine = new Mock<IBuildEngine>();
17+
this.errors = new List<BuildErrorEventArgs>();
18+
this.buildEngine.Setup(x => x.LogErrorEvent(It.IsAny<BuildErrorEventArgs>())).Callback<BuildErrorEventArgs>(e => errors.Add(e));
19+
}
20+
21+
[Fact]
22+
[Trait("Category", "MSBuildIntegration")]
23+
public async Task NonExistingFileInProjectProduceError() {
24+
var assembly = new Mock<ITaskItem>();
25+
assembly.SetupAllProperties();
26+
assembly.Object.ItemSpec = $".\\bin\\debug\\test.dll";
27+
var resultProject = new Mock<ITaskItem>();
28+
resultProject.SetupAllProperties();
29+
resultProject.Object.ItemSpec = $"Resources\\non-existing-file.crproj";
30+
31+
var task = new ConfuseTask();
32+
task.Project = resultProject.Object;
33+
task.BuildEngine = buildEngine.Object;
34+
task.OutputAssembly = assembly.Object;
35+
36+
//Act
37+
var success = task.Execute();
38+
39+
//Assert
40+
Assert.True(success);
41+
await Verify(string.Join("\n", errors.Select(_ => _.Message)), GetSettings());
42+
}
43+
44+
[Fact]
45+
[Trait("Category", "MSBuildIntegration")]
46+
public async Task CorrectProjectProduceRunnableExecutable() {
47+
var assembly = new Mock<ITaskItem>();
48+
assembly.SetupAllProperties();
49+
assembly.Object.ItemSpec = $".\\test\\net472\\obfuscated\\Confuser.CLI.exe";
50+
var resultProject = new Mock<ITaskItem>();
51+
resultProject.SetupAllProperties();
52+
resultProject.Object.ItemSpec = $"Resources\\valid.crproj";
53+
54+
var task = new ConfuseTask();
55+
task.Project = resultProject.Object;
56+
task.BuildEngine = buildEngine.Object;
57+
task.OutputAssembly = assembly.Object;
58+
59+
//Act
60+
var success = task.Execute();
61+
62+
//Assert
63+
Assert.True(success);
64+
Assert.Empty(errors);
65+
Assert.True(File.Exists(task.OutputAssembly.ItemSpec));
66+
}
67+
}
68+
}

Tests/Confuser.MSBuild.Tasks.Tests/Confuser.MSBuild.Tasks.Tests.csproj

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
<TargetFramework>net472</TargetFramework>
55
<IsPackable>false</IsPackable>
66
<OutputType>Exe</OutputType>
7+
<TestTargetProject>..\..\Confuser.CLI\Confuser.CLI.csproj</TestTargetProject>
78
</PropertyGroup>
89

910
<ItemGroup>
11+
<None Include="Resources\valid.crproj">
12+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
13+
</None>
14+
<None Include="Resources\non-existing-file.crproj">
15+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16+
</None>
1017
<None Include="Resources\confuser.src.crproj">
1118
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1219
</None>
@@ -20,8 +27,43 @@
2027
</ItemGroup>
2128

2229
<ItemGroup>
30+
<ProjectReference Include="..\..\Confuser.CLI\Confuser.CLI.csproj" ReferenceOutputAssembly="false" />
2331
<ProjectReference Include="..\Confuser.UnitTest\Confuser.UnitTest.csproj" />
2432
<ProjectReference Include="..\..\Confuser.MSBuild.Tasks\Confuser.MSBuild.Tasks.csproj" />
2533
</ItemGroup>
2634

35+
<ItemGroup>
36+
<!--<RuntimeProjectFramework Include="$(TargetFrameworks)" />-->
37+
<RuntimeProjectFramework Include="net472" />
38+
</ItemGroup>
39+
40+
<Target Name="CollectRuntimeOutputs" AfterTargets="Build">
41+
42+
<!-- Query each TFM output -->
43+
<MSBuild Projects="$(TestTargetProject)" Targets="GetTargetPath" Properties="Configuration=$(Configuration);TargetFramework=%(RuntimeProjectFramework.Identity)" BuildInParallel="true">
44+
45+
<Output TaskParameter="TargetOutputs" ItemName="RuntimeOutputs" />
46+
47+
</MSBuild>
48+
49+
<ItemGroup>
50+
<RuntimeOutputsNetFramework Include="@(RuntimeOutputs)" Condition="'%(TargetFrameworkVersion)' == '4.7.2'" />
51+
<RuntimeOutputsNet Include="@(RuntimeOutputs)" Condition="'%(TargetFrameworkVersion)' == '8.0'" />
52+
</ItemGroup>
53+
<PropertyGroup>
54+
<_RuntimeOutputsNetFramework>@(RuntimeOutputsNetFramework)</_RuntimeOutputsNetFramework>
55+
<_RuntimeOutputsNet>@(RuntimeOutputsNet)</_RuntimeOutputsNet>
56+
</PropertyGroup>
57+
<ItemGroup>
58+
<_RuntimeOutputsNetFrameworkFiles Include="$([System.IO.Path]::GetDirectoryName($(_RuntimeOutputsNetFramework)))\*.*"/>
59+
</ItemGroup>
60+
<Copy SourceFiles="@(_RuntimeOutputsNetFrameworkFiles)"
61+
Condition="'$(_RuntimeOutputsNetFramework)' != ''"
62+
DestinationFolder="$(OutDir)test\net472\" />
63+
<Copy SourceFiles="$([System.IO.Path]::GetDirectoryName($(_RuntimeOutputsNet)))\*.*"
64+
Condition="'$(_RuntimeOutputsNet)' != ''"
65+
DestinationFolder="$(OutDir)test\net8.0\" />
66+
67+
</Target>
68+
2769
</Project>

Tests/Confuser.MSBuild.Tasks.Tests/CreateProjectTests.cs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Linq;
5-
using Confuser.UnitTest;
64
using Microsoft.Build.Framework;
75
using Moq;
8-
using VerifyTests;
9-
using VerifyXunit;
106
using Xunit;
117

128
namespace Confuser.MSBuild.Tasks.Tests {
13-
public class CreateProjectTests : VerifyBase {
9+
public class CreateProjectTests : VerifyTestBase {
1410
private readonly ITestOutputHelper outputHelper;
1511
private Mock<IBuildEngine> buildEngine;
1612
private List<BuildErrorEventArgs> errors;
1713

18-
static CreateProjectTests() {
19-
// To disable Visual Studio popping up on every test execution.
20-
Environment.SetEnvironmentVariable("DiffEngine_Disabled", "true");
21-
Environment.SetEnvironmentVariable("Verify_DisableClipboard", "true");
22-
23-
// To prevent from adding UTF-8 BOM to generated test data:
24-
VerifierSettings.UseUtf8NoBom();
25-
}
26-
27-
public CreateProjectTests(ITestOutputHelper outputHelper) : base() {
14+
public CreateProjectTests(ITestOutputHelper outputHelper) {
2815
this.outputHelper = outputHelper ?? throw new ArgumentNullException(nameof(outputHelper));
2916
this.buildEngine = new Mock<IBuildEngine>();
3017
this.errors = new List<BuildErrorEventArgs>();
@@ -91,14 +78,6 @@ public async System.Threading.Tasks.Task BaseDirectoryOverridenTest() {
9178
Assert.Empty(errors);
9279
Assert.True(File.Exists(task.ResultProject.ItemSpec));
9380
await Verify(File.ReadAllText(task.ResultProject.ItemSpec), GetSettings());
94-
}
95-
96-
protected static VerifySettings GetSettings(params object[] parameters) {
97-
var settings = new VerifySettings();
98-
settings.UseDirectory("verified");
99-
if (parameters.Length > 0)
100-
settings.UseParameters(parameters);
101-
return settings;
10281
}
10382
}
10483
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<project outputDir="." baseDir="c:\obfuscation\input" debug="true" xmlns="http://confuser.codeplex.com">
2+
<module path="c:\obfuscation\input\test.dll" />
3+
<module path="c:\obfuscation\input\test2.dll" />
4+
<probePath>c:\obfuscation\input\bin</probePath>
5+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<project outputDir="test/net472/obfuscated" baseDir="test/net472" debug="true" xmlns="http://confuser.codeplex.com">
2+
<rule pattern="true" preset="maximum" inherit="false" />
3+
<module path="Confuser.CLI.exe" />
4+
<probePath>test/net472</probePath>
5+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using VerifyTests;
7+
8+
namespace Confuser.MSBuild.Tasks.Tests {
9+
public abstract class VerifyTestBase: VerifyXunit.VerifyBase {
10+
11+
static VerifyTestBase() {
12+
// To disable Visual Studio popping up on every test execution.
13+
Environment.SetEnvironmentVariable("DiffEngine_Disabled", "true");
14+
Environment.SetEnvironmentVariable("Verify_DisableClipboard", "true");
15+
16+
// To prevent from adding UTF-8 BOM to generated test data:
17+
VerifierSettings.UseUtf8NoBom();
18+
}
19+
20+
protected VerifyTestBase() : base() { }
21+
22+
protected static VerifySettings GetSettings(params object[] parameters) {
23+
var settings = new VerifySettings();
24+
settings.UseDirectory("verified");
25+
if (parameters.Length > 0)
26+
settings.UseParameters(parameters);
27+
return settings;
28+
}
29+
}
30+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
emptyString
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
An IO error occurred, check if all input/output locations are readable/writable.
2+
Could not open file c:\obfuscation\input\test.dll for reading. Error: 00000003

0 commit comments

Comments
 (0)