1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-13 02:12:41 +01:00

Make bin/lipsum generate hanldle generator keys and arguments more clearly

Summary:
Ref T12319. Currently, `bin/lipsum` uses substring matches against human-readable text to chose which objects to generate.

Instead:

  - Use separate selector keys which are guaranteed to be unique.
  - When a match is exact, select only that generator.
  - When a match is ambiguous, fail and warn the user.

Test Plan: Generated several types of objects, tried to generate ambiguous objects like "e".

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T12319

Differential Revision: https://secure.phabricator.com/D17420
This commit is contained in:
epriestley 2017-02-27 04:16:04 -08:00
parent 1b2c047ce0
commit 99bcf5f112
9 changed files with 70 additions and 18 deletions

View file

@ -3,6 +3,8 @@
final class PhabricatorDifferentialRevisionTestDataGenerator final class PhabricatorDifferentialRevisionTestDataGenerator
extends PhabricatorTestDataGenerator { extends PhabricatorTestDataGenerator {
const GENERATORKEY = 'revisions';
public function getGeneratorName() { public function getGeneratorName() {
return pht('Differential Revisions'); return pht('Differential Revisions');
} }

View file

@ -3,6 +3,8 @@
final class PhabricatorFileTestDataGenerator final class PhabricatorFileTestDataGenerator
extends PhabricatorTestDataGenerator { extends PhabricatorTestDataGenerator {
const GENERATORKEY = 'files';
public function getGeneratorName() { public function getGeneratorName() {
return pht('Files'); return pht('Files');
} }

View file

@ -16,6 +16,10 @@ abstract class PhabricatorTestDataGenerator extends Phobject {
return $this->viewer; return $this->viewer;
} }
final public function getGeneratorKey() {
return $this->getPhobjectClassConstant('GENERATORKEY', 64);
}
protected function loadRandomPHID($table) { protected function loadRandomPHID($table) {
$conn_r = $table->establishConnection('r'); $conn_r = $table->establishConnection('r');

View file

@ -30,14 +30,30 @@ final class PhabricatorLipsumGenerateWorkflow
$all_generators = id(new PhutilClassMapQuery()) $all_generators = id(new PhutilClassMapQuery())
->setAncestorClass('PhabricatorTestDataGenerator') ->setAncestorClass('PhabricatorTestDataGenerator')
->setUniqueMethod('getGeneratorKey')
->execute(); ->execute();
$argv = $args->getArg('args'); $argv = $args->getArg('args');
$all = 'all'; $all = 'all';
if (isset($all_generators[$all])) {
throw new Exception(
pht(
'A lipsum generator is registered with key "%s". This key is '.
'reserved.',
$all));
}
if (!$argv) { if (!$argv) {
$names = mpull($all_generators, 'getGeneratorName'); ksort($all_generators);
sort($names);
$names = array();
foreach ($all_generators as $generator) {
$names[] = tsprintf(
'%s (%s)',
$generator->getGeneratorKey(),
$generator->getGeneratorName());
}
$list = id(new PhutilConsoleList()) $list = id(new PhutilConsoleList())
->setWrap(false) ->setWrap(false)
@ -59,31 +75,49 @@ final class PhabricatorLipsumGenerateWorkflow
foreach ($argv as $arg_original) { foreach ($argv as $arg_original) {
$arg = phutil_utf8_strtolower($arg_original); $arg = phutil_utf8_strtolower($arg_original);
$match = false; if ($arg == 'all') {
foreach ($all_generators as $generator) { $matches = $all_generators;
$name = phutil_utf8_strtolower($generator->getGeneratorName()); } else {
$matches = array();
foreach ($all_generators as $generator) {
$name = phutil_utf8_strtolower($generator->getGeneratorKey());
if ($arg == $all) { // If there's an exact match, select just that generator.
$generators[] = $generator; if ($arg == $name) {
$match = true; $matches = array($generator);
break; break;
}
// If there's a partial match, match that generator but continue.
if (strpos($name, $arg) !== false) {
$matches[] = $generator;
}
} }
if (strpos($name, $arg) !== false) { if (!$matches) {
$generators[] = $generator; throw new PhutilArgumentUsageException(
$match = true; pht(
break; 'Argument "%s" does not match the name of any generators.',
$arg_original));
}
if (count($matches) > 1) {
throw new PhutilArgumentUsageException(
pht(
'Argument "%s" is ambiguous, and matches multiple '.
'generators: %s.',
$arg_original,
implode(', ', mpull($matches, 'getGeneratorName'))));
} }
} }
if (!$match) { foreach ($matches as $match) {
throw new PhutilArgumentUsageException( $generators[] = $match;
pht(
'Argument "%s" does not match the name of any generators.',
$arg_original));
} }
} }
$generators = mpull($generators, null, 'getGeneratorKey');
echo tsprintf( echo tsprintf(
"**<bg:blue> %s </bg>** %s\n", "**<bg:blue> %s </bg>** %s\n",
pht('GENERATORS'), pht('GENERATORS'),

View file

@ -3,6 +3,8 @@
final class PhabricatorManiphestTaskTestDataGenerator final class PhabricatorManiphestTaskTestDataGenerator
extends PhabricatorTestDataGenerator { extends PhabricatorTestDataGenerator {
const GENERATORKEY = 'tasks';
public function getGeneratorName() { public function getGeneratorName() {
return pht('Maniphest Tasks'); return pht('Maniphest Tasks');
} }

View file

@ -3,6 +3,8 @@
final class PhabricatorPasteTestDataGenerator final class PhabricatorPasteTestDataGenerator
extends PhabricatorTestDataGenerator { extends PhabricatorTestDataGenerator {
const GENERATORKEY = 'pastes';
public function getGeneratorName() { public function getGeneratorName() {
return pht('Pastes'); return pht('Pastes');
} }

View file

@ -3,6 +3,8 @@
final class PhabricatorPeopleTestDataGenerator final class PhabricatorPeopleTestDataGenerator
extends PhabricatorTestDataGenerator { extends PhabricatorTestDataGenerator {
const GENERATORKEY = 'users';
public function getGeneratorName() { public function getGeneratorName() {
return pht('User Accounts'); return pht('User Accounts');
} }

View file

@ -3,6 +3,8 @@
final class PhabricatorPholioMockTestDataGenerator final class PhabricatorPholioMockTestDataGenerator
extends PhabricatorTestDataGenerator { extends PhabricatorTestDataGenerator {
const GENERATORKEY = 'mocks';
public function getGeneratorName() { public function getGeneratorName() {
return pht('Pholio Mocks'); return pht('Pholio Mocks');
} }

View file

@ -3,6 +3,8 @@
final class PhabricatorProjectTestDataGenerator final class PhabricatorProjectTestDataGenerator
extends PhabricatorTestDataGenerator { extends PhabricatorTestDataGenerator {
const GENERATORKEY = 'projects';
public function getGeneratorName() { public function getGeneratorName() {
return pht('Projects'); return pht('Projects');
} }