jenkins-bot has submitted this change and it was merged.
Change subject: Add loadWithArgv() to Maintenance class
......................................................................
Add loadWithArgv() to Maintenance class
Very useful for passing in arguments to test Maintenance scripts.
Also, add a comment clarifying when $orderedOptions is available.
Change-Id: Ib25b3b36816bdf566c427b67646554a31a9fef0f
---
M maintenance/Maintenance.php
M tests/phpunit/maintenance/MaintenanceTest.php
2 files changed, 51 insertions(+), 48 deletions(-)
Approvals:
TTO: Looks good to me, approved
jenkins-bot: Verified
diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php
index 3dd7ee8..e90812d 100644
--- a/maintenance/Maintenance.php
+++ b/maintenance/Maintenance.php
@@ -123,12 +123,13 @@
private $config;
/**
- * Used to read the options in the order
- * they were passed. Useful for option
- * chaining. (Ex. dumpBackup.php)
+ * Used to read the options in the order they were passed.
+ * Useful for option chaining (Ex. dumpBackup.php). It will
+ * be an empty array if the options are passed in through
+ * loadParamsAndArgs( $self, $opts, $args ).
*
* This is an array of arrays where
- * 0 => the option and 1 => parameter value
+ * 0 => the option and 1 => parameter value.
*
* @var array
*/
@@ -654,41 +655,13 @@
}
/**
- * Process command line arguments
- * $mOptions becomes an array with keys set to the option names
- * $mArgs becomes a zero-based array containing the non-option arguments
+ * Load params and arguments from a given array
+ * of command-line arguments
*
- * @param string $self The name of the script, if any
- * @param array $opts An array of options, in form of key=>value
- * @param array $args An array of command line arguments
+ * @since 1.27
+ * @param array $argv
*/
- public function loadParamsAndArgs( $self = null, $opts = null, $args =
null ) {
- # If we were given opts or args, set those and return early
- if ( $self ) {
- $this->mSelf = $self;
- $this->mInputLoaded = true;
- }
- if ( $opts ) {
- $this->mOptions = $opts;
- $this->mInputLoaded = true;
- }
- if ( $args ) {
- $this->mArgs = $args;
- $this->mInputLoaded = true;
- }
-
- # If we've already loaded input (either by user values or from
$argv)
- # skip on loading it again. The array_shift() will corrupt
values if
- # it's run again and again
- if ( $this->mInputLoaded ) {
- $this->loadSpecialVars();
-
- return;
- }
-
- global $argv;
- $this->mSelf = array_shift( $argv );
-
+ public function loadWithArgv( $argv ) {
$options = array();
$args = array();
$this->orderedOptions = array();
@@ -791,6 +764,44 @@
}
/**
+ * Process command line arguments
+ * $mOptions becomes an array with keys set to the option names
+ * $mArgs becomes a zero-based array containing the non-option arguments
+ *
+ * @param string $self The name of the script, if any
+ * @param array $opts An array of options, in form of key=>value
+ * @param array $args An array of command line arguments
+ */
+ public function loadParamsAndArgs( $self = null, $opts = null, $args =
null ) {
+ # If we were given opts or args, set those and return early
+ if ( $self ) {
+ $this->mSelf = $self;
+ $this->mInputLoaded = true;
+ }
+ if ( $opts ) {
+ $this->mOptions = $opts;
+ $this->mInputLoaded = true;
+ }
+ if ( $args ) {
+ $this->mArgs = $args;
+ $this->mInputLoaded = true;
+ }
+
+ # If we've already loaded input (either by user values or from
$argv)
+ # skip on loading it again. The array_shift() will corrupt
values if
+ # it's run again and again
+ if ( $this->mInputLoaded ) {
+ $this->loadSpecialVars();
+
+ return;
+ }
+
+ global $argv;
+ $this->mSelf = $argv[0];
+ $this->loadWithArgv( array_slice( $argv, 1 ) );
+ }
+
+ /**
* Run some validation checks on the params, etc
*/
protected function validateParamsAndArgs() {
diff --git a/tests/phpunit/maintenance/MaintenanceTest.php
b/tests/phpunit/maintenance/MaintenanceTest.php
index 7b84dfa..245a97a 100644
--- a/tests/phpunit/maintenance/MaintenanceTest.php
+++ b/tests/phpunit/maintenance/MaintenanceTest.php
@@ -841,14 +841,10 @@
}
function testParseArgs() {
- global $argv;
- $oldArgv = $argv;
-
- $argv = array( '', '--multi', 'this1', '--multi', 'this2' );
$m2 = new MaintenanceFixup( $this );
// Create an option with an argument allowed to be specified
multiple times
$m2->addOption( 'multi', 'This option does stuff', false, true,
false, true );
- $m2->loadParamsAndArgs();
+ $m2->loadWithArgv( array( '--multi', 'this1', '--multi',
'this2' ) );
$this->assertEquals( array( 'this1', 'this2' ), $m2->getOption(
'multi' ) );
$this->assertEquals( array( array( 'multi', 'this1' ), array(
'multi', 'this2' ) ),
@@ -856,28 +852,24 @@
$m2->simulateShutdown();
- $argv = array( '', '--multi', '--multi' );
$m2 = new MaintenanceFixup( $this );
$m2->addOption( 'multi', 'This option does stuff', false,
false, false, true );
- $m2->loadParamsAndArgs();
+ $m2->loadWithArgv( array( '--multi', '--multi' ) );
$this->assertEquals( array( 1, 1 ), $m2->getOption( 'multi' ) );
$this->assertEquals( array( array( 'multi', 1 ), array(
'multi', 1 ) ), $m2->orderedOptions );
$m2->simulateShutdown();
- $argv = array( '', '--multi=yo' );
$m2 = new MaintenanceFixup( $this );
// Create an option with an argument allowed to be specified
multiple times
$m2->addOption( 'multi', 'This option doesn\'t actually support
multiple occurrences' );
- $m2->loadParamsAndArgs();
+ $m2->loadWithArgv( array( '--multi=yo' ) );
$this->assertEquals( 'yo', $m2->getOption( 'multi' ) );
$this->assertEquals( array( array( 'multi', 'yo' ) ),
$m2->orderedOptions );
$m2->simulateShutdown();
-
- $argv = $oldArgv;
}
}
--
To view, visit https://gerrit.wikimedia.org/r/261707
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ib25b3b36816bdf566c427b67646554a31a9fef0f
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Unicornisaurous <[email protected]>
Gerrit-Reviewer: Parent5446 <[email protected]>
Gerrit-Reviewer: TTO <[email protected]>
Gerrit-Reviewer: Unicornisaurous <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits