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

Commit

Permalink
react to removal of PlatformAbstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
analogrelay committed Apr 19, 2017
1 parent 853b384 commit 99cd97d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Options" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.RazorViews.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.RuntimeEnvironment.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.StackTrace.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
Expand Down
8 changes: 3 additions & 5 deletions src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand All @@ -15,7 +15,6 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.PlatformAbstractions;

namespace Microsoft.AspNetCore.Hosting
{
Expand Down Expand Up @@ -274,9 +273,8 @@ private IServiceCollection BuildCommonServices(out AggregateException hostingSta

_options = new WebHostOptions(_config);

var appEnvironment = PlatformServices.Default.Application;
var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, appEnvironment.ApplicationBasePath);
var applicationName = _options.ApplicationName ?? appEnvironment.ApplicationName;
var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, AppContext.BaseDirectory);
var applicationName = _options.ApplicationName;

// Initialize the hosting environment
_hostingEnvironment.Initialize(applicationName, contentRootPath, _options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using PlatformAbstractions = Microsoft.DotNet.PlatformAbstractions;

namespace Microsoft.AspNetCore.Server.IntegrationTesting
{
Expand Down Expand Up @@ -214,18 +214,35 @@ protected void StopTimer()

private string GetRuntimeIdentifier()
{
var architecture = PlatformAbstractions.RuntimeEnvironment.RuntimeArchitecture;
switch (PlatformAbstractions.RuntimeEnvironment.OperatingSystemPlatform)
var architecture = GetArchitecture();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
case PlatformAbstractions.Platform.Windows:
return "win7-" + architecture;
case PlatformAbstractions.Platform.Linux:
return "linux-" + architecture;
case PlatformAbstractions.Platform.Darwin:
return "osx.10.12-" + architecture;
return "win7-" + architecture;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return "linux-" + architecture;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return "osx.10.12-" + architecture;
}
else
{
throw new InvalidOperationException("Unrecognized operation system platform");
}
}

private string GetArchitecture()
{
switch (RuntimeInformation.OSArchitecture)
{
case Architecture.X86:
return "x86";
case Architecture.X64:
return "x64";
default:
throw new InvalidOperationException(
"Unrecognized operation system platform: " + PlatformAbstractions.RuntimeEnvironment.OperatingSystemPlatform);
throw new NotSupportedException($"Unsupported architecture: {RuntimeInformation.OSArchitecture}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Testing" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="$(DotNetPlatformAbstractionsVersion)" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Process.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.RuntimeEnvironment.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
<PackageReference Include="Microsoft.NETCore.Windows.ApiSets" Version="$(WindowsApiSetsVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.IO;
using Microsoft.Extensions.PlatformAbstractions;

namespace Microsoft.AspNetCore.Server.IntegrationTesting.xunit
{
Expand All @@ -13,8 +12,7 @@ public class TestProjectHelpers

public static string GetSolutionRoot()
{
var applicationName = PlatformServices.Default.Application.ApplicationName;
var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath;
var applicationBasePath = AppContext.BaseDirectory;

var directoryInfo = new DirectoryInfo(applicationBasePath);
do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<PackageReference Include="Microsoft.AspNetCore.Testing" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Testing" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Options" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
Expand Down
50 changes: 38 additions & 12 deletions test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand All @@ -21,7 +21,6 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.PlatformAbstractions;
using Xunit;

[assembly: HostingStartup(typeof(WebHostBuilderTests.TestHostingStartup))]
Expand Down Expand Up @@ -650,52 +649,79 @@ public void DefaultContentRootIsApplicationBasePath()
.UseStartup("Microsoft.AspNetCore.Hosting.Tests")
.Build())
{
var appBase = PlatformServices.Default.Application.ApplicationBasePath;
var appBase = AppContext.BaseDirectory;
Assert.Equal(appBase, host.Services.GetService<IHostingEnvironment>().ContentRootPath);
}
}

[Fact]
public void DefaultApplicationNameToStartupAssemblyName()
public void DefaultApplicationNameWithNoStartupThrows()
{
var builder = new ConfigurationBuilder();
var host = new WebHostBuilder()
.UseServer(new TestServer());

var ex = Assert.Throws<ArgumentException>(() => host.Build());

// ArgumentException adds "Parameter name" to the message and this is the cleanest way to make sure we get the right
// expected string
Assert.Equal(new ArgumentException("A valid non-empty application name must be provided.", "applicationName").Message , ex.Message);
}

[Fact]
public void DefaultApplicationNameWithUseStartupOfString()
{
var builder = new ConfigurationBuilder();
using (var host = new WebHostBuilder()
.UseServer(new TestServer())
.UseStartup("Microsoft.AspNetCore.Hosting.Tests")
.UseStartup(typeof(Startup).Assembly.GetName().Name)
.Build())
{
var hostingEnv = host.Services.GetService<IHostingEnvironment>();
Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName);
Assert.Equal(typeof(Startup).Assembly.GetName().Name, hostingEnv.ApplicationName);
}
}

[Fact]
public void DefaultApplicationNameToStartupType()
public void DefaultApplicationNameWithUseStartupOfT()
{
var builder = new ConfigurationBuilder();
using (var host = new WebHostBuilder()
.UseServer(new TestServer())
.UseStartup<StartupNoServices>()
.UseStartup("Microsoft.AspNetCore.Hosting.Tests.NonExistent")
.Build())
{
var hostingEnv = host.Services.GetService<IHostingEnvironment>();
Assert.Equal("Microsoft.AspNetCore.Hosting.Tests.NonExistent", hostingEnv.ApplicationName);
Assert.Equal(typeof(StartupNoServices).Assembly.GetName().Name, hostingEnv.ApplicationName);
}
}

[Fact]
public void DefaultApplicationNameAndBasePathToStartupMethods()
public void DefaultApplicationNameWithUseStartupOfType()
{
var builder = new ConfigurationBuilder();
var host = new WebHostBuilder()
.UseServer(new TestServer())
.UseStartup(typeof(StartupNoServices))
.Build();

var hostingEnv = host.Services.GetService<IHostingEnvironment>();
Assert.Equal(typeof(StartupNoServices).Assembly.GetName().Name, hostingEnv.ApplicationName);
}

[Fact]
public void DefaultApplicationNameWithConfigure()
{
var builder = new ConfigurationBuilder();
using (var host = new WebHostBuilder()
.UseServer(new TestServer())
.Configure(app => { })
.UseStartup("Microsoft.AspNetCore.Hosting.Tests.NonExistent")
.Build())
{
var hostingEnv = host.Services.GetService<IHostingEnvironment>();
Assert.Equal("Microsoft.AspNetCore.Hosting.Tests.NonExistent", hostingEnv.ApplicationName);

// Should be the assembly containing this test, because that's where the delegate comes from
Assert.Equal(typeof(WebHostBuilderTests).Assembly.GetName().Name, hostingEnv.ApplicationName);
}
}

Expand Down

0 comments on commit 99cd97d

Please sign in to comment.