From a729b97b356740ce0591e81f66ee95df829c34de Mon Sep 17 00:00:00 2001 From: Andrew Stanton-Nurse Date: Mon, 25 Apr 2016 13:53:02 -0700 Subject: [PATCH] align code in dotnet-run (#2679) we used to use different code when --framework was specified than when it was not specified, this synchronizes them to use the same code path which removes a hidden NullRef also adds tests to cover both cases --- .../WorkspaceContext.cs | 17 +++---- src/dotnet/commands/dotnet-run/RunCommand.cs | 42 +++++++++------- .../Commands/RunCommand.cs | 50 +++---------------- ...boutScriptVariablesFromAManagedCompiler.cs | 3 +- .../PublishDesktopTests.cs | 10 ++-- 5 files changed, 44 insertions(+), 78 deletions(-) diff --git a/src/Microsoft.DotNet.ProjectModel/WorkspaceContext.cs b/src/Microsoft.DotNet.ProjectModel/WorkspaceContext.cs index e9ce6d74a..dc6c4812c 100644 --- a/src/Microsoft.DotNet.ProjectModel/WorkspaceContext.cs +++ b/src/Microsoft.DotNet.ProjectModel/WorkspaceContext.cs @@ -171,9 +171,7 @@ namespace Microsoft.DotNet.ProjectModel EmptyArray.Value; } - public ProjectContext GetProjectContext(string projectPath, NuGetFramework framework) => GetProjectContext(projectPath, framework, EmptyArray.Value); - - public ProjectContext GetProjectContext(string projectPath, NuGetFramework framework, IEnumerable runtimeIdentifier) + public ProjectContext GetProjectContext(string projectPath, NuGetFramework framework) { var contexts = GetProjectContextCollection(projectPath); if (contexts == null) @@ -183,11 +181,16 @@ namespace Microsoft.DotNet.ProjectModel return contexts .ProjectContexts - .FirstOrDefault(c => Equals(c.TargetFramework, framework) && RidsMatch(c.RuntimeIdentifier, runtimeIdentifier)); + .FirstOrDefault(c => Equals(c.TargetFramework, framework) && string.IsNullOrEmpty(c.RuntimeIdentifier)); } public ProjectContext GetRuntimeContext(ProjectContext context, IEnumerable runtimeIdentifiers) { + if(!runtimeIdentifiers.Any()) + { + return context; + } + var contexts = GetProjectContextCollection(context.ProjectDirectory); if (contexts == null) { @@ -515,11 +518,5 @@ namespace Microsoft.DotNet.ProjectModel yield return description; } } - - private static bool RidsMatch(string rid, IEnumerable compatibleRids) - { - return (string.IsNullOrEmpty(rid) && !compatibleRids.Any()) || - (compatibleRids.Contains(rid)); - } } } diff --git a/src/dotnet/commands/dotnet-run/RunCommand.cs b/src/dotnet/commands/dotnet-run/RunCommand.cs index 4716b777a..8f6ed9e19 100644 --- a/src/dotnet/commands/dotnet-run/RunCommand.cs +++ b/src/dotnet/commands/dotnet-run/RunCommand.cs @@ -20,6 +20,13 @@ namespace Microsoft.DotNet.Tools.Run public string Project = null; public IReadOnlyList Args = null; + public static readonly string[] DefaultFrameworks = new[] + { + FrameworkConstants.FrameworkIdentifiers.NetCoreApp, + FrameworkConstants.FrameworkIdentifiers.NetStandardApp, + }; + + ProjectContext _context; List _args; private WorkspaceContext _workspace; @@ -61,41 +68,38 @@ namespace Microsoft.DotNet.Tools.Run Configuration = Constants.DefaultConfiguration; } - var frameworkContexts = _workspace.GetProjectContexts(Project).Where(c => string.IsNullOrEmpty(c.RuntimeIdentifier)); + var frameworkContexts = _workspace.GetProjectContextCollection(Project).FrameworkOnlyContexts; var rids = PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers(); + ProjectContext frameworkContext; if (Framework == null) { - var defaultFrameworks = new[] - { - FrameworkConstants.FrameworkIdentifiers.NetCoreApp, - FrameworkConstants.FrameworkIdentifiers.NetStandardApp, - }; - - ProjectContext context; if (frameworkContexts.Count() == 1) { - context = frameworkContexts.Single(); + frameworkContext = frameworkContexts.Single(); } else { - context = frameworkContexts.FirstOrDefault(c => defaultFrameworks.Contains(c.TargetFramework.Framework)); - if (context == null) - { - throw new InvalidOperationException($"Couldn't find target to run. Possible causes:" + Environment.NewLine + - "1. No project.lock.json file or restore failed - run `dotnet restore`" + Environment.NewLine + - $"2. project.lock.json has multiple targets none of which is in default list ({string.Join(", ", defaultFrameworks)})"); - } + frameworkContext = frameworkContexts.FirstOrDefault(c => DefaultFrameworks.Contains(c.TargetFramework.Framework)); } - - _context = _workspace.GetRuntimeContext(context, rids); } else { - _context = _workspace.GetProjectContext(Project, NuGetFramework.Parse(Framework), rids); + frameworkContext = frameworkContexts.FirstOrDefault(c => Equals(c.TargetFramework, NuGetFramework.Parse(Framework))); } + if (frameworkContext == null) + { + throw new InvalidOperationException($"Couldn't find target to run. Possible causes:" + Environment.NewLine + + "1. No project.lock.json file or restore failed - run `dotnet restore`" + Environment.NewLine + + Framework == null ? + $"2. project.lock.json has multiple targets none of which is in default list ({string.Join(", ", DefaultFrameworks)})" : + $"2. The project does not support the desired framework: {Framework}"); + } + + _context = _workspace.GetRuntimeContext(frameworkContext, rids); + if (Args == null) { _args = new List(); diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs index 652bc599f..7319e416e 100644 --- a/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs @@ -14,50 +14,15 @@ namespace Microsoft.DotNet.Tools.Test.Utilities private bool _preserveTemporary; private string _appArgs; - private string ProjectPathOption - { - get - { - return _projectPath == string.Empty ? - "" : - $"-p \"{_projectPath}\""; - } - } + private string ProjectPathOption => string.IsNullOrEmpty(_projectPath) ? "" : $"-p \"{_projectPath}\""; - private string FrameworkOption - { - get - { - return _framework == string.Empty ? - "" : - $"-f {_framework}"; - } - } + private string FrameworkOption => string.IsNullOrEmpty(_framework) ? "" : $"-f {_framework}"; - private string ConfigurationOption - { - get - { - return _configuration == string.Empty ? - "" : - $"-c {_configuration}"; - } - } + private string ConfigurationOption => string.IsNullOrEmpty(_configuration) ? "" : $"-c {_configuration}"; - private string PreserveTemporaryOption - { - get - { - return _preserveTemporary ? - $"-t \"{_projectPath}\"" : - ""; - } - } + private string PreserveTemporaryOption => _preserveTemporary ? $"-t \"{_projectPath}\"" : ""; - private string AppArgsArgument - { - get { return _appArgs; } - } + private string AppArgsArgument => _appArgs; public RunCommand( string projectPath, @@ -92,9 +57,6 @@ namespace Microsoft.DotNet.Tools.Test.Utilities return base.ExecuteAsync(args); } - private string BuildArgs() - { - return $"{ProjectPathOption} {FrameworkOption} {ConfigurationOption} {PreserveTemporaryOption} {AppArgsArgument}"; - } + private string BuildArgs() => $"{ProjectPathOption} {FrameworkOption} {ConfigurationOption} {PreserveTemporaryOption} {AppArgsArgument}"; } } diff --git a/test/dotnet-compile.UnitTests/GivenThatICareAboutScriptVariablesFromAManagedCompiler.cs b/test/dotnet-compile.UnitTests/GivenThatICareAboutScriptVariablesFromAManagedCompiler.cs index efa1bec8d..473e644c8 100644 --- a/test/dotnet-compile.UnitTests/GivenThatICareAboutScriptVariablesFromAManagedCompiler.cs +++ b/test/dotnet-compile.UnitTests/GivenThatICareAboutScriptVariablesFromAManagedCompiler.cs @@ -220,7 +220,8 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests } var workspace = WorkspaceContext.Create(ProjectReaderSettings.ReadFromEnvironment(), designTime: false); - var context = workspace.GetProjectContext(projectJson, TestAssetFramework, rids); + var context = workspace.GetRuntimeContext(workspace.GetProjectContext(projectJson, TestAssetFramework), rids); + context = workspace.GetRuntimeContext(context, rids); managedCompiler.Compile(context, _args); RuntimeOutputDir = Path.Combine(OutputPath, rid); diff --git a/test/dotnet-publish.Tests/PublishDesktopTests.cs b/test/dotnet-publish.Tests/PublishDesktopTests.cs index 752bc27dc..4d91a3009 100644 --- a/test/dotnet-publish.Tests/PublishDesktopTests.cs +++ b/test/dotnet-publish.Tests/PublishDesktopTests.cs @@ -89,9 +89,11 @@ namespace Microsoft.DotNet.Tools.Publish.Tests } [WindowsOnlyTheory] - [InlineData("KestrelDesktop", "http://localhost:20301")] - [InlineData("KestrelDesktopWithRuntimes", "http://localhost:20302")] - public async Task DesktopApp_WithKestrel_WorksWhenRun(string project, string url) + [InlineData("KestrelDesktop", "http://localhost:20301", null)] + [InlineData("KestrelDesktopWithRuntimes", "http://localhost:20302", null)] + [InlineData("KestrelDesktop", "http://localhost:20303", "net451")] + [InlineData("KestrelDesktopWithRuntimes", "http://localhost:20304", "net451")] + public async Task DesktopApp_WithKestrel_WorksWhenRun(string project, string url, string framework) { // Disabled due to https://github.com/dotnet/cli/issues/2428 if (RuntimeInformation.ProcessArchitecture == Architecture.X86) @@ -104,7 +106,7 @@ namespace Microsoft.DotNet.Tools.Publish.Tests .WithBuildArtifacts(); Task exec = null; - var command = new RunCommand(Path.Combine(testInstance.TestRoot, project)); + var command = new RunCommand(Path.Combine(testInstance.TestRoot, project), framework); try { exec = command.ExecuteAsync(url);