public void BootSettings_DownloadDirFromEnvVars()
        {
            // 0. Setup
            TestLogger logger = new TestLogger();

            // 1. Legacy TFS variable will be used if available
            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(BootstrapperSettings.BuildDirectory_Legacy, "legacy tf build");
                scope.SetVariable(BootstrapperSettings.BuildDirectory_TFS2015, null);

                IBootstrapperSettings settings = new BootstrapperSettings(AnalysisPhase.PreProcessing, null, "http://sq", LoggerVerbosity.Debug, logger);
                AssertExpectedDownloadDir(Path.Combine("legacy tf build", DownloadFolderRelativePath), settings);
            }

            // 2. TFS2015 variable will be used if available
            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(BootstrapperSettings.BuildDirectory_Legacy, null);
                scope.SetVariable(BootstrapperSettings.BuildDirectory_TFS2015, "tfs build");

                IBootstrapperSettings settings = new BootstrapperSettings(AnalysisPhase.PreProcessing, null, "http://sq", LoggerVerbosity.Debug, logger);
                AssertExpectedDownloadDir(Path.Combine("tfs build", DownloadFolderRelativePath), settings);
            }

            // 3. CWD has least precedence over env variables
            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(BootstrapperSettings.BuildDirectory_Legacy, null);
                scope.SetVariable(BootstrapperSettings.BuildDirectory_TFS2015, null);

                IBootstrapperSettings settings = new BootstrapperSettings(AnalysisPhase.PreProcessing, null, "http://sq", LoggerVerbosity.Debug, logger);
                AssertExpectedDownloadDir(Path.Combine(Directory.GetCurrentDirectory(), DownloadFolderRelativePath), settings);
            }
        }
        /// <summary>
        /// Creates and returns an environment scope configured as if it
        /// is not running under TeamBuild
        /// </summary>
        public static EnvironmentVariableScope CreateValidNonTeamBuildScope()
        {
            EnvironmentVariableScope scope = new EnvironmentVariableScope();
            scope.SetVariable(TeamBuildSettings.EnvironmentVariables.IsInTeamBuild, "false");

            scope.SetVariable(TeamBuildSettings.EnvironmentVariables.TfsCollectionUri_Legacy, null);
            scope.SetVariable(TeamBuildSettings.EnvironmentVariables.TfsCollectionUri_TFS2015, null);

            scope.SetVariable(TeamBuildSettings.EnvironmentVariables.BuildUri_Legacy, null);
            scope.SetVariable(TeamBuildSettings.EnvironmentVariables.BuildUri_TFS2015, null);

            return scope;
        }
        public void BootSettings_Properties()
        {
            // Check the properties values and that relative paths are turned into absolute paths

            // 0. Setup
            TestLogger logger = new TestLogger();

            using (EnvironmentVariableScope envScope = new EnvironmentVariableScope())
            {
                envScope.SetVariable(BootstrapperSettings.BuildDirectory_Legacy, @"c:\temp");

                // 1. Default value -> relative to download dir
                IBootstrapperSettings settings = new BootstrapperSettings(AnalysisPhase.PreProcessing, null, "http://sq", LoggerVerbosity.Debug, logger);
                AssertExpectedServerUrl("http://sq", settings);
                AssertExpectedPreProcessPath(Path.Combine(@"c:\temp", DownloadFolderRelativePath, BootstrapperSettings.PreProcessorExeName), settings);
                AssertExpectedPostProcessPath(Path.Combine(@"c:\temp", DownloadFolderRelativePath, BootstrapperSettings.PostProcessorExeName), settings);
            }
        }
        public void SonarRunnerHome_MessageLoggedIfAlreadySet()
        {
            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(SonarRunnerWrapper.SonarRunnerHomeVariableName, "some_path");

                // Arrange
                TestLogger testLogger = new TestLogger();
                string exePath = CreateDummarySonarRunnerBatchFile();
                string propertiesFilePath = CreateDummySonarRunnerPropertiesFile();
                AnalysisConfig config = new AnalysisConfig() { SonarRunnerWorkingDirectory = this.TestContext.DeploymentDirectory };

                // Act
                bool success = SonarRunnerWrapper.ExecuteJavaRunner(config, Enumerable.Empty<string>(), testLogger, exePath, propertiesFilePath);

                // Assert
                VerifySuccessfullRun(testLogger, success , this.TestContext.DeploymentDirectory);
            }
        }
        public void SonarRunnerHome_NoMessageIfNotAlreadySet()
        {
            // Arrange
            TestLogger testLogger = new TestLogger();
            string exePath = CreateDummarySonarRunnerBatchFile();
            string propertiesFilePath = CreateDummySonarRunnerPropertiesFile();

            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(SonarRunnerWrapper.SonarRunnerHomeVariableName, null);

                // Act
                bool success = SonarRunnerWrapper.ExecuteJavaRunner(new AnalysisConfig(), Enumerable.Empty<string>(), testLogger, exePath, propertiesFilePath);
                Assert.IsTrue(success, "Expecting execution to succeed");

                // Assert
                testLogger.AssertMessageNotLogged(SonarRunner.Shim.Resources.MSG_SonarRunnerHomeIsSet);
            }
        }
        public void SonarRunnerHome_NoMessageIfNotAlreadySet()
        {
            // Arrange
            TestLogger testLogger = new TestLogger();
            string exePath = CreateDummarySonarRunnerBatchFile();
            string propertiesFilePath = CreateDummySonarRunnerPropertiesFile();

            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(SonarRunnerWrapper.SonarRunnerHomeVariableName, null);
                AnalysisConfig config = new AnalysisConfig() { SonarRunnerWorkingDirectory = this.TestContext.DeploymentDirectory };

                // Act
                bool success = SonarRunnerWrapper.ExecuteJavaRunner(config, Enumerable.Empty<string>(), testLogger, exePath, propertiesFilePath);

                // Assert
                VerifyProcessRunOutcome(testLogger, this.TestContext.DeploymentDirectory, success, true);
                testLogger.AssertMessageNotLogged(SonarRunner.Shim.Resources.MSG_SonarRunnerHomeIsSet);
            }
        }
        public void TBSettings_SkipLegacyCodeCoverage()
        {
            // 0. Setup
            bool result;

            // 1. Env var not set
            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.SkipLegacyCodeCoverage, null);
                result = TeamBuildSettings.SkipLegacyCodeCoverageProcessing;
                Assert.IsFalse(result);
            }

            // 2. Env var set to a non-boolean -> false
            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.SkipLegacyCodeCoverage, "wibble");
                result = TeamBuildSettings.SkipLegacyCodeCoverageProcessing;
                Assert.IsFalse(result);
            }

            // 3. Env var set to false -> false
            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.SkipLegacyCodeCoverage, "false");
                result = TeamBuildSettings.SkipLegacyCodeCoverageProcessing;
                Assert.IsFalse(result);
            }

            // 4. Env var set to true -> true
            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.SkipLegacyCodeCoverage, "TRUE");
                result = TeamBuildSettings.SkipLegacyCodeCoverageProcessing;
                Assert.IsTrue(result);
            }
        }
 private static void CheckExpectedTimeoutReturned(string envValue, int expected)
 {
     using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
     {
         scope.SetVariable(TeamBuildSettings.EnvironmentVariables.LegacyCodeCoverageTimeoutInMs, envValue);
         int result = TeamBuildSettings.LegacyCodeCoverageProcessingTimeout;
         Assert.AreEqual(expected, result, "Unexpected timeout value returned. Environment value: {0}", envValue);
     }
 }
        public void TBSettings_NonLegacyTeamBuild()
        {
            // Arrange
            TestLogger logger = new TestLogger();
            TeamBuildSettings settings;

            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.IsInTeamBuild, "TRUE");
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.BuildUri_TFS2015, "http://builduri");
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.TfsCollectionUri_TFS2015, "http://collectionUri");
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.BuildDirectory_TFS2015, "non-legacy team build");
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.SourcesDirectory_TFS2015, @"c:\agent\_work\1"); ;

                // Act
                settings = TeamBuildSettings.GetSettingsFromEnvironment(logger);
            }

            // Assert
            Assert.IsNotNull(settings, "Failed to create the TeamBuildSettings");
            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);

            // Check the environment properties
            CheckExpectedSettings(
                settings,
                BuildEnvironment.TeamBuild,
                Directory.GetCurrentDirectory(),
                "http://builduri",
                "http://collectionUri",
                "non-legacy team build",
                @"c:\agent\_work\1");
        }
        public void TBSettings_NotTeamBuild()
        {
            // 0. Setup
            TestLogger logger;
            TeamBuildSettings settings;

            // 1. No environment vars set
            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.IsInTeamBuild, null);

                logger = new TestLogger();
                settings = TeamBuildSettings.GetSettingsFromEnvironment(logger);

                // Check the environment properties
                CheckExpectedSettings(
                    settings,
                    BuildEnvironment.NotTeamBuild,
                    Directory.GetCurrentDirectory(),
                    null,
                    null,
                    null,
                    null);
            }

            // 2. Some Team build settings provided, but not marked as in team build
            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.IsInTeamBuild, null);
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.BuildUri_Legacy, "build uri");
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.TfsCollectionUri_Legacy, "collection uri");
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.BuildDirectory_Legacy, "should be ignored");
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.BuildDirectory_TFS2015, "should be ignored");

                logger = new TestLogger();
                settings = TeamBuildSettings.GetSettingsFromEnvironment(logger);

                CheckExpectedSettings(
                    settings,
                    BuildEnvironment.NotTeamBuild,
                    Directory.GetCurrentDirectory(),
                    null,
                    null,
                    null,
                    null);
            }
        }
        public void TBSettings_LegacyTeamBuild()
        {
            // Arrange
            TestLogger logger = new TestLogger();
            TeamBuildSettings settings;

            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.IsInTeamBuild, "TRUE");
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.BuildUri_Legacy, "http://legacybuilduri");
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.TfsCollectionUri_Legacy, "http://legacycollectionUri");
                scope.SetVariable(TeamBuildSettings.EnvironmentVariables.BuildDirectory_Legacy, "legacy build dir");

                // Act
                settings = TeamBuildSettings.GetSettingsFromEnvironment(logger);
            }

            // Assert
            Assert.IsNotNull(settings, "Failed to create the TeamBuildSettings");
            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);

            // Check the environment properties
            CheckExpectedSettings(settings, BuildEnvironment.LegacyTeamBuild, Directory.GetCurrentDirectory(), "http://legacybuilduri", "http://legacycollectionUri", "legacy build dir");
        }
 private static EnvironmentVariableScope InitializeNonTeamBuildEnvironment(string workingDirectory)
 {
     Directory.SetCurrentDirectory(workingDirectory);
     EnvironmentVariableScope scope = new EnvironmentVariableScope();
     scope.SetVariable(BootstrapperSettings.BuildDirectory_Legacy, null);
     scope.SetVariable(BootstrapperSettings.BuildDirectory_TFS2015, null);
     return scope;
 }