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");
        }
예제 #2
0
        [TestMethod] // Old VS Bootstrapper should be forceably disabled: https://jira.sonarsource.com/browse/SONARMSBRU-122
        public void FileGen_VSBootstrapperIsDisabled()
        {
            // 0. Arrange
            TestLogger logger = new TestLogger();

            // Act
            ProjectInfoAnalysisResult result = ExecuteAndCheckSucceeds("disableBootstrapper", logger);

            // Assert
            SQPropertiesFileReader provider = new SQPropertiesFileReader(result.FullPropertiesFilePath);

            provider.AssertSettingExists(PropertiesFileGenerator.VSBootstrapperPropertyKey, "false");
            logger.AssertWarningsLogged(0);
        }
예제 #3
0
        [TestMethod] // Old VS Bootstrapper should be forceably disabled: https://jira.sonarsource.com/browse/SONARMSBRU-122
        public void FileGen_VSBootstrapperIsDisabled()
        {
            // 0. Arrange
            var logger = new TestLogger();

            // Act
            var result = ExecuteAndCheckSucceeds("disableBootstrapper", logger);

            // Assert
            var provider = new SQPropertiesFileReader(result.FullPropertiesFilePath);

            provider.AssertSettingExists(AnalysisConfigExtensions.VSBootstrapperPropertyKey, "false");
            logger.AssertWarningsLogged(0);
        }
        public void PropertiesWriter_GlobalSettingsWritten()
        {
            // Tests that global settings in the ProjectInfo are written to the file

            // Arrange
            var projectBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_GlobalSettingsWritten");

            var config = new AnalysisConfig()
            {
                SonarOutputDir = @"C:\my_folder"
            };

            var globalSettings = 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

                // Specific test for sonar.branch property
                new Property()
                {
                    Id = "sonar.branch", Value = "aBranch"
                }                                                         // path that will be escaped
            };

            // Act
            var writer = new PropertiesWriter(config, new TestLogger());

            writer.WriteGlobalSettings(globalSettings);
            var fullActualPath = SaveToResultFile(projectBaseDir, "Actual.txt", writer.Flush());

            // Assert
            var propertyReader = new SQPropertiesFileReader(fullActualPath);

            propertyReader.AssertSettingExists("my.setting1", "setting1");
            propertyReader.AssertSettingExists("my.setting2", "setting 2 with spaces");
            propertyReader.AssertSettingExists("my.setting.3", @"c:\dir1\dir2\foo.txt");

            propertyReader.AssertSettingExists("sonar.branch", "aBranch");
        }
예제 #5
0
        public void FileGen_SharedFiles_CaseInsensitive()
        {
            // Arrange
            string testDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            // Create 2 uuids and order them so that test is reproducible
            Guid[] uuids = new Guid[] { Guid.NewGuid(), Guid.NewGuid() };
            Array.Sort(uuids);

            string project1Dir             = TestUtils.EnsureTestSpecificFolder(this.TestContext, "project1");
            string project1Path            = Path.Combine(project1Dir, "project1.proj");
            string project1Info            = CreateProjectInfoInSubDir(testDir, "projectName1", uuids[0], ProjectType.Product, false, project1Path, "UTF-8"); // not excluded
            var    sharedFile              = Path.Combine(testDir, "contentFile.txt");
            var    sharedFileDifferentCase = Path.Combine(testDir, "ContentFile.TXT");

            CreateEmptyFile(testDir, "contentFile.txt");

            // Reference shared file, but not under the project directory
            string contentFileList1 = CreateFile(project1Dir, "contentList.txt", sharedFile);

            AddAnalysisResult(project1Info, AnalysisType.FilesToAnalyze, contentFileList1);

            string project2Dir  = TestUtils.EnsureTestSpecificFolder(this.TestContext, "project2");
            string project2Path = Path.Combine(project2Dir, "project2.proj");
            string project2Info = CreateProjectInfoInSubDir(testDir, "projectName2", uuids[1], ProjectType.Product, false, project2Path, "UTF-8"); // not excluded

            // Reference shared file, but not under the project directory
            string contentFileList2 = CreateFile(project2Dir, "contentList.txt", sharedFileDifferentCase);

            AddAnalysisResult(project2Info, AnalysisType.FilesToAnalyze, contentFileList2);

            TestLogger     logger = new TestLogger();
            AnalysisConfig config = CreateValidConfig(testDir);

            // Act
            ProjectInfoAnalysisResult result = PropertiesFileGenerator.GenerateFile(config, logger);

            // Assert
            SQPropertiesFileReader provider = new SQPropertiesFileReader(result.FullPropertiesFilePath);

            provider.AssertSettingExists("sonar.projectBaseDir", testDir);
            // First one wins
            provider.AssertSettingExists("sonar.sources", sharedFile);
        }
