From a5b2d0340385c376e4be780fac78048c54b5f942 Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 31 Jan 2023 16:41:45 +0100 Subject: [PATCH 1/2] allow DOTNET_SCRIPT_CACHE_LOCATION to be relative --- .../ProjectSystem/FileUtils.cs | 9 +++- src/Dotnet.Script.Tests/FileUtilsTests.cs | 42 +++++++++++++++++++ .../NuGetSourceReferenceResolverTests.cs | 3 +- 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 src/Dotnet.Script.Tests/FileUtilsTests.cs diff --git a/src/Dotnet.Script.DependencyModel/ProjectSystem/FileUtils.cs b/src/Dotnet.Script.DependencyModel/ProjectSystem/FileUtils.cs index aa52c14a..37c37e7c 100644 --- a/src/Dotnet.Script.DependencyModel/ProjectSystem/FileUtils.cs +++ b/src/Dotnet.Script.DependencyModel/ProjectSystem/FileUtils.cs @@ -1,9 +1,9 @@ -using Dotnet.Script.DependencyModel.Environment; -using System; +using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Text; +using Dotnet.Script.DependencyModel.Environment; using SysEnvironment = System.Environment; namespace Dotnet.Script.DependencyModel.ProjectSystem @@ -51,8 +51,13 @@ public static string GetTempPath() { // prefer the custom env variable if set var cachePath = SysEnvironment.GetEnvironmentVariable("DOTNET_SCRIPT_CACHE_LOCATION"); + if (!string.IsNullOrEmpty(cachePath)) { + if (!Path.IsPathRooted(cachePath)) + { + cachePath = Path.Combine(Directory.GetCurrentDirectory(), cachePath); + } return cachePath; } diff --git a/src/Dotnet.Script.Tests/FileUtilsTests.cs b/src/Dotnet.Script.Tests/FileUtilsTests.cs new file mode 100644 index 00000000..8a2267ab --- /dev/null +++ b/src/Dotnet.Script.Tests/FileUtilsTests.cs @@ -0,0 +1,42 @@ +using System; +using System.IO; +using Dotnet.Script.DependencyModel.ProjectSystem; +using Xunit; + +namespace Dotnet.Script.Tests +{ + public class FileUtilsTests + { + [Fact] + public void GetTempPathCanBeOverridenWithAbsolutePathViaEnvVar() + { + var path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + try + { + Environment.SetEnvironmentVariable("DOTNET_SCRIPT_CACHE_LOCATION", path); + var tempPath = FileUtils.GetTempPath(); + Assert.Equal(path, tempPath); + } + finally + { + Environment.SetEnvironmentVariable("DOTNET_SCRIPT_CACHE_LOCATION", null); + } + } + + [Fact] + public void GetTempPathCanBeOverridenWithRelativePathViaEnvVar() + { + var path = "foo"; + try + { + Environment.SetEnvironmentVariable("DOTNET_SCRIPT_CACHE_LOCATION", path); + var tempPath = FileUtils.GetTempPath(); + Assert.Equal(Path.Combine(Directory.GetCurrentDirectory(), path), tempPath); + } + finally + { + Environment.SetEnvironmentVariable("DOTNET_SCRIPT_CACHE_LOCATION", null); + } + } + } +} diff --git a/src/Dotnet.Script.Tests/NuGetSourceReferenceResolverTests.cs b/src/Dotnet.Script.Tests/NuGetSourceReferenceResolverTests.cs index 20ad3161..82578387 100644 --- a/src/Dotnet.Script.Tests/NuGetSourceReferenceResolverTests.cs +++ b/src/Dotnet.Script.Tests/NuGetSourceReferenceResolverTests.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Collections.Immutable; using System.IO; using System.Text; From 92a605e549af97ba619f9e173b7a1f4a9f0e395b Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 31 Jan 2023 16:42:12 +0100 Subject: [PATCH 2/2] extra comment --- src/Dotnet.Script.DependencyModel/ProjectSystem/FileUtils.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Dotnet.Script.DependencyModel/ProjectSystem/FileUtils.cs b/src/Dotnet.Script.DependencyModel/ProjectSystem/FileUtils.cs index 37c37e7c..b23a0d1a 100644 --- a/src/Dotnet.Script.DependencyModel/ProjectSystem/FileUtils.cs +++ b/src/Dotnet.Script.DependencyModel/ProjectSystem/FileUtils.cs @@ -54,6 +54,7 @@ public static string GetTempPath() if (!string.IsNullOrEmpty(cachePath)) { + // if the path is not absolute, make it relative to the current folder if (!Path.IsPathRooted(cachePath)) { cachePath = Path.Combine(Directory.GetCurrentDirectory(), cachePath);