Skip to content

Commit 1459206

Browse files
committed
Ensure that log, package, state and temp directory relative paths are fully expanded.
1 parent 42ce8f7 commit 1459206

4 files changed

Lines changed: 113 additions & 7 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.26
1+
2.1.27

src/VirtualClient/VirtualClient.Main/ExecuteProfileCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ private void LogContextToConsole(IServiceCollection dependencies)
827827
ConsoleLogger.Default.LogMessage($"Log Directory: {platformSpecifics.LogsDirectory}", telemetryContext);
828828
ConsoleLogger.Default.LogMessage($"Package Directory: {platformSpecifics.PackagesDirectory}", telemetryContext);
829829
ConsoleLogger.Default.LogMessage($"State Directory: {platformSpecifics.StateDirectory}", telemetryContext);
830+
ConsoleLogger.Default.LogMessage($"Temp Directory: {platformSpecifics.TempDirectory}", telemetryContext);
830831

831832
if (!string.IsNullOrWhiteSpace(this.LayoutPath))
832833
{

src/VirtualClient/VirtualClient.Main/OptionFactory.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,13 @@ public static Option CreateLogDirectoryOption(bool required = true, object defau
617617
AllowMultipleArgumentsPerToken = false
618618
};
619619

620-
OptionFactory.SetOptionRequirements(option, required, defaultValue);
620+
string defaultPath = defaultValue?.ToString();
621+
if (!string.IsNullOrWhiteSpace(defaultPath))
622+
{
623+
defaultPath = OptionFactory.ToFullPath(defaultPath);
624+
}
625+
626+
OptionFactory.SetOptionRequirements(option, required, defaultPath);
621627

622628
return option;
623629
}
@@ -849,7 +855,13 @@ public static Option CreatePackageDirectoryOption(bool required = true, object d
849855
AllowMultipleArgumentsPerToken = false
850856
};
851857

852-
OptionFactory.SetOptionRequirements(option, required, defaultValue);
858+
string defaultPath = defaultValue?.ToString();
859+
if (!string.IsNullOrWhiteSpace(defaultPath))
860+
{
861+
defaultPath = OptionFactory.ToFullPath(defaultPath);
862+
}
863+
864+
OptionFactory.SetOptionRequirements(option, required, defaultPath);
853865

854866
return option;
855867
}
@@ -1126,7 +1138,13 @@ public static Option CreateStateDirectoryOption(bool required = true, object def
11261138
AllowMultipleArgumentsPerToken = false
11271139
};
11281140

1129-
OptionFactory.SetOptionRequirements(option, required, defaultValue);
1141+
string defaultPath = defaultValue?.ToString();
1142+
if (!string.IsNullOrWhiteSpace(defaultPath))
1143+
{
1144+
defaultPath = OptionFactory.ToFullPath(defaultPath);
1145+
}
1146+
1147+
OptionFactory.SetOptionRequirements(option, required, defaultPath);
11301148

11311149
return option;
11321150
}
@@ -1168,7 +1186,13 @@ public static Option CreateTargetDirectoryOption(bool required = false, object d
11681186
AllowMultipleArgumentsPerToken = false
11691187
};
11701188

1171-
OptionFactory.SetOptionRequirements(option, required, defaultValue);
1189+
string defaultPath = defaultValue?.ToString();
1190+
if (!string.IsNullOrWhiteSpace(defaultPath))
1191+
{
1192+
defaultPath = OptionFactory.ToFullPath(defaultPath);
1193+
}
1194+
1195+
OptionFactory.SetOptionRequirements(option, required, defaultPath);
11721196

11731197
return option;
11741198
}
@@ -1222,7 +1246,13 @@ public static Option CreateTempDirectoryOption(bool required = true, object defa
12221246
AllowMultipleArgumentsPerToken = false
12231247
};
12241248

1225-
OptionFactory.SetOptionRequirements(option, required, defaultValue);
1249+
string defaultPath = defaultValue?.ToString();
1250+
if (!string.IsNullOrWhiteSpace(defaultPath))
1251+
{
1252+
defaultPath = OptionFactory.ToFullPath(defaultPath);
1253+
}
1254+
1255+
OptionFactory.SetOptionRequirements(option, required, defaultPath);
12261256

12271257
return option;
12281258
}
@@ -1635,7 +1665,7 @@ private static string ToFullPath(string path)
16351665
{
16361666
if (!Path.IsPathRooted(path))
16371667
{
1638-
// Relative path
1668+
// Convert relative path to full path.
16391669
fullPath = Path.GetFullPath(path);
16401670
}
16411671
}

src/VirtualClient/VirtualClient.UnitTests/OptionFactoryTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,21 @@ public void LogDirectoryOptionSupportsRelativePaths(string path)
605605
Assert.AreEqual(expectedPath, actualPath);
606606
}
607607

