Skip to content

Commit 2d66ece

Browse files
DmitryLukyanovrstam
authored andcommitted
CSHARP-3427: Can't make use of compression when application path contain space. (#529)
CSHARP-3427: Can't make use of compression when application path contain space.
1 parent c6276f9 commit 2d66ece

File tree

2 files changed

+105
-7
lines changed

2 files changed

+105
-7
lines changed

src/MongoDB.Driver.Core/Core/NativeLibraryLoader/RelativeLibraryLocatorBase.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,25 @@ internal abstract class RelativeLibraryLocatorBase : ILibraryLocator
2626
public virtual bool IsX32ModeSupported => false;
2727

2828
// public methods
29+
public virtual string GetBaseAssemblyUri() => typeof(RelativeLibraryLocatorBase).GetTypeInfo().Assembly.CodeBase;
30+
2931
public string GetLibraryAbsolutePath(OperatingSystemPlatform currentPlatform)
3032
{
3133
var relativePath = GetLibraryRelativePath(currentPlatform);
3234
return GetAbsolutePath(relativePath);
3335
}
3436

35-
public virtual Assembly GetLibraryBaseAssembly()
37+
public virtual string GetLibraryBaseAssemblyPath()
3638
{
37-
return typeof(RelativeLibraryLocatorBase).GetTypeInfo().Assembly;
39+
var baseAssemblyPathUri = GetBaseAssemblyUri();
40+
var uri = new Uri(baseAssemblyPathUri);
41+
return Uri.UnescapeDataString(uri.AbsolutePath);
3842
}
3943

4044
public virtual string GetLibraryBasePath()
4145
{
42-
var assembly = GetLibraryBaseAssembly();
43-
var codeBase = assembly.CodeBase;
44-
var uri = new Uri(codeBase);
45-
var absolutePath = uri.AbsolutePath;
46-
return Path.GetDirectoryName(absolutePath);
46+
var absoluteAssemblyPath = GetLibraryBaseAssemblyPath();
47+
return Path.GetDirectoryName(absoluteAssemblyPath);
4748
}
4849

4950
public abstract string GetLibraryRelativePath(OperatingSystemPlatform currentPlatform);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* Copyright 2021-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.IO;
18+
using FluentAssertions;
19+
using MongoDB.Driver.Core.NativeLibraryLoader;
20+
using MongoDB.Driver.TestHelpers;
21+
using MongoDB.Shared;
22+
using Xunit;
23+
24+
namespace MongoDB.Driver.Core.Tests.Core.NativeLibraryLoader
25+
{
26+
public class NativeLibraryLoaderTests
27+
{
28+
[Theory]
29+
[InlineData(null, "mongo-csharp-driver")] // the default assembly-based logic
30+
[InlineData("mongo-csharp-driver", "mongo-csharp-driver")]
31+
[InlineData("mongo csharp driver", "mongo csharp driver")]
32+
[InlineData("&mongo$csharp@driver%", "&mongo$csharp@driver%")]
33+
public void GetLibraryBasePath_should_get_correct_paths(string rootTestFolder, string expectedRootTestFolder)
34+
{
35+
string testAssemblyCodeBaseUri = null; // use assembly-based CodeBase for null
36+
if (rootTestFolder != null)
37+
{
38+
// mock assembly-based CodeBase path
39+
var assemblyPath = Path.Combine(
40+
RequirePlatform.GetCurrentOperatingSystem() == SupportedOperatingSystem.Windows ? "C:/" : @"\\data",
41+
rootTestFolder,
42+
GetCommonTestAssemblyFolderEnding(),
43+
"MongoDB.Driver.Core.dll");
44+
testAssemblyCodeBaseUri = new Uri(assemblyPath).ToString();
45+
}
46+
47+
var subject = new TestRelativeLibraryLocator(testAssemblyCodeBaseUri);
48+
49+
var result = subject.GetLibraryBasePath();
50+
51+
var expectedResult = Path.Combine(expectedRootTestFolder, GetCommonTestAssemblyFolderEnding());
52+
result.Should().EndWith(expectedResult);
53+
}
54+
55+
// private methods
56+
private string GetCommonTestAssemblyFolderEnding() =>
57+
Path.Combine(
58+
"tests",
59+
"MongoDB.Driver.Core.Tests",
60+
"bin",
61+
GetConfigurationName(),
62+
GetTargetFrameworkMonikerName());
63+
64+
private string GetConfigurationName() =>
65+
#if DEBUG
66+
"Debug";
67+
#else
68+
"Release";
69+
#endif
70+
71+
private string GetTargetFrameworkMonikerName() =>
72+
#if NETCOREAPP1_1
73+
"netcoreapp1.1";
74+
#elif NETCOREAPP2_1
75+
"netcoreapp2.1";
76+
#elif NETCOREAPP3_0
77+
"netcoreapp3.0";
78+
#elif NET452
79+
"net452";
80+
#endif
81+
82+
// nested types
83+
private class TestRelativeLibraryLocator : RelativeLibraryLocatorBase
84+
{
85+
private readonly string _testAssemblyUri;
86+
87+
public TestRelativeLibraryLocator(string testAssemblyUri)
88+
{
89+
_testAssemblyUri = testAssemblyUri; // can be null
90+
}
91+
92+
public override string GetBaseAssemblyUri() => _testAssemblyUri ?? base.GetBaseAssemblyUri();
93+
94+
public override string GetLibraryRelativePath(OperatingSystemPlatform currentPlatform) => throw new NotImplementedException();
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)