Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Modify ConfigurationBuilder.GetBasePath() to return an absolute path …
Browse files Browse the repository at this point in the history
…on Mono

Fixes #336
  • Loading branch information
pranavkm committed Dec 15, 2015
1 parent b56dcf5 commit 73e1620
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;

namespace Microsoft.Extensions.Configuration
{
Expand All @@ -26,7 +27,7 @@ public static IConfigurationBuilder SetBasePath(this IConfigurationBuilder confi
}

configurationBuilder.Properties["BasePath"] = basePath;

return configurationBuilder;
}

Expand All @@ -49,8 +50,13 @@ public static string GetBasePath(this IConfigurationBuilder configurationBuilder
}

#if NET451
return AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") as string ??
AppDomain.CurrentDomain.BaseDirectory ?? string.Empty;
var stringBasePath =
AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") as string ??
AppDomain.CurrentDomain.BaseDirectory ??
string.Empty;

// On Mono, the value of APP_CONTEXT_BASE_DIRECTORY is ".". We'll expand it to get the full base path.
return Path.GetFullPath(stringBasePath);
#else
return AppContext.BaseDirectory ?? string.Empty;
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using Microsoft.AspNet.FileProviders;

namespace Microsoft.Extensions.Configuration
Expand All @@ -20,8 +21,13 @@ public static IConfigurationRoot ReloadOnChanged(this IConfigurationRoot config,
throw new ArgumentNullException(nameof(filename));
}
#if NET451
var basePath = AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") as string ??
AppDomain.CurrentDomain.BaseDirectory ?? string.Empty;
var basePath =
AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") as string ??
AppDomain.CurrentDomain.BaseDirectory ??
string.Empty;

// On Mono, the value of APP_CONTEXT_BASE_DIRECTORY is ".". We'll expand it to get the full base path.
basePath = Path.GetFullPath(basePath);
#else
var basePath = AppContext.BaseDirectory ?? string.Empty;
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ public void GetBasePath_ReturnBaseDirectoryIfNotSet()
// Act
var actualPath = configurationBuilder.GetBasePath();

string expectedPath = string.Empty;
string expectedPath;

#if DNXCORE50
expectedPath = AppContext.BaseDirectory;
expectedPath = AppContext.BaseDirectory;
#else
expectedPath = AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") as string ??
AppDomain.CurrentDomain.BaseDirectory ?? string.Empty;
expectedPath = Path.GetFullPath((string)AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY"));
#endif

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,12 @@ public void GetDefaultBasePathForSources()
{
// Arrange
var builder = new ConfigurationBuilder();
string filePath = string.Empty;
string filePath;

#if DNXCORE50
filePath = AppContext.BaseDirectory ?? string.Empty;
filePath = AppContext.BaseDirectory;
#else
filePath = AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") as string ??
AppDomain.CurrentDomain.BaseDirectory ?? string.Empty;
filePath = Path.GetFullPath((string)AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY"));
#endif

var jsonConfigFilePath = Path.Combine(filePath, "test.json");
Expand Down

0 comments on commit 73e1620

Please sign in to comment.