From 28957d9dec872c246c16155f3404d4cf4c72a6f3 Mon Sep 17 00:00:00 2001 From: Patrik Svensson Date: Thu, 23 Jun 2016 00:48:00 +0200 Subject: [PATCH] Make sure that cake.config can be found For some reason, we used the working directory to locate the cake.config which works fine if the script that is being executed resides in the root directory. This commit will make sure that Cake always will look for cake.config in the same directory as the build script. Closes #1002 --- .../CakeConfigurationProviderFixture.cs | 6 ++- .../CakeConfigurationProviderTests.cs | 14 +++++++ .../CakeConfigurationProvider.cs | 13 +++++-- src/Cake.Tests/Cake.Tests.csproj | 6 +++ .../Unit/Modules/ConfigurationModuleTests.cs | 39 +++++++++++++++++++ src/Cake/Modules/ConfigurationModule.cs | 10 ++--- src/Cake/Program.cs | 4 +- 7 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 src/Cake.Tests/Unit/Modules/ConfigurationModuleTests.cs diff --git a/src/Cake.Core.Tests/Fixtures/CakeConfigurationProviderFixture.cs b/src/Cake.Core.Tests/Fixtures/CakeConfigurationProviderFixture.cs index a8ff116e77..c7149b1db2 100644 --- a/src/Cake.Core.Tests/Fixtures/CakeConfigurationProviderFixture.cs +++ b/src/Cake.Core.Tests/Fixtures/CakeConfigurationProviderFixture.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; using Cake.Core.Configuration; +using Cake.Core.IO; using Cake.Testing; namespace Cake.Core.Tests.Fixtures @@ -11,19 +12,22 @@ internal sealed class CakeConfigurationProviderFixture { public FakeFileSystem FileSystem { get; set; } public FakeEnvironment Environment { get; set; } + + public DirectoryPath Path { get; set; } public IDictionary Arguments { get; set; } public CakeConfigurationProviderFixture() { Environment = FakeEnvironment.CreateUnixEnvironment(); FileSystem = new FakeFileSystem(Environment); + Path = "./"; Arguments = new Dictionary(); } public ICakeConfiguration Create() { var provider = new CakeConfigurationProvider(FileSystem, Environment); - return provider.CreateConfiguration(Arguments); + return provider.CreateConfiguration(Path, Arguments); } } } diff --git a/src/Cake.Core.Tests/Unit/Configuration/CakeConfigurationProviderTests.cs b/src/Cake.Core.Tests/Unit/Configuration/CakeConfigurationProviderTests.cs index a4668fb72d..cc4c0d4b0a 100644 --- a/src/Cake.Core.Tests/Unit/Configuration/CakeConfigurationProviderTests.cs +++ b/src/Cake.Core.Tests/Unit/Configuration/CakeConfigurationProviderTests.cs @@ -42,6 +42,20 @@ public void Should_Throw_If_Environment_Is_Null() public sealed class TheCreateMethod { + [Fact] + public void Should_Throw_If_Path_Is_Null() + { + // Given + var fixture = new CakeConfigurationProviderFixture(); + fixture.Path = null; + + // When + var result = Record.Exception(() => fixture.Create()); + + // Then + Assert.IsArgumentNullException(result, "path"); + } + [Fact] public void Should_Throw_If_Arguments_Are_Null() { diff --git a/src/Cake.Core/Configuration/CakeConfigurationProvider.cs b/src/Cake.Core/Configuration/CakeConfigurationProvider.cs index 37f2b3a0d3..779305965f 100644 --- a/src/Cake.Core/Configuration/CakeConfigurationProvider.cs +++ b/src/Cake.Core/Configuration/CakeConfigurationProvider.cs @@ -38,10 +38,15 @@ public CakeConfigurationProvider(IFileSystem fileSystem, ICakeEnvironment enviro /// /// Creates a configuration from the provided arguments. /// + /// The directory to look for the configuration file. /// The arguments. /// The created configuration. - public ICakeConfiguration CreateConfiguration(IDictionary arguments) + public ICakeConfiguration CreateConfiguration(DirectoryPath path, IDictionary arguments) { + if (path == null) + { + throw new ArgumentNullException("path"); + } if (arguments == null) { throw new ArgumentNullException("arguments"); @@ -60,11 +65,11 @@ public ICakeConfiguration CreateConfiguration(IDictionary argume } // Parse the configuration file. - var path = new FilePath("./cake.config").MakeAbsolute(_environment); - if (_fileSystem.Exist(path)) + var configurationPath = path.CombineWithFilePath("cake.config").MakeAbsolute(_environment); + if (_fileSystem.Exist(configurationPath)) { var parser = new ConfigurationParser(_fileSystem, _environment); - var configuration = parser.Read(path); + var configuration = parser.Read(configurationPath); foreach (var key in configuration.Keys) { result[KeyNormalizer.Normalize(key)] = configuration[key]; diff --git a/src/Cake.Tests/Cake.Tests.csproj b/src/Cake.Tests/Cake.Tests.csproj index 66bbb94831..ff8c3adeae 100644 --- a/src/Cake.Tests/Cake.Tests.csproj +++ b/src/Cake.Tests/Cake.Tests.csproj @@ -33,6 +33,10 @@ true + + False + ..\packages\Autofac.3.5.2\lib\net40\Autofac.dll + ..\packages\NSubstitute.1.8.1.0\lib\net45\NSubstitute.dll @@ -83,6 +87,7 @@ + Code @@ -133,6 +138,7 @@ +