コード例 #1
0
        public void ProcArgs_GetSettingOrDefault()
        {
            // 0. Setup
            ListPropertiesProvider cmdLineProps = new ListPropertiesProvider();
            cmdLineProps.AddProperty("cmd.key.1", "cmd value 1");
            cmdLineProps.AddProperty("shared.key.1", "shared cmd value");

            ListPropertiesProvider fileProps = new ListPropertiesProvider();
            fileProps.AddProperty("file.key.1", "file value 1");
            fileProps.AddProperty("shared.key.1", "shared file value");

            ProcessedArgs args = new ProcessedArgs("key", "name", "ver", true, cmdLineProps, fileProps);

            // 1. Missing key -> default returned
            string result = args.GetSetting("missing.property", "default value");
            Assert.AreEqual("default value", result);

            // 2. Returns existing values
            result = args.GetSetting("file.key.1", "default value");
            Assert.AreEqual("file value 1", result);

            // 3. Precedence - command line properties should win
            Assert.AreEqual("shared cmd value", args.GetSetting("shared.key.1", "default ValueType"));

            // 4. Preprocessor only settings
            Assert.AreEqual(true, args.InstallLoaderTargets);
        }
        public void AnalysisConfGen_AnalysisConfigDoesNotContainSensitiveData()
        {
            // Arrange
            string analysisDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            TestLogger logger = new TestLogger();

            ListPropertiesProvider cmdLineArgs = new ListPropertiesProvider();
            // Public args - should be written to the config file
            cmdLineArgs.AddProperty("sonar.host.url", "http://host");
            cmdLineArgs.AddProperty("public.key", "public value");

            // Sensitive values - should not be written to the config file
            cmdLineArgs.AddProperty(SonarProperties.DbPassword, "secret db password");
            cmdLineArgs.AddProperty(SonarProperties.DbUserName, "secret db user");

            // Create a settings file with public and sensitive data
            AnalysisProperties fileSettings = new AnalysisProperties();
            fileSettings.Add(new Property() { Id = "file.public.key", Value = "file public value" });
            fileSettings.Add(new Property() {Id = SonarProperties.DbUserName, Value = "secret db user"});
            fileSettings.Add(new Property() { Id = SonarProperties.DbPassword, Value = "secret db password"});
            string fileSettingsPath = Path.Combine(analysisDir, "fileSettings.txt");
            fileSettings.Save(fileSettingsPath);
            FilePropertyProvider fileProvider = FilePropertyProvider.Load(fileSettingsPath);

            ProcessedArgs args = new ProcessedArgs("key", "name", "1.0", false, cmdLineArgs, fileProvider);

            IDictionary<string, string> serverProperties = new Dictionary<string, string>();
            // Public server settings
            serverProperties.Add("server.key.1", "server value 1");
            // Sensitive server settings
            serverProperties.Add(SonarProperties.SonarUserName, "secret user");
            serverProperties.Add(SonarProperties.SonarPassword, "secret pwd");

            TeamBuildSettings settings = TeamBuildSettings.CreateNonTeamBuildSettingsForTesting(analysisDir);
            Directory.CreateDirectory(settings.SonarConfigDirectory); // config directory needs to exist

            // Act
            AnalysisConfig config = AnalysisConfigGenerator.GenerateFile(args, settings, serverProperties, logger);

            // Assert
            AssertConfigFileExists(config);
            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);

            // Check the config

            // "Public" arguments should be in the file
            Assert.AreEqual("key", config.SonarProjectKey, "Unexpected project key");
            Assert.AreEqual("name", config.SonarProjectName, "Unexpected project name");
            Assert.AreEqual("1.0", config.SonarProjectVersion, "Unexpected project version");

            AssertExpectedLocalSetting(SonarProperties.HostUrl, "http://host", config);
            AssertExpectedServerSetting("server.key.1", "server value 1", config);

            AssertFileDoesNotContainText(config.FileName, "file.public.key"); // file settings values should not be in the config

            // SONARMSBRU-136: TODO - uncomment the following code:
            AssertFileDoesNotContainText(config.FileName, "secret"); // sensitive data should not be in config
        }
