Exemple #1
0
        public void TryGetNuGetVersionForSdk_WhenStateContainsNoVersion_ReturnsNull()
        {
            var sdkResolverContext = new MockSdkResolverContext(ProjectName)
            {
                State = new Dictionary <string, string>()
            };

            VerifyTryGetNuGetVersionForSdk(
                version: null,
                expectedVersion: null,
                sdkResolverContext);
        }
        public void EmptyGlobalJson()
        {
            using (var testEnvironment = TestEnvironment.Create())
            {
                var folder = testEnvironment.CreateFolder().FolderPath;

                File.WriteAllText(Path.Combine(folder, GlobalJsonReader.GlobalJsonFileName), @" { } ");

                var context = new MockSdkResolverContext(Path.Combine(folder, "foo.proj"));

                GlobalJsonReader.GetMSBuildSdkVersions(context).Should().BeNull();
            }
        }
        public void TryGetNuGetVersionForSdkGetsVersionFromState()
        {
            var context = new MockSdkResolverContext("foo.proj")
            {
                State = new Dictionary <string, string>
                {
                    { "foo", "1.2.3" }
                }
            };

            VerifyTryGetNuGetVersionForSdk(
                version: null,
                expectedVersion: NuGetVersion.Parse("1.2.3"),
                context: context);
        }
        public void TryGetNuGetVersionForSdkInvalidVersionInGlobalJson()
        {
            var context = new MockSdkResolverContext("foo.proj")
            {
                State = new Dictionary <string, string>
                {
                    { "foo", "abc" }
                }
            };

            VerifyTryGetNuGetVersionForSdk(
                version: "abc",
                expectedVersion: null,
                context: context);
        }
        public void TryGetNuGetVersionForSdk_WhenInvalidVersionInGlobalJson_ReturnsNull()
        {
            var allVersions = new Dictionary <string, string>
            {
                [PackageA] = "InvalidVersion"
            };

            var sdkResolverContext = new MockSdkResolverContext(ProjectName);

            VerifyTryGetNuGetVersionForSdk(
                allVersions,
                version: null,
                expectedVersion: null,
                sdkResolverContext);
        }
Exemple #6
0
        public void TryGetNuGetVersionForSdk_WhenStateIsNotNull_StateIsUsed()
        {
            var sdkResolverContext = new MockSdkResolverContext(ProjectName)
            {
                State = new Dictionary <string, string>
                {
                    [PackageA] = "1.2.3"
                }
            };

            VerifyTryGetNuGetVersionForSdk(
                version: null,
                expectedVersion: NuGetVersion.Parse("1.2.3"),
                sdkResolverContext);
        }
