Skip to content

Commit 5405bdf

Browse files
committed
Also allow relative path
introduce ifdef for the different plattforms to test the path on all 3 operating systems
1 parent 2bb0ebb commit 5405bdf

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

src/ImageSharp.Web/Caching/PhysicalFileSystemCache.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public PhysicalFileSystemCache(
7272

7373
// Allow configuration of the cache without having to register everything.
7474
this.cacheOptions = cacheOptions != null ? cacheOptions.Value : new PhysicalFileSystemCacheOptions();
75-
this.cacheRootPath = GetCacheRoot(this.cacheOptions, environment.WebRootPath);
75+
this.cacheRootPath = GetCacheRoot(this.cacheOptions, environment.WebRootPath, environment.ContentRootPath);
7676
if (!Directory.Exists(this.cacheRootPath))
7777
{
7878
Directory.CreateDirectory(this.cacheRootPath);
@@ -89,13 +89,17 @@ public PhysicalFileSystemCache(
8989
/// </summary>
9090
/// <param name="cacheOptions">the cache options.</param>
9191
/// <param name="webRootPath">the webRootPath.</param>
92+
/// <param name="contentRootPath">the contentRootPath.</param>
9293
/// <returns>root path.</returns>
93-
internal static string GetCacheRoot(PhysicalFileSystemCacheOptions cacheOptions, string webRootPath)
94+
internal static string GetCacheRoot(in PhysicalFileSystemCacheOptions cacheOptions, in string webRootPath, in string contentRootPath)
9495
{
9596
var cacheRoot = string.IsNullOrEmpty(cacheOptions.CacheRoot)
9697
? webRootPath
9798
: cacheOptions.CacheRoot;
98-
return Path.Combine(cacheRoot, cacheOptions.CacheFolder);
99+
100+
return Path.IsPathFullyQualified(cacheRoot)
101+
? Path.Combine(cacheRoot, cacheOptions.CacheFolder)
102+
: Path.GetFullPath(Path.Combine(cacheRoot, cacheOptions.CacheFolder), contentRootPath);
99103
}
100104

101105
/// <inheritdoc/>

tests/ImageSharp.Web.Tests/Caching/PhysicialFileSystemCacheTests.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,29 @@ public void FilePathMatchesReference()
2121
Assert.Equal(expected, actual);
2222
}
2323

24-
[Fact]
25-
public void CacheRootFromOptions()
26-
{
27-
var cacheOptions = new PhysicalFileSystemCacheOptions();
28-
cacheOptions.CacheFolder = "cacheFolder";
29-
cacheOptions.CacheRoot = "C:\\Temp";
30-
31-
var cacheRoot = PhysicalFileSystemCache.GetCacheRoot(cacheOptions, null);
32-
33-
Assert.Equal(Path.Combine(cacheOptions.CacheRoot, cacheOptions.CacheFolder), cacheRoot);
34-
}
35-
36-
[Fact]
37-
public void CacheRootFromEnvironment()
24+
[Theory]
25+
#if Linux
26+
[InlineData("cacheFolder", "/Users/username", null, null, "/Users/username/cacheFolder")]
27+
[InlineData("cacheFolder", null, "/Users/WebRoot", null, "/Users/WebRoot/cacheFolder")]
28+
[InlineData("cacheFolder", "../Temp", null, "/Users/this/a/root", "/Users/this/a/Temp/cacheFolder")]
29+
#elif OSX
30+
[InlineData("cacheFolder", "/Users/username", null, null, "/Users/username/cacheFolder")]
31+
[InlineData("cacheFolder", null, "/Users/WebRoot", null, "/Users/WebRoot/cacheFolder")]
32+
[InlineData("cacheFolder", "../Temp", null, "/Users/this/a/root", "/Users/this/a/Temp/cacheFolder")]
33+
#elif Windows
34+
[InlineData("cacheFolder", "C:/Temp", null, null, "C:/Temp/cacheFolder")]
35+
[InlineData("cacheFolder", null, "C:/WebRoot", null, "C:/WebRoot/cacheFolder")]
36+
[InlineData("cacheFolder", "../Temp", null, "C:/this/a/root", "C:/this/a/Temp/cacheFolder")]
37+
#endif
38+
public void CacheRootFromOptions(string cacheFolder, string cacheRoot, string webRootPath, string contentRootPath, string expected)
3839
{
3940
var cacheOptions = new PhysicalFileSystemCacheOptions();
40-
cacheOptions.CacheFolder = "cacheFolder";
41+
cacheOptions.CacheFolder = cacheFolder;
42+
cacheOptions.CacheRoot = cacheRoot;
4143

42-
var cacheRoot = PhysicalFileSystemCache.GetCacheRoot(cacheOptions, "C:\\WebRoot");
44+
var cacheRootResult = PhysicalFileSystemCache.GetCacheRoot(cacheOptions, webRootPath, contentRootPath);
4345

44-
Assert.Equal(Path.Combine("C:\\WebRoot", cacheOptions.CacheFolder), cacheRoot);
46+
Assert.Equal(expected, cacheRootResult);
4547
}
4648
}
4749
}

tests/ImageSharp.Web.Tests/ImageSharp.Web.Tests.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88

99
<!--Used to show test project to dotnet test-->
1010
<IsTestProject>true</IsTestProject>
11+
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows>
12+
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
13+
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux>
14+
</PropertyGroup>
15+
16+
<PropertyGroup Condition="'$(IsWindows)'=='true'">
17+
<DefineConstants>Windows</DefineConstants>
18+
</PropertyGroup>
19+
<PropertyGroup Condition="'$(IsOSX)'=='true'">
20+
<DefineConstants>OSX</DefineConstants>
21+
</PropertyGroup>
22+
<PropertyGroup Condition="'$(IsLinux)'=='true'">
23+
<DefineConstants>Linux</DefineConstants>
1124
</PropertyGroup>
1225

1326
<ItemGroup>

0 commit comments

Comments
 (0)