コード例 #3
0
        public void ProcArgs_TryGetSetting()
        {
            // 0. Setup
            ListPropertiesProvider cmdLineProps = new ListPropertiesProvider();
            cmdLineProps.AddProperty("cmd.key.1", "cmd value 1");
            cmdLineProps.AddProperty("shared.key.1", "shared cmd value");

            ListPropertiesProvider fileProps = new ListPropertiesProvider();
            fileProps.AddProperty("file.key.1", "file value 1");
            fileProps.AddProperty("shared.key.1", "shared file value");

            ProcessedArgs args = new ProcessedArgs("key", "name", "ver", false, cmdLineProps, fileProps);

            // 1. Missing key -> null
            string result;
            Assert.IsFalse(args.TryGetSetting("missing.property", out result), "Expecting false when the specified key does not exist");
            Assert.IsNull(result, "Expecting the value to be null when the specified key does not exist");

            // 2. Returns existing values
            Assert.IsTrue(args.TryGetSetting("cmd.key.1", out result));
            Assert.AreEqual("cmd value 1", result);

            // 3. Precedence - command line properties should win
            Assert.AreEqual("shared cmd value", args.GetSetting("shared.key.1"));

            // 4. Preprocessor only settings
            Assert.AreEqual(false, args.InstallLoaderTargets);
        }
コード例 #4
0
        public void ProcArgs_GetSetting()
        {
            // 0. Setup
            ListPropertiesProvider cmdLineProps = new ListPropertiesProvider();
            cmdLineProps.AddProperty("cmd.key.1", "cmd value 1");
            cmdLineProps.AddProperty("shared.key.1", "shared cmd value");

            ListPropertiesProvider fileProps = new ListPropertiesProvider();
            fileProps.AddProperty("file.key.1", "file value 1");
            fileProps.AddProperty("shared.key.1", "shared file value");

            ProcessedArgs args = new ProcessedArgs("key", "name", "ver", true, cmdLineProps, fileProps);

            // 1. Throws on missing value
            AssertException.Expects<InvalidOperationException>(() => args.GetSetting("missing.property"));

            // 2. Returns existing values
            Assert.AreEqual("cmd value 1", args.GetSetting("cmd.key.1"));
            Assert.AreEqual("file value 1", args.GetSetting("file.key.1"));

            // 3. Precedence - command line properties should win
            Assert.AreEqual("shared cmd value", args.GetSetting("shared.key.1"));

            // 4. Preprocessor only settings
            Assert.AreEqual(true, args.InstallLoaderTargets);
        }
コード例 #5
0
        public void AnalysisConfGen_Simple()
        {
            // Arrange
            string analysisDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            TestLogger logger = new TestLogger();

            ListPropertiesProvider propertyProvider = new ListPropertiesProvider();
            propertyProvider.AddProperty(SonarProperties.HostUrl, "http://foo");
            ProcessedArgs args = new ProcessedArgs("valid.key", "valid.name", "1.0", false, EmptyPropertyProvider.Instance, propertyProvider);

            TeamBuildSettings tbSettings = TeamBuildSettings.CreateNonTeamBuildSettingsForTesting(analysisDir);

            Dictionary<string, string> serverSettings = new Dictionary<string, string>();
            serverSettings.Add("server.key.1", "server.value.1");

            AnalyzerSettings analyzerSettings = new AnalyzerSettings();
            analyzerSettings.RuleSetFilePath = "c:\\xxx.ruleset";
            analyzerSettings.AdditionalFilePaths = new List<string>();
            analyzerSettings.AdditionalFilePaths.Add("f:\\additionalPath1.txt");
            analyzerSettings.AnalyzerAssemblyPaths = new List<string>();
            analyzerSettings.AnalyzerAssemblyPaths.Add("f:\\temp\\analyzer1.dll");

            Directory.CreateDirectory(tbSettings.SonarConfigDirectory); // config directory needs to exist

            // Act
            AnalysisConfig actualConfig = AnalysisConfigGenerator.GenerateFile(args, tbSettings, serverSettings, analyzerSettings, logger);

            // Assert
            AssertConfigFileExists(actualConfig);
            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);

            Assert.AreEqual("valid.key", actualConfig.SonarProjectKey);
            Assert.AreEqual("valid.name", actualConfig.SonarProjectName);
            Assert.AreEqual("1.0", actualConfig.SonarProjectVersion);

            Assert.AreEqual("http://foo", actualConfig.SonarQubeHostUrl);

            Assert.AreEqual(tbSettings.SonarBinDirectory, actualConfig.SonarBinDir);
            Assert.AreEqual(tbSettings.SonarConfigDirectory, actualConfig.SonarConfigDir);
            Assert.AreEqual(tbSettings.SonarOutputDirectory, actualConfig.SonarOutputDir);
            Assert.AreEqual(tbSettings.SonarRunnerWorkingDirectory, actualConfig.SonarRunnerWorkingDirectory);
            Assert.AreEqual(tbSettings.BuildUri, actualConfig.GetBuildUri());
            Assert.AreEqual(tbSettings.TfsUri, actualConfig.GetTfsUri());

            Assert.IsNotNull(actualConfig.ServerSettings);
            Property serverProperty = actualConfig.ServerSettings.SingleOrDefault(s => string.Equals(s.Id, "server.key.1", System.StringComparison.Ordinal));
            Assert.IsNotNull(serverProperty);
            Assert.AreEqual("server.value.1", serverProperty.Value);

            Assert.AreSame(analyzerSettings, actualConfig.AnalyzerSettings);
        }
