/// <summary> /// Copies the contents of a test project. /// </summary> /// <param name="source">The source test project.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="source"/> is null.</exception> public TestProjectData(TestProject source) : this() { if (source == null) throw new ArgumentNullException("source"); testPackage = new TestPackageData(source.TestPackage); GenericCollectionUtils.ConvertAndAddAll(source.TestFilters, testFilters, x => x.Copy()); testRunnerExtensions.AddRange(source.TestRunnerExtensionSpecifications); reportNameFormat = source.ReportNameFormat; reportDirectory = source.ReportDirectory; }
public void It_can_build_with_dynamic_loading_enabled(string targetFramework, string rollForwardValue, bool shouldSetRollForward, bool?copyLocal, bool shouldCopyLocal) { var testProject = new TestProject() { Name = "EnableDynamicLoading", TargetFrameworks = targetFramework, }; testProject.AdditionalProperties["EnableDynamicLoading"] = "true"; if (!string.IsNullOrEmpty(rollForwardValue)) { testProject.AdditionalProperties["RollForward"] = rollForwardValue; } testProject.PackageReferences.Add(new TestPackageReference("Newtonsoft.Json", "11.0.2")); if (copyLocal.HasValue) { testProject.AdditionalProperties["CopyLocalLockFileAssemblies"] = copyLocal.ToString().ToLower(); } var identifier = targetFramework + shouldSetRollForward + shouldCopyLocal + (rollForwardValue == null? "Null" : rollForwardValue); var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: identifier); var buildCommand = new BuildCommand(testAsset); buildCommand .Execute() .Should() .Pass(); string runtimeConfigName = $"{testProject.Name}.runtimeconfig.json"; var outputDirectory = buildCommand.GetOutputDirectory(testProject.TargetFrameworks); outputDirectory.Should().HaveFiles(new[] { runtimeConfigName, $"{testProject.Name}.runtimeconfig.dev.json" }); if (shouldCopyLocal) { outputDirectory.Should().HaveFile("Newtonsoft.Json.dll"); } else { outputDirectory.Should().NotHaveFile("Newtonsoft.Json.dll"); } string runtimeConfigFile = Path.Combine(outputDirectory.FullName, runtimeConfigName); string runtimeConfigContents = File.ReadAllText(runtimeConfigFile); JObject runtimeConfig = JObject.Parse(runtimeConfigContents); JToken rollForward = runtimeConfig["runtimeOptions"]["rollForward"]; if (shouldSetRollForward) { rollForward.Value <string>().Should().Be(string.IsNullOrEmpty(rollForwardValue) ? "LatestMinor" : rollForwardValue); } else { rollForward.Should().BeNull(); } }
public void It_can_use_implicitly_defined_compilation_constants(string targetFramework, string[] expectedOutput, string targetPlatformIdentifier = null, string targetPlatformVersion = null) { var testProj = new TestProject() { Name = "CompilationConstants", TargetFrameworks = targetFramework, IsExe = true, }; if (targetPlatformIdentifier != null) { testProj.AdditionalProperties["TargetPlatformIdentifier"] = targetPlatformIdentifier; testProj.AdditionalProperties["TargetPlatformVersion"] = targetPlatformVersion; } var testAsset = _testAssetsManager.CreateTestProject(testProj, targetFramework); File.WriteAllText(Path.Combine(testAsset.Path, testProj.Name, $"{testProj.Name}.cs"), @" using System; class Program { static void Main(string[] args) { #if NETCOREAPP Console.WriteLine(""NETCOREAPP""); #endif #if NETCOREAPP2_1 Console.WriteLine(""NETCOREAPP2_1""); #endif #if NETCOREAPP3_1 Console.WriteLine(""NETCOREAPP3_1""); #endif #if NETCOREAPP3_1_OR_GREATER Console.WriteLine(""NETCOREAPP3_1_OR_GREATER""); #endif #if NET Console.WriteLine(""NET""); #endif #if NET5_0 Console.WriteLine(""NET5_0""); #endif #if NET5_0_OR_GREATER Console.WriteLine(""NET5_0_OR_GREATER""); #endif #if WINDOWS Console.WriteLine(""WINDOWS""); #endif #if WINDOWS7_0 Console.WriteLine(""WINDOWS7_0""); #endif #if WINDOWS7_0_OR_GREATER Console.WriteLine(""WINDOWS7_0_OR_GREATER""); #endif #if IOS Console.WriteLine(""IOS""); #endif } }"); var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.Path, testProj.Name)); buildCommand .Execute() .Should() .Pass(); var runCommand = new RunExeCommand(Log, Path.Combine(buildCommand.GetOutputDirectory(targetFramework).FullName, $"{testProj.Name}.exe")); var stdOut = runCommand.Execute().StdOut.Split(Environment.NewLine.ToCharArray()).Where(line => !string.IsNullOrWhiteSpace(line)); stdOut.Should().BeEquivalentTo(expectedOutput); }
private void It_targets_the_right_framework( string testIdentifier, string targetFramework, string runtimeFrameworkVersion, bool selfContained, bool isExe, string expectedPackageVersion, string expectedRuntimeVersion, string extraMSBuildArguments = null) { string runtimeIdentifier = null; if (selfContained) { runtimeIdentifier = EnvironmentInfo.GetCompatibleRid(targetFramework); } var testProject = new TestProject() { Name = "FrameworkTargetTest", TargetFrameworks = targetFramework, RuntimeFrameworkVersion = runtimeFrameworkVersion, IsExe = isExe, RuntimeIdentifier = runtimeIdentifier }; var extraArgs = extraMSBuildArguments?.Split(' ') ?? Array.Empty <string>(); var testAsset = _testAssetsManager.CreateTestProject(testProject, testIdentifier); NuGetConfigWriter.Write(testAsset.TestRoot, NuGetConfigWriter.DotnetCoreBlobFeed); var buildCommand = new BuildCommand(testAsset); buildCommand .Execute(extraArgs) .Should() .Pass(); var outputDirectory = buildCommand.GetOutputDirectory(targetFramework, runtimeIdentifier: runtimeIdentifier); if (isExe) { // Self-contained apps don't write a framework version to the runtimeconfig, so only check this for framework-dependent apps if (!selfContained) { string runtimeConfigFile = Path.Combine(outputDirectory.FullName, testProject.Name + ".runtimeconfig.json"); string runtimeConfigContents = File.ReadAllText(runtimeConfigFile); JObject runtimeConfig = JObject.Parse(runtimeConfigContents); string actualRuntimeFrameworkVersion = ((JValue)runtimeConfig["runtimeOptions"]["framework"]["version"]).Value <string>(); actualRuntimeFrameworkVersion.Should().Be(expectedRuntimeVersion); } var runtimeconfigDevFileName = testProject.Name + ".runtimeconfig.dev.json"; outputDirectory.Should() .HaveFile(runtimeconfigDevFileName); string devruntimeConfigContents = File.ReadAllText(Path.Combine(outputDirectory.FullName, runtimeconfigDevFileName)); JObject devruntimeConfig = JObject.Parse(devruntimeConfigContents); var additionalProbingPaths = ((JArray)devruntimeConfig["runtimeOptions"]["additionalProbingPaths"]).Values <string>(); // can't use Path.Combine on segments with an illegal `|` character var expectedPath = $"{Path.Combine(FileConstants.UserProfileFolder, ".dotnet", "store")}{Path.DirectorySeparatorChar}|arch|{Path.DirectorySeparatorChar}|tfm|"; additionalProbingPaths.Should().Contain(expectedPath); } LockFile lockFile = LockFileUtilities.GetLockFile(Path.Combine(buildCommand.ProjectRootPath, "obj", "project.assets.json"), NullLogger.Instance); var target = lockFile.GetTarget(NuGetFramework.Parse(targetFramework), null); var netCoreAppLibrary = target.Libraries.Single(l => l.Name == "Microsoft.NETCore.App"); netCoreAppLibrary.Version.ToString().Should().Be(expectedPackageVersion); }
public void RunAppFromOutputFolder(string testName, bool useRid, bool includeConflicts) { if (UsingFullFrameworkMSBuild) { // Disabled on full framework MSBuild until CI machines have VS with bundled .NET Core / .NET Standard versions // See https://github.com/dotnet/sdk/issues/1077 return; } var targetFramework = "netcoreapp2.0"; var runtimeIdentifier = useRid ? EnvironmentInfo.GetCompatibleRid(targetFramework) : null; TestProject project = new TestProject() { Name = testName, IsSdkProject = true, TargetFrameworks = targetFramework, RuntimeIdentifier = runtimeIdentifier, IsExe = true, }; string outputMessage = $"Hello from {project.Name}!"; project.SourceFiles["Program.cs"] = @" using System; public static class Program { public static void Main() { TestConflictResolution(); Console.WriteLine(""" + outputMessage + @"""); } " + ConflictResolutionAssets.ConflictResolutionTestMethod + @" } "; var testAsset = _testAssetsManager.CreateTestProject(project, project.Name) .WithProjectChanges(p => { if (includeConflicts) { var ns = p.Root.Name.Namespace; var itemGroup = new XElement(ns + "ItemGroup"); p.Root.Add(itemGroup); foreach (var dependency in ConflictResolutionAssets.ConflictResolutionDependencies) { itemGroup.Add(new XElement(ns + "PackageReference", new XAttribute("Include", dependency.Item1), new XAttribute("Version", dependency.Item2))); } } }) .Restore(project.Name); string projectFolder = Path.Combine(testAsset.Path, project.Name); var buildCommand = new BuildCommand(Stage0MSBuild, projectFolder); buildCommand .Execute() .Should() .Pass(); string outputFolder = buildCommand.GetOutputDirectory(project.TargetFrameworks, runtimeIdentifier: runtimeIdentifier ?? "").FullName; Command.Create(RepoInfo.DotNetHostPath, new[] { Path.Combine(outputFolder, project.Name + ".dll") }) .CaptureStdOut() .Execute() .Should() .Pass() .And .HaveStdOutContaining(outputMessage); }
public void WinMDInteropProjectCanBeReferenced() { var projectionProject = new TestProject("SimpleMathProjection") { TargetFrameworks = "net5.0-windows10.0.19041.0" }; projectionProject.AdditionalProperties["CsWinRTIncludes"] = "SimpleMathComponent"; projectionProject.AdditionalProperties["CsWinRTGeneratedFilesDir"] = "$(OutDir)"; projectionProject.AdditionalProperties["CsWinRTWindowsMetadata"] = "10.0.19041.0"; // TODO: Update to latest version projectionProject.PackageReferences.Add(new TestPackageReference("Microsoft.Windows.CsWinRT", "1.2.3")); // Don't auto-generate a source file projectionProject.SourceFiles["Empty.cs"] = ""; projectionProject.ProjectChanges.Add(project => { var ns = project.Root.Name.Namespace; var itemGroup = new XElement(ns + "ItemGroup"); project.Root.Add(itemGroup); itemGroup.Add(new XElement(ns + "ProjectReference", new XAttribute("Include", @"..\SimpleMathComponent\SimpleMathComponent.vcxproj"))); }); var consoleApp = new TestProject("ConsoleApp") { TargetFrameworks = "net5.0-windows10.0.19041.0", IsExe = true }; consoleApp.ReferencedProjects.Add(projectionProject); // Workaround for PrivateAssets consoleApp.PackageReferences.Add(new TestPackageReference("Microsoft.Windows.CsWinRT", "1.2.3")); consoleApp.SourceFiles["Program.cs"] = @" using System; var x = new SimpleMathComponent.SimpleMath(); Console.WriteLine(""Adding 5.5 + 6.5...""); Console.WriteLine(x.add(5.5, 6.5).ToString());"; var testAsset = _testAssetsManager.CreateTestProject(consoleApp); // Copy C++ project file which is referenced var cppWinMDSourceDirectory = Path.Combine(_testAssetsManager.GetAndValidateTestProjectDirectory("CppWinMDComponent"), "SimpleMathComponent"); var cppWinTargetDirectory = Path.Combine(testAsset.TestRoot, "SimpleMathComponent"); Directory.CreateDirectory(cppWinTargetDirectory); foreach (var file in Directory.GetFiles(cppWinMDSourceDirectory)) { File.Copy(file, Path.Combine(cppWinTargetDirectory, Path.GetFileName(file))); } new NuGetExeRestoreCommand(Log, cppWinTargetDirectory) { PackagesDirectory = Path.Combine(testAsset.Path, "packages") } .Execute() .Should() .Pass(); var buildCommand = new BuildCommand(testAsset); buildCommand.Execute() .Should() .Pass(); }
private TestProject DiamondShapeGraphWithRuntimeDependencies() { var project5 = new TestProject { Name = "5", IsSdkProject = true, TargetFrameworks = "netstandard1.3", SourceFiles = { ["Program.cs"] = string.Format(SourceFile, "5", string.Empty) } }; var project4 = new TestProject { Name = "4", IsSdkProject = true, TargetFrameworks = "netstandard1.3;netstandard1.6;net461", ReferencedProjects = { project5 }, SourceFiles = { ["Program.cs"] = string.Format(SourceFile, "4", "_5.Class1.Message();") } }; var project3 = new TestProject { Name = "3", IsSdkProject = true, TargetFrameworks = "netstandard2.0;net462", ReferencedProjects = { project4 }, SourceFiles = { ["Program.cs"] = string.Format(SourceFile, "3", "_4.Class1.Message();") } }; var project2 = new TestProject { Name = "2", IsSdkProject = true, TargetFrameworks = "netstandard1.5", ReferencedProjects = { project4 }, SourceFiles = { ["Program.cs"] = string.Format(SourceFile, "2", "_4.Class1.Message();") } }; var project1 = new TestProject { Name = "1", IsExe = true, IsSdkProject = true, TargetFrameworks = "netcoreapp2.1;net472", ReferencedProjects = { project2, project3 }, SourceFiles = { ["Program.cs"] = string.Format(SourceFile, "1", " _2.Class1.Message(); _3.Class1.Message();") } }; return(project1); }
public void It_trims_conflicts_from_the_deps_file(string targetFramework) { TestProject project = new TestProject() { Name = "NetCore2App", TargetFrameworks = targetFramework, IsExe = true, IsSdkProject = true }; project.SourceFiles["Program.cs"] = @" using System; public static class Program { public static void Main() { TestConflictResolution(); Console.WriteLine(""Hello, World!""); } " + ConflictResolutionAssets.ConflictResolutionTestMethod + @" } "; var testAsset = _testAssetsManager.CreateTestProject(project, identifier: targetFramework) .WithProjectChanges(p => { var ns = p.Root.Name.Namespace; var itemGroup = new XElement(ns + "ItemGroup"); p.Root.Add(itemGroup); foreach (var dependency in ConflictResolutionAssets.ConflictResolutionDependencies) { itemGroup.Add(new XElement(ns + "PackageReference", new XAttribute("Include", dependency.Item1), new XAttribute("Version", dependency.Item2))); } }) .Restore(Log, project.Name); string projectFolder = Path.Combine(testAsset.Path, project.Name); var buildCommand = new BuildCommand(Log, projectFolder); buildCommand .Execute() .Should() .Pass(); string outputFolder = buildCommand.GetOutputDirectory(project.TargetFrameworks).FullName; using (var depsJsonFileStream = File.OpenRead(Path.Combine(outputFolder, $"{project.Name}.deps.json"))) { var dependencyContext = new DependencyContextJsonReader().Read(depsJsonFileStream); dependencyContext.Should() .OnlyHaveRuntimeAssemblies("", project.Name) .And .HaveNoDuplicateRuntimeAssemblies("") .And .HaveNoDuplicateNativeAssets("");; } }
internal ParserTestBase() { TestProjectRoot = TestProject.GetProjectDirectory(GetType()); }
public void TestProjectEqualsWhenAllPropertiesEqual() { // Arrange var testProjectAnalyzerResult = Mock.Of <IAnalyzerResult>(); var testCase1Id = Guid.NewGuid(); var fileA = new TestFile { FilePath = "/c/", Source = "bla", Tests = new HashSet <TestCase> { new TestCase { Id = testCase1Id, Line = 1, Name = "test1", Source = "bla" }, new TestCase { Id = Guid.NewGuid(), Line = 2, Name = "test2", Source = "bla" } } }; var fileB = new TestFile { FilePath = "/c/", Source = "bla", Tests = new HashSet <TestCase> { new TestCase { Id = testCase1Id, Line = 1, Name = "test1", Source = "bla" } } }; var testProjectA = new TestProject { TestProjectAnalyzerResult = testProjectAnalyzerResult, TestFiles = new HashSet <TestFile> { fileA } }; var testProjectB = new TestProject { TestProjectAnalyzerResult = testProjectAnalyzerResult, TestFiles = new HashSet <TestFile> { fileB } }; // Assert testProjectA.ShouldBe(testProjectB); testProjectA.GetHashCode().ShouldBe(testProjectB.GetHashCode()); }
bool AllProjectsBuildOnNonWindows(TestProject referencerProject) { return(referencerProject.BuildsOnNonWindows && referencerProject.ReferencedProjects.All(rp => rp.BuildsOnNonWindows)); }
public void RunFilesCopiedToPublishDirTest(bool specifyRid, bool singleFile) { var testProject = new TestProject() { Name = "TestFilesCopiedToPublishDir", TargetFrameworks = "netcoreapp3.0", IsSdkProject = true, IsExe = true }; testProject.AdditionalProperties["RuntimeIdentifiers"] = "win-x86"; testProject.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\..\pkg"; if (specifyRid) { testProject.AdditionalProperties["RuntimeIdentifier"] = "win-x86"; } if (singleFile) { testProject.AdditionalProperties["PublishSingleFile"] = "true"; } var testAsset = _testAssetsManager.CreateTestProject(testProject); var restoreCommand = new RestoreCommand(Log, testAsset.Path, testProject.Name); restoreCommand .Execute() .Should() .Pass(); var command = new GetValuesCommand( Log, Path.Combine(testAsset.Path, testProject.Name), testProject.TargetFrameworks, "FilesCopiedToPublishDir", GetValuesCommand.ValueType.Item) { DependsOnTargets = "ComputeFilesCopiedToPublishDir", MetadataNames = { "RelativePath", "IsKeyOutput" }, }; command.Execute().Should().Pass(); var items = from item in command.GetValuesWithMetadata() select new { Identity = item.value, RelativePath = item.metadata["RelativePath"] }; Log.WriteLine("FilesCopiedToPublishDir contains '{0}' items:", items.Count()); foreach (var item in items) { Log.WriteLine(" '{0}': RelativePath = '{1}'", item.Identity, item.RelativePath); } // Check for the main exe if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { string exeSuffix = specifyRid ? ".exe" : Constants.ExeSuffix; items.Should().ContainSingle(i => i.RelativePath.Equals($"{testProject.Name}{exeSuffix}", StringComparison.OrdinalIgnoreCase)); } // Framework assemblies should be there if we specified and rid and this isn't in the single file case if (specifyRid && !singleFile) { FrameworkAssemblies.ForEach(fa => items.Should().ContainSingle(i => i.RelativePath.Equals(fa, StringComparison.OrdinalIgnoreCase))); } else { FrameworkAssemblies.ForEach(fa => items.Should().NotContain(i => i.RelativePath.Equals(fa, StringComparison.OrdinalIgnoreCase))); } // FilesCopiedToPublishDir should never contain the deps.json file items.Should().NotContain(i => i.RelativePath.Equals($"{testProject.Name}.deps.json", StringComparison.OrdinalIgnoreCase)); }
public void It_can_use_HttpClient_and_exchange_the_type_with_a_NETStandard_library() { var netStandardLibrary = new TestProject() { Name = "NETStandardLibrary", TargetFrameworks = "netstandard1.4", IsSdkProject = true }; netStandardLibrary.SourceFiles["NETStandard.cs"] = @" using System.Net.Http; public class NETStandard { public static HttpClient GetHttpClient() { return new HttpClient(); } } "; var netFrameworkLibrary = new TestProject() { Name = "NETFrameworkLibrary", TargetFrameworks = "net461", IsSdkProject = true }; netFrameworkLibrary.ReferencedProjects.Add(netStandardLibrary); netFrameworkLibrary.SourceFiles["NETFramework.cs"] = @" using System.Net.Http; public class NETFramework { public void Method1() { System.Net.Http.HttpClient client = NETStandard.GetHttpClient(); } } "; var testAsset = _testAssetsManager.CreateTestProject(netFrameworkLibrary, "ExchangeHttpClient") .WithProjectChanges((projectPath, project) => { if (Path.GetFileName(projectPath).Equals(netFrameworkLibrary.Name + ".csproj", StringComparison.OrdinalIgnoreCase)) { var ns = project.Root.Name.Namespace; var itemGroup = new XElement(ns + "ItemGroup"); project.Root.Add(itemGroup); itemGroup.Add(new XElement(ns + "Reference", new XAttribute("Include", "System.Net.Http"))); } }) .Restore(Log, netFrameworkLibrary.Name); var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, netFrameworkLibrary.Name)); buildCommand .Execute() .Should() .Pass(); }
public void It_marks_extension_references_as_externally_resolved(bool?markAsExternallyResolved) { var project = new TestProject { Name = "NETFrameworkLibrary", TargetFrameworks = "net462", IsSdkProject = true }; var netStandard2Project = new TestProject { Name = "NETStandard20Project", TargetFrameworks = "netstandard2.0", IsSdkProject = true }; project.ReferencedProjects.Add(netStandard2Project); var asset = _testAssetsManager.CreateTestProject( project, "ExternallyResolvedExtensions", markAsExternallyResolved.ToString()) .WithProjectChanges((path, p) => { if (markAsExternallyResolved != null) { var ns = p.Root.Name.Namespace; p.Root.Add( new XElement(ns + "PropertyGroup", new XElement(ns + "MarkNETFrameworkExtensionAssembliesAsExternallyResolved", markAsExternallyResolved))); } }) .Restore(Log, project.Name); var command = new GetValuesCommand( Log, Path.Combine(asset.Path, project.Name), project.TargetFrameworks, "Reference", GetValuesCommand.ValueType.Item); command.MetadataNames.AddRange(new[] { "ExternallyResolved", "HintPath" }); command.Execute().Should().Pass(); int frameworkReferenceCount = 0; int extensionReferenceCount = 0; var references = command.GetValuesWithMetadata(); foreach (var(value, metadata) in references) { if (metadata["HintPath"] == "") { // implicit framework reference (not externally resolved) metadata["ExternallyResolved"].Should().BeEmpty(); frameworkReferenceCount++; } else { // reference added by Microsoft.NET.Build.Extensions metadata["ExternallyResolved"].Should().BeEquivalentTo((markAsExternallyResolved ?? true).ToString()); extensionReferenceCount++; } } // make sure both cases were encountered frameworkReferenceCount.Should().BeGreaterThan(0); extensionReferenceCount.Should().BeGreaterThan(0); }
/// <summary> /// Creates a test project with the contents of this structure. /// </summary> /// <returns>The test project.</returns> public TestProject ToTestProject() { var testProject = new TestProject(); InitializeTestProject(testProject); return testProject; }
public void It_uses_hintpath_when_replacing_simple_name_references(bool useFacades) { TestProject project = new TestProject() { Name = "NETFrameworkLibrary", TargetFrameworks = "net462", IsSdkProject = true }; if (useFacades) { var netStandard2Project = new TestProject() { Name = "NETStandard20Project", TargetFrameworks = "netstandard2.0", IsSdkProject = true }; project.ReferencedProjects.Add(netStandard2Project); } var testAsset = _testAssetsManager.CreateTestProject(project, "SimpleNamesWithHintPaths", identifier: useFacades ? "_useFacades" : "") .WithProjectChanges((path, p) => { if (Path.GetFileNameWithoutExtension(path) == project.Name) { var ns = p.Root.Name.Namespace; var itemGroup = new XElement(ns + "ItemGroup"); p.Root.Add(itemGroup); if (!useFacades) { itemGroup.Add(new XElement(ns + "PackageReference", new XAttribute("Include", "System.Net.Http"), new XAttribute("Version", "4.3.2"))); } itemGroup.Add(new XElement(ns + "Reference", new XAttribute("Include", "System.Net.Http"))); } }) .Restore(Log, project.Name); string projectFolder = Path.Combine(testAsset.Path, project.Name); var getValuesCommand = new GetValuesCommand(Log, projectFolder, project.TargetFrameworks, "Reference", GetValuesCommand.ValueType.Item); getValuesCommand.MetadataNames.Add("HintPath"); getValuesCommand .Execute() .Should() .Pass(); string correctHttpReference; if (useFacades) { correctHttpReference = Path.Combine(TestContext.Current.ToolsetUnderTest.BuildExtensionsMSBuildPath, @"net461\lib\System.Net.Http.dll"); } else { correctHttpReference = Path.Combine(TestContext.Current.NuGetCachePath, "system.net.http", "4.3.2", "ref", "net46", "System.Net.Http.dll"); } var valuesWithMetadata = getValuesCommand.GetValuesWithMetadata(); // There shouldn't be a Reference item where the ItemSpec is the path to the System.Net.Http.dll from a NuGet package valuesWithMetadata.Should().NotContain(v => v.value == correctHttpReference); // There should be a Reference item where the ItemSpec is the simple name System.Net.Http valuesWithMetadata.Should().ContainSingle(v => v.value == "System.Net.Http"); // The Reference item with the simple name should have a HintPath to the DLL in the NuGet package valuesWithMetadata.Single(v => v.value == "System.Net.Http") .metadata["HintPath"] .Should().Be(correctHttpReference); }
public void It_checks_for_valid_references(string referencerTarget, bool referencerIsSdkProject, string dependencyTarget, bool dependencyIsSdkProject, bool restoreSucceeds, bool buildSucceeds) { string identifier = referencerTarget.ToString() + " " + dependencyTarget.ToString(); // MSBuild isn't happy with semicolons in the path when doing file exists checks identifier = identifier.Replace(';', '_'); TestProject referencerProject = GetTestProject("Referencer", referencerTarget, referencerIsSdkProject); TestProject dependencyProject = GetTestProject("Dependency", dependencyTarget, dependencyIsSdkProject); referencerProject.ReferencedProjects.Add(dependencyProject); // Set the referencer project as an Exe unless it targets .NET Standard if (!referencerProject.TargetFrameworkIdentifiers.Contains(ConstantStringValues.NetstandardTargetFrameworkIdentifier)) { referencerProject.IsExe = true; } // Skip running test if not running on Windows if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (!referencerProject.BuildsOnNonWindows || !dependencyProject.BuildsOnNonWindows) { return; } } var testAsset = _testAssetsManager.CreateTestProject(referencerProject, nameof(It_checks_for_valid_references), identifier); var restoreCommand = testAsset.GetRestoreCommand(Log, relativePath: "Referencer"); if (restoreSucceeds) { restoreCommand .Execute() .Should() .Pass(); } else { restoreCommand .Execute() .Should() .Fail(); } if (!referencerProject.IsSdkProject) { // The Restore target currently seems to be a no-op for non-SDK projects, // so we need to explicitly restore the dependency testAsset.GetRestoreCommand(Log, relativePath: "Dependency") .Execute() .Should() .Pass(); } var appProjectDirectory = Path.Combine(testAsset.TestRoot, "Referencer"); var buildCommand = new BuildCommand(Log, appProjectDirectory); var result = buildCommand.Execute(); if (buildSucceeds) { result.Should().Pass(); } else if (referencerIsSdkProject) { result.Should().Fail().And.HaveStdOutContaining("NU1201"); } else { result.Should().Fail() .And.HaveStdOutContaining("It cannot be referenced by a project that targets"); } }
private TestProject CreateTestProjectForILLinkTesting( string targetFramework, string mainProjectName, string referenceProjectName = null, bool usePackageReference = true, [CallerMemberName] string callingMethod = "", string referenceProjectIdentifier = "") { var testProject = new TestProject() { Name = mainProjectName, TargetFrameworks = targetFramework, IsSdkProject = true, }; testProject.SourceFiles[$"{mainProjectName}.cs"] = @" using System; public class Program { public static void Main() { Console.WriteLine(""Hello world""); } } "; if (referenceProjectName == null) { return(testProject); } var referenceProject = new TestProject() { Name = referenceProjectName, // NOTE: If using a package reference for the reference project, it will be retrieved // from the nuget cache. Set the reference project TFM to the lowest common denominator // of these tests to prevent conflicts. TargetFrameworks = usePackageReference ? "netcoreapp3.0" : targetFramework, IsSdkProject = true }; referenceProject.SourceFiles[$"{referenceProjectName}.cs"] = @" using System; public class ClassLib { public void UnusedMethod() { } public void UnusedMethodToRoot() { } public static bool FeatureDisabled { get; } public static void FeatureAPI() { if (FeatureDisabled) return; FeatureImplementation(); } public static void FeatureImplementation() { } } "; if (usePackageReference) { var packageReference = GetPackageReference(referenceProject, callingMethod, referenceProjectIdentifier); testProject.PackageReferences.Add(packageReference); testProject.AdditionalProperties.Add( "RestoreAdditionalProjectSources", "$(RestoreAdditionalProjectSources);" + Path.GetDirectoryName(packageReference.NupkgPath)); } else { testProject.ReferencedProjects.Add(referenceProject); } testProject.SourceFiles[$"{referenceProjectName}.xml"] = $@" <linker> <assembly fullname=""{referenceProjectName}""> <type fullname=""ClassLib""> <method name=""UnusedMethodToRoot"" /> <method name=""FeatureAPI"" /> </type> </assembly> </linker> "; return(testProject); }
public void ItRunsAppsReferencingAProjectDirectlyReferencingAssembliesWhichReferenceAssemblies( string referencerTarget, string dependencyTarget, string dllDependencyTarget) { string identifier = referencerTarget.ToString() + "_" + dependencyTarget.ToString(); TestProject dllDependencyProjectDependency = new TestProject() { Name = "DllDependencyDependency", IsSdkProject = true, TargetFrameworks = dllDependencyTarget, }; dllDependencyProjectDependency.SourceFiles["Class3.cs"] = @" public class Class3 { public static string GetMessage() { return ""Hello from a reference of an indirect reference.""; } } "; // Skip running test if not running on Windows if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !dllDependencyProjectDependency.BuildsOnNonWindows) { return; } TestProject dllDependencyProject = new TestProject() { Name = "DllDependency", IsSdkProject = true, TargetFrameworks = dllDependencyTarget, }; dllDependencyProject.ReferencedProjects.Add(dllDependencyProjectDependency); dllDependencyProject.SourceFiles["Class2.cs"] = @" public class Class2 { public static string GetMessage() { return Class3.GetMessage(); } } "; var dllDependencyAsset = _testAssetsManager.CreateTestProject(dllDependencyProject, identifier: identifier); string dllDependencyAssemblyPath = RestoreAndBuild(dllDependencyAsset, dllDependencyProject); TestProject dependencyProject = new TestProject() { Name = "Dependency", IsSdkProject = true, TargetFrameworks = dependencyTarget, }; dependencyProject.References.Add(dllDependencyAssemblyPath); dependencyProject.SourceFiles["Class1.cs"] = @" public class Class1 { public static string GetMessage() { return Class2.GetMessage(); } } "; TestProject referencerProject = new TestProject() { Name = "Referencer", IsSdkProject = true, TargetFrameworks = referencerTarget, // Need to use a self-contained app for now because we don't use a CLI that has a "2.0" shared framework RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid(referencerTarget), IsExe = true, }; referencerProject.ReferencedProjects.Add(dependencyProject); referencerProject.SourceFiles["Program.cs"] = @" using System; public static class Program { public static void Main() { Console.WriteLine(Class1.GetMessage()); } } "; var referencerAsset = _testAssetsManager.CreateTestProject(referencerProject, identifier: identifier); string applicationPath = RestoreAndBuild(referencerAsset, referencerProject); Command.Create(TestContext.Current.ToolsetUnderTest.DotNetHostPath, new[] { applicationPath }) .CaptureStdOut() .Execute() .Should().Pass() .And.HaveStdOutContaining("Hello from a reference of an indirect reference."); }
public void ManagedWinRTComponentCanBeReferenced() { var managedWinRTComponent = new TestProject() { Name = "ManagedWinRTComponent", TargetFrameworks = "net5.0-windows10.0.19041.0", }; managedWinRTComponent.AdditionalProperties.Add("CsWinRTWindowsMetadata", "10.0.19041.0"); managedWinRTComponent.AdditionalProperties.Add("CsWinRTComponent", "true"); managedWinRTComponent.AdditionalProperties.Add("PlatformTarget", "x64"); // TODO: Update to latest (currently 1.2.5) once it shows up on dotnet-public feed managedWinRTComponent.PackageReferences.Add(new TestPackageReference("Microsoft.Windows.CsWinRT", "1.2.3")); managedWinRTComponent.SourceFiles["Coords.cs"] = @"using System; namespace ManagedWinRTComponent { public sealed class Coord { public double X; public double Y; public Coord() { X = 0.0; Y = 0.0; } public Coord(double x, double y) { X = x; Y = y; } public double Distance(Coord dest) { double deltaX = (this.X - dest.X); double deltaY = (this.Y - dest.Y); return Math.Sqrt(deltaX * deltaX + deltaY * deltaY); } public override string ToString() { return ""("" + this.X + "", "" + this.Y + "")""; } } } "; var consoleApp = new TestProject() { Name = "ConsoleApp", IsExe = true, TargetFrameworks = managedWinRTComponent.TargetFrameworks }; consoleApp.AdditionalProperties["PlatformTarget"] = "x64"; consoleApp.ReferencedProjects.Add(managedWinRTComponent); consoleApp.SourceFiles["Program.cs"] = @" using System; class Program { static void Main(string[] args) { Console.WriteLine(new ManagedWinRTComponent.Coord().ToString()); } }"; var testAsset = _testAssetsManager.CreateTestProject(consoleApp); // Disable workaround for NETSDK1130 which is in Microsoft.Windows.CsWinRT File.WriteAllText(Path.Combine(testAsset.TestRoot, "Directory.Build.targets"), @"<Project> <Target Name=""UpdateResolveableAssembly"" Returns=""@(TargetPathWithTargetPlatformMoniker)"" AfterTargets=""GetTargetPath""> <ItemGroup> <TargetPathWithTargetPlatformMoniker Update=""$(TargetDir)$(AssemblyName).winmd""> <ResolveableAssembly>true</ResolveableAssembly> </TargetPathWithTargetPlatformMoniker> </ItemGroup> </Target> </Project> "); var buildCommand = new BuildCommand(testAsset); buildCommand.Execute() .Should() .Pass(); // Make sure the app can run successfully var exePath = Path.Combine(buildCommand.GetOutputDirectory(consoleApp.TargetFrameworks).FullName, consoleApp.Name + ".exe"); new RunExeCommand(Log, exePath) .Execute() .Should() .Pass() .And .HaveStdOut("(0, 0)"); }
public void ItRunsAppsDirectlyReferencingAssembliesWithSatellites( string referencerTarget, string dependencyTarget) { string identifier = referencerTarget.ToString() + "_" + dependencyTarget.ToString(); TestProject dependencyProject = new TestProject() { Name = "Dependency", IsSdkProject = true, TargetFrameworks = dependencyTarget, }; // Skip running test if not running on Windows if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !dependencyProject.BuildsOnNonWindows) { return; } dependencyProject.SourceFiles["Class1.cs"] = @" using System; using System.Globalization; using System.Reflection; using System.Resources; using System.Threading; public class Class1 { public static string GetMessage() { CultureInfo.CurrentUICulture = new CultureInfo(""en-US""); var resources = new ResourceManager(""Dependency.Strings"", typeof(Class1).GetTypeInfo().Assembly); return resources.GetString(""HelloWorld""); } } "; dependencyProject.EmbeddedResources["Strings.en.resx"] = @"<?xml version=""1.0"" encoding=""utf-8""?> <root> <xsd:schema id=""root"" xmlns="""" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata""> <xsd:element name=""root"" msdata:IsDataSet=""true""> <xsd:complexType> <xsd:choice maxOccurs=""unbounded""> <xsd:element name=""data""> <xsd:complexType> <xsd:sequence> <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> <xsd:element name=""comment"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""2"" /> </xsd:sequence> <xsd:attribute name=""name"" type=""xsd:string"" msdata:Ordinal=""1"" /> <xsd:attribute name=""type"" type=""xsd:string"" msdata:Ordinal=""3"" /> <xsd:attribute name=""mimetype"" type=""xsd:string"" msdata:Ordinal=""4"" /> </xsd:complexType> </xsd:element> <xsd:element name=""resheader""> <xsd:complexType> <xsd:sequence> <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> </xsd:sequence> <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" /> </xsd:complexType> </xsd:element> </xsd:choice> </xsd:complexType> </xsd:element> </xsd:schema> <resheader name=""resmimetype""> <value>text/microsoft-resx</value> </resheader> <resheader name=""version""> <value>1.3</value> </resheader> <resheader name=""reader""> <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> </resheader> <resheader name=""writer""> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> </resheader> <data name=""HelloWorld"" xml:space=""preserve""> <value>Hello World from en satellite assembly for a direct reference.</value> </data> </root> "; var dependencyAsset = _testAssetsManager.CreateTestProject(dependencyProject, identifier: identifier); string dependencyAssemblyPath = RestoreAndBuild(dependencyAsset, dependencyProject); TestProject referencerProject = new TestProject() { Name = "Referencer", IsSdkProject = true, TargetFrameworks = referencerTarget, // Need to use a self-contained app for now because we don't use a CLI that has a "2.0" shared framework RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid(referencerTarget), IsExe = true, }; referencerProject.References.Add(dependencyAssemblyPath); referencerProject.SourceFiles["Program.cs"] = @" using System; public static class Program { public static void Main() { Console.WriteLine(Class1.GetMessage()); } } "; var referencerAsset = _testAssetsManager.CreateTestProject(referencerProject, identifier: identifier); string applicationPath = RestoreAndBuild(referencerAsset, referencerProject); Command.Create(TestContext.Current.ToolsetUnderTest.DotNetHostPath, new[] { applicationPath }) .CaptureStdOut() .Execute() .Should().Pass() .And.HaveStdOutContaining("Hello World from en satellite assembly for a direct reference."); }
public void It_checks_for_valid_references(string referencerTarget, bool referencerIsSdkProject, string dependencyTarget, bool dependencyIsSdkProject, bool restoreSucceeds, bool buildSucceeds) { string identifier = referencerTarget.ToString() + " " + dependencyTarget.ToString(); TestProject referencerProject = GetTestProject("Referencer", referencerTarget, referencerIsSdkProject); TestProject dependencyProject = GetTestProject("Dependency", dependencyTarget, dependencyIsSdkProject); referencerProject.ReferencedProjects.Add(dependencyProject); // Set the referencer project as an Exe unless it targets .NET Standard if (!referencerProject.ShortTargetFrameworkIdentifiers.Contains("netstandard")) { referencerProject.IsExe = true; } // Skip running test if not running on Windows if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (!referencerProject.BuildsOnNonWindows || !dependencyProject.BuildsOnNonWindows) { return; } } var testAsset = _testAssetsManager.CreateTestProject(referencerProject, nameof(It_checks_for_valid_references), identifier); var restoreCommand = testAsset.GetRestoreCommand(relativePath: "Referencer"); if (restoreSucceeds) { restoreCommand .Execute() .Should() .Pass(); } else { restoreCommand .CaptureStdOut() .Execute() .Should() .Fail(); } if (!referencerProject.IsSdkProject) { // The Restore target currently seems to be a no-op for non-SDK projects, // so we need to explicitly restore the dependency testAsset.GetRestoreCommand(relativePath: "Dependency") .Execute() .Should() .Pass(); } var appProjectDirectory = Path.Combine(testAsset.TestRoot, "Referencer"); var buildCommand = new BuildCommand(Stage0MSBuild, appProjectDirectory); if (!buildSucceeds) { buildCommand = buildCommand.CaptureStdOut(); } // Suppress ResolveAssemblyReference warning output due to https://github.com/Microsoft/msbuild/issues/1329 if (buildSucceeds && referencerProject.IsExe && referencerProject.ShortTargetFrameworkIdentifiers.Contains("net")) { buildCommand = buildCommand.CaptureStdOut(); } var result = buildCommand.Execute(); if (buildSucceeds) { result.Should().Pass(); } else { result.Should().Fail() .And.HaveStdOutContaining("has no target framework compatible with"); } }
public static string ConstructNuGetPackageReferencePath(TestProject dependencyProject) { return(TestAssetsManager.GetTestDestinationDirectoryPath(dependencyProject.Name, TestDirectoriesNamePrefix, NuGetSharedDirectoryNamePostfix)); }
public void It_trims_conflicts_from_the_deps_file() { if (UsingFullFrameworkMSBuild) { // Disabled on full framework MSBuild until CI machines have VS with bundled .NET Core / .NET Standard versions // See https://github.com/dotnet/sdk/issues/1077 return; } TestProject project = new TestProject() { Name = "NetCore2App", TargetFrameworks = "netcoreapp2.0", IsExe = true, IsSdkProject = true }; project.SourceFiles["Program.cs"] = @" using System; public static class Program { public static void Main() { TestConflictResolution(); Console.WriteLine(""Hello, World!""); } " + ConflictResolutionAssets.ConflictResolutionTestMethod + @" } "; var testAsset = _testAssetsManager.CreateTestProject(project) .WithProjectChanges(p => { var ns = p.Root.Name.Namespace; var itemGroup = new XElement(ns + "ItemGroup"); p.Root.Add(itemGroup); foreach (var dependency in ConflictResolutionAssets.ConflictResolutionDependencies) { itemGroup.Add(new XElement(ns + "PackageReference", new XAttribute("Include", dependency.Item1), new XAttribute("Version", dependency.Item2))); } }) .Restore(project.Name); string projectFolder = Path.Combine(testAsset.Path, project.Name); var buildCommand = new BuildCommand(Stage0MSBuild, projectFolder); buildCommand .Execute() .Should() .Pass(); string outputFolder = buildCommand.GetOutputDirectory(project.TargetFrameworks).FullName; using (var depsJsonFileStream = File.OpenRead(Path.Combine(outputFolder, $"{project.Name}.deps.json"))) { var dependencyContext = new DependencyContextJsonReader().Read(depsJsonFileStream); dependencyContext.Should() .OnlyHaveRuntimeAssemblies("", project.Name) .And .HaveNoDuplicateRuntimeAssemblies("") .And .HaveNoDuplicateNativeAssets("");; } }
void Conflicts_are_resolved_when_publishing(bool selfContained, bool ridSpecific, [CallerMemberName] string callingMethod = "") { if (selfContained && !ridSpecific) { throw new ArgumentException("Self-contained apps must be rid specific"); } var targetFramework = "netcoreapp2.0"; if (!EnvironmentInfo.SupportsTargetFramework(targetFramework)) { return; } var rid = ridSpecific ? EnvironmentInfo.GetCompatibleRid(targetFramework) : null; TestProject testProject = new TestProject() { Name = selfContained ? "SelfContainedWithConflicts" : (ridSpecific ? "RidSpecificSharedConflicts" : "PortableWithConflicts"), TargetFrameworks = targetFramework, RuntimeIdentifier = rid, IsExe = true, }; string outputMessage = $"Hello from {testProject.Name}!"; testProject.AdditionalProperties["CopyLocalLockFileAssemblies"] = "true"; testProject.SourceFiles["Program.cs"] = @" using System; public static class Program { public static void Main() { TestConflictResolution(); Console.WriteLine(""" + outputMessage + @"""); } " + ConflictResolutionAssets.ConflictResolutionTestMethod + @" } "; var testProjectInstance = _testAssetsManager.CreateTestProject(testProject, testProject.Name) .WithProjectChanges(p => { var ns = p.Root.Name.Namespace; var itemGroup = new XElement(ns + "ItemGroup"); p.Root.Add(itemGroup); foreach (var dependency in ConflictResolutionAssets.ConflictResolutionDependencies) { itemGroup.Add(new XElement(ns + "PackageReference", new XAttribute("Include", dependency.Item1), new XAttribute("Version", dependency.Item2))); } if (!selfContained && ridSpecific) { var propertyGroup = new XElement(ns + "PropertyGroup"); p.Root.Add(propertyGroup); propertyGroup.Add(new XElement(ns + "SelfContained", "false")); } }); var publishCommand = new PublishCommand(testProjectInstance); var publishResult = publishCommand.Execute(); publishResult.Should().Pass(); var publishDirectory = publishCommand.GetOutputDirectory( targetFramework: targetFramework, runtimeIdentifier: rid ?? string.Empty); var outputDirectory = publishDirectory.Parent; DependencyContext dependencyContext; using (var depsJsonFileStream = File.OpenRead(Path.Combine(publishDirectory.FullName, $"{testProject.Name}.deps.json"))) { dependencyContext = new DependencyContextJsonReader().Read(depsJsonFileStream); } dependencyContext.Should() .HaveNoDuplicateRuntimeAssemblies(rid ?? "") .And .HaveNoDuplicateNativeAssets(rid ?? "") .And .OnlyHavePackagesWithPathProperties(); TestCommand runCommand; if (selfContained) { var selfContainedExecutable = testProject.Name + Constants.ExeSuffix; string selfContainedExecutableFullPath = Path.Combine(publishDirectory.FullName, selfContainedExecutable); var libPrefix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "lib"; var filesPublished = new[] { selfContainedExecutable, $"{testProject.Name}.dll", $"{testProject.Name}.pdb", $"{testProject.Name}.deps.json", $"{testProject.Name}.runtimeconfig.json", $"{libPrefix}coreclr{FileConstants.DynamicLibSuffix}", $"{libPrefix}hostfxr{FileConstants.DynamicLibSuffix}", $"{libPrefix}hostpolicy{FileConstants.DynamicLibSuffix}", $"mscorlib.dll", $"System.Private.CoreLib.dll", }; outputDirectory.Should().HaveFiles(filesPublished); publishDirectory.Should().HaveFiles(filesPublished); dependencyContext.Should() .OnlyHaveRuntimeAssembliesWhichAreInFolder(rid, publishDirectory.FullName) .And .OnlyHaveNativeAssembliesWhichAreInFolder(rid, publishDirectory.FullName, testProject.Name); runCommand = new RunExeCommand(Log, selfContainedExecutableFullPath); } else { var filesPublished = new[] { $"{testProject.Name}.dll", $"{testProject.Name}.pdb", $"{testProject.Name}.deps.json", $"{testProject.Name}.runtimeconfig.json" }; outputDirectory.Should().HaveFiles(filesPublished); publishDirectory.Should().HaveFiles(filesPublished); dependencyContext.Should() .OnlyHaveRuntimeAssemblies(rid ?? "", testProject.Name); runCommand = new DotnetCommand(Log, Path.Combine(publishDirectory.FullName, $"{testProject.Name}.dll")); } runCommand .Execute() .Should() .Pass() .And .HaveStdOutContaining(outputMessage); }
private void RunAppFromOutputFolder(string testName, bool useRid, bool includeConflicts, string targetFramework = "netcoreapp2.0") { var runtimeIdentifier = useRid ? EnvironmentInfo.GetCompatibleRid(targetFramework) : null; TestProject project = new TestProject() { Name = testName, TargetFrameworks = targetFramework, RuntimeIdentifier = runtimeIdentifier, IsExe = true, }; string outputMessage = $"Hello from {project.Name}!"; project.SourceFiles["Program.cs"] = @" using System; public static class Program { public static void Main() { TestConflictResolution(); Console.WriteLine(""" + outputMessage + @"""); } " + ConflictResolutionAssets.ConflictResolutionTestMethod + @" } "; var testAsset = _testAssetsManager.CreateTestProject(project, project.Name) .WithProjectChanges(p => { if (includeConflicts) { var ns = p.Root.Name.Namespace; var itemGroup = new XElement(ns + "ItemGroup"); p.Root.Add(itemGroup); foreach (var dependency in ConflictResolutionAssets.ConflictResolutionDependencies) { itemGroup.Add(new XElement(ns + "PackageReference", new XAttribute("Include", dependency.Item1), new XAttribute("Version", dependency.Item2))); } } }); var buildCommand = new BuildCommand(testAsset); buildCommand .Execute() .Should() .Pass(); string outputFolder = buildCommand.GetOutputDirectory(project.TargetFrameworks, runtimeIdentifier: runtimeIdentifier ?? "").FullName; new DotnetCommand(Log, Path.Combine(outputFolder, project.Name + ".dll")) .Execute() .Should() .Pass() .And .HaveStdOutContaining(outputMessage); }
public void It_publishes_with_a_publish_profile(bool?selfContained, bool?useAppHost) { var tfm = "netcoreapp2.2"; var rid = EnvironmentInfo.GetCompatibleRid(tfm); var testProject = new TestProject() { Name = "ConsoleWithPublishProfile", TargetFrameworks = tfm, ProjectSdk = "Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish", IsExe = true, }; var identifer = (selfContained == null ? "null" : selfContained.ToString()) + (useAppHost == null ? "null" : useAppHost.ToString()); var testProjectInstance = _testAssetsManager.CreateTestProject(testProject, identifier: identifer); var projectDirectory = Path.Combine(testProjectInstance.Path, testProject.Name); var publishProfilesDirectory = Path.Combine(projectDirectory, "Properties", "PublishProfiles"); Directory.CreateDirectory(publishProfilesDirectory); File.WriteAllText(Path.Combine(publishProfilesDirectory, "test.pubxml"), $@" <Project> <PropertyGroup> <RuntimeIdentifier>{rid}</RuntimeIdentifier> {(selfContained.HasValue ? $"<SelfContained>{selfContained}</SelfContained>" : "")} {((!(selfContained ?? true) && useAppHost.HasValue) ? $"<UseAppHost>{useAppHost}</UseAppHost>" : "")} </PropertyGroup> </Project> "); var command = new PublishCommand(testProjectInstance); command .Execute("/p:PublishProfile=test") .Should() .Pass(); var output = command.GetOutputDirectory(targetFramework: tfm, runtimeIdentifier: rid); output.Should().HaveFiles(new[] { $"{testProject.Name}.dll", $"{testProject.Name}.pdb", $"{testProject.Name}.deps.json", $"{testProject.Name}.runtimeconfig.json", }); if (selfContained ?? true) { output.Should().HaveFiles(new[] { $"{FileConstants.DynamicLibPrefix}hostfxr{FileConstants.DynamicLibSuffix}", $"{FileConstants.DynamicLibPrefix}hostpolicy{FileConstants.DynamicLibSuffix}", }); } else { output.Should().NotHaveFiles(new[] { $"{FileConstants.DynamicLibPrefix}hostfxr{FileConstants.DynamicLibSuffix}", $"{FileConstants.DynamicLibPrefix}hostpolicy{FileConstants.DynamicLibSuffix}", }); } if ((selfContained ?? true) || (useAppHost ?? true)) { output.Should().HaveFile($"{testProject.Name}{Constants.ExeSuffix}"); } else { output.Should().NotHaveFile($"{testProject.Name}{Constants.ExeSuffix}"); } }
private void TestInvalidTargetFramework(string testName, string targetFramework, bool useSolution, string expectedOutput) { var testProject = new TestProject() { Name = testName, TargetFrameworks = targetFramework, }; string identifier = ((useSolution ? "_Solution" : "") + targetFramework + expectedOutput).GetHashCode().ToString(); var testAsset = _testAssetsManager.CreateTestProject(testProject, testProject.Name, identifier); if (targetFramework.Contains(";")) { // The TestProject class doesn't differentiate between TargetFramework and TargetFrameworks, and helpfully selects // which property to use based on whether there's a semicolon. // For this test, we need to override this behavior testAsset = testAsset.WithProjectChanges(project => { var ns = project.Root.Name.Namespace; project.Root.Element(ns + "PropertyGroup") .Element(ns + "TargetFrameworks") .Name = ns + "TargetFramework"; }); } RestoreCommand restoreCommand; BuildCommand buildCommand; if (useSolution) { var dotnetCommand = new DotnetCommand(Log) { WorkingDirectory = testAsset.TestRoot }; dotnetCommand.Execute("new", "sln") .Should() .Pass(); var relativePathToProject = Path.Combine(testProject.Name, testProject.Name + ".csproj"); dotnetCommand.Execute($"sln", "add", relativePathToProject) .Should() .Pass(); var relativePathToSln = Path.GetFileName(testAsset.Path) + ".sln"; restoreCommand = testAsset.GetRestoreCommand(Log, relativePathToSln); buildCommand = new BuildCommand(testAsset, relativePathToSln); } else { restoreCommand = testAsset.GetRestoreCommand(Log, testProject.Name); buildCommand = new BuildCommand(testAsset); } // Set RestoreContinueOnError=ErrorAndContinue to force failure on error // See https://github.com/NuGet/Home/issues/5309 var restore = restoreCommand.Execute("/p:RestoreContinueOnError=ErrorAndContinue"); // Intentionally not checking the error message on restore here as we can't put ourselves in front of // restore and customize the message for invalid target frameworks as that would break restoring packages // like MSBuild.Sdk.Extras that add support for extra TFMs. restore.Should().Fail(); buildCommand .ExecuteWithoutRestore() .Should() .Fail() .And .HaveStdOutContaining(expectedOutput) .And.NotHaveStdOutContaining(">="); // old error about comparing empty string to version when TargetFramework was blank; }
public void It_restores_multitargeted_net_framework_project_successfully(bool includeExplicitReference) { var testProject = new TestProject() { Name = "ProjectWithoutTargetingPackRef", TargetFrameworks = "net471;net472;netcoreapp3.0", IsSdkProject = true, }; TestAsset testAsset = null; if (includeExplicitReference) { // Add explicit reference to assembly packs testAsset = _testAssetsManager.CreateTestProject(testProject).WithProjectChanges(project => { var ns = project.Root.Name.Namespace; var itemGroup = project.Root.Elements(ns + "ItemGroup").FirstOrDefault(); itemGroup.Add(new XElement(ns + "PackageReference", new XAttribute("Include", $"Microsoft.NETFramework.ReferenceAssemblies"), new XAttribute("Version", $"1.0.0-preview.2"))); }); } else { testAsset = _testAssetsManager.CreateTestProject(testProject); } string projectAssetsJsonPath = Path.Combine( testAsset.Path, testProject.Name, "obj", "project.assets.json"); var restoreCommand = testAsset.GetRestoreCommand(Log, relativePath: testProject.Name); restoreCommand.Execute() .Should() .Pass() .And .NotHaveStdOutContaining("NETSDK1023"); LockFile lockFile = LockFileUtilities.GetLockFile( projectAssetsJsonPath, NullLogger.Instance); var net471FrameworkLibrary = lockFile.GetTarget(NuGetFramework.Parse(".NETFramework,Version=v4.7.1"), null).Libraries.FirstOrDefault((file) => file.Name.Contains("net471")); if (TestProject.ReferenceAssembliesAreInstalled(TargetDotNetFrameworkVersion.Version471) && !includeExplicitReference) { net471FrameworkLibrary.Should().BeNull(); } else { net471FrameworkLibrary.Name.Should().Be("Microsoft.NETFramework.ReferenceAssemblies.net471"); net471FrameworkLibrary.Type.Should().Be("package"); } var net472FrameworkLibrary = lockFile.GetTarget(NuGetFramework.Parse(".NETFramework,Version=v4.7.2"), null).Libraries.FirstOrDefault((file) => file.Name.Contains("net472")); if (TestProject.ReferenceAssembliesAreInstalled(TargetDotNetFrameworkVersion.Version472) && !includeExplicitReference) { net472FrameworkLibrary.Should().BeNull(); } else { net472FrameworkLibrary.Name.Should().Be("Microsoft.NETFramework.ReferenceAssemblies.net472"); net472FrameworkLibrary.Type.Should().Be("package"); } }
/// <summary> /// Initializes a test project with the contents of this structure. /// </summary> /// <param name="testProject">The test project to populate.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="testProject"/> is null.</exception> public void InitializeTestProject(TestProject testProject) { if (testProject == null) throw new ArgumentNullException("testProject"); testPackage.InitializeTestPackage(testProject.TestPackage); GenericCollectionUtils.ForEach(testFilters, x => testProject.AddTestFilter(x)); GenericCollectionUtils.ForEach(testRunnerExtensions, x => testProject.AddTestRunnerExtensionSpecification(x)); testProject.ReportNameFormat = reportNameFormat; testProject.ReportDirectory = reportDirectory; }
Task PopulateTasksAsync() { // Missing: // api-diff testSelector.SelectTests(); DeviceLoader.LoadAllAsync().DoNotAwait(); var simTasksFactory = new RunSimulatorTasksFactory(); var loadsim = simTasksFactory.CreateAsync(this, processManager, testVariationsFactory) .ContinueWith((v) => { if (v.Status == TaskStatus.RanToCompletion) { Console.WriteLine("Simulator tasks created"); Tasks.AddRange(v.Result); } else { Console.WriteLine($"Failed to create simulator tasks: {v.Exception}"); } }); //Tasks.AddRange (await CreateRunSimulatorTasksAsync ()); var crashReportSnapshotFactory = new CrashSnapshotReporterFactory(processManager); // all factories are enumerators \o/ var testFactories = new IEnumerable <AppleTestTask> [] { new MacTestTasksEnumerable(this, processManager, crashReportSnapshotFactory, testVariationsFactory), new NUnitTestTasksEnumerable(this, processManager), new MakeTestTaskEnumerable(this, processManager) }; // add all tests defined by the factory foreach (var f in testFactories) { Tasks.AddRange(f); } // individual special tasks var buildXtroTests = new MakeTask(jenkins: this, processManager: processManager) { Platform = TestPlatform.All, TestName = "Xtro", Target = "wrench", WorkingDirectory = Path.Combine(HarnessConfiguration.RootDirectory, "xtro-sharpie"), Ignored = !IncludeXtro, Timeout = TimeSpan.FromMinutes(15), }; var runXtroReporter = new RunXtroTask(this, buildXtroTests, processManager, crashReportSnapshotFactory) { Platform = TestPlatform.Mac, TestName = buildXtroTests.TestName, Ignored = buildXtroTests.Ignored, WorkingDirectory = buildXtroTests.WorkingDirectory, }; Tasks.Add(runXtroReporter); var buildDotNetGeneratorProject = new TestProject(Path.GetFullPath(Path.Combine(HarnessConfiguration.RootDirectory, "bgen", "bgen-tests.csproj"))) { IsDotNetProject = true, }; var buildDotNetGenerator = new DotNetBuildTask(jenkins: this, testProject: buildDotNetGeneratorProject, processManager: processManager) { TestProject = buildDotNetGeneratorProject, SpecifyPlatform = false, SpecifyConfiguration = false, Platform = TestPlatform.iOS, }; var runDotNetGenerator = new DotNetTestTask(this, buildDotNetGenerator, processManager) { TestProject = buildDotNetGeneratorProject, Platform = TestPlatform.iOS, TestName = "Generator tests", Mode = ".NET", Ignored = !IncludeBtouch, }; Tasks.Add(runDotNetGenerator); var buildDotNetTestsProject = new TestProject(Path.GetFullPath(Path.Combine(HarnessConfiguration.RootDirectory, "dotnet", "UnitTests", "DotNetUnitTests.csproj"))) { IsDotNetProject = true, }; var buildDotNetTests = new DotNetBuildTask(this, testProject: buildDotNetTestsProject, processManager: processManager) { SpecifyPlatform = false, Platform = TestPlatform.All, ProjectConfiguration = "Debug", Ignored = !IncludeDotNet, }; var runDotNetTests = new DotNetTestTask(this, buildDotNetTests, processManager) { TestProject = buildDotNetTestsProject, Platform = TestPlatform.All, TestName = "DotNet tests", Timeout = TimeSpan.FromMinutes(5), Ignored = !IncludeDotNet, }; Tasks.Add(runDotNetTests); var deviceTestFactory = new RunDeviceTasksFactory(); var loaddev = deviceTestFactory.CreateAsync(this, processManager, testVariationsFactory).ContinueWith((v) => { Console.WriteLine("Got device tasks completed"); Tasks.AddRange(v.Result); }); return(Task.WhenAll(loadsim, loaddev)); }
// 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); }
public void ItRunsAppsDirectlyReferencingAssemblies( string referencerTarget, string dependencyTarget) { if (UsingFullFrameworkMSBuild) { // Disabled on full framework MSBuild until CI machines have VS with bundled .NET Core / .NET Standard versions // See https://github.com/dotnet/sdk/issues/1077 return; } string identifier = referencerTarget.ToString() + "_" + dependencyTarget.ToString(); TestProject dependencyProject = new TestProject() { Name = "Dependency", IsSdkProject = true, TargetFrameworks = dependencyTarget, }; // Skip running test if not running on Windows if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !dependencyProject.BuildsOnNonWindows) { return; } dependencyProject.SourceFiles["Class1.cs"] = @" public class Class1 { public static string GetMessage() { return ""Hello from a direct reference.""; } } "; var dependencyAsset = _testAssetsManager.CreateTestProject(dependencyProject, identifier: identifier); string dependencyAssemblyPath = RestoreAndBuild(dependencyAsset, dependencyProject); TestProject referencerProject = new TestProject() { Name = "Referencer", IsSdkProject = true, TargetFrameworks = referencerTarget, // Need to use a self-contained app for now because we don't use a CLI that has a "2.0" shared framework RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid(referencerTarget), IsExe = true, }; referencerProject.References.Add(dependencyAssemblyPath); referencerProject.SourceFiles["Program.cs"] = @" using System; public static class Program { public static void Main() { Console.WriteLine(Class1.GetMessage()); } } "; var referencerAsset = _testAssetsManager.CreateTestProject(referencerProject, identifier: identifier); string applicationPath = RestoreAndBuild(referencerAsset, referencerProject); Command.Create(RepoInfo.DotNetHostPath, new[] { applicationPath }) .CaptureStdOut() .Execute() .Should().Pass() .And.HaveStdOutContaining("Hello from a direct reference."); }