public void Exe_PreProc_VersionCheckSucceeds_PreProcSucceeds()
        {
            // Arrange
            BootstrapperTestUtils.EnsureDefaultPropertiesFileDoesNotExist();

            string rootDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            using (InitializeNonTeamBuildEnvironment(rootDir))
            {
                string binDir = CalculateBinDir(rootDir);

                MockBuildAgentUpdater mockUpdater = CreateValidUpdater(binDir, "http://anotherHost");

                mockUpdater.Updating += (sender, args) =>
                {
                    AssertDirectoryExists(args.TargetDir);
                    DummyExeHelper.CreateDummyPreProcessor(args.TargetDir, 0 /* pre-proc succeeds */);
                };

                // Act
                TestLogger logger = CheckExecutionSucceeds(mockUpdater,
                                                           "/d:sonar.host.url=http://anotherHost", "begin");

                // Assert
                mockUpdater.AssertUpdateAttempted();
                mockUpdater.AssertVersionChecked();

                logger.AssertWarningsLogged(0);
                logger.AssertVerbosity(VerbosityCalculator.DefaultLoggingVerbosity);

                string logPath = DummyExeHelper.AssertDummyPreProcLogExists(binDir, this.TestContext);
                DummyExeHelper.AssertExpectedLogContents(logPath, "/d:sonar.host.url=http://anotherHost");
            }
        }
        public void Exe_PreProc_VersionCheckFails()
        {
            // Arrange
            string rootDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            using (InitializeNonTeamBuildEnvironment(rootDir))
            {
                string binDir = CalculateBinDir(rootDir);
                MockBuildAgentUpdater mockUpdater = CreateValidUpdater(binDir, "http://ahost");
                mockUpdater.VersionCheckReturnValue = false;

                // Act
                TestLogger logger = CheckExecutionFails(mockUpdater, "/d:sonar.host.url=http://ahost", "begin");

                // Assert
                mockUpdater.AssertUpdateAttempted();
                mockUpdater.AssertVersionChecked();

                AssertDirectoryExists(binDir);
                DummyExeHelper.AssertDummyPreProcLogDoesNotExist(binDir);
                DummyExeHelper.AssertDummyPostProcLogDoesNotExist(binDir);
                logger.AssertErrorsLogged();
                logger.AssertWarningsLogged(0);
            }
        }
        public void Exe_PreProc_VersionCheckSucceeds_PreProcFails()
        {
            // Arrange
            BootstrapperTestUtils.EnsureDefaultPropertiesFileDoesNotExist();

            string rootDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            using (InitializeNonTeamBuildEnvironment(rootDir))
            {
                string binDir = CalculateBinDir(rootDir);
                MockBuildAgentUpdater mockUpdater = CreateValidUpdater(binDir, "http://host:9");

                mockUpdater.Updating += (sender, args) =>
                {
                    AssertDirectoryExists(args.TargetDir);
                    DummyExeHelper.CreateDummyPreProcessor(args.TargetDir, 1 /* pre-proc fails */);
                };

                // Act
                TestLogger logger = CheckExecutionFails(mockUpdater,
                                                        "begin",
                                                        "/install:true", // this argument should just pass through
                                                        "/d:sonar.verbose=true",
                                                        "/d:sonar.host.url=http://host:9",
                                                        "/d:another.key=will be ignored");

                // Assert
                mockUpdater.AssertUpdateAttempted();
                mockUpdater.AssertVersionChecked();

                logger.AssertWarningsLogged(0);
                logger.AssertVerbosity(LoggerVerbosity.Debug); // sonar.verbose=true was specified

                string logPath = DummyExeHelper.AssertDummyPreProcLogExists(binDir, this.TestContext);
                DummyExeHelper.AssertExpectedLogContents(logPath,
                                                         "/install:true",
                                                         "/d:sonar.verbose=true",
                                                         "/d:sonar.host.url=http://host:9",
                                                         "/d:another.key=will be ignored");
            }
        }
        public void Exe__Version0_9Compatibility()
        {
            // Tests compatibility with the bootstrapper API used in v0.9
            // The pre-processor should be called if any arguments are passed.
            // The post-processor should be called if no arguments are passed.

            // Default settings:
            // There must be a default settings file next to the bootstrapper exe to supply
            // the necessary settings, and the bootstrapper should pass this settings path
            // to the pre-processor (since the pre-process is downloaded to a different
            // directory).

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

            using (InitializeNonTeamBuildEnvironment(rootDir))
            {
                string binDir = CalculateBinDir(rootDir);
                MockBuildAgentUpdater mockUpdater = CreateValidUpdater(binDir, "http://host");

                mockUpdater.Updating += (sender, args) =>
                {
                    Assert.IsTrue(Directory.Exists(args.TargetDir), "Expecting the target directory to have been created");
                    DummyExeHelper.CreateDummyPreProcessor(args.TargetDir, 0 /* post-proc succeeds */);
                    DummyExeHelper.CreateDummyPostProcessor(args.TargetDir, 0 /* post-proc succeeds */);
                };

                // Create a default properties file next to the exe
                AnalysisProperties defaultProperties = new AnalysisProperties();
                defaultProperties.Add(new Property()
                {
                    Id = SonarProperties.HostUrl, Value = "http://host"
                });
                string defaultPropertiesFilePath = CreateDefaultPropertiesFile(defaultProperties);

                // Act
                try
                {
                    // Call the pre-processor
                    TestLogger logger = CheckExecutionSucceeds(mockUpdater, "/v:version", "/n:name", "/k:key");
                    logger.AssertWarningsLogged(1); // Should be warned once about the missing "begin" / "end"
                    logger.AssertSingleWarningExists(ArgumentProcessor.BeginVerb, ArgumentProcessor.EndVerb);

                    mockUpdater.AssertUpdateAttempted();
                    mockUpdater.AssertVersionChecked();

                    string logPath = DummyExeHelper.AssertDummyPreProcLogExists(binDir, this.TestContext);
                    DummyExeHelper.AssertExpectedLogContents(logPath,
                                                             "/v:version",
                                                             "/n:name",
                                                             "/k:key",
                                                             "/s:" + defaultPropertiesFilePath);

                    DummyExeHelper.AssertDummyPostProcLogDoesNotExist(binDir);

                    // Call the post-process (no arguments)
                    logger = CheckExecutionSucceeds(mockUpdater);

                    logPath = DummyExeHelper.AssertDummyPostProcLogExists(binDir, this.TestContext);
                    DummyExeHelper.AssertExpectedLogContents(logPath, null);

                    logger.AssertWarningsLogged(1); // Should be warned once about the missing "begin" / "end"
                    logger.AssertSingleWarningExists(ArgumentProcessor.BeginVerb, ArgumentProcessor.EndVerb);
                }
                finally
                {
                    File.Delete(defaultPropertiesFilePath);
                }
            }
        }