Beispiel #1
0
 protected function checkProject($files, $expected_defects)
 {
     $lint_engine = new XRef_LintEngine_ProjectCheck($this->xref, false);
     $file_provider = new XRef_FileProvider_InMemory($files);
     $report = $lint_engine->getReport($file_provider);
     // 1. check that there were no fatal (parse) errors
     // if any file can't be parsed, the rest of the report is invalid
     foreach ($report as $file_name => $list) {
         foreach ($list as $e) {
             if ($e->severity == XRef::FATAL) {
                 throw new XRef_DummyException("Can't parse file {$file_name}: {$e->message}");
             }
         }
     }
     // 2. count errors
     $count_found = 0;
     foreach ($report as $file_name => $list) {
         $count_found += count($list);
     }
     $count_expected = count($expected_defects);
     if ($count_found != $count_expected) {
         print_r($report);
         $this->fail("Wrong number of errors: found={$count_found}, expected={$count_expected}");
     } else {
         $this->assertTrue($count_found == $count_expected, "Expected number of defects");
     }
     // 3. check every entry individually
     $i = 0;
     foreach ($report as $file_name => $list) {
         foreach ($list as $e) {
             list($expected_file_name, $token_text, $line_number, $severity) = $expected_defects[$i];
             $this->checkFoundProjectDefect($e, $file_name, $expected_file_name, $token_text, $severity, $line_number);
             ++$i;
         }
     }
 }
Beispiel #2
0
 public function testIncrementalProjectCheckOnDeletedFile()
 {
     // assert than there are no errors if both files are modified
     $lint_engine = new XRef_LintEngine_ProjectCheck($this->xref, false);
     $file_provider1 = new XRef_FileProvider_InMemory(array('fileA.php' => '<?php class A { const FOO = 1; } ', 'fileB.php' => '<?php echo A::FOO; '));
     $file_provider2 = new XRef_FileProvider_InMemory(array('fileA.php' => '<?php class A { } ', 'fileB.php' => '<?php echo "hi"; '));
     $report = $lint_engine->getIncrementalReport($file_provider1, $file_provider2, array("fileA.php", "fileB.php"));
     $this->assertTrue(count($report) == 0);
     // and no errors if one file is deleted
     $lint_engine = new XRef_LintEngine_ProjectCheck($this->xref, false);
     $file_provider1 = new XRef_FileProvider_InMemory(array('fileA.php' => '<?php class A { const FOO = 1; } ', 'fileB.php' => '<?php echo A::FOO; '));
     $file_provider2 = new XRef_FileProvider_InMemory(array('fileA.php' => '<?php class A { } '));
     $report = $lint_engine->getIncrementalReport($file_provider1, $file_provider2, array("fileA.php", "fileB.php"));
     $this->assertTrue(count($report) == 0);
 }
Beispiel #3
0
    public function testCI()
    {
        $old_rev = "377d2edc1da549a80b5b44286e7dcaf59cee300a";
        $current_rev = "190fc6a9fddcae0313decbbbce92e4a83bf47ab9";
        XRef::setConfigValue('mail.reply-to', '*****@*****.**');
        XRef::setConfigValue('mail.from', '*****@*****.**');
        XRef::setConfigValue('mail.to', array('*****@*****.**', '{%ae}', '{%an}@xref-lint.net'));
        XRef::setConfigValue('project.name', 'unit-test');
        XRef::setConfigValue('project.source-url', 'https://github.com/gariev/xref/blob/{%revision}/{%fileName}#L{%lineNumber}');
        XRef::setConfigValue('xref.smarty-class', self::SMARTY_CLASS_PATH);
        XRef::setConfigValue('xref.data-dir', 'tmp');
        $scm = $this->xref->getSourceCodeManager();
        $file_provider_old = $scm->getFileProvider($old_rev);
        $file_provider_new = $scm->getFileProvider($current_rev);
        $modified_files = $scm->getListOfModifiedFiles($old_rev, $current_rev);
        $lint_engine = new XRef_LintEngine_ProjectCheck($this->xref, false);
        $errors = $lint_engine->getIncrementalReport($file_provider_old, $file_provider_new, $modified_files);
        list($recipients, $subject, $body, $headers) = $this->xref->getNotificationEmail($errors, 'tests-git', $old_rev, $current_rev);
        //print_r(array($recipients, $subject, $body, $headers));
        // assert that our comparison function works
        $this->assertTrue($this->strSmartSpaces("\t hello,\r \n world  ") == $this->strSmartSpaces("hello, world"));
        $this->assertTrue($this->strSmartSpaces("\t hello,\r \n world  ") != $this->strSmartSpaces("hello , world"));
        $this->assertTrue(count($recipients) == 3);
        $this->assertTrue($recipients[0] == '*****@*****.**');
        $this->assertTrue($recipients[1] == '*****@*****.**');
        $this->assertTrue($recipients[2] == '*****@*****.**');
        $this->assertTrue($this->strSmartSpaces($subject) == "XRef CI unit-test: tests-git/190fc6a");
        $expected_body = <<<END
            <html><body>
            Hi, you've got this e-mail as the author (gariev) of commit 190fc6a to branch tests-git.
            It looks like there are problems in file(s) modified since previous revision 377d2ed:
                <ul>
                    <li>broken.php</li>
                    <ul>
                        <li>
                            <span class='error'>error</span>
                            (<a href="https://github.com/gariev/xref/blob/master/README.md#xr010">xr010</a>): Use of unknown variable (\$error)
                             at <a href="https://github.com/gariev/xref/blob/190fc6a9fddcae0313decbbbce92e4a83bf47ab9/broken.php#L3">line 3</a>
                        </li>
                    </ul>
                </ul>
            <p>If the problems above are real, you can fix them.
            If these warnings are from code merged from other branch and not result of merge conflict, you can ignore them or find the author of the original commit.
            If the report is wrong, you can help <a href='mailto:gariev@hotmail.com?subject=xref'>improve XRef CI</a> itself and/or ignore this e-mail.
            </p>
            <p><small>
              Generated by XRef CI version <<VERSION>>. About XRef:
                <a href="https://github.com/gariev/xref/blob/master/README.md">documentation</a>,
                <a href="https://github.com/gariev/xref/">source code</a>,
                <a href="http://xref-lint.net/bin/xref-lint.php">online tool</a>.
            </small></p>
            </body></html>
END;
        $expected_body = str_replace('<<VERSION>>', XRef::version(), $expected_body);
        $this->assertTrue($this->strSmartSpaces($body) == $this->strSmartSpaces($expected_body));
        $expected_headers = <<<END
            MIME-Version: 1.0
            Content-type: text/html
            Reply-to: no-reply@xref-lint.net
            From: ci-server@xref-lint.net
END;
        $this->assertTrue($this->strSmartSpaces($headers) == $this->strSmartSpaces($expected_headers));
    }
