Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/4442 directory and combinedfilepath #4455

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Cake.Common.Tests/Unit/IO/Paths/ConvertableFilePathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -70,6 +74,65 @@ 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<ArgumentNullException>(() => (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<ArgumentNullException>(() => (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<Core.ICakeContext>();

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
Expand Down
21 changes: 21 additions & 0 deletions src/Cake.Common/IO/Paths/ConvertableFilePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,26 @@ public override string ToString()
{
return Path.FullPath;
}

/// <summary>
/// Combines the directory path and file path with the separator.
/// </summary>
/// <param name="dir">DirectoryPath.</param>
/// <param name="file">ConvertableFilePath.</param>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
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);
}
}
}