From d72a7859a99ad7ae7409734df9cded178072be38 Mon Sep 17 00:00:00 2001 From: Guy Oliver Date: Thu, 12 Mar 2015 15:39:29 -0400 Subject: [PATCH] combinePaths with no trimming operations Previous versions of combinePath stripped of leading slashes from path2, which caused behaviors to be different between linux and windows. This new version of combinePaths doesn't trim the leading slashes, allowing the underlying Path.Combine to behave the same on both windows and linux. This fixes fsharp/FAKE#670 --- src/app/FakeLib/EnvironmentHelper.fs | 2 + .../Test.FAKECore/EnvironmentHelperSpecs.cs | 51 +++++++++++++++++++ src/test/Test.FAKECore/Test.FAKECore.csproj | 1 + 3 files changed, 54 insertions(+) create mode 100644 src/test/Test.FAKECore/EnvironmentHelperSpecs.cs diff --git a/src/app/FakeLib/EnvironmentHelper.fs b/src/app/FakeLib/EnvironmentHelper.fs index 8c5dcee01f6..d52ee3def69 100644 --- a/src/app/FakeLib/EnvironmentHelper.fs +++ b/src/app/FakeLib/EnvironmentHelper.fs @@ -19,9 +19,11 @@ let environVar name = Environment.GetEnvironmentVariable name /// Combines two path strings using Path.Combine let inline combinePaths path1 (path2 : string) = Path.Combine(path1, path2.TrimStart [| '\\'; '/' |]) +let inline combinePathsNoTrim path1 path2 = Path.Combine(path1, path2) /// Combines two path strings using Path.Combine let inline (@@) path1 path2 = combinePaths path1 path2 +let inline () path1 path2 = combinePathsNoTrim path1 path2 /// Retrieves all environment variables from the given target let environVars target = diff --git a/src/test/Test.FAKECore/EnvironmentHelperSpecs.cs b/src/test/Test.FAKECore/EnvironmentHelperSpecs.cs new file mode 100644 index 00000000000..9547876171a --- /dev/null +++ b/src/test/Test.FAKECore/EnvironmentHelperSpecs.cs @@ -0,0 +1,51 @@ +using System.IO; +using System.Collections.Generic; +using System.Linq; +using Fake; +using Machine.Specifications; + +namespace Test.FAKECore +{ + public class when_combining_paths + { + It should_strip_leading_slashes_when_using_combinePaths = + () => + { + var testValues = new List { + new[]{"/test/path", "/of/the/thing", "/test/path" + Path.DirectorySeparatorChar + "of/the/thing"}, + new[]{"/test/path", "of/the/thing", "/test/path" + Path.DirectorySeparatorChar + "of/the/thing"}, + }; + + if (Path.VolumeSeparatorChar != Path.DirectorySeparatorChar) + { + var path2 = "X" + Path.VolumeSeparatorChar + "/test"; + testValues.Add(new[]{"/test/path", path2, path2}); + } + + foreach (var item in testValues) + { + EnvironmentHelper.combinePaths(item[0], item[1]).ShouldEqual(item[2]); + } + }; + + It should_work_like_path_dot_combine_when_using_combinePathsNoTrim = + () => + { + var testValues = new List { + new[]{"/test/path", "/of/the/thing", "/of/the/thing"}, + new[]{"/test/path", "of/the/thing", "/test/path" + Path.DirectorySeparatorChar + "of/the/thing"}, + }; + + if (Path.VolumeSeparatorChar != Path.DirectorySeparatorChar) + { + var path2 = "X" + Path.VolumeSeparatorChar + "/test"; + testValues.Add(new[]{"/test/path", path2, path2}); + } + + foreach (var item in testValues) + { + EnvironmentHelper.combinePathsNoTrim(item[0], item[1]).ShouldEqual(item[2]); + } + }; + } +} diff --git a/src/test/Test.FAKECore/Test.FAKECore.csproj b/src/test/Test.FAKECore/Test.FAKECore.csproj index 15a94b0f76f..c45fa5f3dc1 100644 --- a/src/test/Test.FAKECore/Test.FAKECore.csproj +++ b/src/test/Test.FAKECore/Test.FAKECore.csproj @@ -108,6 +108,7 @@ +