Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ public void Should_Quote_Root_Path(string text, string expected)
Assert.Equal(expected, result.Args);
}

[Fact]
public void Should_Resolve_Restore_Paths_Relative_To_WorkingDirectory()
{
// Given
var fixture = new DotNetRestorerFixture();
fixture.Settings.WorkingDirectory = "./source/MyProject";
fixture.Settings.PackagesDirectory = "./packages/";
fixture.Settings.ConfigFile = "./NuGet.config";
fixture.Settings.LockFilePath = "./obj/packages.lock.json";

// When
var result = fixture.Run();

// Then
Assert.Equal("restore --packages \"/Working/source/MyProject/packages\" --configfile \"/Working/source/MyProject/NuGet.config\" --lock-file-path \"/Working/source/MyProject/obj/packages.lock.json\"", result.Args);
}

[Fact]
public void Should_Add_Settings()
{
Expand Down
41 changes: 41 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,46 @@ private ProcessArgumentBuilder AppendCommonArguments(ProcessArgumentBuilder buil

return builder;
}
/// <summary>
/// Resolves a relative directory path against <see cref="ToolSettings.WorkingDirectory"/> when set.
/// </summary>
protected static DirectoryPath GetAbsoluteDirectoryPath(DirectoryPath path, DotNetSettings settings, ICakeEnvironment environment)
{
ArgumentNullException.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(settings);
ArgumentNullException.ThrowIfNull(environment);

if (settings.WorkingDirectory != null && path.IsRelative)
{
return settings.WorkingDirectory.MakeAbsolute(environment).Combine(path);
}

return path;
}

/// <summary>
/// Resolves a relative file path against <see cref="ToolSettings.WorkingDirectory"/> when set.
/// </summary>
protected static FilePath GetAbsoluteFilePath(FilePath path, DotNetSettings settings, ICakeEnvironment environment)
{
ArgumentNullException.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(settings);
ArgumentNullException.ThrowIfNull(environment);

if (settings.WorkingDirectory != null && path.IsRelative)
{
return settings.WorkingDirectory.MakeAbsolute(environment).CombineWithFilePath(path);
}

return path;
}

/// <summary>
/// Resolves a relative output directory against <see cref="ToolSettings.WorkingDirectory"/> when set.
/// </summary>
protected static DirectoryPath GetAbsoluteOutputDirectory(DirectoryPath outputDirectory, DotNetSettings settings, ICakeEnvironment environment)
{
return GetAbsoluteDirectoryPath(outputDirectory, settings, environment);
}
}
}
9 changes: 6 additions & 3 deletions src/Cake.Common/Tools/DotNet/Restore/DotNetRestorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ private ProcessArgumentBuilder GetArguments(string root, DotNetRestoreSettings s
if (settings.PackagesDirectory != null)
{
builder.Append("--packages");
builder.AppendQuoted(settings.PackagesDirectory.MakeAbsolute(_environment).FullPath);
var packagesDirectory = GetAbsoluteDirectoryPath(settings.PackagesDirectory, settings, _environment);
builder.AppendQuoted(packagesDirectory.MakeAbsolute(_environment).FullPath);
}

// Sources
Expand All @@ -88,7 +89,8 @@ private ProcessArgumentBuilder GetArguments(string root, DotNetRestoreSettings s
if (settings.ConfigFile != null)
{
builder.Append("--configfile");
builder.AppendQuoted(settings.ConfigFile.MakeAbsolute(_environment).FullPath);
var configFile = GetAbsoluteFilePath(settings.ConfigFile, settings, _environment);
builder.AppendQuoted(configFile.MakeAbsolute(_environment).FullPath);
}

// Ignore failed sources
Expand Down Expand Up @@ -142,7 +144,8 @@ private ProcessArgumentBuilder GetArguments(string root, DotNetRestoreSettings s
// Lock file path
if (settings.LockFilePath != null)
{
builder.AppendSwitchQuoted("--lock-file-path", " ", settings.LockFilePath.MakeAbsolute(_environment).FullPath);
var lockFilePath = GetAbsoluteFilePath(settings.LockFilePath, settings, _environment);
builder.AppendSwitchQuoted("--lock-file-path", " ", lockFilePath.MakeAbsolute(_environment).FullPath);
}

// Force Evaluate
Expand Down