Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Throw Command Unknown for dependency tools in libraries. (#2933)
Browse files Browse the repository at this point in the history
* Throw Command Unknown for dependency tools in libraries.

* Add testProjects to test tools command for libraries.

* update failing tests

* Add tests verifying that dependency tools are not available in libraries
  • Loading branch information
brthor authored and Piotr Puszkiewicz committed May 8, 2016
1 parent e53acd9 commit 0336f6b
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": false
},
"dependencies": {
"dotnet-desktop-and-portable": "1.0.0-*"
},
"frameworks": {
"netstandard1.5": {
"dependencies": {
"NETStandard.Library": "1.5.0-rc2-24027"
},
"imports": [
"portable-net45+win8",
"dnxcore50",
"netcoreapp1.0"
]
},
"net451": {}
},
"tools": {
"dotnet-dependency-tool-invoker": {
"version": "1.0.0-*",
"imports": [
"dnxcore50",
"portable-net45+win8"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.DotNet.Tools.DependencyInvoker
{
public class Program
{
public static void Main(string[] args)
public static int Main(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);

Expand All @@ -21,7 +21,7 @@ public static void Main(string[] args)
{
Console.WriteLine("A command name must be provided");

return;
return 1;
}

var projectContexts =
Expand Down Expand Up @@ -59,8 +59,10 @@ public static void Main(string[] args)
catch (CommandUnknownException)
{
Console.WriteLine($"Command not found");
return 1;
}
}
return 0;
}

private static IEnumerable<ProjectContext> CreateProjectContexts(string projectPath = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ internal override string ResolveCommandPath(CommandResolverArguments commandReso

var buildOutputPath = projectContext.GetOutputPaths(configuration, buildBasePath, outputPath).RuntimeFiles.BasePath;

if (! Directory.Exists(buildOutputPath))
{
Reporter.Verbose.WriteLine($"outputpathresolver: {buildOutputPath} does not exist");
return null;
}

return _environment.GetCommandPathFromRootPath(buildOutputPath, commandName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,21 @@ public CommandSpec Resolve(CommandResolverArguments commandResolverArguments)
var depsFilePath =
projectContext.GetOutputPaths(configuration, buildBasePath, outputPath).RuntimeFiles.DepsJson;

if (! File.Exists(depsFilePath))
{
Reporter.Verbose.WriteLine($"projectdependenciescommandresolver: {depsFilePath} does not exist");
return null;
}

var runtimeConfigPath =
projectContext.GetOutputPaths(configuration, buildBasePath, outputPath).RuntimeFiles.RuntimeConfigJson;

if (! File.Exists(runtimeConfigPath))
{
Reporter.Verbose.WriteLine($"projectdependenciescommandresolver: {runtimeConfigPath} does not exist");
return null;
}

var toolLibrary = GetToolLibraryForContext(projectContext, commandName);

return _packagedCommandSpecFactory.CreateCommandSpecFromLibrary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ public void It_passes_depsfile_arg_to_corehost_when_returning_a_commandspec()
}

[Fact]
public void It_sets_depsfile_based_on_output_path_when_returning_a_commandspec()
public void It_sets_depsfile_in_output_path_in_commandspec()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var outputDir = Path.Combine(AppContext.BaseDirectory, "outdir");

var commandResolverArguments = new CommandResolverArguments
{
Expand All @@ -190,16 +191,22 @@ public void It_sets_depsfile_based_on_output_path_when_returning_a_commandspec()
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10,
OutputPath = AppContext.BaseDirectory
OutputPath = outputDir
};

var buildCommand = new BuildCommand(
Path.Combine(s_liveProjectDirectory, "project.json"),
output: outputDir,
framework: FrameworkConstants.CommonFrameworks.NetCoreApp10.ToString())
.Execute().Should().Pass();

var projectContext = ProjectContext.Create(
s_liveProjectDirectory,
FrameworkConstants.CommonFrameworks.NetCoreApp10,
RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers());

var depsFilePath =
projectContext.GetOutputPaths("Debug", outputPath: AppContext.BaseDirectory).RuntimeFiles.DepsJson;
projectContext.GetOutputPaths("Debug", outputPath: outputDir).RuntimeFiles.DepsJson;

var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);

Expand All @@ -208,9 +215,10 @@ public void It_sets_depsfile_based_on_output_path_when_returning_a_commandspec()
}

[Fact]
public void It_sets_depsfile_based_on_build_base_path_when_returning_a_commandspec()
public void It_sets_depsfile_in_build_base_path_in_commandspec()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var buildBasePath = Path.Combine(AppContext.BaseDirectory, "basedir");

var commandResolverArguments = new CommandResolverArguments
{
Expand All @@ -219,16 +227,22 @@ public void It_sets_depsfile_based_on_build_base_path_when_returning_a_commandsp
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetCoreApp10,
BuildBasePath = AppContext.BaseDirectory
BuildBasePath = buildBasePath
};

var buildCommand = new BuildCommand(
Path.Combine(s_liveProjectDirectory, "project.json"),
buildBasePath: buildBasePath,
framework: FrameworkConstants.CommonFrameworks.NetCoreApp10.ToString())
.Execute().Should().Pass();

var projectContext = ProjectContext.Create(
s_liveProjectDirectory,
FrameworkConstants.CommonFrameworks.NetCoreApp10,
RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers());

var depsFilePath =
projectContext.GetOutputPaths("Debug", AppContext.BaseDirectory).RuntimeFiles.DepsJson;
projectContext.GetOutputPaths("Debug", buildBasePath).RuntimeFiles.DepsJson;

var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);

Expand Down
71 changes: 54 additions & 17 deletions test/dotnet.Tests/PackagedCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ public class PackagedCommandTests : TestBase
private readonly string _testProjectsRoot;
private readonly string _desktopTestProjectsRoot;

public static IEnumerable<object[]> DependencyToolArguments
{
get
{
var rid = RuntimeEnvironmentRidExtensions.GetLegacyRestoreRuntimeIdentifier();
var projectOutputPath = $"AppWithDirectDependencyDesktopAndPortable\\bin\\Debug\\net451\\{rid}\\dotnet-desktop-and-portable.exe";
return new[]
{
new object[] { ".NETCoreApp,Version=v1.0", "CoreFX", "lib\\netcoreapp1.0\\dotnet-desktop-and-portable.dll", true },
new object[] { ".NETFramework,Version=v4.5.1", "NetFX", projectOutputPath, true }
};
}
}

public static IEnumerable<object[]> LibraryDependencyToolArguments
{
get
{
var rid = RuntimeEnvironmentRidExtensions.GetLegacyRestoreRuntimeIdentifier();
var projectOutputPath = $"LibraryWithDirectDependencyDesktopAndPortable\\bin\\Debug\\net451\\dotnet-desktop-and-portable.exe";
return new[]
{
new object[] { ".NETStandard,Version=v1.5", "CoreFX", "lib\\netstandard1.5\\dotnet-desktop-and-portable.dll", true },
new object[] { ".NETFramework,Version=v4.5.1", "NetFX", projectOutputPath, true }
};
}
}

public PackagedCommandTests()
{
_testProjectsRoot = Path.Combine(AppContext.BaseDirectory, "TestAssets", "TestProjects");
Expand All @@ -39,7 +67,7 @@ public void TestProjectToolIsAvailableThroughDriver(string appName)
CommandResult result = new PortableCommand { WorkingDirectory = appDirectory }
.ExecuteWithCapturedOutput();

result.Should().HaveStdOut("Hello Portable World!" + Environment.NewLine);
result.Should().HaveStdOutContaining("Hello Portable World!" + Environment.NewLine);
result.Should().NotHaveStdErr();
result.Should().Pass();
}
Expand Down Expand Up @@ -84,9 +112,9 @@ public void CanInvokeToolFromDirectDependenciesIfPackageNameDifferentFromToolNam
// need conditional theories so we can skip on non-Windows
[Theory]
[MemberData("DependencyToolArguments")]
public void TestFrameworkSpecificDependencyToolsCanBeInvoked(string framework, string args, string expectedDependencyToolPath)
public void TestFrameworkSpecificDependencyToolsCanBeInvoked(string framework, string args, string expectedDependencyToolPath, bool windowsOnly)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && windowsOnly)
{
return;
}
Expand All @@ -108,6 +136,29 @@ public void TestFrameworkSpecificDependencyToolsCanBeInvoked(string framework, s
result.Should().Pass();
}

[Theory]
[MemberData("LibraryDependencyToolArguments")]
public void TestFrameworkSpecificLibraryDependencyToolsCannotBeInvoked(string framework, string args, string expectedDependencyToolPath, bool windowsOnly)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && windowsOnly)
{
return;
}

