|
4 | 4 | namespace VirtualClient.Contracts
|
5 | 5 | {
|
6 | 6 | using System;
|
| 7 | + using System.Collections; |
| 8 | + using System.Collections.Generic; |
7 | 9 | using System.IO;
|
8 | 10 | using System.Reflection;
|
9 | 11 | using System.Runtime.InteropServices;
|
@@ -246,6 +248,124 @@ public void TheListOfSupportedProcessorArchitecturesMatchesExpected(Architecture
|
246 | 248 | }
|
247 | 249 | }
|
248 | 250 |
|
| 251 | + [Test] |
| 252 | + [Platform(Include ="Win")] |
| 253 | + public void CanResolveRelativePathsCorrectly() |
| 254 | + { |
| 255 | + IDictionary<string, string> paths = new Dictionary<string, string> |
| 256 | + { |
| 257 | + { @"script.exe", @"script.exe" }, |
| 258 | + { @"S:\Path\Contains\None", @"S:\Path\Contains\None" }, |
| 259 | + { @".\packages", Path.GetFullPath(@".\packages") }, |
| 260 | + { @"..\packages", Path.GetFullPath(@"..\packages") }, |
| 261 | + { @"..\..\directory\..\packages", Path.GetFullPath(@"..\..\directory\..\packages") }, |
| 262 | + { @".\scripts\script.exe", Path.GetFullPath(@".\scripts\script.exe") }, |
| 263 | + { @".\scripts\script.exe --any=option --flag", $"{Path.GetFullPath(@".\scripts\script.exe")} --any=option --flag" }, |
| 264 | + { @""".\scripts\script.exe --any=option --flag""", $"\"{Path.GetFullPath(@".\scripts\script.exe")} --any=option --flag\"" }, |
| 265 | + { @""".\scripts\script.exe"" --any=option --flag", $"\"{Path.GetFullPath(@".\scripts\script.exe")}\" --any=option --flag" } |
| 266 | + }; |
| 267 | + |
| 268 | + foreach (var entry in paths) |
| 269 | + { |
| 270 | + string relativePath = entry.Key; |
| 271 | + string expectedPath = entry.Value; |
| 272 | + string actualPath = PlatformSpecifics.ResolveRelativePaths(relativePath); |
| 273 | + |
| 274 | + Assert.AreEqual(expectedPath, actualPath, $"Relative path did not resolve correctly: '{relativePath}'"); |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + [Test] |
| 279 | + [Platform(Include = "Win")] |
| 280 | + public void CanResolveRelativePathsCorrectly_More_Advanced_Cases() |
| 281 | + { |
| 282 | + IDictionary<string, string> paths = new Dictionary<string, string> |
| 283 | + { |
| 284 | + { |
| 285 | + $@"cmd -c "".\scripts\script.exe --any=option --flag""", |
| 286 | + $@"cmd -c ""{Path.GetFullPath(@".\scripts\script.exe")} --any=option --flag""" |
| 287 | + }, |
| 288 | + { |
| 289 | + $@"cmd -c ""'.\scripts\script.exe' --any=option --flag""", |
| 290 | + $@"cmd -c ""'{Path.GetFullPath(@".\scripts\script.exe")}' --any=option --flag""" |
| 291 | + }, |
| 292 | + { |
| 293 | + $@"..\..\any\path\anycommand.exe --this=true --that=123 --log-path=.\another\path.log ..\and\one\other.thing .\", |
| 294 | + $@"{Path.GetFullPath(@"..\..\any\path\anycommand.exe")} --this=true --that=123 --log-path={Path.GetFullPath(@".\another\path.log")} {Path.GetFullPath(@"..\and\one\other.thing")} {Path.GetFullPath(@".\")}" |
| 295 | + }, |
| 296 | + { |
| 297 | + $@"..\..\any\path\anycommand.exe --who=..\..\Any\Path\AnyCommand.exe", |
| 298 | + $@"{Path.GetFullPath(@"..\..\any\path\anycommand.exe")} --who={Path.GetFullPath(@"..\..\Any\Path\AnyCommand.exe")}" |
| 299 | + } |
| 300 | + }; |
| 301 | + |
| 302 | + foreach (var entry in paths) |
| 303 | + { |
| 304 | + string relativePath = entry.Key; |
| 305 | + string expectedPath = entry.Value; |
| 306 | + string actualPath = PlatformSpecifics.ResolveRelativePaths(relativePath); |
| 307 | + |
| 308 | + Assert.AreEqual(expectedPath, actualPath, $"Relative path did not resolve correctly: '{relativePath}'"); |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + [Test] |
| 313 | + [TestCase("anycommand", "anycommand", null)] |
| 314 | + [TestCase("anycommand ", "anycommand", null)] |
| 315 | + [TestCase("./anycommand", "./anycommand", null)] |
| 316 | + [TestCase("./anycommand --argument=value", "./anycommand", "--argument=value")] |
| 317 | + [TestCase("./anycommand --argument=value --argument2 value2", "./anycommand", "--argument=value --argument2 value2")] |
| 318 | + [TestCase("./anycommand --argument=value --argument2 value2 --flag", "./anycommand", "--argument=value --argument2 value2 --flag")] |
| 319 | + [TestCase("./anycommand --argument=value --argument2 value2 --flag ", "./anycommand", "--argument=value --argument2 value2 --flag")] |
| 320 | + [TestCase("../../anycommand --argument=value --argument2 value2 --flag ", "../../anycommand", "--argument=value --argument2 value2 --flag")] |
| 321 | + [TestCase("/home/user/anycommand", "/home/user/anycommand", null)] |
| 322 | + [TestCase("/home/user/anycommand --argument=value --argument2 value2", "/home/user/anycommand", "--argument=value --argument2 value2")] |
| 323 | + [TestCase("\"/home/user/anycommand\"", "\"/home/user/anycommand\"", null)] |
| 324 | + [TestCase("\"/home/user/dir with space/anycommand\" --argument=value --argument2 value2", "\"/home/user/dir with space/anycommand\"", "--argument=value --argument2 value2")] |
| 325 | + [TestCase("sudo anycommand", "sudo", "anycommand")] |
| 326 | + [TestCase("sudo ./anycommand", "sudo", "./anycommand")] |
| 327 | + [TestCase("sudo /home/user/anycommand", "sudo", "/home/user/anycommand")] |
| 328 | + [TestCase("sudo /home/user/anycommand --argument=value --argument2 value2", "sudo", "/home/user/anycommand --argument=value --argument2 value2")] |
| 329 | + [TestCase("sudo \"/home/user/dir with space/anycommand\"", "sudo", "\"/home/user/dir with space/anycommand\"")] |
| 330 | + [TestCase("sudo \"/home/user/dir with space/anycommand\" --argument=value --argument2 value2", "sudo", "\"/home/user/dir with space/anycommand\" --argument=value --argument2 value2")] |
| 331 | + public void CorrectlyIdentifiesThePartsOfTheCommandOnUnixSystems(string fullCommand, string expectedCommand, string expectedCommandArguments) |
| 332 | + { |
| 333 | + Assert.IsTrue(PlatformSpecifics.TryGetCommandParts(fullCommand, out string actualCommand, out string actualCommandArguments)); |
| 334 | + Assert.AreEqual(expectedCommand, actualCommand); |
| 335 | + Assert.AreEqual(expectedCommandArguments, actualCommandArguments); |
| 336 | + } |
| 337 | + |
| 338 | + [Test] |
| 339 | + [TestCase("bash -c \"/home/user/anyscript.sh\"", "bash", "-c \"/home/user/anyscript.sh\"")] |
| 340 | + [TestCase("bash -c \"/home/user/anyscript.sh --argument=value --argument2 value2\"", "bash", "-c \"/home/user/anyscript.sh --argument=value --argument2 value2\"")] |
| 341 | + [TestCase("bash -c \"/home/dir with space/anyscript.sh\"", "bash", "-c \"/home/dir with space/anyscript.sh\"")] |
| 342 | + [TestCase("sudo bash -c \"/home/user/anyscript.sh --argument=value --argument2 value2\"", "sudo", "bash -c \"/home/user/anyscript.sh --argument=value --argument2 value2\"")] |
| 343 | + public void CorrectlyIdentifiesThePartsOfBashCommandsOnUnixSystems(string fullCommand, string expectedCommand, string expectedCommandArguments) |
| 344 | + { |
| 345 | + Assert.IsTrue(PlatformSpecifics.TryGetCommandParts(fullCommand, out string actualCommand, out string actualCommandArguments)); |
| 346 | + Assert.AreEqual(expectedCommand, actualCommand); |
| 347 | + Assert.AreEqual(expectedCommandArguments, actualCommandArguments); |
| 348 | + } |
| 349 | + |
| 350 | + [Test] |
| 351 | + [TestCase("anycommand.exe", "anycommand.exe", null)] |
| 352 | + [TestCase("anycommand.exe ", "anycommand.exe", null)] |
| 353 | + [TestCase(".\\anycommand.exe", ".\\anycommand.exe", null)] |
| 354 | + [TestCase(".\\anycommand.exe --argument=value --argument2 value2", ".\\anycommand.exe", "--argument=value --argument2 value2")] |
| 355 | + [TestCase(".\\anycommand.exe --argument=value --argument2 value2 --flag", ".\\anycommand.exe", "--argument=value --argument2 value2 --flag")] |
| 356 | + [TestCase(".\\anycommand.exe --argument=value --argument2 value2 --flag ", ".\\anycommand.exe", "--argument=value --argument2 value2 --flag")] |
| 357 | + [TestCase(".\\anycommand.exe --argument=value", ".\\anycommand.exe", "--argument=value")] |
| 358 | + [TestCase("..\\..\\anycommand.exe --argument=value", "..\\..\\anycommand.exe", "--argument=value")] |
| 359 | + [TestCase("C:\\Users\\User\\anycommand.exe", "C:\\Users\\User\\anycommand.exe", null)] |
| 360 | + [TestCase("C:\\Users\\User\\anycommand.exe --argument=value --argument2 value2", "C:\\Users\\User\\anycommand.exe", "--argument=value --argument2 value2")] |
| 361 | + [TestCase("\"C:\\Users\\User\\Dir With Space\\anycommand.exe\"--argument=value --argument2 value2", "\"C:\\Users\\User\\Dir With Space\\anycommand.exe\"", "--argument=value --argument2 value2")] |
| 362 | + public void CorrectlyIdentifiesThePartsOfTheCommandOnWindowsSystems(string fullCommand, string expectedCommand, string expectedCommandArguments) |
| 363 | + { |
| 364 | + Assert.IsTrue(PlatformSpecifics.TryGetCommandParts(fullCommand, out string actualCommand, out string actualCommandArguments)); |
| 365 | + Assert.AreEqual(expectedCommand, actualCommand); |
| 366 | + Assert.AreEqual(expectedCommandArguments, actualCommandArguments); |
| 367 | + } |
| 368 | + |
249 | 369 | [Test]
|
250 | 370 | [TestCase(@"\packages", @"\packages")]
|
251 | 371 | [TestCase(@"/packages", @"\packages")]
|
|
0 commit comments