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 expectedGlobalJsonPath = WriteGlobalJson(testDirectory, expectedVersions, additionalContent: ", invalid JSON!");

                var context = new MockSdkResolverContext(testDirectory);

                var globalJsonReader = new GlobalJsonReader();

                string actualGlobalJsonPath = null;

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

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

                context.MockSdkLogger.LoggedMessages.Count.Should().Be(1);
                context.MockSdkLogger.LoggedMessages.First().Message.Should().Be(
                    $"Failed to parse \"{expectedGlobalJsonPath}\". Invalid character after parsing property name. Expected ':' but got: J. Path 'msbuild-sdks.Sdk2', line 5, position 10.");

                actualGlobalJsonPath.Should().Be(expectedGlobalJsonPath);
            }
        }
Пример #2
0
        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();

                try
                {
                    var projectFile = testEnvironment.CreateFile(testFolder, ".proj");

                    WriteGlobalJson(testFolder.FolderPath, expectedVersions);

                    var context = new MockSdkResolverContext(projectFile.Path);

                    GlobalJsonReader.GetMSBuildSdkVersions(context).Should().Equal(expectedVersions);
                }
                finally
                {
                    testFolder.Revert();
                }
            }
        }
Пример #3
0
        public void InvalidJsonLogsMessage()
        {
            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");


                var globalJsonPath = WriteGlobalJson(testFolder.FolderPath, expectedVersions, additionalcontent: ", abc");

                var context = new MockSdkResolverContext(projectFile.Path);

                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.");
            }
        }
Пример #4
0
        public void GetMSBuildSdkVersions_IgnoresDuplicates_WhenGlobalJsonContainsDuplicates()
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "Sdk1", "3.0.0" },
                { "Sdk2", "2.0.0" },
            };

            using (var testDirectory = TestDirectory.Create())
            {
                File.WriteAllText(
                    Path.Combine(testDirectory, GlobalJsonReader.GlobalJsonFileName),
                    @"{
  // This is a comment
  ""msbuild-sdks"": {
    ""Sdk1"": ""1.0.0"",
    ""Sdk2"": ""2.0.0"",
    ""Sdk1"": ""3.0.0"",
  }
}
");

                var context = new MockSdkResolverContext(testDirectory);

                GlobalJsonReader.GetMSBuildSdkVersions(context).Should().Equal(expectedVersions);
            }
        }
        public void GetMSBuildSdkVersions_Succeeds_WhenGlobalJsonContainsComments()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                File.WriteAllText(
                    Path.Combine(testDirectory, GlobalJsonReader.GlobalJsonFileName),
                    @"{
  // This is a comment
  ""msbuild-sdks"": {
    /* This is another comment */
    ""Sdk1"": ""1.0.0""
  }
}");

                var context = new MockSdkResolverContext(testDirectory);

                var globalJsonReader = new GlobalJsonReader();

                bool wasGlobalJsonRead = false;

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

                globalJsonReader.GetMSBuildSdkVersions(context).Should().BeEquivalentTo(new Dictionary <string, string>
                {
                    ["Sdk1"] = "1.0.0"
                });

                wasGlobalJsonRead.Should().BeTrue();
            }
        }
        public void GetMSBuildSdkVersions_ParsesSdkVersions_WhenGlobalJsonIsValid()
        {
            var expectedVersions = new Dictionary <string, string>
            {
                { "Sdk1", "1.0.0" },
                { "Sdk2", "2.0.0" },
            };

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

                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 GetStartingPath_ReturnsProjectPath_WhenSolutionPathIsNullOrWhitespace(string solutionPath)
        {
            const string projectPath = "PROJECT_PATH";

            var context = new MockSdkResolverContext(projectPath, solutionPath);

            GlobalJsonReader.GetStartingPath(context).Should().Be(projectPath);
        }
        public void GetStartingPath_ReturnsSolutionPath_WhenSolutionPathIsNotNullOrWhitespace()
        {
            const string solutionPath = "SOLUTION_PATH";

            var context = new MockSdkResolverContext(projectPath: null, solutionPath);

            GlobalJsonReader.GetStartingPath(context).Should().Be(solutionPath);
        }
Пример #9
0
        public void GetMSBuildSdkVersions_Succeeds_WhenGlobalJsonContainsComments()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                File.WriteAllText(
                    Path.Combine(testDirectory, GlobalJsonReader.GlobalJsonFileName),
                    @"// Comment before content
// Comment before content
{ // Comment on same line as token
// Comment after start token
// Comment after start token
  ""unrelated-section-before"" : {
    // Comment in unrelated section
    // Comment in unrelated section
    ""property1"": ""value1""
  }, // comment after token, whitespace below is intentional

  ""msbuild-sdks"": {  // Comment after token
    /* This is another comment */
    // Comment before value
    ""Sdk1"": ""1.0.0"", // Comment after value
    // Comment between value
    // Comment between value
    ""Sdk2"": ""2.0.0"" // Comment after value
    // Comment after value
    // Comment after value
  }, // Comment after end token
  ""unrelated-section-after"" : {
    // Comment in unrelated section
    // Comment in unrelated section
    ""property1"": ""value1""
  }, // comment after token
} // Comment after end token
// Comment at end of file
// Comment at end of file");

                var context = new MockSdkResolverContext(testDirectory);

                var globalJsonReader = new GlobalJsonReader();

                bool wasGlobalJsonRead = false;

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

                globalJsonReader.GetMSBuildSdkVersions(context).Should().BeEquivalentTo(new Dictionary <string, string>
                {
                    ["Sdk1"] = "1.0.0",
                    ["Sdk2"] = "2.0.0"
                });

                wasGlobalJsonRead.Should().BeTrue();
            }
        }
        public void EmptyGlobalJson()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                File.WriteAllText(Path.Combine(testDirectory.Path, GlobalJsonReader.GlobalJsonFileName), @" { } ");

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

                GlobalJsonReader.GetMSBuildSdkVersions(context).Should().BeNull();
            }
        }
