From c1fff9649a090fe6bdab10c438b0a9d730aabef3 Mon Sep 17 00:00:00 2001 From: Mike Lorbetske Date: Wed, 21 Mar 2018 21:19:24 -0700 Subject: [PATCH 1/3] Update launch settings for ApplicationUrl handling --- ...pWithApplicationUrlInLaunchSettings.csproj | 9 +++ .../Program.cs | 16 +++++ .../Properties/launchSettings.json | 27 +++++++++ .../ProjectLaunchSettingsProvider.cs | 10 ++-- .../GivenDotnetRunRunsCsProj.cs | 60 +++++++++++++++++++ 5 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/AppWithApplicationUrlInLaunchSettings.csproj create mode 100644 TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Program.cs create mode 100644 TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Properties/launchSettings.json diff --git a/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/AppWithApplicationUrlInLaunchSettings.csproj b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/AppWithApplicationUrlInLaunchSettings.csproj new file mode 100644 index 0000000000..9434c36c94 --- /dev/null +++ b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/AppWithApplicationUrlInLaunchSettings.csproj @@ -0,0 +1,9 @@ + + + + + Exe + netcoreapp2.1 + win7-x64;win7-x86;osx.10.12-x64;ubuntu.14.04-x64;ubuntu.16.04-x64;ubuntu.16.10-x64;rhel.6-x64;centos.7-x64;rhel.7-x64;debian.8-x64;fedora.24-x64;opensuse.42.1-x64;alpine.3.6-x64 + + diff --git a/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Program.cs b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Program.cs new file mode 100644 index 0000000000..33322e771b --- /dev/null +++ b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Program.cs @@ -0,0 +1,16 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; + +namespace MSBuildTestApp +{ + public class Program + { + public static void Main(string[] args) + { + var message = Environment.GetEnvironmentVariable("ASPNETCORE_URLS"); + Console.WriteLine(message); + } + } +} diff --git a/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Properties/launchSettings.json b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Properties/launchSettings.json new file mode 100644 index 0000000000..b61eabd571 --- /dev/null +++ b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:49850/", + "sslPort": 0 + } + }, + "profiles": { + "First": { + "commandName": "Project", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "ASPNETCORE_URLS": "http://localhost:12345/" + }, + "applicationUrl": "http://localhost:67890/" + }, + "Second": { + "commandName": "Project", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:54321/" + } + } +} \ No newline at end of file diff --git a/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs b/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs index 7808704949..5bb5ce85d4 100644 --- a/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs +++ b/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs @@ -15,6 +15,11 @@ public LaunchSettingsApplyResult TryApplySettings(JObject document, JObject mode { var config = model.ToObject(); + if (!string.IsNullOrEmpty(config.ApplicationUrl)) + { + command.EnvironmentVariable("ASPNETCORE_URLS", config.ApplicationUrl); + } + //For now, ignore everything but the environment variables section foreach (var entry in config.EnvironmentVariables) @@ -24,11 +29,6 @@ public LaunchSettingsApplyResult TryApplySettings(JObject document, JObject mode command.EnvironmentVariable(entry.Key, value); } - if (!string.IsNullOrEmpty(config.ApplicationUrl)) - { - command.EnvironmentVariable("ASPNETCORE_URLS", config.ApplicationUrl); - } - return new LaunchSettingsApplyResult(true, null, config.LaunchUrl); } diff --git a/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs b/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs index 35660db735..a308533400 100644 --- a/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs +++ b/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs @@ -364,6 +364,66 @@ public void ItDefaultsToTheFirstUsableLaunchProfile() cmd.StdErr.Should().BeEmpty(); } + [Fact] + public void ItPrefersTheValueOfApplicationUrlFromEnvironmentVariablesOverTheProperty() + { + var testAppName = "AppWithApplicationUrlInLaunchSettings"; + var testInstance = TestAssets.Get(testAppName) + .CreateInstance() + .WithSourceFiles(); + + var testProjectDirectory = testInstance.Root.FullName; + + new RestoreCommand() + .WithWorkingDirectory(testProjectDirectory) + .Execute("/p:SkipInvalidConfigurations=true") + .Should().Pass(); + + new BuildCommand() + .WithWorkingDirectory(testProjectDirectory) + .Execute() + .Should().Pass(); + + var cmd = new RunCommand() + .WithWorkingDirectory(testProjectDirectory) + .ExecuteWithCapturedOutput("--launch-profile First"); + + cmd.Should().Pass() + .And.HaveStdOutContaining("http://localhost:12345/"); + + cmd.StdErr.Should().BeEmpty(); + } + + [Fact] + public void ItUsesTheValueOfApplicationUrlIfTheEnvironmentVariableIsNotSet() + { + var testAppName = "AppWithApplicationUrlInLaunchSettings"; + var testInstance = TestAssets.Get(testAppName) + .CreateInstance() + .WithSourceFiles(); + + var testProjectDirectory = testInstance.Root.FullName; + + new RestoreCommand() + .WithWorkingDirectory(testProjectDirectory) + .Execute("/p:SkipInvalidConfigurations=true") + .Should().Pass(); + + new BuildCommand() + .WithWorkingDirectory(testProjectDirectory) + .Execute() + .Should().Pass(); + + var cmd = new RunCommand() + .WithWorkingDirectory(testProjectDirectory) + .ExecuteWithCapturedOutput("--launch-profile Second"); + + cmd.Should().Pass() + .And.HaveStdOutContaining("http://localhost:54321/"); + + cmd.StdErr.Should().BeEmpty(); + } + [Fact] public void ItGivesAnErrorWhenTheLaunchProfileNotFound() { From 9ea0c38f81cee0b88552300c1cfb62744cb27995 Mon Sep 17 00:00:00 2001 From: Mike Lorbetske Date: Thu, 22 Mar 2018 14:37:05 -0700 Subject: [PATCH 2/3] Remove runtime identifiers from the test project --- .../AppWithApplicationUrlInLaunchSettings.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/AppWithApplicationUrlInLaunchSettings.csproj b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/AppWithApplicationUrlInLaunchSettings.csproj index 9434c36c94..be02066ff7 100644 --- a/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/AppWithApplicationUrlInLaunchSettings.csproj +++ b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/AppWithApplicationUrlInLaunchSettings.csproj @@ -4,6 +4,5 @@ Exe netcoreapp2.1 - win7-x64;win7-x86;osx.10.12-x64;ubuntu.14.04-x64;ubuntu.16.04-x64;ubuntu.16.10-x64;rhel.6-x64;centos.7-x64;rhel.7-x64;debian.8-x64;fedora.24-x64;opensuse.42.1-x64;alpine.3.6-x64 From eec79d83fcbce19f179806bdd550a429aab51c61 Mon Sep 17 00:00:00 2001 From: Mike Lorbetske Date: Fri, 23 Mar 2018 10:30:47 -0700 Subject: [PATCH 3/3] Try shorter test names --- test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs b/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs index a308533400..f85d7446eb 100644 --- a/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs +++ b/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs @@ -365,7 +365,7 @@ public void ItDefaultsToTheFirstUsableLaunchProfile() } [Fact] - public void ItPrefersTheValueOfApplicationUrlFromEnvironmentVariablesOverTheProperty() + public void ItPrefersTheValueOfAppUrlFromEnvVarOverTheProp() { var testAppName = "AppWithApplicationUrlInLaunchSettings"; var testInstance = TestAssets.Get(testAppName) @@ -395,7 +395,7 @@ public void ItPrefersTheValueOfApplicationUrlFromEnvironmentVariablesOverTheProp } [Fact] - public void ItUsesTheValueOfApplicationUrlIfTheEnvironmentVariableIsNotSet() + public void ItUsesTheValueOfAppUrlIfTheEnvVarIsNotSet() { var testAppName = "AppWithApplicationUrlInLaunchSettings"; var testInstance = TestAssets.Get(testAppName)