예제 #6
0
        public void FileGen_ValidFiles_WithFixableSarif()
        {
            // Arrange
            string testDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            // SARIF file path
            string testSarifPath = Path.Combine(testDir, "testSarif.json");

            // Create SARIF report path property and add it to the project info
            AnalysisProperties projectSettings = new AnalysisProperties
            {
                new Property()
                {
                    Id = PropertiesFileGenerator.ReportFileCsharpPropertyKey, Value = testSarifPath
                }
            };
            Guid projectGuid = Guid.NewGuid();

            CreateProjectWithFiles("withFiles1", testDir, projectGuid, true, projectSettings);

            TestLogger     logger = new TestLogger();
            AnalysisConfig config = CreateValidConfig(testDir);

            // Mock SARIF fixer simulates fixable SARIF with fixed name
            string returnPathDir      = Path.GetDirectoryName(testSarifPath);
            string returnPathFileName = Path.GetFileNameWithoutExtension(testSarifPath) +
                                        RoslynV1SarifFixer.FixedFileSuffix + Path.GetExtension(testSarifPath);

            MockRoslynV1SarifFixer mockSarifFixer = new MockRoslynV1SarifFixer(returnPathFileName);
            string escapedMockReturnPath          = mockSarifFixer.ReturnVal.Replace(@"\", @"\\");

            // Act
            ProjectInfoAnalysisResult result = PropertiesFileGenerator.GenerateFile(config, logger, mockSarifFixer);

            // Assert
            Assert.AreEqual(1, mockSarifFixer.CallCount);
            Assert.AreEqual(RoslynV1SarifFixer.CSharpLanguage, mockSarifFixer.LastLanguage);


            // Fixable SARIF -> new file saved -> changed property
            SQPropertiesFileReader provider = new SQPropertiesFileReader(result.FullPropertiesFilePath);

            provider.AssertSettingExists(projectGuid.ToString().ToUpper() + "." + PropertiesFileGenerator.ReportFileCsharpPropertyKey, escapedMockReturnPath);
        }
예제 #7
0
        public void FileGen_VSBootstrapperIsDisabled_OverrideUserSettings_SameValue()
        {
            // Arrange
            TestLogger logger = new TestLogger();
            Property   bootstrapperProperty = new Property()
            {
                Id = PropertiesFileGenerator.VSBootstrapperPropertyKey, Value = "false"
            };

            // Act
            ProjectInfoAnalysisResult result = ExecuteAndCheckSucceeds("disableBootstrapperSame", logger, bootstrapperProperty);

            // Assert
            SQPropertiesFileReader provider = new SQPropertiesFileReader(result.FullPropertiesFilePath);

            provider.AssertSettingExists(PropertiesFileGenerator.VSBootstrapperPropertyKey, "false");
            logger.AssertSingleDebugMessageExists(PropertiesFileGenerator.VSBootstrapperPropertyKey);
            logger.AssertWarningsLogged(0); // not expecting a warning if the user has supplied the value we want
        }
예제 #8
0
        public void FileGen_SharedFiles_BelongToAnotherProject()
        {
            // Shared files that belong to another project should NOT be attached to the root project

            // Arrange
            string testDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            string project1Dir    = TestUtils.EnsureTestSpecificFolder(this.TestContext, "project1");
            string project1Path   = Path.Combine(project1Dir, "project1.proj");
            Guid   project1Guid   = Guid.NewGuid();
            string project1Info   = CreateProjectInfoInSubDir(testDir, "projectName1", project1Guid, ProjectType.Product, false, project1Path, "UTF-8"); // not excluded
            var    fileInProject1 = Path.Combine(project1Dir, "contentFile.txt");

            CreateEmptyFile(project1Dir, "contentFile.txt");

            // Reference shared file, but not under the project directory
            string contentFileList1 = CreateFile(project1Dir, "contentList.txt", fileInProject1);

            AddAnalysisResult(project1Info, AnalysisType.FilesToAnalyze, contentFileList1);

            string project2Dir  = TestUtils.EnsureTestSpecificFolder(this.TestContext, "project2");
            string project2Path = Path.Combine(project2Dir, "project2.proj");
            string project2Info = CreateProjectInfoInSubDir(testDir, "projectName2", Guid.NewGuid(), ProjectType.Product, false, project2Path, "UTF-8"); // not excluded

            // Reference shared file, but not under the project directory
            string contentFileList2 = CreateFile(project2Dir, "contentList.txt", fileInProject1);

            AddAnalysisResult(project2Info, AnalysisType.FilesToAnalyze, contentFileList2);

            TestLogger     logger = new TestLogger();
            AnalysisConfig config = CreateValidConfig(testDir);

            // Act
            ProjectInfoAnalysisResult result = PropertiesFileGenerator.GenerateFile(config, logger);

            // Assert
            SQPropertiesFileReader provider = new SQPropertiesFileReader(result.FullPropertiesFilePath);

            provider.AssertSettingExists("sonar.projectBaseDir", testDir);
            provider.AssertSettingDoesNotExist("sonar.sources");
            provider.AssertSettingExists(project1Guid.ToString().ToUpper() + ".sonar.sources", fileInProject1);
        }
예제 #9
0
        public void FileGen_VSBootstrapperIsDisabled_OverrideUserSettings_DifferentValue()
        {
            // 0. Arrange
            TestLogger logger = new TestLogger();

            // Try to explicitly enable the setting
            Property bootstrapperProperty = new Property()
            {
                Id = PropertiesFileGenerator.VSBootstrapperPropertyKey, Value = "true"
            };

            // Act
            ProjectInfoAnalysisResult result = ExecuteAndCheckSucceeds("disableBootstrapperDiff", logger, bootstrapperProperty);

            // Assert
            SQPropertiesFileReader provider = new SQPropertiesFileReader(result.FullPropertiesFilePath);

            provider.AssertSettingExists(PropertiesFileGenerator.VSBootstrapperPropertyKey, "false");
            logger.AssertSingleWarningExists(PropertiesFileGenerator.VSBootstrapperPropertyKey);
        }
예제 #10
0
        public void FileGen_ValidFiles_WithUnfixableSarif()
        {
            // Arrange
            string testDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            // SARIF file path
            string testSarifPath    = Path.Combine(testDir, "testSarif.json");
            string escapedSarifPath = testSarifPath.Replace(@"\", @"\\");

            // Create SARIF report path property and add it to the project info
            AnalysisProperties projectSettings = new AnalysisProperties
            {
                new Property()
                {
                    Id = PropertiesFileGenerator.ReportFileCsharpPropertyKey, Value = testSarifPath
                }
            };
            Guid projectGuid = Guid.NewGuid();

            CreateProjectWithFiles("withFiles1", testDir, projectGuid, true, projectSettings);

            TestLogger     logger = new TestLogger();
            AnalysisConfig config = CreateValidConfig(testDir);

            // Mock SARIF fixer simulated unfixable/absent file
            MockRoslynV1SarifFixer mockSarifFixer = new MockRoslynV1SarifFixer(null);

            // Act
            ProjectInfoAnalysisResult result = PropertiesFileGenerator.GenerateFile(config, logger, mockSarifFixer);

            // Assert
            Assert.AreEqual(1, mockSarifFixer.CallCount);

            // One valid project info file -> file created
            AssertPropertiesFilesCreated(result, logger);

            // Unfixable SARIF -> cannot fix -> report file property removed
            SQPropertiesFileReader provider = new SQPropertiesFileReader(result.FullPropertiesFilePath);

            provider.AssertSettingDoesNotExist(projectGuid.ToString().ToUpper() + "." + PropertiesFileGenerator.ReportFileCsharpPropertyKey);
        }
예제 #11
0
        public void PropertiesWriter_GlobalSettingsWritten()
        {
            // Tests that global settings in the ProjectInfo are written to the file

            // Arrange
            string projectBaseDir = TestUtils.CreateTestSpecificFolder(TestContext, "PropertiesWriterTest_GlobalSettingsWritten");

            AnalysisConfig config = new AnalysisConfig()
            {
                SonarOutputDir = @"C:\my_folder"
            };

            AnalysisProperties globalSettings = new AnalysisProperties();

            globalSettings.Add(new Property()
            {
                Id = "my.setting1", Value = "setting1"
            });
            globalSettings.Add(new Property()
            {
                Id = "my.setting2", Value = "setting 2 with spaces"
            });
            globalSettings.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.WriteGlobalSettings(globalSettings);
            string fullActualPath = SaveToResultFile(projectBaseDir, "Actual.txt", writer.Flush());

            // Assert
            SQPropertiesFileReader propertyReader = new SQPropertiesFileReader(fullActualPath);

            propertyReader.AssertSettingExists("my.setting1", "setting1");
            propertyReader.AssertSettingExists("my.setting2", "setting 2 with spaces");
            propertyReader.AssertSettingExists("my.setting.3", @"c:\\dir1\\dir2\\foo.txt");
        }
예제 #12
0
        public void FileGen_SharedFiles()
        {
            // Shared files should be attached to the root project

            // Arrange
            var testDir = TestUtils.CreateTestSpecificFolder(TestContext);

            var project1Dir  = TestUtils.EnsureTestSpecificFolder(TestContext, "project1");
            var project1Path = Path.Combine(project1Dir, "project1.proj");
            var project1Info = CreateProjectInfoInSubDir(testDir, "projectName1", Guid.NewGuid(), ProjectType.Product, false, project1Path, "UTF-8"); // not excluded
            var sharedFile   = Path.Combine(testDir, "contentFile.txt");

            CreateEmptyFile(testDir, "contentFile.txt");

            // Reference shared file, but not under the project directory
            var contentFileList1 = CreateFile(project1Dir, "contentList.txt", sharedFile);

            AddAnalysisResult(project1Info, AnalysisType.FilesToAnalyze, contentFileList1);

            var project2Dir  = TestUtils.EnsureTestSpecificFolder(TestContext, "project2");
            var project2Path = Path.Combine(project2Dir, "project2.proj");
            var project2Info = CreateProjectInfoInSubDir(testDir, "projectName2", Guid.NewGuid(), ProjectType.Product, false, project2Path, "UTF-8"); // not excluded

            // Reference shared file, but not under the project directory
            var contentFileList2 = CreateFile(project2Dir, "contentList.txt", sharedFile);

            AddAnalysisResult(project2Info, AnalysisType.FilesToAnalyze, contentFileList2);

            var logger = new TestLogger();
            var config = CreateValidConfig(testDir);

            // Act
            var result = new PropertiesFileGenerator(config, logger).GenerateFile();

            // Assert
            var provider = new SQPropertiesFileReader(result.FullPropertiesFilePath);

            provider.AssertSettingExists("sonar.projectBaseDir", testDir);
            provider.AssertSettingExists("sonar.sources", sharedFile);
        }
예제 #13
0
        public void FileGen_ValidFiles_WithAlreadyValidSarif()
        {
            // Arrange
            string testDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            // SARIF file path
            string testSarifPath = Path.Combine(testDir, "testSarif.json");

            // Create SARIF report path property and add it to the project info
            AnalysisProperties projectSettings = new AnalysisProperties
            {
                new Property()
                {
                    Id = PropertiesFileGenerator.ReportFileCsharpPropertyKey, Value = testSarifPath
                }
            };
            Guid projectGuid = Guid.NewGuid();

            CreateProjectWithFiles("withFiles1", testDir, projectGuid, true, projectSettings);

            TestLogger     logger = new TestLogger();
            AnalysisConfig config = CreateValidConfig(testDir);

            // Mock SARIF fixer simulates already valid sarif
            MockRoslynV1SarifFixer mockSarifFixer = new MockRoslynV1SarifFixer(testSarifPath);
            string mockReturnPath = mockSarifFixer.ReturnVal;

            // Act
            ProjectInfoAnalysisResult result = PropertiesFileGenerator.GenerateFile(config, logger, mockSarifFixer);

            // Assert
            Assert.AreEqual(1, mockSarifFixer.CallCount);

            // Already valid SARIF -> no change in file -> unchanged property
            SQPropertiesFileReader provider = new SQPropertiesFileReader(result.FullPropertiesFilePath);

            provider.AssertSettingExists(projectGuid.ToString().ToUpper() + "." + PropertiesFileGenerator.ReportFileCsharpPropertyKey, mockReturnPath);
        }
예제 #14
0
        public void FileGen_AdditionalProperties()
        {
            // 0. Arrange
            string     analysisRootDir = TestUtils.CreateTestSpecificFolder(this.TestContext);
            TestLogger logger          = new TestLogger();

            CreateProjectWithFiles("project1", analysisRootDir);
            AnalysisConfig config = CreateValidConfig(analysisRootDir);

            // Add additional properties
            config.LocalSettings = new AnalysisProperties();
            config.LocalSettings.Add(new Property()
            {
                Id = "key1", Value = "value1"
            });
            config.LocalSettings.Add(new Property()
            {
                Id = "key.2", Value = "value two"
            });
            config.LocalSettings.Add(new Property()
            {
                Id = "key.3", Value = " "
            });

            // Sensitive data should not be written
            config.LocalSettings.Add(new Property()
            {
                Id = SonarProperties.DbPassword, Value = "secret db pwd"
            });
            config.LocalSettings.Add(new Property()
            {
                Id = SonarProperties.SonarPassword, Value = "secret pwd"
            });

            // Server properties should not be added
            config.ServerSettings = new AnalysisProperties();
            config.ServerSettings.Add(new Property()
            {
                Id = "server.key", Value = "should not be added"
            });

            // Act
            ProjectInfoAnalysisResult result = PropertiesFileGenerator.GenerateFile(config, logger);

            // Assert
            AssertExpectedProjectCount(1, result);

            // One valid project info file -> file created
            AssertPropertiesFilesCreated(result, logger);

            SQPropertiesFileReader provider = new SQPropertiesFileReader(result.FullPropertiesFilePath);

            provider.AssertSettingExists("key1", "value1");
            provider.AssertSettingExists("key.2", "value two");
            provider.AssertSettingExists("key.3", " ");

            provider.AssertSettingDoesNotExist("server.key");

            provider.AssertSettingDoesNotExist(SonarProperties.DbPassword);
            provider.AssertSettingDoesNotExist(SonarProperties.SonarPassword);
        }