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

Improve lispum generation of pastes

Summary: Fixes T8482, or something. I can't actually repro that but I think it should be fixed either here or earlier

Test Plan:
```
$ ./bin/lipsum generate paste
 GENERATORS  Selected generators: Pastes.
 WARNING  This command generates synthetic test data, including user accounts. It is intended for use in development environments so you can test features more easily. There is no easy way to delete this data or undo the effects of this command. If you run it in a production environment, it will pollute your data with large amounts of meaningless garbage that you can not get rid of.

    Are you sure you want to generate piles of garbage? [y/N] y

 LIPSUM  Generating synthetic test objects forever. Use ^C to stop when satisfied.
Generated "Paste": P223 forgotten_memory_disks_backup.java
Generated "Paste": P224 backup_disk_tables_and_administrate_backup_memory_account.java
Generated "Paste": P225 sync_backup_disk_and_undo_memory.php
Generated "Paste": P226 administrate_memory_shard_helper.php
Generated "Paste": P227 cancel_disk_users
Generated "Paste": P228 backups_pro.txt
Generated "Paste": P229 undo_host.txt
Generated "Paste": P230 accelerate_database_accounts.java
Generated "Paste": P231 entomb_accounts.java
Generated "Paste": P232 legendary_legendary_shards_helper.java
Generated "Paste": P233 compact_backup_and_user_and_purge_memory
Generated "Paste": P234 account_script_script_backup_helper_helper.java
Generated "Paste": P235 purge_disk.php
Generated "Paste": P236 forgotten_elder_account.txt
Generated "Paste": P237 ancient_ancient_disks.txt
Generated "Paste": P238 disk_user.php
```

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T8482

Differential Revision: https://secure.phabricator.com/D14883
This commit is contained in:
epriestley 2015-12-25 06:20:25 -08:00
parent e0ad791247
commit 8f81b34ea1
5 changed files with 182 additions and 85 deletions

View file

