//[Fact]
        public void The_design_time_build_succeeds_before_nuget_restore()
        {
            //  This test needs the design-time targets, which come with Visual Studio.  So we will use the VSINSTALLDIR
            //  environment variable to find the install path to Visual Studio and the design-time targets under it.
            //  This will be set when running from a developer command prompt.  Unfortunately, unless VS is launched
            //  from a developer command prompt, it won't be set when running tests from VS.  So in that case the
            //  test will simply be skipped.
            string vsInstallDir = Environment.GetEnvironmentVariable("VSINSTALLDIR");

            if (vsInstallDir == null)
            {
                return;
            }

            string csharpDesignTimeTargets = Path.Combine(vsInstallDir, @"MSBuild\Microsoft\VisualStudio\Managed\Microsoft.CSharp.DesignTime.targets");

            var testAsset = _testAssetsManager
                            .CopyTestAsset("AppWithLibrary")
                            .WithSource();

            var libraryProjectDirectory = Path.Combine(testAsset.TestRoot, "TestLibrary");
            var projectFile             = Path.Combine(libraryProjectDirectory, "TestLibrary.csproj");

            var args = new[]
            {
                projectFile,
                "/p:DesignTimeBuild=true",
                "/p:SkipCompilerExecution=true",
                "/p:ProvideCommandLineArgs=true",
                $"/p:CSharpDesignTimeTargetsPath={csharpDesignTimeTargets}",
                "/t:ResolveProjectReferencesDesignTime",
                "/t:ResolveComReferencesDesignTime",
                "/t:CompileDesignTime",
                "/t:ResolvePackageDependenciesDesignTime"
            };

            var command = Stage0MSBuild.CreateCommandForTarget("ResolveAssemblyReferencesDesignTime", args);

            var result = command
                         .CaptureStdOut()
                         .Execute();

            //  In CI builds, VSINSTALLDIR is set but the CompileDesignTime target doesn't exist, probably because
            //  it's an earlier version of Visual Studio
            if (result.ExitCode != 0)
            {
                result
                .StdOut
                .Should()
                .Contain("The target \"CompileDesignTime\" does not exist");
            }
        }
Ejemplo n.º 2
0
        //[Fact]
        public void The_clean_target_removes_all_files_from_the_output_folder()
        {
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return;
            }

            var testAsset = _testAssetsManager
                            .CopyTestAsset("AppWithTransitiveProjectRefs")
                            .WithSource()
                            .Restore("TestApp");

            var appProjectDirectory = Path.Combine(testAsset.TestRoot, "TestApp");

            var buildCommand = new BuildCommand(Stage0MSBuild, appProjectDirectory);

            buildCommand
            .Execute()
            .Should()
            .Pass();

            var outputDirectory = buildCommand.GetOutputDirectory("netcoreapp1.0");

            outputDirectory.Should().OnlyHaveFiles(new[] {
                "TestApp.dll",
                "TestApp.pdb",
                "TestApp.deps.json",
                "TestApp.runtimeconfig.dev.json",
                "TestApp.runtimeconfig.json",
                "MainLibrary.dll",
                "MainLibrary.pdb",
                "AuxLibrary.dll",
                "AuxLibrary.pdb"
            });

            var cleanCommand = Stage0MSBuild.CreateCommandForTarget("Clean", buildCommand.FullPathProjectFile);

            cleanCommand
            .Execute()
            .Should()
            .Pass();

            outputDirectory.Should().OnlyHaveFiles(Array.Empty <string>());
        }
        public void It_passes_ridless_target_to_compiler()
        {
            var testAsset = _testAssetsManager
                            .CopyTestAsset("AppWithLibrary", "RidlessLib")
                            .WithSource()
                            .Restore(relativePath: "TestLibrary");

            var libraryProjectDirectory = Path.Combine(testAsset.TestRoot, "TestLibrary");
            var fullPathProjectFile     = new BuildCommand(Stage0MSBuild, libraryProjectDirectory).FullPathProjectFile;

            // compile should still pass with unknown RID because references are always pulled
            // from RIDLess target
            var buildCommand = Stage0MSBuild.CreateCommandForTarget(
                "Compile", fullPathProjectFile, "/p:RuntimeIdentifier=unknownrid");

            buildCommand
            .Execute()
            .Should()
            .Pass();
        }
        private void VerifyClean(TestAsset testAsset, string project, string targetFramework,
                                 params string[] expectedFiles)
        {
            var appProjectDirectory = Path.Combine(testAsset.TestRoot, project);

            var buildCommand    = new BuildCommand(Stage0MSBuild, appProjectDirectory);
            var outputDirectory = buildCommand.GetOutputDirectory(targetFramework);

            buildCommand
            .Execute()
            .Should()
            .Pass();

            outputDirectory.Should().HaveFiles(expectedFiles);

            var cleanCommand = Stage0MSBuild.CreateCommandForTarget("Clean", buildCommand.FullPathProjectFile);

            cleanCommand
            .Execute()
            .Should()
            .Pass();

            outputDirectory.Should().OnlyHaveFiles(Array.Empty <string>());
        }
