Skip to content

Commit 9b18acc

Browse files
committed
Add test.
Sign all assemblies. Add InternalsVisibleTo to the product assembly, so that tests can access internal APIs.
1 parent e18b10a commit 9b18acc

11 files changed

+179
-8
lines changed

Directory.Build.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<OutputPath>$(MSBuildThisFileDirectory)bin\$(MSBuildProjectName)\$(Configuration)\</OutputPath>
66
<BaseIntermediateOutputPath>$(MSBuildThisFileDirectory)obj\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
77
<DefineConstants Condition=" '$(ExtraDefine)' != '' ">$(DefineConstants);$(ExtraDefine)</DefineConstants>
8+
<SignAssembly>true</SignAssembly>
9+
<AssemblyOriginatorKeyFile>..\libgit2sharp.snk</AssemblyOriginatorKeyFile>
810
</PropertyGroup>
911

1012
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net461</TargetFramework>
6+
<PlatformTarget>AnyCPU</PlatformTarget>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

LibGit2Sharp.TestApp/TestApp.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.InteropServices;
4+
using System.Text;
5+
6+
namespace LibGit2Sharp.Tests
7+
{
8+
public class TestApp
9+
{
10+
[DllImport("kernel32")]
11+
private static extern IntPtr GetModuleHandle(string path);
12+
13+
[DllImport("kernel32")]
14+
private static extern int GetModuleFileName(IntPtr handle, [Out]StringBuilder path, int size);
15+
16+
static int Main(string[] args)
17+
{
18+
if (args.Length < 1 || args.Length > 2)
19+
{
20+
Console.Error.WriteLine("Usage: <module-name> <directory>");
21+
return -1;
22+
}
23+
24+
var moduleName = args[0];
25+
var loadFromDirectory = args[1];
26+
var expectedPath = Path.Combine(loadFromDirectory, (IntPtr.Size == 4) ? "x86" : "x64", moduleName + ".dll");
27+
28+
GlobalSettings.NativeLibraryPath = loadFromDirectory;
29+
var isValid = Repository.IsValid(Path.GetTempPath());
30+
31+
var capacity = ushort.MaxValue;
32+
var moduleHandle = GetModuleHandle(moduleName);
33+
var buffer = new StringBuilder(capacity);
34+
int actualLength = GetModuleFileName(moduleHandle, buffer, capacity);
35+
var actualPath = buffer.ToString(0, actualLength);
36+
37+
if (expectedPath != actualPath)
38+
{
39+
Console.WriteLine(actualPath);
40+
return 1;
41+
}
42+
43+
return 0;
44+
}
45+
}
46+
}

LibGit2Sharp.Tests/GlobalSettingsFixture.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Text.RegularExpressions;
1+
using System;
2+
using System.IO;
3+
using System.Text.RegularExpressions;
4+
using LibGit2Sharp.Core;
25
using LibGit2Sharp.Tests.TestHelpers;
36
using Xunit;
47

@@ -49,5 +52,35 @@ public void TryingToResetNativeLibraryPathAfterLoadedThrows()
4952

5053
Assert.Throws<LibGit2SharpException>(() => { GlobalSettings.NativeLibraryPath = "C:/Foo"; });
5154
}
55+
56+
[ConditionalFact(typeof(NetFramework))]
57+
public void LoadFromSpecifiedPath()
58+
{
59+
#if NET461
60+
var nativeDllFileName = NativeDllName.Name + ".dll";
61+
62+
var testAppExe = typeof(TestApp).Assembly.Location;
63+
var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
64+
var platformDir = Path.Combine(tempDir, "plat");
65+
66+
try
67+
{
68+
Directory.CreateDirectory(Path.Combine(platformDir, "x86"));
69+
Directory.CreateDirectory(Path.Combine(platformDir, "x64"));
70+
71+
File.Copy(Path.Combine(GlobalSettings.NativeLibraryPath, "x86", nativeDllFileName), Path.Combine(platformDir, "x86", nativeDllFileName));
72+
File.Copy(Path.Combine(GlobalSettings.NativeLibraryPath, "x64", nativeDllFileName), Path.Combine(platformDir, "x64", nativeDllFileName));
73+
74+
var (output, exitCode) = ProcessHelper.RunProcess(testAppExe, arguments: $@"{NativeDllName.Name} ""{platformDir}""", workingDirectory: tempDir);
75+
76+
Assert.Empty(output);
77+
Assert.Equal(0, exitCode);
78+
}
79+
finally
80+
{
81+
DirectoryHelper.DeleteDirectory(tempDir);
82+
}
83+
#endif
84+
}
5285
}
5386
}

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
5-
<DefineConstants Condition=" '$(TargetFramework)' == 'net461' ">$(DefineConstants);DESKTOP</DefineConstants>
65
</PropertyGroup>
76

87
<ItemGroup>
98
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj" />
9+
<ProjectReference Include="..\LibGit2Sharp.TestApp\LibGit2Sharp.TestApp.csproj" Condition="'$(TargetFramework)' == 'net461'" />
1010
</ItemGroup>
1111

1212
<ItemGroup>
@@ -19,8 +19,6 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<Compile Include="..\LibGit2Sharp\Core\Epoch.cs" Link="TestHelpers\Epoch.cs" />
23-
<Compile Include="..\LibGit2Sharp\Core\Platform.cs" Link="TestHelpers\Platform.cs" />
2422
<Compile Remove="desktop\**" Condition=" '$(TargetFramework)' != 'net461' " />
2523
</ItemGroup>
2624