Exemple #7
0
        public void GetMSBuildSdkVersions_ParsesSdkVersions_WhenGlobalJsonIsValid()
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "Sdk1", "1.0.0" },
                { "Sdk2", "2.0.0" },
            };

            using (var testDirectory = TestDirectory.Create())
            {
                WriteGlobalJson(testDirectory, expectedVersions);

                var context = new MockSdkResolverContext(testDirectory);

                GlobalJsonReader.GetMSBuildSdkVersions(context).Should().Equal(expectedVersions);
            }
        }
        public void GetMSBuildSdkVersions_ReturnsNull_WhenSolutionFilePathAndProjectFilePathIsNull()
        {
            var context = new MockSdkResolverContext(projectPath: null, solutionPath: null);

            var globalJsonReader = new GlobalJsonReader();

            bool wasGlobalJsonRead = false;

            globalJsonReader.FileRead += (sender, args) =>
            {
                wasGlobalJsonRead = true;
            };

            globalJsonReader.GetMSBuildSdkVersions(context).Should().BeNull();

            wasGlobalJsonRead.Should().BeFalse();
        }
        public void SdkVersionsAreSuccessfullyLoaded()
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "foo", "1.0.0" },
                { "bar", "2.0.0" }
            };

            using (var testDirectory = TestDirectory.Create())
            {
                WriteGlobalJson(testDirectory, expectedVersions);

                var context = new MockSdkResolverContext(Path.Combine(testDirectory.Path, "foo.proj"));

                GlobalJsonReader.GetMSBuildSdkVersions(context).Should().Equal(expectedVersions);
            }
        }
        public void TryGetNuGetVersionForSdk_WhenGlobalJsonExists_UsesVersionsFromGlobalJson()
        {
            var allVersions = new Dictionary <string, string>
            {
                [PackageA] = "5.11.77",
                [PackageB] = "2.0.0"
            };

            using (var testDirectory = TestDirectory.Create())
            {
                var sdkResolverContext = new MockSdkResolverContext(testDirectory);

                VerifyTryGetNuGetVersionForSdk(
                    allVersions,
                    version: null,
                    expectedVersion: NuGetVersion.Parse(allVersions[PackageA]),
                    sdkResolverContext);
            }
        }
        public void Resolve_WhenPackageDoesNotExists_ReturnsFailedSdkResultAndLogsError()
        {
            using (var pathContext = new SimpleTestPathContext())
            {
                var sdkReference       = new SdkReference(PackageA, VersionOnePointZero, minimumVersion: null);
                var sdkResolverContext = new MockSdkResolverContext(pathContext.WorkingDirectory);
                var sdkResultFactory   = new MockSdkResultFactory();
                var sdkResolver        = new NuGetSdkResolver();

                MockSdkResult result = sdkResolver.Resolve(sdkReference, sdkResolverContext, sdkResultFactory) as MockSdkResult;

                result.Should().NotBeNull();
                result.Success.Should().BeFalse();
                result.Path.Should().BeNull();
                result.Version.Should().BeNull();
                result.Errors.Should().BeEquivalentTo(new[] { $"Unable to find package {sdkReference.Name}. No packages exist with this id in source(s): source" });
                result.Warnings.Should().BeEmpty();
            }
        }
        public void SdkVersionsAreSuccessfullyLoaded()
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "foo", "1.0.0" },
                { "bar", "2.0.0" }
            };

            using (var testEnvironment = TestEnvironment.Create())
            {
                var testFolder  = testEnvironment.CreateFolder();
                var projectFile = testEnvironment.CreateFile(testFolder, ".proj");

                WriteGlobalJson(testFolder.FolderPath, expectedVersions);

                var context = new MockSdkResolverContext(projectFile.Path);

                GlobalJsonReader.GetMSBuildSdkVersions(context).Should().Equal(expectedVersions);
            }
        }
Exemple #13
0
        public void TryGetNuGetVersionForSdkGetsVersionFromGlobalJson()
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "foo", "5.11.77" },
                { "bar", "2.0.0" }
            };

            using (var testDirectory = TestDirectory.Create())
            {
                GlobalJsonReaderTests.WriteGlobalJson(testDirectory, expectedVersions);

                var context = new MockSdkResolverContext(Path.Combine(testDirectory.Path, "foo.proj"));

                VerifyTryGetNuGetVersionForSdk(
                    version: null,
                    expectedVersion: NuGetVersion.Parse(expectedVersions["foo"]),
                    context: context);
            }
        }