@ -2687,6 +2687,7 @@ phutil_register_library_map(array(
'PhabricatorPasteEditController' => 'applications/paste/controller/PhabricatorPasteEditController.php',
'PhabricatorPasteEditEngine' => 'applications/paste/editor/PhabricatorPasteEditEngine.php',
'PhabricatorPasteEditor' => 'applications/paste/editor/PhabricatorPasteEditor.php',
'PhabricatorPasteFilenameContextFreeGrammar' => 'applications/paste/lipsum/PhabricatorPasteFilenameContextFreeGrammar.php',
'PhabricatorPasteListController' => 'applications/paste/controller/PhabricatorPasteListController.php',
'PhabricatorPastePastePHIDType' => 'applications/paste/phid/PhabricatorPastePastePHIDType.php',
'PhabricatorPasteQuery' => 'applications/paste/query/PhabricatorPasteQuery.php',
@ -6987,6 +6988,7 @@ phutil_register_library_map(array(
'PhabricatorPasteEditController' => 'PhabricatorPasteController',
'PhabricatorPasteEditEngine' => 'PhabricatorEditEngine',
'PhabricatorPasteEditor' => 'PhabricatorApplicationTransactionEditor',
'PhabricatorPasteFilenameContextFreeGrammar' => 'PhutilContextFreeGrammar',
'PhabricatorPasteListController' => 'PhabricatorPasteController',
'PhabricatorPastePastePHIDType' => 'PhabricatorPHIDType',
'PhabricatorPasteQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',

View file

@ -74,6 +74,22 @@ abstract class PhabricatorTestDataGenerator extends Phobject {
return $sum;
}
protected function newEmptyTransaction() {
throw new PhutilMethodNotImplementedException();
}
protected function newTransaction($type, $value, $metadata = array()) {
$xaction = $this->newEmptyTransaction()
->setTransactionType($type)
->setNewValue($value);
foreach ($metadata as $key => $value) {
$xaction->setMetadataValue($key, $value);
}
return $xaction;
}
@ -89,7 +105,6 @@ abstract class PhabricatorTestDataGenerator extends Phobject {
}
}
public function loadPhabrictorUserPHID() {
return $this->loadOneRandom('PhabricatorUser')->getPHID();
}

View file

@ -0,0 +1,87 @@
<?php
final class PhabricatorPasteFilenameContextFreeGrammar
extends PhutilContextFreeGrammar {
protected function getRules() {
return array(
'start' => array(
'[scripty]',
),
'scripty' => array(
'[thing]',
'[thing]',
'[thing]_[tail]',
'[action]_[thing]',
'[action]_[thing]',
'[action]_[thing]_[tail]',
'[scripty]_and_[scripty]',
),
'tail' => array(
'script',
'helper',
'backup',
'pro',
'[tail]_[tail]',
),
'thing' => array(
'[thingnoun]',
'[thingadjective]_[thingnoun]',
'[thingadjective]_[thingadjective]_[thingnoun]',
),
'thingnoun' => array(
'backup',
'backups',
'database',
'databases',
'table',
'tables',
'memory',
'disk',
'disks',
'user',
'users',
'account',
'accounts',
'shard',
'shards',
'node',
'nodes',
'host',
'hosts',
'account',
'accounts',
),
'thingadjective' => array(
'backup',
'database',
'memory',
'disk',
'user',
'account',
'forgotten',
'lost',
'elder',
'ancient',
'legendary',
),
'action' => array(
'manage',
'update',
'compact',
'quick',
'probe',
'sync',
'undo',
'administrate',
'assess',
'purge',
'cancel',
'entomb',
'accelerate',
'plan',
),
);
}
}

View file

@ -7,92 +7,93 @@ final class PhabricatorPasteTestDataGenerator
return pht('Pastes');
}
// Better Support for this in the future
public $supportedLanguages = array(
'Java' => 'java',
'PHP' => 'php',
);
public function generateObject() {
$author = $this->loadPhabrictorUser();
$authorphid = $author->getPHID();
$language = $this->generateLanguage();
$content = $this->generateContent($language);
$title = $this->generateTitle($language);
$paste_file = PhabricatorFile::newFromFileData(
$content,
array(
'name' => $title,
'mime-type' => 'text/plain; charset=utf-8',
'authorPHID' => $authorphid,
));
$policy = $this->generatePolicy();
$filephid = $paste_file->getPHID();
$parentphid = $this->loadPhabrictorPastePHID();
$paste = PhabricatorPaste::initializeNewPaste($author)
->setParentPHID($parentphid)
->setTitle($title)
->setLanguage($language)
->setViewPolicy($policy)
->setEditPolicy($policy)
->setFilePHID($filephid)
->save();
$author = $this->loadRandomUser();
list($name, $language, $content) = $this->newPasteContent();
$paste = PhabricatorPaste::initializeNewPaste($author);
$xactions = array();
$xactions[] = $this->newTransaction(
PhabricatorPasteTransaction::TYPE_TITLE,
$name);
$xactions[] = $this->newTransaction(
PhabricatorPasteTransaction::TYPE_LANGUAGE,
$language);
$xactions[] = $this->newTransaction(
PhabricatorPasteTransaction::TYPE_CONTENT,
$content);
$editor = id(new PhabricatorPasteEditor())
->setActor($author)
->setContentSource($this->getLipsumContentSource())
->setContinueOnNoEffect(true)
->applyTransactions($paste, $xactions);
return $paste;
}
private function loadPhabrictorPastePHID() {
$random = rand(0, 1);
if ($random == 1) {
$paste = id($this->loadOneRandom('PhabricatorPaste'));
if ($paste) {
return $paste->getPHID();
}
protected function newEmptyTransaction() {
return new PhabricatorPasteTransaction();
}
private function newPasteContent() {
$languages = array(
'txt' => array(),
'php' => array(
'content' => 'PhutilPHPCodeSnippetContextFreeGrammar',
),
'java' => array(
'content' => 'PhutilJavaCodeSnippetContextFreeGrammar',
),
);
$language = array_rand($languages);
$spec = $languages[$language];
$title_generator = idx($spec, 'title');
if (!$title_generator) {
$title_generator = 'PhabricatorPasteFilenameContextFreeGrammar';
}
return null;
}
public function generateTitle($language = null) {
$taskgen = new PhutilLipsumContextFreeGrammar();
// Remove Punctuation
$title = preg_replace('/[^a-zA-Z 0-9]+/', '', $taskgen->generate());
// Capitalize First Letters
$title = ucwords($title);
// Remove Spaces
$title = preg_replace('/\s+/', '', $title);
if ($language == null ||
!in_array($language, array_keys($this->supportedLanguages))) {
return $title.'.txt';
} else {
return $title.'.'.$this->supportedLanguages[$language];
$content_generator = idx($spec, 'content');
if (!$content_generator) {
$content_generator = 'PhutilLipsumContextFreeGrammar';
}
}
public function generateLanguage() {
$supplemented_lang = $this->supportedLanguages;
$supplemented_lang['lipsum'] = 'txt';
return array_rand($supplemented_lang);
}
$title = newv($title_generator, array())
->generate();
public function generateContent($language = null) {
if ($language == null ||
!in_array($language, array_keys($this->supportedLanguages))) {
return id(new PhutilLipsumContextFreeGrammar())
->generateSeveral(rand(30, 40));
} else {
$cfg_class = 'Phutil'.$language.'CodeSnippetContextFreeGrammar';
return newv($cfg_class, array())->generate();
}
}
$content = newv($content_generator, array())
->generateSeveral($this->roll(4, 12, 10));
public function generatePolicy() {
// Make sure 4/5th of all generated Pastes are viewable to all
switch (rand(0, 4)) {
case 0:
return PhabricatorPolicies::POLICY_PUBLIC;
// Usually add the language as a suffix.
if ($this->roll(1, 20) > 2) {
$title = $title.'.'.$language;
}
switch ($this->roll(1, 20)) {
case 1:
return PhabricatorPolicies::POLICY_NOONE;
// On critical miss, set a different, random language.
$highlight_as = array_rand($languages);
break;
case 18:
case 19:
case 20:
// Sometimes set it to the correct language.
$highlight_as = $language;
break;
default:
return PhabricatorPolicies::POLICY_USER;
// Usually leave it as autodetect.
$highlight_as = '';
break;
}
return array($title, $highlight_as, $content);
}
}

View file

@ -51,16 +51,8 @@ final class PhabricatorProjectTestDataGenerator
return $project;
}
private function newTransaction($type, $value, $metadata = array()) {
$xaction = id(new PhabricatorProjectTransaction())
->setTransactionType($type)
->setNewValue($value);
foreach ($metadata as $key => $value) {
$xaction->setMetadataValue($key, $value);
}
return $xaction;
protected function newEmptyTransaction() {
return new PhabricatorProjectTransaction();
}
public function newProjectTitle() {