|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.IO.Compression; |
| 4 | + |
| 5 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 6 | + |
| 7 | +using pyRevitLabs.Common; |
| 8 | +using pyRevitLabs.PyRevit; |
| 9 | + |
| 10 | +namespace pyRevitLabs.UnitTests { |
| 11 | + [TestClass] |
| 12 | + public class CommonUtilsPathTests { |
| 13 | + private string _tempRoot; |
| 14 | + private string _previousPyRevitPathOverride; |
| 15 | + private string _previousTemp; |
| 16 | + private string _previousLocalAppData; |
| 17 | + |
| 18 | + [TestInitialize] |
| 19 | + public void Setup() { |
| 20 | + _tempRoot = Path.Combine(Path.GetTempPath(), "pyRevitPathTests_" + Guid.NewGuid().ToString("N")); |
| 21 | + Directory.CreateDirectory(_tempRoot); |
| 22 | + _previousPyRevitPathOverride = Environment.GetEnvironmentVariable(PyRevitLabsConsts.PyRevitPathOverrideEnvVar); |
| 23 | + _previousTemp = Environment.GetEnvironmentVariable("TEMP"); |
| 24 | + _previousLocalAppData = Environment.GetEnvironmentVariable("LOCALAPPDATA"); |
| 25 | + Environment.SetEnvironmentVariable( |
| 26 | + PyRevitLabsConsts.PyRevitPathOverrideEnvVar, |
| 27 | + Path.Combine(_tempRoot, "AppDataPyRevit")); |
| 28 | + Directory.CreateDirectory(PyRevitLabsConsts.PyRevitPath); |
| 29 | + } |
| 30 | + |
| 31 | + [TestCleanup] |
| 32 | + public void Cleanup() { |
| 33 | + Environment.SetEnvironmentVariable( |
| 34 | + PyRevitLabsConsts.PyRevitPathOverrideEnvVar, |
| 35 | + _previousPyRevitPathOverride); |
| 36 | + Environment.SetEnvironmentVariable("TEMP", _previousTemp); |
| 37 | + Environment.SetEnvironmentVariable("LOCALAPPDATA", _previousLocalAppData); |
| 38 | + if (Directory.Exists(_tempRoot)) |
| 39 | + Directory.Delete(_tempRoot, recursive: true); |
| 40 | + } |
| 41 | + |
| 42 | + [TestMethod] |
| 43 | + public void ExpandEnvironmentPath_ExpandsNestedVariables() { |
| 44 | + Environment.SetEnvironmentVariable("LOCALAPPDATA", _tempRoot); |
| 45 | + var expanded = CommonUtils.ExpandEnvironmentPath("%LOCALAPPDATA%\\pyRevit"); |
| 46 | + Assert.AreEqual(Path.Combine(_tempRoot, "pyRevit"), expanded); |
| 47 | + } |
| 48 | + |
| 49 | + [TestMethod] |
| 50 | + public void GetUserTempDirectory_ExpandsPercentTemp() { |
| 51 | + Environment.SetEnvironmentVariable("LOCALAPPDATA", _tempRoot); |
| 52 | + Environment.SetEnvironmentVariable("TEMP", "%LOCALAPPDATA%\\Temp"); |
| 53 | + var tempDir = CommonUtils.GetUserTempDirectory(); |
| 54 | + Assert.IsFalse(tempDir.Contains("%"), "Temp path should not contain unexpanded variables."); |
| 55 | + StringAssert.StartsWith(tempDir, _tempRoot); |
| 56 | + Assert.IsTrue(tempDir.EndsWith("Temp", StringComparison.OrdinalIgnoreCase)); |
| 57 | + } |
| 58 | + |
| 59 | + [TestMethod] |
| 60 | + public void GetUserTempDirectory_FallsBackToPathGetTempPathWhenUnexpanded() { |
| 61 | + Environment.SetEnvironmentVariable("TEMP", "%NONEXISTENT_PYREVIT_VAR%\\Temp"); |
| 62 | + var tempDir = CommonUtils.GetUserTempDirectory(); |
| 63 | + Assert.IsFalse(tempDir.Contains("%")); |
| 64 | + var expected = Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); |
| 65 | + Assert.AreEqual(expected, tempDir); |
| 66 | + } |
| 67 | + |
| 68 | + [TestMethod] |
| 69 | + public void UserTemp_MatchesGetUserTempDirectory() { |
| 70 | + Environment.SetEnvironmentVariable("LOCALAPPDATA", _tempRoot); |
| 71 | + Environment.SetEnvironmentVariable("TEMP", "%LOCALAPPDATA%\\Temp"); |
| 72 | + Assert.AreEqual(CommonUtils.GetUserTempDirectory(), UserEnv.UserTemp); |
| 73 | + } |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + public void DeployFromImage_UsesExpandedTempForStaging() { |
| 77 | + var localAppData = Path.Combine(_tempRoot, "LocalAppData"); |
| 78 | + Directory.CreateDirectory(localAppData); |
| 79 | + Environment.SetEnvironmentVariable("LOCALAPPDATA", localAppData); |
| 80 | + Environment.SetEnvironmentVariable("TEMP", "%LOCALAPPDATA%\\Temp"); |
| 81 | + |
| 82 | + var zipPath = Path.Combine(_tempRoot, "testclone.zip"); |
| 83 | + var destPath = Path.Combine(_tempRoot, "cloneDest"); |
| 84 | + var cloneName = "TempDeployTest"; |
| 85 | + CreateMinimalCloneZip(zipPath); |
| 86 | + |
| 87 | + try { |
| 88 | + PyRevitClones.DeployFromImage( |
| 89 | + cloneName: cloneName, |
| 90 | + deploymentName: "core", |
| 91 | + branchName: null, |
| 92 | + imagePath: zipPath, |
| 93 | + destPath: destPath, |
| 94 | + installBinaries: false); |
| 95 | + |
| 96 | + Assert.IsTrue(Directory.Exists(Path.Combine(destPath, "bin")), |
| 97 | + "core deployment should copy bin when TEMP is expanded."); |
| 98 | + Assert.IsTrue(Directory.Exists(Path.Combine(destPath, "pyrevitlib", "pyrevit")), |
| 99 | + "core deployment should copy pyrevitlib when TEMP is expanded."); |
| 100 | + |
| 101 | + var expandedTemp = CommonUtils.GetUserTempDirectory(); |
| 102 | + Assert.IsFalse(expandedTemp.Contains("%")); |
| 103 | + Assert.IsTrue(Directory.Exists(expandedTemp), |
| 104 | + "Expanded temp directory should exist after staging."); |
| 105 | + } |
| 106 | + finally { |
| 107 | + try { |
| 108 | + var clone = PyRevitClones.GetRegisteredClone(cloneName); |
| 109 | + PyRevitClones.UnregisterClone(clone); |
| 110 | + } |
| 111 | + catch { |
| 112 | + // clone may not have registered if deploy failed |
| 113 | + } |
| 114 | + if (Directory.Exists(destPath)) |
| 115 | + CommonUtils.DeleteDirectory(destPath); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static void CreateMinimalCloneZip(string zipPath) { |
| 120 | + var root = Path.Combine(Path.GetDirectoryName(zipPath), "zipcontent"); |
| 121 | + if (Directory.Exists(root)) |
| 122 | + Directory.Delete(root, recursive: true); |
| 123 | + Directory.CreateDirectory(Path.Combine(root, "bin")); |
| 124 | + Directory.CreateDirectory(Path.Combine(root, "pyrevitlib", "pyrevit")); |
| 125 | + Directory.CreateDirectory(Path.Combine(root, "site-packages")); |
| 126 | + File.WriteAllText( |
| 127 | + Path.Combine(root, "pyRevitfile"), |
| 128 | + "[deployments]\r\ncore = ['bin', 'pyrevitlib', 'site-packages', 'pyRevitfile']\r\n"); |
| 129 | + if (File.Exists(zipPath)) |
| 130 | + File.Delete(zipPath); |
| 131 | + ZipFile.CreateFromDirectory(root, zipPath); |
| 132 | + Directory.Delete(root, recursive: true); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments