-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supports downloading multiple files with curl
- Loading branch information
1 parent
360e0ee
commit 9f07228
Showing
6 changed files
with
463 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
using System; | ||
using Cake.Core; | ||
using Cake.Core.IO; | ||
using Cake.Curl.Tests.Fixtures; | ||
using Cake.Testing; | ||
using Xunit; | ||
|
||
namespace Cake.Curl.Tests | ||
{ | ||
public sealed class CurlDownloadMultipleFilesTests | ||
{ | ||
public sealed class TheExecutable | ||
{ | ||
[Fact] | ||
public void Should_Throw_If_Hosts_Is_Null() | ||
{ | ||
// Given | ||
var fixture = new CurlDownloadMultipleFilesFixture(); | ||
fixture.Hosts = null; | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
Assert.IsType<ArgumentNullException>(result); | ||
Assert.Equal("hosts", ((ArgumentNullException)result).ParamName); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_If_Hosts_Is_Empty() | ||
{ | ||
// Given | ||
var fixture = new CurlDownloadMultipleFilesFixture(); | ||
fixture.Hosts = new Uri[0]; | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
Assert.IsType<ArgumentException>(result); | ||
Assert.Equal("hosts", ((ArgumentException)result).ParamName); | ||
Assert.Contains("Hosts cannot be empty", ((ArgumentException)result).Message); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_If_Settings_Is_Null() | ||
{ | ||
// Given | ||
var fixture = new CurlDownloadMultipleFilesFixture(); | ||
fixture.Settings = null; | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
Assert.IsType<ArgumentNullException>(result); | ||
Assert.Equal("settings", ((ArgumentNullException)result).ParamName); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_If_Curl_Executable_Was_Not_Found() | ||
{ | ||
// Given | ||
var fixture = new CurlDownloadMultipleFilesFixture(); | ||
fixture.GivenDefaultToolDoNotExist(); | ||
|
||
// When | ||
var result = Record.Exception(() => fixture.Run()); | ||
|
||
// Then | ||
Assert.IsType<CakeException>(result); | ||
Assert.Equal("curl: Could not locate executable.", result.Message); | ||
} | ||
|
||
[Theory] | ||
[InlineData("/bin/curl", "/bin/curl")] | ||
[InlineData("./tools/curl", "/Working/tools/curl")] | ||
public void Should_Use_Curl_Runner_From_Tool_Path_If_Provided( | ||
string toolPath, | ||
string expected) | ||
{ | ||
// Given | ||
var fixture = new CurlDownloadMultipleFilesFixture | ||
{ | ||
Settings = { ToolPath = toolPath } | ||
}; | ||
fixture.GivenSettingsToolPathExist(); | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal(expected, result.Path.FullPath); | ||
} | ||
|
||
#if NETFX | ||
[Theory] | ||
[InlineData(@"C:\bin\curl.exe", "C:/bin/curl.exe")] | ||
[InlineData(@".\tools\curl.exe", "/Working/tools/curl.exe")] | ||
public void Should_Use_Curl_Runner_From_Tool_Path_If_Provided_On_Windows( | ||
string toolPath, | ||
string expected) | ||
{ | ||
// Given | ||
var fixture = new CurlDownloadMultipleFilesFixture | ||
{ | ||
Settings = { ToolPath = toolPath } | ||
}; | ||
fixture.GivenSettingsToolPathExist(); | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal(expected, result.Path.FullPath); | ||
} | ||
#endif | ||
|
||
[Fact] | ||
public void Should_Find_Curl_Runner_If_Tool_Path_Not_Provided() | ||
{ | ||
// Given | ||
var fixture = new CurlDownloadMultipleFilesFixture(); | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Equal("/Working/tools/curl", result.Path.FullPath); | ||
} | ||
|
||
[Fact] | ||
public void Should_Set_The_Remote_Name_Switches_And_The_Urls_Of_The_Hosts_As_Arguments() | ||
{ | ||
// Given | ||
var fixture = new CurlDownloadMultipleFilesFixture | ||
{ | ||
Hosts = new[] | ||
{ | ||
new Uri("protocol://host/path"), | ||
new Uri("protocol://anotherhost/path") | ||
} | ||
}; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Contains("-O protocol://host/path", result.Args); | ||
Assert.Contains("-O protocol://anotherhost/path", result.Args); | ||
} | ||
|
||
[Fact] | ||
public void Should_Set_The_Absolute_Output_File_Paths_In_Quotes_And_The_Urls_Of_The_Hosts_As_Arguments() | ||
{ | ||
// Given | ||
var fixture = new CurlDownloadMultipleFilesFixture | ||
{ | ||
Hosts = new[] | ||
{ | ||
new Uri("protocol://host/path"), | ||
new Uri("protocol://anotherhost/path") | ||
}, | ||
Settings = | ||
{ | ||
OutputPaths = new FilePath[] | ||
{ | ||
"output/file", | ||
"output/anotherfile" | ||
} | ||
} | ||
}; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Contains( | ||
"-o \"/Working/output/file\" protocol://host/path", | ||
result.Args); | ||
Assert.Contains( | ||
"-o \"/Working/output/anotherfile\" protocol://anotherhost/path", | ||
result.Args); | ||
} | ||
|
||
[Fact] | ||
public void Should_Set_The_User_Credentials_In_Quotes_As_Argument() | ||
{ | ||
// Given | ||
var fixture = new CurlDownloadMultipleFilesFixture | ||
{ | ||
Settings = { Username = "user", Password = "password" } | ||
}; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.Contains("--user \"user:password\"", result.Args); | ||
} | ||
|
||
[Fact] | ||
public void Should_Not_Set_The_User_Credentials_As_Argument_If_Username_Is_Null() | ||
{ | ||
// Given | ||
var fixture = new CurlDownloadMultipleFilesFixture | ||
{ | ||
Settings = { Username = null, Password = "password" } | ||
}; | ||
|
||
// When | ||
var result = fixture.Run(); | ||
|
||
// Then | ||
Assert.DoesNotContain($"--user", result.Args); | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Cake.Curl.Tests/Fixtures/CurlDownloadMultipleFilesFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Cake.Testing.Fixtures; | ||
|
||
namespace Cake.Curl.Tests.Fixtures | ||
{ | ||
internal sealed class CurlDownloadMultipleFilesFixture | ||
: ToolFixture<CurlDownloadSettings> | ||
{ | ||
public CurlDownloadMultipleFilesFixture() | ||
: base("curl") | ||
{ | ||
Hosts = new[] | ||
{ | ||
new Uri("protocol://host/path"), | ||
new Uri("protocol://anotherhost/path") | ||
}; | ||
} | ||
|
||
public IEnumerable<Uri> Hosts { get; set; } | ||
|
||
protected override void RunTool() | ||
{ | ||
var tool = new CurlDownloadRunner( | ||
FileSystem, | ||
Environment, | ||
ProcessRunner, | ||
Tools); | ||
tool.DownloadFiles(Hosts, Settings); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.