summaryrefslogtreecommitdiff
path: root/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-09-05 13:37:40 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-09-05 13:37:40 -0400
commit5d1c77884f6686a59f121639dc177b7095e8c477 (patch)
tree538d405cd157e05c9557adf83acd49d5b52e9cff /src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs
parent9ff6126a662791f511829ca7df0cc0cd96700761 (diff)
downloadSMAPI-5d1c77884f6686a59f121639dc177b7095e8c477.tar.gz
SMAPI-5d1c77884f6686a59f121639dc177b7095e8c477.tar.bz2
SMAPI-5d1c77884f6686a59f121639dc177b7095e8c477.zip
add unit tests for PathUtilities, fix some edge cases
Diffstat (limited to 'src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs')
-rw-r--r--src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs236
1 files changed, 236 insertions, 0 deletions
diff --git a/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs b/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs
new file mode 100644
index 00000000..1d6c371d
--- /dev/null
+++ b/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs
@@ -0,0 +1,236 @@
+using NUnit.Framework;
+using StardewModdingAPI.Toolkit.Utilities;
+
+namespace SMAPI.Tests.Utilities
+{
+ /// <summary>Unit tests for <see cref="PathUtilities"/>.</summary>
+ [TestFixture]
+ internal class PathUtilitiesTests
+ {
+ /*********
+ ** Sample data
+ *********/
+ /// <summary>Sample paths used in unit tests.</summary>
+ public static readonly SamplePath[] SamplePaths = {
+ // Windows absolute path
+ new SamplePath
+ {
+ OriginalPath = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley",
+
+ Segments = new[] { "C:", "Program Files (x86)", "Steam", "steamapps", "common", "Stardew Valley" },
+ SegmentsLimit3 = new [] { "C:", "Program Files (x86)", @"Steam\steamapps\common\Stardew Valley" },
+
+ NormalizedOnWindows = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley",
+ NormalizedOnUnix = @"C:/Program Files (x86)/Steam/steamapps/common/Stardew Valley"
+ },
+
+ // Windows relative path
+ new SamplePath
+ {
+ OriginalPath = @"Content\Characters\Dialogue\Abigail",
+
+ Segments = new [] { "Content", "Characters", "Dialogue", "Abigail" },
+ SegmentsLimit3 = new [] { "Content", "Characters", @"Dialogue\Abigail" },
+
+ NormalizedOnWindows = @"Content\Characters\Dialogue\Abigail",
+ NormalizedOnUnix = @"Content/Characters/Dialogue/Abigail"
+ },
+
+ // Windows relative path (with directory climbing)
+ new SamplePath
+ {
+ OriginalPath = @"..\..\Content",
+
+ Segments = new [] { "..", "..", "Content" },
+ SegmentsLimit3 = new [] { "..", "..", "Content" },
+
+ NormalizedOnWindows = @"..\..\Content",
+ NormalizedOnUnix = @"../../Content"
+ },
+
+ // Windows UNC path
+ new SamplePath
+ {
+ OriginalPath = @"\\unc\path",
+
+ Segments = new [] { "unc", "path" },
+ SegmentsLimit3 = new [] { "unc", "path" },
+
+ NormalizedOnWindows = @"\\unc\path",
+ NormalizedOnUnix = "/unc/path" // there's no good way to normalize this on Unix since UNC paths aren't supported; path normalization is meant for asset names anyway, so this test only ensures it returns some sort of sane value
+ },
+
+ // Linux absolute path
+ new SamplePath
+ {
+ OriginalPath = @"/home/.steam/steam/steamapps/common/Stardew Valley",
+
+ Segments = new [] { "home", ".steam", "steam", "steamapps", "common", "Stardew Valley" },
+ SegmentsLimit3 = new [] { "home", ".steam", "steam/steamapps/common/Stardew Valley" },
+
+ NormalizedOnWindows = @"home\.steam\steam\steamapps\common\Stardew Valley",
+ NormalizedOnUnix = @"/home/.steam/steam/steamapps/common/Stardew Valley"
+ },
+
+ // Linux absolute path (with ~)
+ new SamplePath
+ {
+ OriginalPath = @"~/.steam/steam/steamapps/common/Stardew Valley",
+
+ Segments = new [] { "~", ".steam", "steam", "steamapps", "common", "Stardew Valley" },
+ SegmentsLimit3 = new [] { "~", ".steam", "steam/steamapps/common/Stardew Valley" },
+
+ NormalizedOnWindows = @"~\.steam\steam\steamapps\common\Stardew Valley",
+ NormalizedOnUnix = @"~/.steam/steam/steamapps/common/Stardew Valley"
+ },
+
+ // Linux relative path
+ new SamplePath
+ {
+ OriginalPath = @"Content/Characters/Dialogue/Abigail",
+
+ Segments = new [] { "Content", "Characters", "Dialogue", "Abigail" },
+ SegmentsLimit3 = new [] { "Content", "Characters", "Dialogue/Abigail" },
+
+ NormalizedOnWindows = @"Content\Characters\Dialogue\Abigail",
+ NormalizedOnUnix = @"Content/Characters/Dialogue/Abigail"
+ },
+
+ // Linux relative path (with directory climbing)
+ new SamplePath
+ {
+ OriginalPath = @"../../Content",
+
+ Segments = new [] { "..", "..", "Content" },
+ SegmentsLimit3 = new [] { "..", "..", "Content" },
+
+ NormalizedOnWindows = @"..\..\Content",
+ NormalizedOnUnix = @"../../Content"
+ },
+
+ // Mixed directory separators
+ new SamplePath
+ {
+ OriginalPath = @"C:\some/mixed\path/separators",
+
+ Segments = new [] { "C:", "some", "mixed", "path", "separators" },
+ SegmentsLimit3 = new [] { "C:", "some", @"mixed\path/separators" },
+
+ NormalizedOnWindows = @"C:\some\mixed\path\separators",
+ NormalizedOnUnix = @"C:/some/mixed/path/separators"
+ },
+ };
+
+
+ /*********
+ ** Unit tests
+ *********/
+ /****
+ ** GetSegments
+ ****/
+ [Test(Description = "Assert that PathUtilities.GetSegments splits paths correctly.")]
+ [TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))]
+ public void GetSegments(SamplePath path)
+ {
+ // act
+ string[] segments = PathUtilities.GetSegments(path.OriginalPath);
+
+ // assert
+ Assert.AreEqual(path.Segments, segments);
+ }
+
+ [Test(Description = "Assert that PathUtilities.GetSegments splits paths correctly when given a limit.")]
+ [TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))]
+ public void GetSegments_WithLimit(SamplePath path)
+ {
+ // act
+ string[] segments = PathUtilities.GetSegments(path.OriginalPath, 3);
+
+ // assert
+ Assert.AreEqual(path.SegmentsLimit3, segments);
+ }
+
+ /****
+ ** NormalizePathSeparators
+ ****/
+ [Test(Description = "Assert that PathUtilities.NormalizePathSeparators normalizes paths correctly.")]
+ [TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))]
+ public void NormalizePathSeparators(SamplePath path)
+ {
+ // act
+ string normalized = PathUtilities.NormalizePathSeparators(path.OriginalPath);
+
+ // assert
+#if SMAPI_FOR_WINDOWS
+ Assert.AreEqual(path.NormalizedOnWindows, normalized);
+#else
+ Assert.AreEqual(path.NormalizedOnUnix, normalized);
+#endif
+ }
+
+ /****
+ ** GetRelativePath
+ ****/
+ [Test(Description = "Assert that PathUtilities.GetRelativePath returns the expected values.")]
+#if SMAPI_FOR_WINDOWS
+ [TestCase(
+ @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley",
+ @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\Automate",
+ ExpectedResult = @"Mods\Automate"
+ )]
+ [TestCase(
+ @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\Automate",
+ @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Content",
+ ExpectedResult = @"..\..\Content"
+ )]
+ [TestCase(
+ @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\Automate",
+ @"D:\another-drive",
+ ExpectedResult = @"D:\another-drive"
+ )]
+ [TestCase(
+ @"\\parent\unc",
+ @"\\parent\unc\path\to\child",
+ ExpectedResult = @"path\to\child"
+ )]
+ [TestCase(
+ @"\\parent\unc",
+ @"\\adjacent\unc",
+ ExpectedResult = @"\\adjacent\unc"
+ )]
+ [TestCase(
+ @"C:\parent",
+ @"C:\PARENT\child",
+ ExpectedResult = @"child" // note: incorrect on Linux and sometimes MacOS, but not worth the complexity of detecting whether the filesystem is case-sensitive for SMAPI's purposes
+ )]
+#else
+ [TestCase(
+ @"~/.steam/steam/steamapps/common/Stardew Valley",
+ @"~/.steam/steam/steamapps/common/Stardew Valley/Mods/Automate",
+ ExpectedResult = @"Mods\Automate"
+ )]
+#endif
+ public string GetRelativePath(string sourceDir, string targetPath)
+ {
+ return PathUtilities.GetRelativePath(sourceDir, targetPath);
+ }
+
+
+ /*********
+ ** Private classes
+ *********/
+ public class SamplePath
+ {
+ public string OriginalPath { get; set; }
+ public string[] Segments { get; set; }
+ public string[] SegmentsLimit3 { get; set; }
+ public string NormalizedOnWindows { get; set; }
+ public string NormalizedOnUnix { get; set; }
+
+ public override string ToString()
+ {
+ return this.OriginalPath;
+ }
+ }
+ }
+}