Ejemplo n.º 5
0
        //  This method duplicates a lot of logic from the CLI in order to test generating deps files for tools in the SDK repo
        private CommandResult GenerateDepsAndRunTool(TestProject toolProject, [CallerMemberName] string callingMethod = "")
        {
            DeleteFolder(Path.Combine(RepoInfo.NuGetCachePath, toolProject.Name.ToLowerInvariant()));
            DeleteFolder(Path.Combine(RepoInfo.NuGetCachePath, ".tools", toolProject.Name.ToLowerInvariant()));

            var toolProjectInstance = _testAssetsManager.CreateTestProject(toolProject, callingMethod, identifier: toolProject.Name)
                                      .Restore(toolProject.Name);

            var packCommand = new PackCommand(Stage0MSBuild, Path.Combine(toolProjectInstance.TestRoot, toolProject.Name));

            packCommand.Execute()
            .Should()
            .Pass();

            string nupkgPath = Path.Combine(packCommand.ProjectRootPath, "bin", "Debug");

            TestProject toolReferencer = new TestProject()
            {
                Name             = "ToolReferencer",
                IsSdkProject     = true,
                TargetFrameworks = "netcoreapp2.0"
            };

            var toolReferencerInstance = _testAssetsManager.CreateTestProject(toolReferencer, callingMethod, identifier: toolReferencer.Name)
                                         .WithProjectChanges(project =>
            {
                var ns = project.Root.Name.Namespace;

                var itemGroup = new XElement(ns + "ItemGroup");
                project.Root.Add(itemGroup);

                itemGroup.Add(new XElement(ns + "DotNetCliToolReference",
                                           new XAttribute("Include", toolProject.Name),
                                           new XAttribute("Version", "1.0.0")));
            });

            var restoreCommand = toolReferencerInstance.GetRestoreCommand(toolReferencer.Name);

            restoreCommand.AddSource(nupkgPath);
            restoreCommand.Execute().Should().Pass();

            string toolAssetsFilePath = Path.Combine(RepoInfo.NuGetCachePath, ".tools", toolProject.Name.ToLowerInvariant(), "1.0.0", toolProject.TargetFrameworks, "project.assets.json");
            var    toolAssetsFile     = new LockFileFormat().Read(toolAssetsFilePath);

            var args = new List <string>();

            string generateDepsProjectPath = Path.Combine(RepoInfo.SdksPath, "Microsoft.NET.Sdk", "build", "GenerateDeps", "GenerateDeps.proj");

            args.Add(generateDepsProjectPath);

            args.Add($"/p:ProjectAssetsFile=\"{toolAssetsFilePath}\"");

            args.Add($"/p:ToolName={toolProject.Name}");

            string depsFilePath = Path.Combine(Path.GetDirectoryName(toolAssetsFilePath), toolProject.Name + ".deps.json");

            args.Add($"/p:ProjectDepsFilePath={depsFilePath}");

            var toolTargetFramework = toolAssetsFile.Targets.First().TargetFramework.GetShortFolderName();

            args.Add($"/p:TargetFramework={toolProject.TargetFrameworks}");

            //  Look for the .props file in the Microsoft.NETCore.App package, until NuGet
            //  generates .props and .targets files for tool restores (https://github.com/NuGet/Home/issues/5037)
            var platformLibrary = toolAssetsFile.Targets
                                  .Single()
                                  .Libraries
                                  .FirstOrDefault(e => e.Name.Equals("Microsoft.NETCore.App", StringComparison.OrdinalIgnoreCase));

            if (platformLibrary != null)
            {
                string buildRelativePath = platformLibrary.Build.FirstOrDefault()?.Path;

                var platformLibraryPath = GetPackageDirectory(toolAssetsFile, platformLibrary);

                if (platformLibraryPath != null && buildRelativePath != null)
                {
                    //  Get rid of "_._" filename
                    buildRelativePath = Path.GetDirectoryName(buildRelativePath);

                    string platformLibraryBuildFolderPath = Path.Combine(platformLibraryPath, buildRelativePath);
                    var    platformLibraryPropsFile       = Directory.GetFiles(platformLibraryBuildFolderPath, "*.props").FirstOrDefault();

                    if (platformLibraryPropsFile != null)
                    {
                        args.Add($"/p:AdditionalImport={platformLibraryPropsFile}");
                    }
                }
            }

            var generateDepsCommand = Stage0MSBuild.CreateCommandForTarget("BuildDepsJson", args.ToArray());

            generateDepsCommand.Execute()
            .Should()
            .Pass();

            var toolLibrary = toolAssetsFile.Targets
                              .Single()
                              .Libraries.FirstOrDefault(
                l => StringComparer.OrdinalIgnoreCase.Equals(l.Name, toolProject.Name));

            var toolAssembly = toolLibrary?.RuntimeAssemblies
                               .FirstOrDefault(r => Path.GetFileNameWithoutExtension(r.Path) == toolProject.Name);

            var toolPackageDirectory = GetPackageDirectory(toolAssetsFile, toolLibrary);

            var toolAssemblyPath = Path.Combine(
                toolPackageDirectory,
                toolAssembly.Path);

            var dotnetArgs = new List <string>();

            dotnetArgs.Add("exec");

            dotnetArgs.Add("--depsfile");
            dotnetArgs.Add(depsFilePath);

            foreach (var packageFolder in GetNormalizedPackageFolders(toolAssetsFile))
            {
                dotnetArgs.Add("--additionalprobingpath");
                dotnetArgs.Add(packageFolder);
            }

            dotnetArgs.Add(toolAssemblyPath);

            ICommand toolCommand = Command.Create(RepoInfo.DotNetHostPath, dotnetArgs)
                                   .CaptureStdOut();

            toolCommand = RepoInfo.AddTestEnvironmentVariables(toolCommand);


            var toolResult = toolCommand.Execute();

            return(toolResult);
        }