Beispiel #4
0
 public function init()
 {
     $cwd = getcwd();
     // create data dir
     if (!file_exists(".xref")) {
         echo "Creating dir .xref\n";
         if (!mkdir(".xref")) {
             throw new Exception("Can't create dir .xref");
         }
     }
     $config_filename = ".xref/xref.ini";
     if (file_exists($config_filename)) {
         echo "Config file {$config_filename} exists; won't overwrite\n";
     } else {
         echo "Creating config file {$config_filename}\n";
         // create config file
         $fh = fopen($config_filename, "w");
         if (!$fh) {
             throw new Exception("Can't create file {$config_filename}");
         }
         $config = array();
         $config[] = "[project]";
         $config[] = array("name", basename($cwd));
         $config[] = array("source-code-dir[]", realpath($cwd));
         $config[] = array(";source-url", 'https://github.com/<author>/<project>/blob/{%revision}/{%fileName}#L{%lineNumber}');
         $config[] = null;
         $config[] = "[xref]";
         $config[] = array("data-dir", realpath("{$cwd}/.xref"));
         $config[] = array("project-check", "true");
         $config[] = array(";smarty-class", "/path/to/Smarty.class.php");
         $config[] = array(";script-url", "http://xref.your.domain.com/bin");
         $config[] = null;
         $config[] = "[lint]";
         $config[] = array("ignore-missing-class[]", 'PHPUnit_Framework_TestCase');
         $config[] = array(";ignore-error[]", 'xr052');
         $config[] = null;
         $config[] = "[doc]";
         $config[] = array(";output-dir", "/path/for/generated/doc");
         $config[] = null;
         if (file_exists(".git")) {
             // TODO: need a better way to check that this is a git project
             // http://stackoverflow.com/questions/957928
             // git rev-parse --show-toplevel
             $config[] = "[git]";
             $config[] = array("repository-dir", realpath($cwd));
             $config[] = null;
             $config[] = "[ci]";
             $config[] = array("incremental", "true");
             $config[] = array("update-repository", "true");
             $config[] = null;
             $config[] = "[mail]";
             $config[] = array("from", "XRef Continuous Integration");
             $config[] = array(";reply-to", "*****@*****.**");
             $config[] = array(";to[]", "{%ae}");
             $config[] = array(";to[]", "*****@*****.**");
             $config[] = null;
         }
         foreach ($config as $line) {
             $l = null;
             if (!$line) {
                 $l = "\n";
             } elseif (!is_array($line)) {
                 $l = "{$line}\n";
             } else {
                 list($k, $v) = $line;
                 if (preg_match('#^\\w+$#', $v)) {
                     $l = sprintf("%-24s= %s\n", $k, $v);
                 } else {
                     $v = preg_replace('#\\\\#', '\\\\', $v);
                     $l = sprintf("%-24s= \"%s\"\n", $k, $v);
                 }
             }
             fwrite($fh, $l);
         }
         fclose($fh);
         // re-read config values from the file
         self::getConfig(true);
     }
     // index files for the first time
     $this->loadPluginGroup('lint');
     $file_provider = new XRef_FileProvider_FileSystem($cwd);
     $lint_engine = new XRef_LintEngine_ProjectCheck($this);
     $lint_engine->setShowProgressBar(true);
     $lint_engine->setRewriteCache(true);
     // re-index files and refill cache
     echo "Indexing files\n";
     $lint_engine->getReport($file_provider);
     echo "\nDone.\n";
     // done
 }