Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to process the supplied command line arguments and
        /// reports any errors using the logger.
        /// Returns null unless all of the properties are valid.
        /// </summary>
        public static ProcessedArgs TryProcessArgs(string[] commandLineArgs, ILogger logger)
        {
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            ProcessedArgs processed = null;
            IEnumerable <ArgumentInstance> arguments;

            // This call will fail if there are duplicate, missing, or unrecognized arguments
            CommandLineParser parser = new CommandLineParser(Descriptors, false /* don't allow unrecognized */);
            bool parsedOk            = parser.ParseArguments(commandLineArgs, logger, out arguments);

            // Handle the /install: command line only argument
            bool installLoaderTargets;

            parsedOk &= TryGetInstallTargetsEnabled(arguments, logger, out installLoaderTargets);

            // Handler for command line analysis properties
            IAnalysisPropertyProvider cmdLineProperties;

            parsedOk &= CmdLineArgPropertyProvider.TryCreateProvider(arguments, logger, out cmdLineProperties);

            // Handler for scanner environment properties
            IAnalysisPropertyProvider scannerEnvProperties;

            parsedOk &= EnvScannerPropertiesProvider.TryCreateProvider(logger, out scannerEnvProperties);

            // Handler for property file
            IAnalysisPropertyProvider globalFileProperties;
            string asmPath = Path.GetDirectoryName(typeof(PreProcessor.ArgumentProcessor).Assembly.Location);

            parsedOk &= FilePropertyProvider.TryCreateProvider(arguments, asmPath, logger, out globalFileProperties);

            if (parsedOk)
            {
                Debug.Assert(cmdLineProperties != null);
                Debug.Assert(globalFileProperties != null);

                processed = new ProcessedArgs(
                    GetArgumentValue(KeywordIds.ProjectKey, arguments),
                    GetArgumentValue(KeywordIds.ProjectName, arguments),
                    GetArgumentValue(KeywordIds.ProjectVersion, arguments),
                    GetArgumentValue(KeywordIds.Organization, arguments),
                    installLoaderTargets,
                    cmdLineProperties,
                    globalFileProperties,
                    scannerEnvProperties);

                if (!AreParsedArgumentsValid(processed, logger))
                {
                    processed = null;
                }
            }

            return(processed);
        }
Ejemplo n.º 2
0
        public void ParseInvalidJson()
        {
            var logger = new TestLogger();

            Environment.SetEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "trash");
            var result = EnvScannerPropertiesProvider.TryCreateProvider(logger, out IAnalysisPropertyProvider provider);

            Assert.IsFalse(result);
            logger.AssertErrorLogged("Failed to parse properties from the environment variable 'SONARQUBE_SCANNER_PARAMS'");
        }
        public void ParseInvalidJson()
        {
            var logger = new TestLogger();

            // Make sure the test isn't affected by the hosting environment and
            // does not affect the hosting environment
            // The SonarCloud VSTS extension sets additional properties in an environment variable that
            // would affect the test.
            using (var scope = new EnvironmentVariableScope())
            {
                scope.SetVariable("SONARQUBE_SCANNER_PARAMS", "trash");
                var result = EnvScannerPropertiesProvider.TryCreateProvider(logger, out var provider);
                result.Should().BeFalse();
                logger.AssertErrorLogged("Failed to parse properties from the environment variable 'SONARQUBE_SCANNER_PARAMS'");
            }
        }