Пример #11
0
        public void EmptyGlobalJson()
        {
            using (TestEnvironment testEnvironment = TestEnvironment.Create())
            {
                TransientTestFolder folder = testEnvironment.CreateFolder();

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

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

                GlobalJsonReader.GetMSBuildSdkVersions(context).ShouldBeNull();
            }
        }
Пример #12
0
        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 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);
            }
        }
Пример #14
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();
        }
Пример #16
0
        public void SdkVersionsAreSuccessfullyLoaded()
        {
            Dictionary <string, string> expectedVersions = new Dictionary <string, string>
            {
                { "foo", "1.0.0" },
                { "bar", "2.0.0" }
            };

            using (TestEnvironment testEnvironment = TestEnvironment.Create())
            {
                TransientTestProjectWithFiles projectWithFiles = testEnvironment.CreateTestProjectWithFiles("", relativePathFromRootToProject: @"a\b\c");

                WriteGlobalJson(projectWithFiles.TestRoot, expectedVersions);

                MockSdkResolverContext context = new MockSdkResolverContext(projectWithFiles.ProjectFile);

                GlobalJsonReader.GetMSBuildSdkVersions(context).ShouldBe(expectedVersions);
            }
        }
        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();
            }
        }
        [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);
            }
        }
Пример #22
0
        public void InvalidJsonLogsMessage()
        {
            Dictionary <string, string> expectedVersions = new Dictionary <string, string>
            {
                { "foo", "1.0.0" },
                { "bar", "2.0.0" }
            };

            using (TestEnvironment testEnvironment = TestEnvironment.Create())
            {
                TransientTestProjectWithFiles projectWithFiles = testEnvironment.CreateTestProjectWithFiles("");

                string globalJsonPath = WriteGlobalJson(projectWithFiles.TestRoot, expectedVersions, additionalcontent: ", abc");

                MockSdkResolverContext context = new MockSdkResolverContext(projectWithFiles.ProjectFile);

                GlobalJsonReader.GetMSBuildSdkVersions(context).ShouldBeNull();

                context.MockSdkLogger.LoggedMessages
                .ShouldHaveSingleItem()
                .Key
                .ShouldBe($"Failed to parse \"{globalJsonPath}\". Invalid JavaScript property identifier character: }}. Path \'msbuild-sdks\', line 6, position 5.");
            }
        }
        [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();
        }
Пример #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);
            }
        }