LibGit2Sharp.Tests/SetErrorFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void FormatAggregateException()
4949
Exception exceptionToThrow = new AggregateException(aggregateExceptionMessage, new Exception(innerExceptionMessage), new Exception(innerExceptionMessage2));
5050

5151
StringBuilder sb = new StringBuilder();
52-
#if DESKTOP
52+
#if NET461
5353
sb.AppendLine(aggregateExceptionMessage);
5454
#else
5555
sb.AppendLine($"{aggregateExceptionMessage} ({innerExceptionMessage}) ({innerExceptionMessage2})");
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
2+
3+
using System;
4+
using LibGit2Sharp.Core;
5+
using Xunit;
6+
7+
namespace LibGit2Sharp.Tests
8+
{
9+
public class ConditionalFactAttribute : FactAttribute
10+
{
11+
public ConditionalFactAttribute(params Type[] skipConditions)
12+
{
13+
foreach (var skipCondition in skipConditions)
14+
{
15+
ExecutionCondition condition = (ExecutionCondition)Activator.CreateInstance(skipCondition);
16+
if (condition.ShouldSkip)
17+
{
18+
Skip = condition.SkipReason;
19+
break;
20+
}
21+
}
22+
}
23+
}
24+
25+
public abstract class ExecutionCondition
26+
{
27+
public abstract bool ShouldSkip { get; }
28+
public abstract string SkipReason { get; }
29+
}
30+
31+
public class NetFramework : ExecutionCondition
32+
{
33+
public override bool ShouldSkip => !Platform.IsRunningOnNetFramework();
34+
public override string SkipReason => ".NET Framework only test";
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Text;
5+
6+
namespace LibGit2Sharp.Tests
7+
{
8+
public static class ProcessHelper
9+
{
10+
public static (string, int) RunProcess(string fileName, string arguments, string workingDirectory = null)
11+
{
12+
var process = new Process
13+
{
14+
StartInfo = new ProcessStartInfo(fileName, arguments)
15+
{
16+
RedirectStandardError = true,
17+
RedirectStandardOutput = true,
18+
CreateNoWindow = true,
19+
UseShellExecute = false,
20+
WorkingDirectory = workingDirectory ?? string.Empty
21+
}
22+
};
23+
24+
var output = new StringBuilder();
25+
26+
process.OutputDataReceived += (_, e) => output.AppendLine(e.Data);
27+
process.ErrorDataReceived += (_, e) => output.AppendLine(e.Data);
28+
29+
process.Start();
30+
31+
process.WaitForExit();
32+
33+
return (output.ToString(), process.ExitCode);
34+
}
35+
}
36+
}

LibGit2Sharp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1515
version.json = version.json
1616
EndProjectSection
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibGit2Sharp.TestApp", "LibGit2Sharp.TestApp\LibGit2Sharp.TestApp.csproj", "{86453D2C-4953-4DF4-B12A-ADE579608BAA}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -29,6 +31,10 @@ Global
2931
{286E63EB-04DD-4ADE-88D6-041B57800761}.Debug|Any CPU.Build.0 = Debug|Any CPU
3032
{286E63EB-04DD-4ADE-88D6-041B57800761}.Release|Any CPU.ActiveCfg = Release|Any CPU
3133
{286E63EB-04DD-4ADE-88D6-041B57800761}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{86453D2C-4953-4DF4-B12A-ADE579608BAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{86453D2C-4953-4DF4-B12A-ADE579608BAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{86453D2C-4953-4DF4-B12A-ADE579608BAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{86453D2C-4953-4DF4-B12A-ADE579608BAA}.Release|Any CPU.Build.0 = Release|Any CPU
3238
EndGlobalSection
3339
GlobalSection(SolutionProperties) = preSolution
3440
HideSolutionNode = FALSE

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
<PackageTags>libgit2 git</PackageTags>
1010
<PackageProjectUrl>https://github.com/libgit2/libgit2sharp/</PackageProjectUrl>
1111
<Authors>LibGit2Sharp contributors</Authors>
12-
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
13-
<SignAssembly>true</SignAssembly>
14-
<AssemblyOriginatorKeyFile>..\libgit2sharp.snk</AssemblyOriginatorKeyFile>
12+
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
1513
</PropertyGroup>
1614

1715
<!-- Disable SourceLink when running on Travis CI -->

LibGit2Sharp/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.CompilerServices;
23
using System.Runtime.InteropServices;
34

45
// General Information about an assembly is controlled through the following
@@ -16,3 +17,5 @@
1617
// The following GUID is for the ID of the typelib if this project is exposed to COM
1718

1819
[assembly: Guid("c6f71967-5be1-49f5-b48e-861bff498ea3")]
20+
21+
[assembly: InternalsVisibleTo("LibGit2Sharp.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010013172CAC3D61EF825164EF8443ED2F97316D0C2A4A65D3B2F6E5C9175C6C589D6A0EAE803E3E7FC0DA9E6672B1DE036CF74E1D33E21DD83E1145E3A454F92E52107495082DCCD1D9F521592F79F41DF26ED727059F8A4E5D3C23ECC525306831A15F1E56B693FDE112137E973B599A13209A5B63E05EE00886DE594E70A993B5")]

0 commit comments

Comments
 (0)