Exemple #14
0
        public void GetMSBuildSdkVersions_LogsMessage_WhenGlobalJsonContainsInvalidJson()
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "Sdk1", "1.0.0" },
                { "Sdk2", "2.0.0" }
            };

            using (var testDirectory = TestDirectory.Create())
            {
                var globalJsonPath = WriteGlobalJson(testDirectory, expectedVersions, additionalContent: ", invalid JSON!");

                var context = new MockSdkResolverContext(testDirectory);

                GlobalJsonReader.GetMSBuildSdkVersions(context).Should().BeNull();

                context.MockSdkLogger.LoggedMessages.Count.Should().Be(1);
                context.MockSdkLogger.LoggedMessages.First().Message.Should().Be(
                    $"Failed to parse \"{globalJsonPath}\". Invalid character after parsing property name. Expected ':' but got: J. Path 'msbuild-sdks.Sdk2', line 5, position 10.");
            }
        }
        public void InvalidJsonLogsMessage()
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "foo", "1.0.0" },
                { "bar", "2.0.0" }
            };

            using (var testDirectory = TestDirectory.Create())
            {
                var globalJsonPath = WriteGlobalJson(testDirectory, expectedVersions, additionalContent: ", abc");

                var context = new MockSdkResolverContext(Path.Combine(testDirectory.Path, "foo.proj"));

                GlobalJsonReader.GetMSBuildSdkVersions(context).Should().BeNull();

                context.MockSdkLogger.LoggedMessages.Count.Should().Be(1);
                context.MockSdkLogger.LoggedMessages.First().Key.Should().Be(
                    $"Failed to parse \"{globalJsonPath}\". Invalid JavaScript property identifier character: }}. Path 'msbuild-sdks', line 6, position 5.");
            }
        }
        public void GetMSBuildSdkVersions_IgnoresDuplicates_WhenGlobalJsonContainsDuplicates()
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "Sdk1", "3.0.0" },
                { "Sdk2", "2.0.0" },
            };

            using (var testDirectory = TestDirectory.Create())
            {
                string expectedGlobalJsonPath = Path.Combine(testDirectory, GlobalJsonReader.GlobalJsonFileName);

                File.WriteAllText(
                    expectedGlobalJsonPath,
                    @"{
  // This is a comment
  ""msbuild-sdks"": {
    ""Sdk1"": ""1.0.0"",
    ""Sdk2"": ""2.0.0"",
    ""Sdk1"": ""3.0.0"",
  }
}
");

                var context = new MockSdkResolverContext(testDirectory);

                var globalJsonReader = new GlobalJsonReader();

                string actualGlobalJsonPath = null;

                globalJsonReader.FileRead += (_, globalJsonPath) =>
                {
                    actualGlobalJsonPath = globalJsonPath;
                };

                globalJsonReader.GetMSBuildSdkVersions(context).Should().Equal(expectedVersions);

                actualGlobalJsonPath.Should().Be(expectedGlobalJsonPath);
            }
        }
        public void GetMSBuildSdkVersions_ReturnsNull_WhenGlobalJsonIsEmpty()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                File.WriteAllText(Path.Combine(testDirectory.Path, GlobalJsonReader.GlobalJsonFileName), @" { } ");

                var context = new MockSdkResolverContext(testDirectory);

                var globalJsonReader = new GlobalJsonReader();

                bool wasGlobalJsonRead = false;

                globalJsonReader.FileRead += (sender, args) =>
                {
                    wasGlobalJsonRead = true;
                };

                globalJsonReader.GetMSBuildSdkVersions(context).Should().BeNull();

                wasGlobalJsonRead.Should().BeTrue();
            }
        }
        public void GlobalJsonWithComments()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                File.WriteAllText(
                    Path.Combine(testDirectory, GlobalJsonReader.GlobalJsonFileName),
                    @"{
  // This is a comment
  ""msbuild-sdks"": {
    /* This is another comment */
    ""foo"": ""1.0.0""
  }
}");

                var context = new MockSdkResolverContext(Path.Combine(testDirectory, "foo.proj"));

                GlobalJsonReader.GetMSBuildSdkVersions(context).Should().BeEquivalentTo(new Dictionary <string, string>
                {
                    ["foo"] = "1.0.0"
                });
            }
        }
        public void GetMSBuildSdkVersions_ReturnsNull_WhenGlobalJsonDoesNotExist()
        {
            // In some cases, a global.json exists because tests are run in the repo so a different file name must be passed in
            const string globalJsonFileName = "global.test.json";

            using (var testDirectory = TestDirectory.Create())
            {
                var context = new MockSdkResolverContext(testDirectory);

                var globalJsonReader = new GlobalJsonReader();

                bool wasGlobalJsonRead = false;

                globalJsonReader.FileRead += (sender, args) =>
                {
                    wasGlobalJsonRead = true;
                };

                globalJsonReader.GetMSBuildSdkVersions(context, globalJsonFileName).Should().BeNull();

                wasGlobalJsonRead.Should().BeFalse();
            }
        }
        public void Resolve_WhenPackageExists_ReturnsSucceededSdkResult()
        {
            using (var pathContext = new SimpleTestPathContext())
            {
                var sdkReference = new SdkReference(PackageA, VersionOnePointZero, minimumVersion: null);
                var package      = new SimpleTestPackageContext(sdkReference.Name, sdkReference.Version);
                package.AddFile("Sdk/Sdk.props", "<Project />");
                package.AddFile("Sdk/Sdk.targets", "<Project />");
                SimpleTestPackageUtility.CreateFolderFeedV3Async(pathContext.PackageSource, PackageSaveMode.Defaultv3, package).Wait();
                var sdkResolverContext = new MockSdkResolverContext(pathContext.WorkingDirectory);
                var sdkResultFactory   = new MockSdkResultFactory();
                var sdkResolver        = new NuGetSdkResolver();

                MockSdkResult result = sdkResolver.Resolve(sdkReference, sdkResolverContext, sdkResultFactory) as MockSdkResult;

                result.Should().NotBeNull();
                result.Success.Should().BeTrue();
                result.Path.Should().Be(Path.Combine(pathContext.UserPackagesFolder, sdkReference.Name.ToLowerInvariant(), sdkReference.Version, "Sdk"));
                result.Version.Should().Be(sdkReference.Version);
                result.Errors.Should().BeEmpty();
                result.Warnings.Should().BeEmpty();
            }
        }
        public void TryGetNuGetVersionForSdkGetsVersionFromGlobalJson()
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "foo", "5.11.77" },
                { "bar", "2.0.0" }
            };

            using (var testEnvironment = TestEnvironment.Create())
            {
                var testFolder  = testEnvironment.CreateFolder();
                var projectFile = testEnvironment.CreateFile(testFolder, ".proj");

                GlobalJsonReaderTests.WriteGlobalJson(testFolder.FolderPath, expectedVersions);

                var context = new MockSdkResolverContext(projectFile.Path);

                VerifyTryGetNuGetVersionForSdk(
                    version: null,
                    expectedVersion: NuGetVersion.Parse(expectedVersions["foo"]),
                    context: context);
            }
        }
        [InlineData("{  } ")]                                                   // Empty object
        public void GetMSBuildSdkVersions_IgnoresInvalidVersions_WhenMSBuildSdksSectionContainsInvalidValues(string objectValue)
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "Sdk1", "1.0.0" },
            };

            using (var testDirectory = TestDirectory.Create())
            {
                var    context = new MockSdkResolverContext(testDirectory);
                string expectedGlobalJsonPath = Path.Combine(testDirectory, GlobalJsonReader.GlobalJsonFileName);

                File.WriteAllText(expectedGlobalJsonPath, $@"{{
  ""sdk"" : {{
    ""version"": ""1.2.300""
  }},
  ""msbuild-sdks"": {{
    ""Sdk1"": ""1.0.0"",
    ""Sdk2"": {objectValue}
}}
}}");

                var globalJsonReader = new GlobalJsonReader();

                string actualGlobalJsonPath = null;

                globalJsonReader.FileRead += (_, globalJsonPath) =>
                {
                    actualGlobalJsonPath = globalJsonPath;
                };

                globalJsonReader.GetMSBuildSdkVersions(context).Should().BeEquivalentTo(expectedVersions);

                actualGlobalJsonPath.Should().Be(expectedGlobalJsonPath);
            }
        }
        [InlineData("{  } ")]                                                   // Empty object
        public void GetMSBuildSdkVersions_ReturnsNull_WhenMSBuildSdksSectionIsNotDeclaredCorrectly(string msbuildSdksSection)
        {
            using (var testDirectory = TestDirectory.Create())
            {
                var context = new MockSdkResolverContext(testDirectory);

                File.WriteAllText(Path.Combine(testDirectory, GlobalJsonReader.GlobalJsonFileName), $@"{{
  ""msbuild-sdks"": {msbuildSdksSection}
}}");

                var globalJsonReader = new GlobalJsonReader();

                bool wasGlobalJsonRead = false;

                globalJsonReader.FileRead += (sender, args) =>
                {
                    wasGlobalJsonRead = true;
                };

                globalJsonReader.GetMSBuildSdkVersions(context).Should().BeNull();

                wasGlobalJsonRead.Should().BeTrue();
            }
        }
        public void GetStartingPath_ReturnsNull_WhenSolutionPathAndProjectPathIsNullOrWhitespace(string solutionPath, string projectPath)
        {
            var context = new MockSdkResolverContext(projectPath, solutionPath);

            GlobalJsonReader.GetStartingPath(context).Should().BeNull();
        }