var appDirectory = Path.Combine(_desktopTestProjectsRoot, "LibraryWithDirectDependencyDesktopAndPortable");

new BuildCommand(Path.Combine(appDirectory, "project.json"))
.Execute()
.Should()
.Pass();

CommandResult result = new DependencyToolInvokerCommand { WorkingDirectory = appDirectory }
.ExecuteWithCapturedOutput("desktop-and-portable", framework, args);

result.Should().HaveStdOutContaining("Command not found");
result.Should().Fail();
}

[Fact]
public void ToolsCanAccessDependencyContextProperly()
{
Expand All @@ -119,20 +170,6 @@ public void ToolsCanAccessDependencyContextProperly()
result.Should().Pass();
}

public static IEnumerable<object[]> DependencyToolArguments
{
get
{
var rid = RuntimeEnvironmentRidExtensions.GetLegacyRestoreRuntimeIdentifier();
var projectOutputPath = $"AppWithDirectDependencyDesktopAndPortable\\bin\\Debug\\net451\\{rid}\\dotnet-desktop-and-portable.exe";
return new[]
{
new object[] { ".NETCoreApp,Version=v1.0", "CoreFX", "lib\\netcoreapp1.0\\dotnet-desktop-and-portable.dll" },
new object[] { ".NETFramework,Version=v4.5.1", "NetFX", projectOutputPath }
};
}
}

[Fact]
public void TestProjectDependencyIsNotAvailableThroughDriver()
{
Expand Down
3 changes: 2 additions & 1 deletion test/dotnet.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"../../TestAssets/TestProjects/AppWithDirectDependencyWithOutputName/**/*",
"../../TestAssets/TestProjects/AppWithToolDependency/**/*",
"../../TestAssets/TestProjects/DependencyContextFromTool/**/*",
"../../TestAssets/DesktopTestProjects/AppWithDirectDependencyDesktopAndPortable/**/*"
"../../TestAssets/DesktopTestProjects/AppWithDirectDependencyDesktopAndPortable/**/*",
"../../TestAssets/DesktopTestProjects/LibraryWithDirectDependencyDesktopAndPortable/**/*"
],
"testRunner": "xunit"
}

0 comments on commit 0336f6b

Please sign in to comment.