608+
[Test]
609+
[TestCase(".\\Any\\Directory\\Path")]
610+
[TestCase("..\\Any\\Directory\\Path")]
611+
[TestCase("..\\..\\Any\\Directory\\Path")]
612+
public void LogDirectoryOptionSupportsRelativePathsInDefaultValues(string path)
613+
{
614+
Option option = OptionFactory.CreateLogDirectoryOption(defaultValue: path);
615+
ParseResult result = option.Parse($"--profile=ANY-PROFILE.json");
616+
617+
string expectedPath = Path.GetFullPath(path);
618+
string actualPath = result.ValueForOption("--log-dir")?.ToString();
619+
620+
Assert.AreEqual(expectedPath, actualPath);
621+
}
622+
608623
[Test]
609624
[TestCase("--log-level")]
610625
[TestCase("--ll")]
@@ -813,6 +828,21 @@ public void PackageDirectoryOptionSupportsRelativePaths(string path)
813828
Assert.AreEqual(expectedPath, actualPath);
814829
}
815830

831+
[Test]
832+
[TestCase(".\\Any\\Directory\\Path")]
833+
[TestCase("..\\Any\\Directory\\Path")]
834+
[TestCase("..\\..\\Any\\Directory\\Path")]
835+
public void PackageDirectoryOptionSupportsRelativePathsInDefaultValues(string path)
836+
{
837+
Option option = OptionFactory.CreatePackageDirectoryOption(defaultValue: path);
838+
ParseResult result = option.Parse($"--profile=ANY-PROFILE.json");
839+
840+
string expectedPath = Path.GetFullPath(path);
841+
string actualPath = result.ValueForOption("--package-dir")?.ToString();
842+
843+
Assert.AreEqual(expectedPath, actualPath);
844+
}
845+
816846
[Test]
817847
[TestCase("--package-store")]
818848
[TestCase("--packageStore")]
@@ -1184,6 +1214,21 @@ public void StateDirectoryOptionSupportsRelativePaths(string path)
11841214
Assert.AreEqual(expectedPath, actualPath);
11851215
}
11861216

1217+
[Test]
1218+
[TestCase(".\\Any\\Directory\\Path")]
1219+
[TestCase("..\\Any\\Directory\\Path")]
1220+
[TestCase("..\\..\\Any\\Directory\\Path")]
1221+
public void StateDirectoryOptionSupportsRelativePathsInDefaultValues(string path)
1222+
{
1223+
Option option = OptionFactory.CreateStateDirectoryOption(defaultValue: path);
1224+
ParseResult result = option.Parse($"--profile=ANY-PROFILE.json");
1225+
1226+
string expectedPath = Path.GetFullPath(path);
1227+
string actualPath = result.ValueForOption("--state-dir")?.ToString();
1228+
1229+
Assert.AreEqual(expectedPath, actualPath);
1230+
}
1231+
11871232
[Test]
11881233
[TestCase("--system")]
11891234
[TestCase("--s")]
@@ -1234,6 +1279,21 @@ public void TargetDirectoryOptionSupportsRelativePaths(string path)
12341279
Assert.AreEqual(expectedPath, actualPath);
12351280
}
12361281

1282+
[Test]
1283+
[TestCase(".\\Any\\Directory\\Path")]
1284+
[TestCase("..\\Any\\Directory\\Path")]
1285+
[TestCase("..\\..\\Any\\Directory\\Path")]
1286+
public void TargetDirectoryOptionSupportsRelativePathsInDefaultValues(string path)
1287+
{
1288+
Option option = OptionFactory.CreateTargetDirectoryOption(defaultValue: path);
1289+
ParseResult result = option.Parse($"--profile=ANY-PROFILE.json");
1290+
1291+
string expectedPath = Path.GetFullPath(path);
1292+
string actualPath = result.ValueForOption("--directory")?.ToString();
1293+
1294+
Assert.AreEqual(expectedPath, actualPath);
1295+
}
1296+
12371297
[Test]
12381298
[TestCase("--files")]
12391299
public void TargetFilesOptionSupportsExpectedAliases(string alias)
@@ -1347,6 +1407,21 @@ public void TempDirectoryOptionSupportsRelativePaths(string path)
13471407
Assert.AreEqual(expectedPath, actualPath);
13481408
}
13491409

1410+
[Test]
1411+
[TestCase(".\\Any\\Directory\\Path")]
1412+
[TestCase("..\\Any\\Directory\\Path")]
1413+
[TestCase("..\\..\\Any\\Directory\\Path")]
1414+
public void TempDirectoryOptionSupportsRelativePathsInDefaultValues(string path)
1415+
{
1416+
Option option = OptionFactory.CreateTempDirectoryOption(defaultValue: path);
1417+
ParseResult result = option.Parse($"--profile=ANY-PROFILE.json");
1418+
1419+
string expectedPath = Path.GetFullPath(path);
1420+
string actualPath = result.ValueForOption("--temp-dir")?.ToString();
1421+
1422+
Assert.AreEqual(expectedPath, actualPath);
1423+
}
1424+
13501425
[Test]
13511426
[TestCase("--timeout")]
13521427
[TestCase("--t")]

0 commit comments

Comments
 (0)