From a4cee25d6d9a3cee92ead14e52e0db04d3491967 Mon Sep 17 00:00:00 2001 From: Chris Darroch Date: Fri, 11 Sep 2020 00:28:28 -0700 Subject: [PATCH] FunctionalTests: support variable local cache root We update the CloneWithDefaultLocalCacheLocation() functional test so it accommodates variable local cache paths on Linux, depending on whether $XDG_CACHE_HOME is set or not. This allows the test to succeed on developer systems where we want to avoid polluting the default ~/.cache/scalar directory during test runs and so set $XDG_CACHE_HOME explicitly. Note that the new ScalarTestConfig.DefaultLocalCacheRoot accessor returns a path on Windows which should be "C:\.scalarCache", to match what would be supplied by the TryGetDefaultLocalCacheRoot() method of the WindowsPlatform if it were passed the default functional test enlistment root path. However, this Windows- specific DefaultLocalCacheRoot value is unused, as the only functional test which uses it is CloneWithDefaultLocalCacheLocation() and that test belongs to the POSIXOnly category. --- Scalar.FunctionalTests/ScalarTestConfig.cs | 36 +++++++++++++++++++ .../Tests/EnlistmentPerFixture/CloneTests.cs | 8 ++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Scalar.FunctionalTests/ScalarTestConfig.cs b/Scalar.FunctionalTests/ScalarTestConfig.cs index 98d91da40d..9c0800d40f 100644 --- a/Scalar.FunctionalTests/ScalarTestConfig.cs +++ b/Scalar.FunctionalTests/ScalarTestConfig.cs @@ -1,4 +1,6 @@ +using System; using System.IO; +using System.Runtime.InteropServices; namespace Scalar.FunctionalTests { @@ -10,6 +12,40 @@ public static class ScalarTestConfig public static string LocalCacheRoot { get; set; } + public static string DefaultLocalCacheRoot { + get + { + string homeDirectory = null; + string cachePath = ".scalarCache"; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + homeDirectory = Path.GetPathRoot(Properties.Settings.Default.EnlistmentRoot); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + homeDirectory = Environment.GetEnvironmentVariable("HOME"); + } + else + { + // On Linux we use a local cache path per the XDG Base Directory Specification. + homeDirectory = Environment.GetEnvironmentVariable("XDG_CACHE_HOME"); + if (!string.IsNullOrEmpty(homeDirectory)) + { + cachePath = "scalar"; + } + else + { + homeDirectory = Environment.GetEnvironmentVariable("HOME"); + cachePath = Path.Combine(".cache", "scalar"); + } + + } + + return Path.Combine(homeDirectory, cachePath); + } + } + public static object[] FileSystemRunners { get; set; } public static object[] GitRepoTestsValidateWorkTree { get; set; } diff --git a/Scalar.FunctionalTests/Tests/EnlistmentPerFixture/CloneTests.cs b/Scalar.FunctionalTests/Tests/EnlistmentPerFixture/CloneTests.cs index 09591b839b..26e5d44aef 100644 --- a/Scalar.FunctionalTests/Tests/EnlistmentPerFixture/CloneTests.cs +++ b/Scalar.FunctionalTests/Tests/EnlistmentPerFixture/CloneTests.cs @@ -61,8 +61,9 @@ public void SparseCloneWithNoFetchOfCommitsAndTreesSucceeds() public void CloneWithDefaultLocalCacheLocation() { FileSystemRunner fileSystem = FileSystemRunner.DefaultRunner; - string homeDirectory = Environment.GetEnvironmentVariable("HOME"); - homeDirectory.ShouldBeADirectory(fileSystem); + string defaultLocalCacheRoot = ScalarTestConfig.DefaultLocalCacheRoot; + fileSystem.CreateDirectory(defaultLocalCacheRoot); + defaultLocalCacheRoot.ShouldBeADirectory(fileSystem); string newEnlistmentRoot = ScalarFunctionalTestEnlistment.GetUniqueEnlistmentRoot(); @@ -80,8 +81,7 @@ public void CloneWithDefaultLocalCacheLocation() string gitObjectsRoot = ScalarHelpers.GetObjectsRootFromGitConfig(Path.Combine(newEnlistmentRoot, "src")); - string defaultScalarCacheRoot = Path.Combine(homeDirectory, ".scalarCache"); - gitObjectsRoot.StartsWith(defaultScalarCacheRoot, StringComparison.Ordinal).ShouldBeTrue($"Git objects root did not default to using {homeDirectory}"); + gitObjectsRoot.StartsWith(defaultLocalCacheRoot, StringComparison.Ordinal).ShouldBeTrue($"Git objects root did not default to using {defaultLocalCacheRoot}"); RepositoryHelpers.DeleteTestDirectory(newEnlistmentRoot); }