コード例 #6
0
        public void ProcArgs_CmdLinePropertiesOverrideFileSettings()
        {
            // Checks command line properties override those from files

            // Arrange
            // The set of command line properties to supply
            ListPropertiesProvider cmdLineProperties = new ListPropertiesProvider();
            cmdLineProperties.AddProperty("shared.key1", "cmd line value1 - should override server value");
            cmdLineProperties.AddProperty("cmd.line.only", "cmd line value4 - only on command line");
            cmdLineProperties.AddProperty("xxx", "cmd line value XXX - lower case");
            cmdLineProperties.AddProperty(SonarProperties.HostUrl, "http://host");

            // The set of file properties to supply
            ListPropertiesProvider fileProperties = new ListPropertiesProvider();
            fileProperties.AddProperty("shared.key1", "file value1 - should be overridden");
            fileProperties.AddProperty("file.only", "file value3 - only in file");
            fileProperties.AddProperty("XXX", "file line value XXX - upper case");

            // Act
            ProcessedArgs args = new ProcessedArgs("key", "name", "version", false, cmdLineProperties, fileProperties);

            AssertExpectedValue("shared.key1", "cmd line value1 - should override server value", args);
            AssertExpectedValue("cmd.line.only", "cmd line value4 - only on command line", args);
            AssertExpectedValue("file.only", "file value3 - only in file", args);
            AssertExpectedValue("xxx", "cmd line value XXX - lower case", args);
            AssertExpectedValue("XXX", "file line value XXX - upper case", args);
            AssertExpectedValue(SonarProperties.HostUrl, "http://host", args);
        }
コード例 #7
0
        public void AnalysisConfGen_CommandLineAndProgrammaticProperties()
        {
            // Arrange
            TestLogger logger = new TestLogger();

            string analysisDir = TestUtils.CreateTestSpecificFolder(this.TestContext);
            TeamBuildSettings tbSettings = TeamBuildSettings.CreateNonTeamBuildSettingsForTesting(analysisDir);

            Dictionary<string, string> emptyServerProperties = new Dictionary<string, string>();
            AnalyzerSettings emptyAnalyzerSettings = new AnalyzerSettings();
            Directory.CreateDirectory(tbSettings.SonarConfigDirectory); // config directory needs to exist

            // Define a set of user properties
            ListPropertiesProvider userProperties = new ListPropertiesProvider();
            userProperties.AddProperty("sonar.host.url", "http://host"); // required
            userProperties.AddProperty("user1", "user value 1");
            userProperties.AddProperty("shared.key.1", "user value for shared.key.1");
            ProcessedArgs cmdLineArgs = new ProcessedArgs("key", "name", "version", false, userProperties, EmptyPropertyProvider.Instance);

            // Define a set of programmatic properties
            AnalysisProperties programmaticProperties = new AnalysisProperties();
            programmaticProperties.Add(new Property() { Id = "prog1", Value = "prog value 1" });
            programmaticProperties.Add(new Property() { Id = "shared.key.1", Value = "prog value for shared.key.1" });

            // Act
            AnalysisConfig actualConfig = AnalysisConfigGenerator.GenerateFile(cmdLineArgs, tbSettings, emptyServerProperties, emptyAnalyzerSettings, programmaticProperties, logger);

            // Assert
            AssertConfigFileExists(actualConfig);
            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);

            // Check that user properties take precedence over the programmatic ones
            AssertExpectedLocalSetting("sonar.host.url", "http://host", actualConfig);
            AssertExpectedLocalSetting("user1", "user value 1", actualConfig);
            AssertExpectedLocalSetting("shared.key.1", "user value for shared.key.1", actualConfig);
            AssertExpectedLocalSetting("prog1", "prog value 1", actualConfig);
        }