Skip to content

Commit 9f7e134

Browse files
committed
Refactor unwrapping of temp repository on unix
Fix #1040
1 parent e1e5fe6 commit 9f7e134

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

LibGit2Sharp.Tests/BlobFixture.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public void CanTellIfTheBlobContentLooksLikeBinary()
222222

223223
private static void SkipIfNotSupported(string autocrlf)
224224
{
225-
InconclusiveIf(() => autocrlf == "true" && IsRunningOnUnix(), "Non-Windows does not support core.autocrlf = true");
225+
InconclusiveIf(() => autocrlf == "true" && Constants.IsRunningOnUnix, "Non-Windows does not support core.autocrlf = true");
226226
}
227227
}
228228
}

LibGit2Sharp.Tests/ShadowCopyFixture.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void CanProbeForNativeBinariesFromAShadowCopiedAssembly()
5757
string cachedAssembliesPath = Path.Combine(setup.CachePath, setup.ApplicationName);
5858
Assert.True(cachedAssemblyLocation.StartsWith(cachedAssembliesPath));
5959

60-
if (!IsRunningOnUnix())
60+
if (!Constants.IsRunningOnUnix)
6161
{
6262
// ...that this cache doesn't contain the `NativeBinaries` folder
6363
string cachedAssemblyParentPath = Path.GetDirectoryName(cachedAssemblyLocation);

LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs

-8
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,6 @@ private static bool IsFileSystemCaseSensitiveInternal()
106106
return !isInsensitive;
107107
}
108108

109-
// Should match LibGit2Sharp.Core.NativeMethods.IsRunningOnUnix()
110-
protected static bool IsRunningOnUnix()
111-
{
112-
// see http://mono-project.com/FAQ%3a_Technical#Mono_Platforms
113-
var p = (int)Environment.OSVersion.Platform;
114-
return (p == 4) || (p == 6) || (p == 128);
115-
}
116-
117109
protected void CreateCorruptedDeadBeefHead(string repoPath)
118110
{
119111
const string deadbeef = "deadbeef";

LibGit2Sharp.Tests/TestHelpers/Constants.cs

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Reflection;
45

56
namespace LibGit2Sharp.Tests.TestHelpers
67
{
@@ -10,6 +11,14 @@ public static class Constants
1011
public const string UnknownSha = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
1112
public static readonly Identity Identity = new Identity("A. U. Thor", "[email protected]");
1213
public static readonly Signature Signature = new Signature(Identity, new DateTimeOffset(2011, 06, 16, 10, 58, 27, TimeSpan.FromHours(2)));
14+
public static readonly bool IsRunningOnUnix = IsUnixPlatform();
15+
16+
private static bool IsUnixPlatform()
17+
{
18+
// see http://mono-project.com/FAQ%3a_Technical#Mono_Platforms
19+
var p = (int)Environment.OSVersion.Platform;
20+
return (p == 4) || (p == 6) || (p == 128);
21+
}
1322

1423
// Populate these to turn on live credential tests: set the
1524
// PrivateRepoUrl to the URL of a repository that requires
@@ -37,19 +46,15 @@ public static string BuildPath()
3746
{
3847
string tempPath = null;
3948

40-
var unixPath = Type.GetType("Mono.Unix.UnixPath, Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756");
41-
42-
if (unixPath != null)
49+
if (IsRunningOnUnix)
4350
{
4451
// We're running on Mono/*nix. Let's unwrap the path
45-
tempPath = (string)unixPath.InvokeMember("GetCompleteRealPath",
46-
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.FlattenHierarchy |
47-
System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public,
48-
null, unixPath, new object[] { Path.GetTempPath() });
52+
tempPath = UnwrapUnixTempPath();
4953
}
5054
else
5155
{
5256
const string LibGit2TestPath = "LibGit2TestPath";
57+
5358
// We're running on .Net/Windows
5459
if (Environment.GetEnvironmentVariables().Contains(LibGit2TestPath))
5560
{
@@ -68,5 +73,16 @@ public static string BuildPath()
6873
Trace.TraceInformation("Test working directory set to '{0}'", testWorkingDirectory);
6974
return testWorkingDirectory;
7075
}
76+
77+
private static string UnwrapUnixTempPath()
78+
{
79+
var assembly = Assembly.Load("Mono.Posix");
80+
var type = assembly.GetType("Mono.Unix.UnixPath");
81+
82+
return (string)type.InvokeMember("GetCompleteRealPath",
83+
BindingFlags.Static | BindingFlags.FlattenHierarchy |
84+
BindingFlags.InvokeMethod | BindingFlags.Public,
85+
null, type, new object[] { Path.GetTempPath() });
86+
}
7187
}
7288
}

0 commit comments

Comments
 (0)