private void VerifyProjectBaseDir(string expectedValue, string teamBuildValue, string userValue, string[] projectPaths) { AnalysisConfig config = new AnalysisConfig(); PropertiesWriter writer = new PropertiesWriter(config); config.SonarOutputDir = TestSonarqubeOutputDir; config.SourcesDirectory = teamBuildValue; config.SetConfigValue(SonarProperties.ProjectBaseDir, userValue); using (new AssertIgnoreScope()) { foreach (string projectPath in projectPaths) { var projectInfo = new ProjectInfo { FullPath = projectPath, ProjectLanguage = ProjectLanguages.CSharp }; writer.WriteSettingsForProject(projectInfo, Enumerable.Empty <string>(), "", ""); } var actual = writer.Flush(); var expected = "\r\nsonar.projectBaseDir=" + PropertiesWriter.Escape(expectedValue); Assert.IsTrue(actual.Contains(expected)); } }
public void PropertiesWriter_InvalidOperations() { AnalysisConfig validConfig = new AnalysisConfig() { SonarProjectKey = "key", SonarProjectName = "name", SonarProjectVersion = "1.0", SonarOutputDir = this.TestContext.DeploymentDirectory }; // 1. Must supply an analysis config on construction AssertException.Expects <ArgumentNullException>(() => new PropertiesWriter(null)); // 2. Can't call WriteSettingsForProject after Flush PropertiesWriter writer = new PropertiesWriter(validConfig); writer.Flush(); AssertException.Expects <InvalidOperationException>(() => writer.Flush()); // 3. Can't call Flush twice writer = new PropertiesWriter(validConfig); writer.Flush(); using (new AssertIgnoreScope()) { AssertException.Expects <InvalidOperationException>(() => writer.WriteSettingsForProject(new ProjectInfo(), new string[] { "file" }, "fxCopReport", "code coverage report")); } }
public void WriteSettingsForProject_ThrowsOnNullArgument() { var propertiesWriter = new PropertiesWriter(new AnalysisConfig(), new TestLogger()); Action action = () => propertiesWriter.WriteSettingsForProject(null); action.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("projectData"); }
public void PropertiesWriter_FxCopRerportForUnrecognisedLanguage() { var productBaseDir = TestUtils.CreateTestSpecificFolder(TestContext); string productProject = CreateEmptyFile(productBaseDir, "MyProduct.proj"); string productFile = CreateEmptyFile(productBaseDir, "File.txt"); string productFxCopFilePath = CreateEmptyFile(productBaseDir, "productFxCopReport.txt"); string productFileListFilePath = Path.Combine(productBaseDir, "productFileList.txt"); List <string> productFiles = new List <string>(); productFiles.Add(productFile); ProjectInfo product = CreateProjectInfo("myproduct", "9507E2E6-7342-4A04-9CB9-B0C47C937019", productProject, false, productFiles, productFileListFilePath, productFxCopFilePath, null, "my.language"); AnalysisConfig config = new AnalysisConfig() { SonarProjectKey = "my_project_key", SonarProjectName = "my_project_name", SonarProjectVersion = "1.0", SonarOutputDir = @"C:\my_folder", SourcesDirectory = @"D:\sources" }; string actual = null; using (new AssertIgnoreScope()) // expecting the property writer to complain about having an FxCop report for an unknown language { PropertiesWriter writer = new PropertiesWriter(config); writer.WriteSettingsForProject(product, new string[] { productFile, }, productFxCopFilePath, null); actual = writer.Flush(); } string expected = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"9507E2E6-7342-4A04-9CB9-B0C47C937019.sonar.projectKey=my_project_key:9507E2E6-7342-4A04-9CB9-B0C47C937019 9507E2E6-7342-4A04-9CB9-B0C47C937019.sonar.projectName=myproduct 9507E2E6-7342-4A04-9CB9-B0C47C937019.sonar.projectBaseDir={0} 9507E2E6-7342-4A04-9CB9-B0C47C937019.sonar.sources=\ {0}\\File.txt sonar.projectKey=my_project_key sonar.projectName=my_project_name sonar.projectVersion=1.0 sonar.working.directory=C:\\my_folder\\.sonar sonar.projectBaseDir={1} # FIXME: Encoding is hardcoded sonar.sourceEncoding=UTF-8 sonar.modules=9507E2E6-7342-4A04-9CB9-B0C47C937019 ", PropertiesWriter.Escape(productBaseDir), PropertiesWriter.Escape(config.SourcesDirectory)); SaveToResultFile(productBaseDir, "Expected.txt", expected.ToString()); SaveToResultFile(productBaseDir, "Actual.txt", actual); Assert.AreEqual(expected, actual); }
public void PropertiesWriter_AnalysisSettingsWritten() { // Tests that analysis settings in the ProjectInfo are written to the file // Arrange string projectBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_AnalysisSettingsWritten"); string productProject = CreateEmptyFile(projectBaseDir, "MyProduct.csproj"); string productFile = CreateEmptyFile(projectBaseDir, "File.cs"); List <string> productFiles = new List <string> { productFile }; string productFileListFilePath = Path.Combine(projectBaseDir, "productManagedFiles.txt"); ProjectInfo product = CreateProjectInfo("AnalysisSettingsTest.proj", "7B3B7244-5031-4D74-9BBD-3316E6B5E7D5", productProject, false, productFiles, productFileListFilePath, null, "language", "UTF-8"); List <ProjectInfo> projects = new List <ProjectInfo> { product }; AnalysisConfig config = new AnalysisConfig() { SonarOutputDir = @"C:\my_folder" }; // These are the settings we are going to check. The other analysis values are not checked. product.AnalysisSettings = new AnalysisProperties { new Property() { Id = "my.setting1", Value = "setting1" }, new Property() { Id = "my.setting2", Value = "setting 2 with spaces" }, new Property() { Id = "my.setting.3", Value = @"c:\dir1\dir2\foo.txt" } // path that will be escaped }; // Act PropertiesWriter writer = new PropertiesWriter(config); writer.WriteSettingsForProject(product, new string[] { productFile }, null); string fullActualPath = SaveToResultFile(projectBaseDir, "Actual.txt", writer.Flush()); // Assert SQPropertiesFileReader propertyReader = new SQPropertiesFileReader(fullActualPath); propertyReader.AssertSettingExists("7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.my.setting1", "setting1"); propertyReader.AssertSettingExists("7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.my.setting2", "setting 2 with spaces"); propertyReader.AssertSettingExists("7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.my.setting.3", @"c:\dir1\dir2\foo.txt"); }
public void PropertiesWriter_WorkdirPerModuleExplicitlySet() { // Tests that .sonar.working.directory is explicityl set per module // Arrange string projectBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_AnalysisSettingsWritten"); string productProject = CreateEmptyFile(projectBaseDir, "MyProduct.csproj"); string productFile = CreateEmptyFile(projectBaseDir, "File.cs"); List <string> productFiles = new List <string> { productFile }; string productFileListFilePath = Path.Combine(projectBaseDir, "productManagedFiles.txt"); string projectKey = "7B3B7244-5031-4D74-9BBD-3316E6B5E7D5"; ProjectInfo product = CreateProjectInfo("AnalysisSettingsTest.proj", projectKey, productProject, false, productFiles, productFileListFilePath, null, "language", "UTF-8"); List <ProjectInfo> projects = new List <ProjectInfo> { product }; AnalysisConfig config = new AnalysisConfig() { SonarOutputDir = @"C:\my_folder" }; // Act PropertiesWriter writer = new PropertiesWriter(config); writer.WriteSettingsForProject(product, new string[] { productFile }, null); writer.WriteSonarProjectInfo("dummy basedir", new List <string>()); string s = writer.Flush(); var props = new JavaProperties(); props.Load(GenerateStreamFromString(s)); string key = projectKey + "." + SonarProperties.WorkingDirectory; Assert.IsTrue(props.ContainsKey(key)); }
public void PropertiesWriter_WorkdirPerModuleExplicitlySet() { // Tests that .sonar.working.directory is explicitly set per module // Arrange var projectBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_AnalysisSettingsWritten"); var productProject = CreateEmptyFile(projectBaseDir, "MyProduct.csproj"); var productFile = CreateEmptyFile(projectBaseDir, "File.cs"); var productFiles = new List <FileInfo> { productFile }; var productFileListFilePath = Path.Combine(projectBaseDir, "productManagedFiles.txt"); var projectKey = "7B3B7244-5031-4D74-9BBD-3316E6B5E7D5"; var product = new ProjectData(CreateProjectInfo("AnalysisSettingsTest.proj", projectKey, productProject, false, productFiles, productFileListFilePath, null, "language", "UTF-8")); product.ReferencedFiles.Add(productFile); var config = new AnalysisConfig() { SonarOutputDir = @"C:\my_folder" }; // Act var writer = new PropertiesWriter(config, new TestLogger()); writer.WriteSettingsForProject(product); writer.WriteSonarProjectInfo(new DirectoryInfo("dummy basedir")); var s = writer.Flush(); var props = new JavaProperties(); props.Load(GenerateStreamFromString(s)); var key = projectKey + "." + SonarProperties.WorkingDirectory; #pragma warning disable DictionaryShouldContainKey // Simplify Assertion props.ContainsKey(key).Should().BeTrue(); #pragma warning restore DictionaryShouldContainKey // Simplify Assertion }
public void WriteSettingsForProject_WhenFlushed_ThrowsInvalidOperationException() { // Arrange var validConfig = new AnalysisConfig() { SonarProjectKey = "key", SonarProjectName = "name", SonarProjectVersion = "1.0", SonarOutputDir = TestUtils.CreateTestSpecificFolder(TestContext) }; var writer = new PropertiesWriter(validConfig, new TestLogger()); writer.Flush(); // Act & Assert using (new AssertIgnoreScope()) { Action act = () => writer.WriteSettingsForProject(new ProjectData(new ProjectInfo())); act.Should().ThrowExactly <InvalidOperationException>(); } }
public void WriteSettingsForProject_WhenFlushed_ThrowsInvalidOperationException() { // Arrange var validConfig = new AnalysisConfig() { SonarProjectKey = "key", SonarProjectName = "name", SonarProjectVersion = "1.0", SonarOutputDir = TestContext.DeploymentDirectory }; var writer = new PropertiesWriter(validConfig, new TestLogger()); writer.Flush(); // Act & Assert using (new AssertIgnoreScope()) { AssertException.Expects <InvalidOperationException>( () => writer.WriteSettingsForProject(new ProjectData(new ProjectInfo()))); } }
private void AssertHasProjectBaseDir(string expectedProjectDir, string fallback, params string[] projectPaths) { var config = new AnalysisConfig(); config.SonarOutputDir = fallback; var writer = new PropertiesWriter(config); using (new AssertIgnoreScope()) { foreach (string projectPath in projectPaths) { var projectInfo = new ProjectInfo { FullPath = projectPath, ProjectLanguage = ProjectLanguages.VisualBasic }; writer.WriteSettingsForProject(projectInfo, Enumerable.Empty <string>(), "", ""); } var actual = writer.Flush(); var expected = "\r\nsonar.projectBaseDir=" + PropertiesWriter.Escape(expectedProjectDir); Console.WriteLine(actual); Assert.IsTrue(actual.Contains(expected)); } }
public void PropertiesWriterToString() { var productBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_ProductBaseDir"); string productProject = CreateEmptyFile(productBaseDir, "MyProduct.csproj"); string productFile = CreateEmptyFile(productBaseDir, "File.cs"); string productChineseFile = CreateEmptyFile(productBaseDir, "你好.cs"); string productFxCopFilePath = CreateEmptyFile(productBaseDir, "productFxCopReport.txt"); string productCoverageFilePath = CreateEmptyFile(productBaseDir, "productCoverageReport.txt"); string productFileListFilePath = Path.Combine(productBaseDir, "productManagedFiles.txt"); string otherDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_OtherDir"); string missingFileOutsideProjectDir = Path.Combine(otherDir, "missing.cs"); List <string> productFiles = new List <string>(); productFiles.Add(productFile); productFiles.Add(productChineseFile); productFiles.Add(missingFileOutsideProjectDir); ProjectInfo productCS = CreateProjectInfo("你好", "DB2E5521-3172-47B9-BA50-864F12E6DFFF", productProject, false, productFiles, productFileListFilePath, productFxCopFilePath, productCoverageFilePath, ProjectLanguages.CSharp); ProjectInfo productVB = CreateProjectInfo("vbProject", "B51622CF-82F4-48C9-9F38-FB981FAFAF3A", productProject, false, productFiles, productFileListFilePath, productFxCopFilePath, productCoverageFilePath, ProjectLanguages.VisualBasic); string testBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_TestBaseDir"); string testProject = CreateEmptyFile(testBaseDir, "MyTest.csproj"); string testFile = CreateEmptyFile(testBaseDir, "File.cs"); string testFileListFilePath = Path.Combine(testBaseDir, "testManagedFiles.txt"); List <string> testFiles = new List <string>(); testFiles.Add(testFile); ProjectInfo test = CreateProjectInfo("my_test_project", "DA0FCD82-9C5C-4666-9370-C7388281D49B", testProject, true, testFiles, testFileListFilePath, null, null, ProjectLanguages.VisualBasic); AnalysisConfig config = new AnalysisConfig() { SonarProjectKey = "my_project_key", SonarProjectName = "my_project_name", SonarProjectVersion = "1.0", SonarOutputDir = @"C:\my_folder", SourcesDirectory = @"d:\source_files\" }; string actual = null; using (new AssertIgnoreScope()) // expecting the property writer to complain about the missing file { PropertiesWriter writer = new PropertiesWriter(config); writer.WriteSettingsForProject(productCS, new string[] { productFile, productChineseFile, missingFileOutsideProjectDir }, productFxCopFilePath, productCoverageFilePath); writer.WriteSettingsForProject(productVB, new string[] { productFile }, productFxCopFilePath, null); writer.WriteSettingsForProject(test, new string[] { testFile }, null, null); actual = writer.Flush(); } string expected = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.projectKey=my_project_key:DB2E5521-3172-47B9-BA50-864F12E6DFFF DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.projectName=\u4F60\u597D DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.projectBaseDir={0} DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.cs.fxcop.reportPath={1} DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.cs.vscoveragexml.reportsPaths={2} DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.sources=\ {0}\\File.cs,\ {0}\\\u4F60\u597D.cs,\ {4} B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.projectKey=my_project_key:B51622CF-82F4-48C9-9F38-FB981FAFAF3A B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.projectName=vbProject B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.projectBaseDir={0} B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.vbnet.fxcop.reportPath={1} B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.sources=\ {0}\\File.cs DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.projectKey=my_project_key:DA0FCD82-9C5C-4666-9370-C7388281D49B DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.projectName=my_test_project DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.projectBaseDir={3} DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.sources= DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.tests=\ {3}\\File.cs sonar.modules=DB2E5521-3172-47B9-BA50-864F12E6DFFF,B51622CF-82F4-48C9-9F38-FB981FAFAF3A,DA0FCD82-9C5C-4666-9370-C7388281D49B ", PropertiesWriter.Escape(productBaseDir), PropertiesWriter.Escape(productFxCopFilePath), PropertiesWriter.Escape(productCoverageFilePath), PropertiesWriter.Escape(testBaseDir), PropertiesWriter.Escape(missingFileOutsideProjectDir), PropertiesWriter.Escape(config.SourcesDirectory)); SaveToResultFile(productBaseDir, "Expected.txt", expected.ToString()); SaveToResultFile(productBaseDir, "Actual.txt", actual); Assert.AreEqual(expected, actual); }
public void PropertiesWriterToString() { var productBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_ProductBaseDir"); string productProject = CreateEmptyFile(productBaseDir, "MyProduct.csproj"); string productFile = CreateEmptyFile(productBaseDir, "File.cs"); string productChineseFile = CreateEmptyFile(productBaseDir, "你好.cs"); string productFxCopFilePath = CreateEmptyFile(productBaseDir, "productFxCopReport.txt"); string productCoverageFilePath = CreateEmptyFile(productBaseDir, "productCoverageReport.txt"); string productFileListFilePath = Path.Combine(productBaseDir, "productManagedFiles.txt"); string otherDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_OtherDir"); string missingFileOutsideProjectDir = Path.Combine(otherDir, "missing.cs"); List<string> productFiles = new List<string>(); productFiles.Add(productFile); productFiles.Add(productChineseFile); productFiles.Add(missingFileOutsideProjectDir); ProjectInfo productCS = CreateProjectInfo("你好", "DB2E5521-3172-47B9-BA50-864F12E6DFFF", productProject, false, productFiles, productFileListFilePath, productFxCopFilePath, productCoverageFilePath, ProjectLanguages.CSharp); ProjectInfo productVB = CreateProjectInfo("vbProject", "B51622CF-82F4-48C9-9F38-FB981FAFAF3A", productProject, false, productFiles, productFileListFilePath, productFxCopFilePath, productCoverageFilePath, ProjectLanguages.VisualBasic); string testBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_TestBaseDir"); string testProject = CreateEmptyFile(testBaseDir, "MyTest.csproj"); string testFile = CreateEmptyFile(testBaseDir, "File.cs"); string testFileListFilePath = Path.Combine(testBaseDir, "testManagedFiles.txt"); List<string> testFiles = new List<string>(); testFiles.Add(testFile); ProjectInfo test = CreateProjectInfo("my_test_project", "DA0FCD82-9C5C-4666-9370-C7388281D49B", testProject, true, testFiles, testFileListFilePath, null, null, ProjectLanguages.VisualBasic); AnalysisConfig config = new AnalysisConfig() { SonarProjectKey = "my_project_key", SonarProjectName = "my_project_name", SonarProjectVersion = "1.0", SonarOutputDir = @"C:\my_folder" }; string actual = null; using (new AssertIgnoreScope()) // expecting the property writer to complain about the missing file { PropertiesWriter writer = new PropertiesWriter(config); writer.WriteSettingsForProject(productCS, new string[] { productFile, productChineseFile, missingFileOutsideProjectDir }, productFxCopFilePath, productCoverageFilePath); writer.WriteSettingsForProject(productVB, new string[] { productFile }, productFxCopFilePath, null); writer.WriteSettingsForProject(test, new string[] { testFile }, null, null); actual = writer.Flush(); } string expected = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.projectKey=my_project_key:DB2E5521-3172-47B9-BA50-864F12E6DFFF DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.projectName=\u4F60\u597D DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.projectBaseDir={0} DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.cs.fxcop.reportPath={1} DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.cs.vscoveragexml.reportsPaths={2} DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.sources=\ {0}\\File.cs,\ {0}\\\u4F60\u597D.cs,\ {4} B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.projectKey=my_project_key:B51622CF-82F4-48C9-9F38-FB981FAFAF3A B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.projectName=vbProject B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.projectBaseDir={0} B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.vbnet.fxcop.reportPath={1} B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.sources=\ {0}\\File.cs DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.projectKey=my_project_key:DA0FCD82-9C5C-4666-9370-C7388281D49B DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.projectName=my_test_project DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.projectBaseDir={3} DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.sources= DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.tests=\ {3}\\File.cs sonar.projectKey=my_project_key sonar.projectName=my_project_name sonar.projectVersion=1.0 sonar.projectBaseDir={5} sonar.working.directory=C:\\my_folder\\.sonar # FIXME: Encoding is hardcoded sonar.sourceEncoding=UTF-8 sonar.modules=DB2E5521-3172-47B9-BA50-864F12E6DFFF,B51622CF-82F4-48C9-9F38-FB981FAFAF3A,DA0FCD82-9C5C-4666-9370-C7388281D49B ", PropertiesWriter.Escape(productBaseDir), PropertiesWriter.Escape(productFxCopFilePath), PropertiesWriter.Escape(productCoverageFilePath), PropertiesWriter.Escape(testBaseDir), PropertiesWriter.Escape(missingFileOutsideProjectDir), PropertiesWriter.Escape(TestUtils.GetTestSpecificFolderName(TestContext))); SaveToResultFile(productBaseDir, "Expected.txt", expected.ToString()); SaveToResultFile(productBaseDir, "Actual.txt", actual); Assert.AreEqual(expected, actual); }
public void PropertiesWriter_AnalysisSettingsWritten() { // Tests that analysis settings in the ProjectInfo are written to the file // Arrange string projectBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_AnalysisSettingsWritten"); string productProject = CreateEmptyFile(projectBaseDir, "MyProduct.csproj"); string productFile = CreateEmptyFile(projectBaseDir, "File.cs"); List<string> productFiles = new List<string>(); productFiles.Add(productFile); string productFileListFilePath = Path.Combine(projectBaseDir, "productManagedFiles.txt"); ProjectInfo product = CreateProjectInfo("AnalysisSettingsTest.proj", "7B3B7244-5031-4D74-9BBD-3316E6B5E7D5", productProject, false, productFiles, productFileListFilePath, null, null, "language"); List<ProjectInfo> projects = new List<ProjectInfo>(); projects.Add(product); AnalysisConfig config = new AnalysisConfig() { SonarOutputDir = @"C:\my_folder" }; // These are the settings we are going to check. The other analysis values are not checked. product.AnalysisSettings = new AnalysisProperties(); product.AnalysisSettings.Add(new Property() { Id = "my.setting1", Value = "setting1" }); product.AnalysisSettings.Add(new Property() { Id = "my.setting2", Value = "setting 2 with spaces" }); product.AnalysisSettings.Add(new Property() { Id = "my.setting.3", Value = @"c:\dir1\dir2\foo.txt" }); // path that will be escaped // Act PropertiesWriter writer = new PropertiesWriter(config); writer.WriteSettingsForProject(product, new string[] { productFile }, null, null); string fullActualPath = SaveToResultFile(projectBaseDir, "Actual.txt", writer.Flush()); // Assert SQPropertiesFileReader propertyReader = new SQPropertiesFileReader(fullActualPath); propertyReader.AssertSettingExists("7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.my.setting1", "setting1"); propertyReader.AssertSettingExists("7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.my.setting2", "setting 2 with spaces"); propertyReader.AssertSettingExists("7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.my.setting.3", @"c:\\dir1\\dir2\\foo.txt"); }
private void AssertHasProjectBaseDir(string expectedProjectDir, string fallback, params string[] projectPaths) { var config = new AnalysisConfig(); config.SonarOutputDir = fallback; var writer = new PropertiesWriter(config); using (new AssertIgnoreScope()) { foreach (string projectPath in projectPaths) { var projectInfo = new ProjectInfo { FullPath = projectPath, ProjectLanguage = ProjectLanguages.VisualBasic }; writer.WriteSettingsForProject(projectInfo, Enumerable.Empty<string>(), "", ""); } var actual = writer.Flush(); var expected = "\r\nsonar.projectBaseDir=" + PropertiesWriter.Escape(expectedProjectDir); Console.WriteLine(actual); Assert.IsTrue(actual.Contains(expected)); } }
public void PropertiesWriter_FxCopRerportForUnrecognisedLanguage() { var productBaseDir = TestUtils.CreateTestSpecificFolder(TestContext); string productProject = CreateEmptyFile(productBaseDir, "MyProduct.proj"); string productFile = CreateEmptyFile(productBaseDir, "File.txt"); string productFxCopFilePath = CreateEmptyFile(productBaseDir, "productFxCopReport.txt"); string productFileListFilePath = Path.Combine(productBaseDir, "productFileList.txt"); List<string> productFiles = new List<string>(); productFiles.Add(productFile); ProjectInfo product = CreateProjectInfo("myproduct", "9507E2E6-7342-4A04-9CB9-B0C47C937019", productProject, false, productFiles, productFileListFilePath, productFxCopFilePath, null, "my.language"); AnalysisConfig config = new AnalysisConfig() { SonarProjectKey = "my_project_key", SonarProjectName = "my_project_name", SonarProjectVersion = "1.0", SonarOutputDir = @"C:\my_folder" }; string actual = null; using (new AssertIgnoreScope()) // expecting the property writer to complain about having an FxCop report for an unknown language { PropertiesWriter writer = new PropertiesWriter(config); writer.WriteSettingsForProject(product, new string[] { productFile, }, productFxCopFilePath, null); actual = writer.Flush(); } string expected = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"9507E2E6-7342-4A04-9CB9-B0C47C937019.sonar.projectKey=my_project_key:9507E2E6-7342-4A04-9CB9-B0C47C937019 9507E2E6-7342-4A04-9CB9-B0C47C937019.sonar.projectName=myproduct 9507E2E6-7342-4A04-9CB9-B0C47C937019.sonar.projectBaseDir={0} 9507E2E6-7342-4A04-9CB9-B0C47C937019.sonar.sources=\ {0}\\File.txt sonar.projectKey=my_project_key sonar.projectName=my_project_name sonar.projectVersion=1.0 sonar.projectBaseDir={1} sonar.working.directory=C:\\my_folder\\.sonar # FIXME: Encoding is hardcoded sonar.sourceEncoding=UTF-8 sonar.modules=9507E2E6-7342-4A04-9CB9-B0C47C937019 ", PropertiesWriter.Escape(productBaseDir), PropertiesWriter.Escape(TestUtils.GetTestSpecificFolderName(TestContext))); SaveToResultFile(productBaseDir, "Expected.txt", expected.ToString()); SaveToResultFile(productBaseDir, "Actual.txt", actual); Assert.AreEqual(expected, actual); }
public void PropertiesWriter_InvalidOperations() { AnalysisConfig validConfig = new AnalysisConfig() { SonarProjectKey = "key", SonarProjectName = "name", SonarProjectVersion = "1.0", SonarOutputDir = this.TestContext.DeploymentDirectory }; // 1. Must supply an analysis config on construction AssertException.Expects<ArgumentNullException>(() => new PropertiesWriter(null)); // 2. Can't call WriteSettingsForProject after Flush PropertiesWriter writer = new PropertiesWriter(validConfig); writer.Flush(); AssertException.Expects<InvalidOperationException>(() => writer.Flush()); // 3. Can't call Flush twice writer = new PropertiesWriter(validConfig); writer.Flush(); using (new AssertIgnoreScope()) { AssertException.Expects<InvalidOperationException>(() => writer.WriteSettingsForProject(new ProjectInfo(), new string[] { "file" }, "fxCopReport", "code coverage report")); } }
public void PropertiesWriterToString() { var productBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_ProductBaseDir"); var productProject = CreateEmptyFile(productBaseDir, "MyProduct.csproj"); var productFile = CreateEmptyFile(productBaseDir, "File.cs"); var productChineseFile = CreateEmptyFile(productBaseDir, "你好.cs"); var productCoverageFilePath = CreateEmptyFile(productBaseDir, "productCoverageReport.txt").FullName; CreateEmptyFile(productBaseDir, "productTrx.trx"); var productFileListFilePath = Path.Combine(productBaseDir, "productManagedFiles.txt"); var otherDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_OtherDir"); var missingFileOutsideProjectDir = new FileInfo(Path.Combine(otherDir, "missing.cs")); var productFiles = new List <FileInfo> { productFile, productChineseFile, missingFileOutsideProjectDir }; var productCS = new ProjectData(CreateProjectInfo("你好", "DB2E5521-3172-47B9-BA50-864F12E6DFFF", productProject, false, productFiles, productFileListFilePath, productCoverageFilePath, ProjectLanguages.CSharp, "UTF-8")); productCS.SonarQubeModuleFiles.Add(productFile); productCS.SonarQubeModuleFiles.Add(productChineseFile); productCS.SonarQubeModuleFiles.Add(missingFileOutsideProjectDir); var productVB = new ProjectData(CreateProjectInfo("vbProject", "B51622CF-82F4-48C9-9F38-FB981FAFAF3A", productProject, false, productFiles, productFileListFilePath, productCoverageFilePath, ProjectLanguages.VisualBasic, "UTF-8")); productVB.SonarQubeModuleFiles.Add(productFile); var testBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_TestBaseDir"); var testProject = CreateEmptyFile(testBaseDir, "MyTest.csproj"); var testFile = CreateEmptyFile(testBaseDir, "File.cs"); var testFileListFilePath = Path.Combine(testBaseDir, "testManagedFiles.txt"); var testFiles = new List <FileInfo> { testFile }; var test = new ProjectData(CreateProjectInfo("my_test_project", "DA0FCD82-9C5C-4666-9370-C7388281D49B", testProject, true, testFiles, testFileListFilePath, null, ProjectLanguages.VisualBasic, "UTF-8")); test.SonarQubeModuleFiles.Add(testFile); var config = new AnalysisConfig() { SonarProjectKey = "my_project_key", SonarProjectName = "my_project_name", SonarProjectVersion = "1.0", SonarOutputDir = @"C:\my_folder", SourcesDirectory = @"d:\source_files\" }; string actual = null; using (new AssertIgnoreScope()) // expecting the property writer to complain about the missing file { var writer = new PropertiesWriter(config, new TestLogger()); writer.WriteSettingsForProject(productCS); writer.WriteSettingsForProject(productVB); writer.WriteSettingsForProject(test); actual = writer.Flush(); } var expected = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.projectKey=my_project_key:DB2E5521-3172-47B9-BA50-864F12E6DFFF DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.projectName=\u4F60\u597D DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.projectBaseDir={0} DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.sourceEncoding=utf-8 DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.sources=\ {0}\\File.cs,\ {0}\\\u4F60\u597D.cs,\ {2} DB2E5521-3172-47B9-BA50-864F12E6DFFF.sonar.working.directory=C:\\my_folder\\.sonar\\mod0 B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.projectKey=my_project_key:B51622CF-82F4-48C9-9F38-FB981FAFAF3A B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.projectName=vbProject B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.projectBaseDir={0} B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.sourceEncoding=utf-8 B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.sources=\ {0}\\File.cs B51622CF-82F4-48C9-9F38-FB981FAFAF3A.sonar.working.directory=C:\\my_folder\\.sonar\\mod1 DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.projectKey=my_project_key:DA0FCD82-9C5C-4666-9370-C7388281D49B DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.projectName=my_test_project DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.projectBaseDir={1} DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.sourceEncoding=utf-8 DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.sources= DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.tests=\ {1}\\File.cs DA0FCD82-9C5C-4666-9370-C7388281D49B.sonar.working.directory=C:\\my_folder\\.sonar\\mod2 sonar.modules=DB2E5521-3172-47B9-BA50-864F12E6DFFF,B51622CF-82F4-48C9-9F38-FB981FAFAF3A,DA0FCD82-9C5C-4666-9370-C7388281D49B ", PropertiesWriter.Escape(productBaseDir), PropertiesWriter.Escape(testBaseDir), PropertiesWriter.Escape(missingFileOutsideProjectDir)); SaveToResultFile(productBaseDir, "Expected.txt", expected.ToString()); SaveToResultFile(productBaseDir, "Actual.txt", actual); actual.Should().Be(expected); }
private void VerifyProjectBaseDir(string expectedValue, string teamBuildValue, string userValue, string[] projectPaths) { AnalysisConfig config = new AnalysisConfig(); PropertiesWriter writer = new PropertiesWriter(config); config.SonarOutputDir = TestSonarqubeOutputDir; config.SourcesDirectory = teamBuildValue; config.SetConfigValue(SonarProperties.ProjectBaseDir, userValue); using (new AssertIgnoreScope()) { foreach (string projectPath in projectPaths) { var projectInfo = new ProjectInfo { FullPath = projectPath, ProjectLanguage = ProjectLanguages.CSharp }; writer.WriteSettingsForProject(projectInfo, Enumerable.Empty<string>(), "", ""); } var actual = writer.Flush(); var expected = "\r\nsonar.projectBaseDir=" + PropertiesWriter.Escape(expectedValue); Assert.IsTrue(actual.Contains(expected)); } }