com phpruntests: code to use an external memory checker (valgrind) framework to add others: src/configuration/rtCommandLineOptions.php
src/configuration/rtExternalTool.php src/configuration/rtRuntestsConfiguration.php src/configuration/tools/rtValgrind.php src/rtClassMap.php
src/testcase/rtTestConfiguration.php src/testcase/sections/executablesections/rtFileSection.php tests/configuration/rtExternalToolTest.php
| From: | David Soria Parra | Date: | Tue, 28 Jul 2009 20:48:15 +0000 |
| Subject: | com phpruntests: code to use an external memory checker (valgrind) framework to add others: src/configuration/rtCommandLineOptions.php src/configuration/rtExternalTool.php src/configuration/rtRuntestsConfiguration.php src/configuration/tools/rtValgrind.php src/rtClassMap.php src/testcase/rtTestConfiguration.php src/testcase/sections/executablesections/rtFileSection.php tests/configuration/rtExternalToolTest.php |
||
| Groups: | php.qa | ||
| Request: | Send a blank email to php-qa+get-66309@lists.php.net to get a copy of this message | ||
Commit: b13441112ddb293a957b9223f3d3b2e00b9c5deb
Author: Zoe Slattery <zoe@php.net> Tue, 28 Jul 2009 20:48:15 +0000
Parents: b6e7604a5d5eb4ef50355fb025a38cc846d858be
Branches: master
Link: http://git.php.net/?p=phpruntests.git;a=commitdiff;h=b13441112ddb293a957b9223f3d3b2e00b9c5deb
Log:
code to use an external memory checker (valgrind)
framework to add others
Changed paths:
M src/configuration/rtCommandLineOptions.php
A src/configuration/rtExternalTool.php
M src/configuration/rtRuntestsConfiguration.php
A src/configuration/tools/rtValgrind.php
M src/rtClassMap.php
M src/testcase/rtTestConfiguration.php
M src/testcase/sections/executablesections/rtFileSection.php
A tests/configuration/rtExternalToolTest.php
diff --git a/src/configuration/rtCommandLineOptions.php b/src/configuration/rtCommandLineOptions.php index 081cf39..8521aa6 100644 --- a/src/configuration/rtCommandLineOptions.php +++ b/src/configuration/rtCommandLineOptions.php @@ -74,6 +74,7 @@ class rtCommandLineOptions 'temp-source', 'temp-target', 'set-timeout', + 'mopts', ); /** diff --git a/src/configuration/rtExternalTool.php b/src/configuration/rtExternalTool.php new file mode 100644 index 0000000..06019e5 --- /dev/null +++ b/src/configuration/rtExternalTool.php @@ -0,0 +1,45 @@ +<?php +/** + * rtExternalTool + * + * Class to handle using an external tool (default is valgrind) + * + * @category Testing + * @package RUNTESTS + * @author Zoe Slattery <zoe@php.net> + * @author Stefan Priebsch <spriebsch@php.net> + * @copyright 2009 The PHP Group + * @license http://www.php.net/license/3_01.txt PHP License 3.01 + * @link http://qa.php.net/ + * + */ + +class rtExternalTool +{ + protected $command; + protected $options; + protected $version; + + public static function getInstance($configuration) + { + if($configuration->hasCommandLineOption('m')) { + return new rtValgrind(); + } else { + $name = 'rt' . $configuration->getCommandLineOption('mtool'); + return new $name(); + } + } + + public function init(rtRuntestsConfiguration $configuration) { + $this->setVersion(); + $this->setCommand($configuration); + $this->setOptions($configuration); + } + + public function getCommand() { + return $this->command; + } + + +} +?> \ No newline at end of file diff --git a/src/configuration/rtRuntestsConfiguration.php b/src/configuration/rtRuntestsConfiguration.php index 1a5e137..8c8f2f5 100644 --- a/src/configuration/rtRuntestsConfiguration.php +++ b/src/configuration/rtRuntestsConfiguration.php @@ -22,6 +22,8 @@ abstract class rtRuntestsConfiguration private $settings; private $environmentVariables; private $commandLine; + + private $externalTool = null; private $settingNames = array ( @@ -67,6 +69,15 @@ abstract class rtRuntestsConfiguration //extend test command line using TEST_PHP_ARGS $options = new rtAddToCommandLine(); $options->parseAdditionalOptions($this->commandLine, $this->environmentVariables); + + //if there is an external tool - configure it + + if($this->commandLine->hasOption('m') || $this->commandLine->hasOption('mtool')) { + $this->externalTool = rtExternalTool::getInstance($this); + $this->externalTool->checkAvailable($this); + $this->externalTool->init($this); + + } //set configuration foreach ($this->settingNames as $name => $className) { @@ -144,5 +155,17 @@ abstract class rtRuntestsConfiguration public function getUserEnvironment() { $this->environmentVariables->getUserSuppliedVariables(); } + + public function hasExternalTool() { + if($this->externalTool != null) { + return true; + } + return false; + } + + public function getExternalToolCommand() { + return $this->externalTool->getCommand(); + } + } ?> diff --git a/src/configuration/tools/rtValgrind.php b/src/configuration/tools/rtValgrind.php new file mode 100644 index 0000000..f2c5f40 --- /dev/null +++ b/src/configuration/tools/rtValgrind.php @@ -0,0 +1,52 @@ +<?php +/** + * rtvalgrind + * + * Class to handle using an external tool (default is valgrind) + * + * @category Testing + * @package RUNTESTS + * @author Zoe Slattery <zoe@php.net> + * @author Stefan Priebsch <spriebsch@php.net> + * @copyright 2009 The PHP Group + * @license http://www.php.net/license/3_01.txt PHP License 3.01 + * @link http://qa.php.net/ + * + */ + + +class rtValgrind extends rtExternalTool +{ + public function checkAvailable($configuration) + { + $p = new rtIsValgrindAvailable(); + if(!$p->check($configuration)) { + throw new rtException($p->getMessage()); + } + } + + + public function setVersion() { + $phpRunner = new rtPhpRunner('valgrind --version'); + $valgrindheader = $phpRunner->runPHP(); + $this->version = preg_replace("/valgrind-([0-9])\.([0-9])\.([0-9]+)([.-]\w+)?(\s+)/", '$1$2$3', $valgrindheader, 1, $replace_count); + } + + public function setCommand() { + $this->command = "valgrind -q --tool=memcheck --trace-children=yes"; + } + + public function setOptions($configuration) { + $options = ""; + if($configuration->hasCommandLineOption('mopts')) { + $options = preg_replace('/\"/', '', $configuration->getCommandLineOption('mopts')); + } + + if($this->version >= 330) { + $this->command .= " " . $options . " --log-file="; + } else { + $this->command .= " " . $options . " --log-file-exactly="; + } + } +} +?> \ No newline at end of file diff --git a/src/rtClassMap.php b/src/rtClassMap.php index 523c8f7..80674f7 100644 --- a/src/rtClassMap.php +++ b/src/rtClassMap.php @@ -23,6 +23,7 @@ $rtClassMap = array( 'rtAddToCommandLine' => 'configuration/rtAddToCommandLine.php', 'rtCommandLineOptions' => 'configuration/rtCommandLineOptions.php', 'rtEnvironmentVariables' => 'configuration/rtEnvironmentVariables.php', + 'rtExternalTool' => 'configuration/rtExternalTool.php', 'rtIniAsCommandLineArgs' => 'configuration/rtIniAsCommandLineArgs.php', 'rtPreCondition' => 'configuration/rtPreCondition.php', 'rtPreConditionList' => 'configuration/rtPreConditionList.php', @@ -36,6 +37,7 @@ $rtClassMap = array( 'rtTestDirectorySetting' => 'configuration/settings/rtTestDirectorySetting.php', 'rtTestFileSetting' => 'configuration/settings/rtTestFileSetting.php', 'rtWorkingDirectorySetting' => 'configuration/settings/rtWorkingDirectorySetting.php', + 'rtValgrind' => 'configuration/tools/rtValgrind.php', 'rtUnixConfiguration' => 'configuration/unix/rtUnixConfiguration.php', 'rtUnixEnvironmentVariables' => 'configuration/unix/rtUnixEnvironmentVariables.php', 'rtUnixPreConditionList' => 'configuration/unix/rtUnixPreConditionList.php', diff --git a/src/testcase/rtTestConfiguration.php b/src/testcase/rtTestConfiguration.php index 1459ae0..dbd1f37 100644 --- a/src/testcase/rtTestConfiguration.php +++ b/src/testcase/rtTestConfiguration.php @@ -109,7 +109,7 @@ class rtTestConfiguration } private function setPhpExecutable($runConfiguration, $sectionHeadings) - { + { if ($this->cgiTest) { if($runConfiguration->getSetting('PhpCgiExecutable') != null) { $this->phpExecutable = $runConfiguration->getSetting('PhpCgiExecutable'). " -C"; diff --git a/src/testcase/sections/executablesections/rtFileSection.php b/src/testcase/sections/executablesections/rtFileSection.php index 7061e17..1fa205b 100644 --- a/src/testcase/sections/executablesections/rtFileSection.php +++ b/src/testcase/sections/executablesections/rtFileSection.php @@ -16,10 +16,12 @@ class rtFileSection extends rtExecutableSection { private $twoBlankLines = '\r?\n\r?\n'; private $headers; + private $memFileName; public function setExecutableFileName($testName) { - $this->fileName = $testName.".php"; + $this->fileName = $testName . ".php"; + $this->memFileName = $testName . ".mem"; } protected function init() { @@ -27,8 +29,16 @@ class rtFileSection extends rtExecutableSection public function run(rtPhpTest $testCase, rtRuntestsConfiguration $runConfiguration) { + $testStatus = $testCase->getStatus(); $this->writeExecutableFile(); + + $commandPrefix = ""; + if($runConfiguration->hasExternalTool()) { + $commandPrefix = $runConfiguration->getExternalToolCommand(); + //This assumes that the external tool write its output to a *.mem file + $commandPrefix .= $this->memFileName; + } $phpExecutable = $testCase->testConfiguration->getPhpExecutable(); @@ -40,12 +50,12 @@ class rtFileSection extends rtExecutableSection } - $phpCommand = $phpExecutable; + $phpCommand = $commandPrefix . " " . $phpExecutable; $phpCommand .= ' '. $testCase->testConfiguration->getPhpCommandLineArguments(); $phpCommand .= ' -f '.$this->fileName; $phpCommand .= ' '.$testCase->testConfiguration->getTestCommandLineArguments(); $phpCommand .= ' 2>&1 '.$testCase->testConfiguration->getInputFileString(); - + $PhpRunner = new rtPhpRunner($phpCommand, $testCase->testConfiguration->getEnvironmentVariables(), diff --git a/tests/configuration/rtExternalToolTest.php b/tests/configuration/rtExternalToolTest.php new file mode 100644 index 0000000..b840156 --- /dev/null +++ b/tests/configuration/rtExternalToolTest.php @@ -0,0 +1,38 @@ +<?php +require_once 'PHPUnit/Framework.php'; +require_once dirname(__FILE__) . '../../../src/rtAutoload.php'; + +class rtExternalToolTest extends PHPUnit_Framework_TestCase +{ + + + public function testCreate() + { + + $config = rtRuntestsConfiguration::getInstance(array('run-tests.php', '-m', 'test.phpt')); + $this->externalTool = rtExternalTool::getInstance($config); + $this->externalTool->checkAvailable($config); + $this->externalTool->init($config); + + $string = substr($this->externalTool->getCommand(), 0, 48); + + $this->assertEquals('valgrind -q --tool=memcheck --trace-children=yes', $string); + + } + + public function testCreate2() + { + + $config = rtRuntestsConfiguration::getInstance(array('run-tests.php', '-m', '--mopts', '"blah blah"', 'test.phpt')); + $this->externalTool = rtExternalTool::getInstance($config); + $this->externalTool->checkAvailable($config); + $this->externalTool->init($config); + + $string = substr($this->externalTool->getCommand(), 0, 58); + $this->assertEquals('valgrind -q --tool=memcheck --trace-children=yes blah blah', $string); + + } + +} +?> +
diff --git a/src/configuration/rtCommandLineOptions.php b/src/configuration/rtCommandLineOptions.php index 081cf39..8521aa6 100644 --- a/src/configuration/rtCommandLineOptions.php +++ b/src/configuration/rtCommandLineOptions.php @@ -74,6 +74,7 @@ class rtCommandLineOptions 'temp-source', 'temp-target', 'set-timeout', + 'mopts', ); /** diff --git a/src/configuration/rtExternalTool.php b/src/configuration/rtExternalTool.php new file mode 100644 index 0000000..06019e5 --- /dev/null +++ b/src/configuration/rtExternalTool.php @@ -0,0 +1,45 @@ +<?php +/** + * rtExternalTool + * + * Class to handle using an external tool (default is valgrind) + * + * @category Testing + * @package RUNTESTS + * @author Zoe Slattery <zoe@php.net> + * @author Stefan Priebsch <spriebsch@php.net> + * @copyright 2009 The PHP Group + * @license http://www.php.net/license/3_01.txt PHP License 3.01 + * @link http://qa.php.net/ + * + */ + +class rtExternalTool +{ + protected $command; + protected $options; + protected $version; + + public static function getInstance($configuration) + { + if($configuration->hasCommandLineOption('m')) { + return new rtValgrind(); + } else { + $name = 'rt' . $configuration->getCommandLineOption('mtool'); + return new $name(); + } + } + + public function init(rtRuntestsConfiguration $configuration) { + $this->setVersion(); + $this->setCommand($configuration); + $this->setOptions($configuration); + } + + public function getCommand() { + return $this->command; + } + + +} +?> \ No newline at end of file diff --git a/src/configuration/rtRuntestsConfiguration.php b/src/configuration/rtRuntestsConfiguration.php index 1a5e137..8c8f2f5 100644 --- a/src/configuration/rtRuntestsConfiguration.php +++ b/src/configuration/rtRuntestsConfiguration.php @@ -22,6 +22,8 @@ abstract class rtRuntestsConfiguration private $settings; private $environmentVariables; private $commandLine; + + private $externalTool = null; private $settingNames = array ( @@ -67,6 +69,15 @@ abstract class rtRuntestsConfiguration //extend test command line using TEST_PHP_ARGS $options = new rtAddToCommandLine(); $options->parseAdditionalOptions($this->commandLine, $this->environmentVariables); + + //if there is an external tool - configure it + + if($this->commandLine->hasOption('m') || $this->commandLine->hasOption('mtool')) { + $this->externalTool = rtExternalTool::getInstance($this); + $this->externalTool->checkAvailable($this); + $this->externalTool->init($this); + + } //set configuration foreach ($this->settingNames as $name => $className) { @@ -144,5 +155,17 @@ abstract class rtRuntestsConfiguration public function getUserEnvironment() { $this->environmentVariables->getUserSuppliedVariables(); } + + public function hasExternalTool() { + if($this->externalTool != null) { + return true; + } + return false; + } + + public function getExternalToolCommand() { + return $this->externalTool->getCommand(); + } + } ?> diff --git a/src/configuration/tools/rtValgrind.php b/src/configuration/tools/rtValgrind.php new file mode 100644 index 0000000..f2c5f40 --- /dev/null +++ b/src/configuration/tools/rtValgrind.php @@ -0,0 +1,52 @@ +<?php +/** + * rtvalgrind + * + * Class to handle using an external tool (default is valgrind) + * + * @category Testing + * @package RUNTESTS + * @author Zoe Slattery <zoe@php.net> + * @author Stefan Priebsch <spriebsch@php.net> + * @copyright 2009 The PHP Group + * @license http://www.php.net/license/3_01.txt PHP License 3.01 + * @link http://qa.php.net/ + * + */ + + +class rtValgrind extends rtExternalTool +{ + public function checkAvailable($configuration) + { + $p = new rtIsValgrindAvailable(); + if(!$p->check($configuration)) { + throw new rtException($p->getMessage()); + } + } + + + public function setVersion() { + $phpRunner = new rtPhpRunner('valgrind --version'); + $valgrindheader = $phpRunner->runPHP(); + $this->version = preg_replace("/valgrind-([0-9])\.([0-9])\.([0-9]+)([.-]\w+)?(\s+)/", '$1$2$3', $valgrindheader, 1, $replace_count); + } + + public function setCommand() { + $this->command = "valgrind -q --tool=memcheck --trace-children=yes"; + } + + public function setOptions($configuration) { + $options = ""; + if($configuration->hasCommandLineOption('mopts')) { + $options = preg_replace('/\"/', '', $configuration->getCommandLineOption('mopts')); + } + + if($this->version >= 330) { + $this->command .= " " . $options . " --log-file="; + } else { + $this->command .= " " . $options . " --log-file-exactly="; + } + } +} +?> \ No newline at end of file diff --git a/src/rtClassMap.php b/src/rtClassMap.php index 523c8f7..80674f7 100644 --- a/src/rtClassMap.php +++ b/src/rtClassMap.php @@ -23,6 +23,7 @@ $rtClassMap = array( 'rtAddToCommandLine' => 'configuration/rtAddToCommandLine.php', 'rtCommandLineOptions' => 'configuration/rtCommandLineOptions.php', 'rtEnvironmentVariables' => 'configuration/rtEnvironmentVariables.php', + 'rtExternalTool' => 'configuration/rtExternalTool.php', 'rtIniAsCommandLineArgs' => 'configuration/rtIniAsCommandLineArgs.php', 'rtPreCondition' => 'configuration/rtPreCondition.php', 'rtPreConditionList' => 'configuration/rtPreConditionList.php', @@ -36,6 +37,7 @@ $rtClassMap = array( 'rtTestDirectorySetting' => 'configuration/settings/rtTestDirectorySetting.php', 'rtTestFileSetting' => 'configuration/settings/rtTestFileSetting.php', 'rtWorkingDirectorySetting' => 'configuration/settings/rtWorkingDirectorySetting.php', + 'rtValgrind' => 'configuration/tools/rtValgrind.php', 'rtUnixConfiguration' => 'configuration/unix/rtUnixConfiguration.php', 'rtUnixEnvironmentVariables' => 'configuration/unix/rtUnixEnvironmentVariables.php', 'rtUnixPreConditionList' => 'configuration/unix/rtUnixPreConditionList.php', diff --git a/src/testcase/rtTestConfiguration.php b/src/testcase/rtTestConfiguration.php index 1459ae0..dbd1f37 100644 --- a/src/testcase/rtTestConfiguration.php +++ b/src/testcase/rtTestConfiguration.php @@ -109,7 +109,7 @@ class rtTestConfiguration } private function setPhpExecutable($runConfiguration, $sectionHeadings) - { + { if ($this->cgiTest) { if($runConfiguration->getSetting('PhpCgiExecutable') != null) { $this->phpExecutable = $runConfiguration->getSetting('PhpCgiExecutable'). " -C"; diff --git a/src/testcase/sections/executablesections/rtFileSection.php b/src/testcase/sections/executablesections/rtFileSection.php index 7061e17..1fa205b 100644 --- a/src/testcase/sections/executablesections/rtFileSection.php +++ b/src/testcase/sections/executablesections/rtFileSection.php @@ -16,10 +16,12 @@ class rtFileSection extends rtExecutableSection { private $twoBlankLines = '\r?\n\r?\n'; private $headers; + private $memFileName; public function setExecutableFileName($testName) { - $this->fileName = $testName.".php"; + $this->fileName = $testName . ".php"; + $this->memFileName = $testName . ".mem"; } protected function init() { @@ -27,8 +29,16 @@ class rtFileSection extends rtExecutableSection public function run(rtPhpTest $testCase, rtRuntestsConfiguration $runConfiguration) { + $testStatus = $testCase->getStatus(); $this->writeExecutableFile(); + + $commandPrefix = ""; + if($runConfiguration->hasExternalTool()) { + $commandPrefix = $runConfiguration->getExternalToolCommand(); + //This assumes that the external tool write its output to a *.mem file + $commandPrefix .= $this->memFileName; + } $phpExecutable = $testCase->testConfiguration->getPhpExecutable(); @@ -40,12 +50,12 @@ class rtFileSection extends rtExecutableSection } - $phpCommand = $phpExecutable; + $phpCommand = $commandPrefix . " " . $phpExecutable; $phpCommand .= ' '. $testCase->testConfiguration->getPhpCommandLineArguments(); $phpCommand .= ' -f '.$this->fileName; $phpCommand .= ' '.$testCase->testConfiguration->getTestCommandLineArguments(); $phpCommand .= ' 2>&1 '.$testCase->testConfiguration->getInputFileString(); - + $PhpRunner = new rtPhpRunner($phpCommand, $testCase->testConfiguration->getEnvironmentVariables(), diff --git a/tests/configuration/rtExternalToolTest.php b/tests/configuration/rtExternalToolTest.php new file mode 100644 index 0000000..b840156 --- /dev/null +++ b/tests/configuration/rtExternalToolTest.php @@ -0,0 +1,38 @@ +<?php +require_once 'PHPUnit/Framework.php'; +require_once dirname(__FILE__) . '../../../src/rtAutoload.php'; + +class rtExternalToolTest extends PHPUnit_Framework_TestCase +{ + + + public function testCreate() + { + + $config = rtRuntestsConfiguration::getInstance(array('run-tests.php', '-m', 'test.phpt')); + $this->externalTool = rtExternalTool::getInstance($config); + $this->externalTool->checkAvailable($config); + $this->externalTool->init($config); + + $string = substr($this->externalTool->getCommand(), 0, 48); + + $this->assertEquals('valgrind -q --tool=memcheck --trace-children=yes', $string); + + } + + public function testCreate2() + { + + $config = rtRuntestsConfiguration::getInstance(array('run-tests.php', '-m', '--mopts', '"blah blah"', 'test.phpt')); + $this->externalTool = rtExternalTool::getInstance($config); + $this->externalTool->checkAvailable($config); + $this->externalTool->init($config); + + $string = substr($this->externalTool->getCommand(), 0, 58); + $this->assertEquals('valgrind -q --tool=memcheck --trace-children=yes blah blah', $string); + + } + +} +?> +