From d30af88ecf7b841105365783272a522239cccb12 Mon Sep 17 00:00:00 2001 From: Sarita Nair Date: Mon, 27 Jan 2025 00:25:28 +1100 Subject: [PATCH 1/4] Created the overload of operator + that accepts the DirectoryPath and ConvertableFilePath objects. This method has a logic to null check directory path and file parameters. It concates the full path of the directory + separator + file. Separator can vary based on the operating system and this code is compatible with every operating system. --- .../IO/Paths/ConvertableFilePath.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Cake.Common/IO/Paths/ConvertableFilePath.cs b/src/Cake.Common/IO/Paths/ConvertableFilePath.cs index e954e642f1..0841d165b7 100644 --- a/src/Cake.Common/IO/Paths/ConvertableFilePath.cs +++ b/src/Cake.Common/IO/Paths/ConvertableFilePath.cs @@ -61,5 +61,26 @@ public override string ToString() { return Path.FullPath; } + + /// + /// Combines the directory path and file path with the separator. + /// + /// DirectoryPath + /// ConvertableFilePath + /// + /// A that represents this instance. + /// + public static string operator +(DirectoryPath dir, ConvertableFilePath file) + { + if (dir == null) + { + throw new ArgumentNullException(nameof(dir)); + } + if (file == null) + { + throw new ArgumentNullException(nameof(file)); + } + return string.Concat(dir.FullPath, dir.Separator.ToString(), file); + } } } \ No newline at end of file From 73df7188d54772dfbce0a894f90d97557c3be924 Mon Sep 17 00:00:00 2001 From: Sarita Nair Date: Mon, 27 Jan 2025 01:21:42 +1100 Subject: [PATCH 2/4] Added the unit test ConvertToDirectoryPath --- .../Unit/IO/Paths/ConvertableFilePathTests.cs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/Cake.Common.Tests/Unit/IO/Paths/ConvertableFilePathTests.cs b/src/Cake.Common.Tests/Unit/IO/Paths/ConvertableFilePathTests.cs index aa67ad0c2f..fd8a00ee4d 100644 --- a/src/Cake.Common.Tests/Unit/IO/Paths/ConvertableFilePathTests.cs +++ b/src/Cake.Common.Tests/Unit/IO/Paths/ConvertableFilePathTests.cs @@ -2,10 +2,14 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Diagnostics.CodeAnalysis; +using System.IO; using Cake.Common.IO.Paths; using Cake.Core.IO; +using NSubstitute; using Xunit; +using Xunit.v3; namespace Cake.Common.Tests.Unit.IO.Paths { @@ -70,6 +74,73 @@ public void Should_Return_Null_If_Convertable_Directory_Path_Is_Null() // Then Assert.Null(result); } + + [Fact] + [SuppressMessage("ReSharper", "ExpressionIsAlwaysNull")] + public void Should_Return_Null_Exception_If_Convertable_Directory_Path_Is_Null() + { + // Given + DirectoryPath dirPath = null; + + // When + var filePath = new ConvertableFilePath("file.txt"); + + + // Then + var ex = Assert.Throws(() => (dirPath + filePath).ToString()); + + } + + [Fact] + [SuppressMessage("ReSharper", "ExpressionIsAlwaysNull")] + public void Should_Return_Null_Exception_If_Convertable_FilePath_Is_Null() + { + // Given + DirectoryPath dirPath = new DirectoryPath("X"); + + // When + ConvertableFilePath filePath = null; + + + // Then + var ex = Assert.Throws(() => (dirPath + filePath).ToString()); + + } + + [Fact] + [SuppressMessage("ReSharper", "ExpressionIsAlwaysNull")] + public void Should_Return_Combined_Directorypath_FilePath_Including_Separator() + { + // Given + DirectoryPath dirPath = new DirectoryPath("X"); + var filePath = new ConvertableFilePath("file.txt"); + + // When + var result = (dirPath + filePath).ToString(); + + // Then + Assert.Equal("X/file.txt", result); + + } + + [Fact] + [SuppressMessage("ReSharper", "ExpressionIsAlwaysNull")] + public void Should_Return_Combined_RootPath_Directorypath_FilePath_Including_Separator() + { + // Given + var contextcake = Substitute.For(); + + DirectoryPath dirPath = new ConvertableDirectoryPath(new DirectoryPath("X")); + var filePath = new ConvertableFilePath(new FilePath("file.txt")); + var rootdir = new ConvertableDirectoryPath(new DirectoryPath("..")); + + // When + var result = (rootdir + dirPath + filePath).ToString(); + + // Then + Assert.Equal("../X/file.txt", result); + + } } public sealed class ConvertToString From c50fb4e3d9934a593a9d21a4650170c714f007ad Mon Sep 17 00:00:00 2001 From: Sarita Nair Date: Tue, 4 Feb 2025 19:22:44 +1100 Subject: [PATCH 3/4] added period in the comments for parameters --- src/Cake.Common/IO/Paths/ConvertableFilePath.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cake.Common/IO/Paths/ConvertableFilePath.cs b/src/Cake.Common/IO/Paths/ConvertableFilePath.cs index 0841d165b7..0d1dff5bf4 100644 --- a/src/Cake.Common/IO/Paths/ConvertableFilePath.cs +++ b/src/Cake.Common/IO/Paths/ConvertableFilePath.cs @@ -65,8 +65,8 @@ public override string ToString() /// /// Combines the directory path and file path with the separator. /// - /// DirectoryPath - /// ConvertableFilePath + /// DirectoryPath. + /// ConvertableFilePath. /// /// A that represents this instance. /// From 72d9a7c9ce761674ceee67c4e72567d9635c8a3d Mon Sep 17 00:00:00 2001 From: Sarita Nair Date: Tue, 4 Feb 2025 19:34:13 +1100 Subject: [PATCH 4/4] removed the unwanted blank spaces --- .../Unit/IO/Paths/ConvertableFilePathTests.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Cake.Common.Tests/Unit/IO/Paths/ConvertableFilePathTests.cs b/src/Cake.Common.Tests/Unit/IO/Paths/ConvertableFilePathTests.cs index fd8a00ee4d..2fc6f7562b 100644 --- a/src/Cake.Common.Tests/Unit/IO/Paths/ConvertableFilePathTests.cs +++ b/src/Cake.Common.Tests/Unit/IO/Paths/ConvertableFilePathTests.cs @@ -84,11 +84,8 @@ public void Should_Return_Null_Exception_If_Convertable_Directory_Path_Is_Null() // When var filePath = new ConvertableFilePath("file.txt"); - - // Then var ex = Assert.Throws(() => (dirPath + filePath).ToString()); - } [Fact] @@ -100,11 +97,8 @@ public void Should_Return_Null_Exception_If_Convertable_FilePath_Is_Null() // When ConvertableFilePath filePath = null; - - // Then var ex = Assert.Throws(() => (dirPath + filePath).ToString()); - } [Fact] @@ -120,7 +114,6 @@ public void Should_Return_Combined_Directorypath_FilePath_Including_Separator() // Then Assert.Equal("X/file.txt", result); - } [Fact] @@ -139,7 +132,6 @@ public void Should_Return_Combined_RootPath_Directorypath_FilePath_Including_Sep // Then Assert.Equal("../X/file.txt", result); - } }