// Dynamically determine which SDK version to copy by checking the TFM of test project assembly and the dotnet.dll. private static string GetSdkToTestByAssemblyPath(string testAssemblyPath) { // The TFM we're testing var testTfm = AssemblyReader.GetTargetFramework(testAssemblyPath); var selectedVersion = Directory.EnumerateDirectories(SdkDirSource) // get all directories in sdk folder .Where(path => { // SDK is for TFM to test if (string.Equals(Path.GetFileName(path), "NuGetFallbackFolder", StringComparison.OrdinalIgnoreCase)) { return(false); } var dotnetPath = Path.Combine(path, "dotnet.dll"); var sdkTfm = AssemblyReader.GetTargetFramework(dotnetPath); return(testTfm == sdkTfm); }) .Select(Path.GetFileName) // just the folder name (version string) .OrderByDescending(path => NuGetVersion.Parse(Path.GetFileName(path))) // in case there are multiple matching SDKs, selected the highest version .FirstOrDefault(); if (selectedVersion == null) { var message = $@"Could not find suitable SDK to test in {SdkDirSource} TFM being tested: {testTfm.DotNetFrameworkName} SDKs found: {string.Join(", ", Directory.EnumerateDirectories(SdkDirSource).Select(Path.GetFileName).Where(d => !string.Equals(d, "NuGetFallbackFolder", StringComparison.OrdinalIgnoreCase)))}"; throw new Exception(message); } return(selectedVersion); }
// For non fullframework code path, we could dynamically determine which SDK version to copy by checking the TFM of test project assembly and the dotnet.dll. public static TestDirectory CopyAndPatchLatestDotnetCli(string testAssemblyPath) { CliDirSource = Path.GetDirectoryName(TestFileSystemUtility.GetDotnetCli()); SdkDirSource = Path.Combine(CliDirSource, "sdk" + Path.DirectorySeparatorChar); // Dynamically determine which SDK version to copy SdkVersion = GetSdkToTestByAssemblyPath(testAssemblyPath); // Dynamically determine the TFM of the dotnet.dll SdkTfm = AssemblyReader.GetTargetFramework(Path.Combine(SdkDirSource, SdkVersion, "dotnet.dll")); var cliDirDestination = TestDirectory.Create(); CopyLatestCliToTestDirectory(cliDirDestination); UpdateCliWithLatestNuGetAssemblies(cliDirDestination); return(cliDirDestination); }
// For non fullframework code path, we could dynamically determine which SDK version to copy by checking the TFM of TestDotnetCLiUtility.dll and the dotnet.dll, // so there is no need to pass sdkVersion or sdkTfm. // But for fullframework code path, the test project dll could not be used to dynamically determine which SDK version to copy, // so we need to specify the sdkVersion and sdkTfm in order to patch the right version of SDK. public static TestDirectory CopyAndPatchLatestDotnetCli(string sdkVersion = null, string sdkTfm = null) { CliDirSource = Path.GetDirectoryName(TestFileSystemUtility.GetDotnetCli()); SdkDirSource = Path.Combine(CliDirSource, "sdk" + Path.DirectorySeparatorChar); if (sdkVersion == null) { #if !IS_DESKTOP // Dynamically determine which SDK version to copy SdkVersion = GetSdkToTest(); #endif } else { // Use specified sdkVersion SdkVersion = GetSdkToTest(sdkVersion); } if (sdkTfm == null) { #if !IS_DESKTOP // Dynamically determine the TFM of the dotnet.dll SdkTfm = AssemblyReader.GetTargetFramework(Path.Combine(SdkDirSource, SdkVersion, "dotnet.dll")); #endif } else { // Use specified sdkVersion SdkTfm = NuGetFramework.Parse(sdkTfm); } var cliDirDestination = TestDirectory.Create(); CopyLatestCliToTestDirectory(cliDirDestination); UpdateCliWithLatestNuGetAssemblies(cliDirDestination); return(cliDirDestination); }