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

GH1002: Make sure that cake.config can be found #1003

Merged
merged 1 commit into from
Jun 24, 2016
Merged
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 @@ -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
Expand All @@ -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<string, string> Arguments { get; set; }

public CakeConfigurationProviderFixture()
{
Environment = FakeEnvironment.CreateUnixEnvironment();
FileSystem = new FakeFileSystem(Environment);
Path = "./";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Environment.CurrentDirectory()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, no. This is a unit test fixture.

Arguments = new Dictionary<string, string>();
}

public ICakeConfiguration Create()
{
var provider = new CakeConfigurationProvider(FileSystem, Environment);
return provider.CreateConfiguration(Arguments);
return provider.CreateConfiguration(Path, Arguments);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
13 changes: 9 additions & 4 deletions src/Cake.Core/Configuration/CakeConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ public CakeConfigurationProvider(IFileSystem fileSystem, ICakeEnvironment enviro
/// <summary>
/// Creates a configuration from the provided arguments.
/// </summary>
/// <param name="path">The directory to look for the configuration file.</param>
/// <param name="arguments">The arguments.</param>
/// <returns>The created configuration.</returns>
public ICakeConfiguration CreateConfiguration(IDictionary<string, string> arguments)
public ICakeConfiguration CreateConfiguration(DirectoryPath path, IDictionary<string, string> arguments)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
if (arguments == null)
{
throw new ArgumentNullException("arguments");
Expand All @@ -60,11 +65,11 @@ public ICakeConfiguration CreateConfiguration(IDictionary<string, string> 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];
Expand Down
6 changes: 6 additions & 0 deletions src/Cake.Tests/Cake.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac, Version=3.5.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Autofac.3.5.2\lib\net40\Autofac.dll</HintPath>
</Reference>
<Reference Include="NSubstitute">
<HintPath>..\packages\NSubstitute.1.8.1.0\lib\net45\NSubstitute.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -83,6 +87,7 @@
<Compile Include="Unit\Diagnostics\CakeBuildLogTests.cs" />
<Compile Include="Unit\Diagnostics\FormatParserTests.cs" />
<Compile Include="Unit\Commands\DebugCommandTests.cs" />
<Compile Include="Unit\Modules\ConfigurationModuleTests.cs" />
<Compile Include="Unit\Scripting\BuildScriptHostTests.cs">
<SubType>Code</SubType>
</Compile>
Expand Down Expand Up @@ -133,6 +138,7 @@
<EmbeddedResource Include="Data\MonoScriptProcessor\Mixed\input" />
<EmbeddedResource Include="Data\MonoScriptProcessor\Mixed\output" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\xunit.core.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.core.props" Condition="Exists('..\packages\xunit.core.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.core.props')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
39 changes: 39 additions & 0 deletions src/Cake.Tests/Unit/Modules/ConfigurationModuleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Composition;
using Cake.Core.Composition;
using Cake.Core.Configuration;
using Cake.Core.IO;
using Cake.Modules;
using Cake.Testing;
using NSubstitute;
using Xunit;

namespace Cake.Tests.Unit.Modules
{
public sealed class ConfigurationModuleTests
{
public sealed class TheRegisterMethod
{
[Fact]
public void Should_Use_The_Script_Directory_As_Root_For_Configuration_File()
{
// Given
var fileSystem = Substitute.For<IFileSystem>();
var environment = FakeEnvironment.CreateUnixEnvironment();
var provider = new CakeConfigurationProvider(fileSystem, environment);
var registry = new ContainerRegistry();
var options = new CakeOptions { Script = "./foo/bar/build.cake" };
var module = new ConfigurationModule(provider, options);

// When
module.Register(registry);

// Then
fileSystem.Received(1).Exist(Arg.Is<FilePath>(f => f.FullPath == "/Working/foo/bar/cake.config"));
}
}
}
}
10 changes: 4 additions & 6 deletions src/Cake/Modules/ConfigurationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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;
using Autofac;
using Cake.Core.Composition;
using Cake.Core.Configuration;

Expand All @@ -14,9 +12,9 @@ internal sealed class ConfigurationModule : ICakeModule
private readonly CakeConfigurationProvider _provider;
private readonly CakeOptions _options;

public ConfigurationModule(IContainer container, CakeOptions options)
public ConfigurationModule(CakeConfigurationProvider provider, CakeOptions options)
{
_provider = container.Resolve<CakeConfigurationProvider>();
_provider = provider;
_options = options;
}

Expand All @@ -27,8 +25,8 @@ public void Register(ICakeContainerRegistry registry)
throw new ArgumentNullException("registry");
}

var configuration = _provider.CreateConfiguration(_options.Arguments);
Debug.Assert(configuration != null, "Configuration should not be null.");
var root = _options.Script.GetDirectory();
var configuration = _provider.CreateConfiguration(root, _options.Arguments);
registry.RegisterInstance(configuration).As<ICakeConfiguration>();
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Cake/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Autofac;
using Cake.Arguments;
using Cake.Composition;
using Cake.Core.Configuration;
using Cake.Core.Diagnostics;
using Cake.Core.Modules;
using Cake.Diagnostics;
Expand Down Expand Up @@ -55,8 +56,9 @@ public static int Main()

// Rebuild the container.
builder = new CakeContainerBuilder();
var provider = container.Resolve<CakeConfigurationProvider>();
builder.Registry.RegisterModule(new ConfigurationModule(provider, options));
builder.Registry.RegisterModule(new ArgumentsModule(options));
builder.Registry.RegisterModule(new ConfigurationModule(container, options));
builder.Registry.RegisterModule(new ScriptingModule(options));
builder.Update(container);

Expand Down