diff --git a/src/Dotnet.Script.DependencyModel/ProjectSystem/FileUtils.cs b/src/Dotnet.Script.DependencyModel/ProjectSystem/FileUtils.cs index aa52c14a..b23a0d1a 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,14 @@ public static string GetTempPath() { // prefer the custom env variable if set var cachePath = SysEnvironment.GetEnvironmentVariable("DOTNET_SCRIPT_CACHE_LOCATION"); + 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); + } 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;