From 9b883fda6a5d2b4e98c61e0dca1b1c9fdb4812e1 Mon Sep 17 00:00:00 2001 From: Thomas Levesque Date: Wed, 1 Mar 2017 01:05:44 +0100 Subject: [PATCH] Sign multiple files at once --- .../Tools/SignToolSignRunnerFixture.cs | 7 ++-- .../Tools/SignTool/SignToolSignRunnerTests.cs | 29 +++++++++++++-- .../Tools/SignTool/SignToolSignAliases.cs | 5 +-- .../Tools/SignTool/SignToolSignRunner.cs | 37 ++++++++++--------- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/Cake.Common.Tests/Fixtures/Tools/SignToolSignRunnerFixture.cs b/src/Cake.Common.Tests/Fixtures/Tools/SignToolSignRunnerFixture.cs index 887865897f..7ad4646e4f 100644 --- a/src/Cake.Common.Tests/Fixtures/Tools/SignToolSignRunnerFixture.cs +++ b/src/Cake.Common.Tests/Fixtures/Tools/SignToolSignRunnerFixture.cs @@ -14,10 +14,9 @@ namespace Cake.Common.Tests.Fixtures.Tools internal sealed class SignToolSignRunnerFixture : ToolFixture { public ISignToolResolver Resolver { get; set; } - public IFile AssemblyFile { get; set; } public IFile CertificateFile { get; set; } - public FilePath AssemblyPath { get; set; } + public FilePath[] AssemblyPaths { get; set; } public SignToolSignRunnerFixture() : base("signtool.exe") @@ -26,7 +25,7 @@ public SignToolSignRunnerFixture() Settings.Password = "secret"; Settings.TimeStampUri = new Uri("https://t.com"); - AssemblyPath = new FilePath("./a.dll"); + AssemblyPaths = new[] { new FilePath("./a.dll") }; FileSystem.CreateFile("/Working/a.dll"); FileSystem.CreateFile("/Working/cert.pfx"); @@ -36,7 +35,7 @@ public SignToolSignRunnerFixture() protected override void RunTool() { var tool = new SignToolSignRunner(FileSystem, Environment, ProcessRunner, Tools, null, Resolver); - tool.Run(AssemblyPath, Settings); + tool.Run(AssemblyPaths, Settings); } } } \ No newline at end of file diff --git a/src/Cake.Common.Tests/Unit/Tools/SignTool/SignToolSignRunnerTests.cs b/src/Cake.Common.Tests/Unit/Tools/SignTool/SignToolSignRunnerTests.cs index 820fe32a3f..5c08591af6 100644 --- a/src/Cake.Common.Tests/Unit/Tools/SignTool/SignToolSignRunnerTests.cs +++ b/src/Cake.Common.Tests/Unit/Tools/SignTool/SignToolSignRunnerTests.cs @@ -6,6 +6,7 @@ using Cake.Common.Tests.Fixtures.Tools; using Cake.Common.Tools.SignTool; using Cake.Core; +using Cake.Core.IO; using Cake.Testing; using Xunit; @@ -61,17 +62,17 @@ public void Should_Throw_If_Process_Runner_Is_Null() public sealed class TheRunMethod { [Fact] - public void Should_Throw_If_Assembly_Path_Is_Null() + public void Should_Throw_If_Assembly_Paths_Is_Null() { // Given var fixture = new SignToolSignRunnerFixture(); - fixture.AssemblyPath = null; + fixture.AssemblyPaths = null; // When var result = Record.Exception(() => fixture.Run()); // Then - Assert.IsArgumentNullException(result, "assemblyPath"); + Assert.IsArgumentNullException(result, "assemblyPaths"); } [Fact] @@ -371,6 +372,28 @@ public void Should_Call_Sign_Tool_With_Correct_Parameters_With_Append_Signature( // Then Assert.Equal("SIGN /t \"https://t.com/\" /f \"/Working/cert.pfx\" /p secret /as \"/Working/a.dll\"", result.Args); } + + [Fact] + public void Should_Call_Sign_Tool_With_Correct_Parameters_With_Thumbprint_And_Multiple_Assemblies() + { + // Given + var fixture = new SignToolSignRunnerFixture(); + fixture.Settings.CertPath = null; + fixture.Settings.Password = null; + fixture.Settings.CertThumbprint = "ThumbprintTest"; + fixture.AssemblyPaths = new[] + { + new FilePath("./a.dll"), + new FilePath("./foo/b.dll") + }; + fixture.FileSystem.CreateFile("/Working/foo/b.dll"); + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("SIGN /t \"https://t.com/\" /sha1 \"ThumbprintTest\" \"/Working/a.dll\" \"/Working/foo/b.dll\"", result.Args); + } } } } \ No newline at end of file diff --git a/src/Cake.Common/Tools/SignTool/SignToolSignAliases.cs b/src/Cake.Common/Tools/SignTool/SignToolSignAliases.cs index 32f9a7984a..ab94242b00 100644 --- a/src/Cake.Common/Tools/SignTool/SignToolSignAliases.cs +++ b/src/Cake.Common/Tools/SignTool/SignToolSignAliases.cs @@ -162,10 +162,7 @@ public static void Sign(this ICakeContext context, IEnumerable assembl } var runner = new SignToolSignRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Registry); - foreach (var assembly in assemblies) - { - runner.Run(assembly, settings); - } + runner.Run(assemblies, settings); } } } \ No newline at end of file diff --git a/src/Cake.Common/Tools/SignTool/SignToolSignRunner.cs b/src/Cake.Common/Tools/SignTool/SignToolSignRunner.cs index 6c5a013995..73307d7cd0 100644 --- a/src/Cake.Common/Tools/SignTool/SignToolSignRunner.cs +++ b/src/Cake.Common/Tools/SignTool/SignToolSignRunner.cs @@ -60,36 +60,36 @@ public sealed class SignToolSignRunner : Tool } /// - /// Signs the specified assembly. + /// Signs the specified assemblies. /// - /// The assembly path. + /// The assembly paths. /// The settings. - public void Run(FilePath assemblyPath, SignToolSignSettings settings) + public void Run(IEnumerable assemblyPaths, SignToolSignSettings settings) { - if (assemblyPath == null) + if (assemblyPaths == null) { - throw new ArgumentNullException(nameof(assemblyPath)); + throw new ArgumentNullException(nameof(assemblyPaths)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } - if (assemblyPath.IsRelative) - { - assemblyPath = assemblyPath.MakeAbsolute(_environment); - } + var absoluteAssemblyPaths = assemblyPaths.Select(p => p.IsRelative ? p.MakeAbsolute(_environment) : p).ToArray(); - Run(settings, GetArguments(assemblyPath, settings)); + Run(settings, GetArguments(absoluteAssemblyPaths, settings)); } - private ProcessArgumentBuilder GetArguments(FilePath assemblyPath, SignToolSignSettings settings) + private ProcessArgumentBuilder GetArguments(FilePath[] absoluteAssemblyPaths, SignToolSignSettings settings) { - if (!_fileSystem.Exist(assemblyPath)) + foreach (var path in absoluteAssemblyPaths) { - const string format = "{0}: The assembly '{1}' does not exist."; - var message = string.Format(CultureInfo.InvariantCulture, format, GetToolName(), assemblyPath.FullPath); - throw new CakeException(message); + if (!_fileSystem.Exist(path)) + { + const string format = "{0}: The assembly '{1}' does not exist."; + var message = string.Format(CultureInfo.InvariantCulture, format, GetToolName(), path.FullPath); + throw new CakeException(message); + } } if (settings.TimeStampUri == null) @@ -224,8 +224,11 @@ private ProcessArgumentBuilder GetArguments(FilePath assemblyPath, SignToolSignS builder.Append("/as"); } - // Target Assembly to sign. - builder.AppendQuoted(assemblyPath.MakeAbsolute(_environment).FullPath); + // Target Assemblies to sign. + foreach (var path in absoluteAssemblyPaths) + { + builder.AppendQuoted(path.FullPath); + } return builder; }