Exemple #25
0
        public void GetMSBuildSdkVersions_ReturnsNull_WhenProjectPathIsNull()
        {
            var context = new MockSdkResolverContext(projectPath: null);

            GlobalJsonReader.GetMSBuildSdkVersions(context).Should().BeNull();
        }
        public void GetMSBuildSdkVersions_ReloadsGlobalJson_WhenGlobalJsonChanges()
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "Sdk1", "1.0.0" },
                { "Sdk2", "2.0.0" },
            };

            using (var testDirectory = TestDirectory.Create())
            {
                var expectedGlobalJsonReaderPath = WriteGlobalJson(testDirectory, expectedVersions);

                var context = new MockSdkResolverContext(testDirectory);

                var globalJsonReader = new GlobalJsonReader();

                Dictionary <string, int> globalJsonReadCountByPath = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase);

                globalJsonReader.FileRead += (_, globalJsonPath) =>
                {
                    if (globalJsonReadCountByPath.ContainsKey(globalJsonPath))
                    {
                        globalJsonReadCountByPath[globalJsonPath]++;
                    }
                    else
                    {
                        globalJsonReadCountByPath[globalJsonPath] = 1;
                    }
                };

                globalJsonReader.GetMSBuildSdkVersions(context).Should().Equal(expectedVersions);

                globalJsonReadCountByPath.ContainsKey(expectedGlobalJsonReaderPath).Should().BeTrue();

                globalJsonReadCountByPath[expectedGlobalJsonReaderPath].Should().Be(1);

                Parallel.For(0, Environment.ProcessorCount * 2, _ =>
                {
                    globalJsonReader.GetMSBuildSdkVersions(context).Should().Equal(expectedVersions);
                });

                globalJsonReadCountByPath.ContainsKey(expectedGlobalJsonReaderPath).Should().BeTrue();

                globalJsonReadCountByPath[expectedGlobalJsonReaderPath].Should().Be(1);

                expectedVersions["Sdk1"] = "2.0.0;";

                string path = WriteGlobalJson(testDirectory, expectedVersions);

                File.SetLastWriteTime(path, DateTime.Now.AddMinutes(1));

                Parallel.For(0, Environment.ProcessorCount * 2, _ =>
                {
                    globalJsonReader.GetMSBuildSdkVersions(context).Should().Equal(expectedVersions);
                });

                globalJsonReadCountByPath.ContainsKey(expectedGlobalJsonReaderPath).Should().BeTrue();

                globalJsonReadCountByPath[expectedGlobalJsonReaderPath].Should().Be(2);
            }
        }