Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Mercurial tests and add tag validation #102

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Liip/RMT/Action/BaseAction.php
Original file line number Diff line number Diff line change
@@ -51,6 +51,20 @@ public function getInformationRequests()
return array();
}

/**
* This method is called after all registered information collectors have
* been called to validate that the action has all necessary information
* if anything is missing an exception should be thrown.
* When called, the 'current-version' and 'new-version' parameters are
* already known, so a check can be made on those.
*
* @throws \Exception
*/
public function validateContext()
{

}

/**
* A common method to confirm success to the user
*/
19 changes: 14 additions & 5 deletions src/Liip/RMT/Action/VcsTagAction.php
Original file line number Diff line number Diff line change
@@ -20,11 +20,20 @@ class VcsTagAction extends BaseAction
{
public function execute()
{
Context::get('vcs')->createTag(
Context::get('vcs')->getTagFromVersion(
Context::getParam('new-version')
)
);
Context::get('vcs')->createTag($this->getVersion());
$this->confirmSuccess();
}

protected function getVersion()
{
return Context::get('vcs')->getTagFromVersion(
Context::getParam('new-version')
);
}

public function validateContext()
{
parent::validateContext();
Context::get('vcs')->validateTag($this->getVersion());
}
}
9 changes: 8 additions & 1 deletion src/Liip/RMT/Command/ReleaseCommand.php
Original file line number Diff line number Diff line change
@@ -136,13 +136,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
);
Context::getInstance()->setParameter('new-version', $newVersion);

foreach (array('prerequisites', 'pre-release-actions', 'post-release-actions') as $listName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following our discussion, I guess we can also remove prerequisites from that list as there have already been executed. Isn't it?

foreach(Context::getInstance()->getList($listName) as $action) {
$action->validateContext();
}
}
Context::get('version-persister')->validateContext();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a validateContext() on the version-generator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, because the generator work is a bit different than the other actions and the persister and there's no need at the time.
Adding it once we need it won't be a big issue.

Can we merge ? please, pretty please, PLEASE ! PLEASE !


$this->executeActionListIfExist('pre-release-actions');

$this->getOutput()->writeSmallTitle('Release process');
$this->getOutput()->indent();

$this->getOutput()->writeln("A new version named [<yellow>$newVersion</yellow>] is going to be released");
Context::get('version-persister')->save($newVersion);
Context::get('version-persister')->save();
$this->getOutput()->writeln('Release: <green>Success</green>');

$this->getOutput()->unIndent();
18 changes: 18 additions & 0 deletions src/Liip/RMT/Exception/InvalidTagNameException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the project RMT
*
* Copyright (c) 2013, Liip AG, http://www.liip.ch
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Liip\RMT\Exception;

use Liip\RMT\Exception as BaseException;

class InvalidTagNameException extends BaseException
{
}
18 changes: 18 additions & 0 deletions src/Liip/RMT/Exception/TagAlreadyExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the project RMT
*
* Copyright (c) 2013, Liip AG, http://www.liip.ch
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Liip\RMT\Exception;

use Liip\RMT\Exception as BaseException;

class TagAlreadyExistsException extends BaseException
{
}
26 changes: 24 additions & 2 deletions src/Liip/RMT/VCS/Git.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,10 @@

namespace Liip\RMT\VCS;

use Liip\RMT\Exception;
use Liip\RMT\Exception\InvalidTagNameException;
use Liip\RMT\Exception\TagAlreadyExistsException;

class Git extends BaseVCS
{
protected $dryRun = false;
@@ -45,6 +49,19 @@ public function getTags()
return $this->executeGitCommand('tag');
}

public function validateTag($tagName)
{
try {
$this->executeGitCommand("check-ref-format --allow-onelevel $tagName");
} catch(Exception $e) {
throw new InvalidTagNameException("'$tagName' is an invalid tag name for git.");
}

if(in_array($tagName, $this->getTags())) {
throw new TagAlreadyExistsException("'$tagName' already exists.");
}
}

public function createTag($tagName)
{
return $this->executeGitCommand("tag $tagName");
@@ -79,14 +96,19 @@ public function getCurrentBranch()
throw new \Liip\RMT\Exception('Not currently on any branch');
}

/**
* @param $cmd
* @throws \Liip\RMT\Exception
* @return string[]
*/
protected function executeGitCommand($cmd)
{
// Avoid using some commands in dry mode
if ($this->dryRun) {
if ($cmd !== 'tag') {
$cmdWords = explode(' ', $cmd);
if (in_array($cmdWords[0], array('tag', 'push', 'add', 'commit'))) {
return;
return [];
}
}
}
@@ -95,7 +117,7 @@ protected function executeGitCommand($cmd)
$cmd = 'git ' . $cmd;
exec($cmd, $result, $exitCode);
if ($exitCode !== 0) {
throw new \Liip\RMT\Exception('Error while executing git command: ' . $cmd . "\n" . implode("\n", $result));
throw new Exception('Error while executing git command: ' . $cmd . "\n" . implode("\n", $result));
}

return $result;
14 changes: 14 additions & 0 deletions src/Liip/RMT/VCS/Hg.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,9 @@

namespace Liip\RMT\VCS;

use Liip\RMT\Exception\InvalidTagNameException;
use Liip\RMT\Exception\TagAlreadyExistsException;

class Hg extends BaseVCS
{
protected $dryRun = false;
@@ -53,6 +56,17 @@ public function getTags()
return $tags;
}

public function validateTag($tagName)
{
if(preg_match("/[:\r\n]/", $tagName) > 0 || preg_match("/^[0-9]*$/", $tagName) > 0) {
throw new InvalidTagNameException("'$tagName' is an invalid tag name for mercurial.");
}

if(in_array($tagName, $this->getTags())) {
throw new TagAlreadyExistsException("'$tagName' already exists.");
}
}

public function createTag($tagName)
{
return $this->executeHgCommand("tag $tagName");
14 changes: 14 additions & 0 deletions src/Liip/RMT/VCS/VCSInterface.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,9 @@

namespace Liip\RMT\VCS;

use Liip\RMT\Exception\InvalidTagNameException;
use Liip\RMT\Exception\TagAlreadyExistsException;

interface VCSInterface
{
/**
@@ -25,6 +28,17 @@ public function getCurrentBranch();
*/
public function getTags();

/**
* Validate that a tag name is valid for the given VCS. If possible
* should also check if this tag already exists or if we can create
* it freely.
*
* @param $tagName
* @throws InvalidTagNameException in case the name is invalid
* @throws TagAlreadyExistsException in case the tag name already exists
*/
public function validateTag($tagName);

/**
* Create a new tag at the current position
*
9 changes: 7 additions & 2 deletions src/Liip/RMT/Version/Persister/ChangelogPersister.php
Original file line number Diff line number Diff line change
@@ -42,18 +42,23 @@ public function getCurrentVersion()
return $this->changelogManager->getCurrentVersion();
}

public function save($versionNumber)
public function save()
{
$comment = Context::get('information-collector')->getValueFor('comment');
$type = Context::get('information-collector')->getValueFor('type', null);
$this->changelogManager->update($versionNumber, $comment, array('type' => $type));
$this->changelogManager->update(Context::getParam('new-version'), $comment, array('type' => $type));
}

public function getInformationRequests()
{
return array('comment');
}

public function validateContext()
{

}

public function init()
{
// TODO: Implement init() method.
4 changes: 3 additions & 1 deletion src/Liip/RMT/Version/Persister/PersisterInterface.php
Original file line number Diff line number Diff line change
@@ -26,10 +26,12 @@ public function __construct($options = array());
*/
public function getCurrentVersion();

public function save($versionNumber);
public function save();

public function getInformationRequests();

public function validateContext();

// Use the very first time to init this persistence
public function init();
}
14 changes: 12 additions & 2 deletions src/Liip/RMT/Version/Persister/VcsTagPersister.php
Original file line number Diff line number Diff line change
@@ -46,13 +46,23 @@ public function getCurrentVersion()
return array_pop($versions);
}

public function save($versionNumber)
public function getNewVersion()
{
$tagName = $this->getTagFromVersion($versionNumber);
return $this->getTagFromVersion(Context::getParam('new-version'));
}

public function save()
{
$tagName = $this->getNewVersion();
Context::get('output')->writeln("Creation of a new VCS tag [<yellow>$tagName</yellow>]");
$this->vcs->createTag($tagName);
}

public function validateContext()
{
Context::get('vcs')->validateTag($this->getNewVersion());
}

public function init()
{
}
24 changes: 16 additions & 8 deletions test/Liip/RMT/Tests/Functional/HgTest.php
Original file line number Diff line number Diff line change
@@ -22,13 +22,21 @@ public static function cleanTags($tags)
}, $tags);
}

public function testInitialVersion()
public function testNoSimpleGenerator()
{
$this->initHg();
$this->createConfig('simple', 'vcs-tag', array('vcs' => 'hg'));
exec('./RMT release -n --confirm-first 2> /dev/null', $result, $code);
$this->assertEquals($code, 1);
}

public function testInitialVersion()
{
$this->initHg();
$this->createConfig('simple', array('name' => 'vcs-tag', 'tag-prefix' => 'v'), array('vcs' => 'hg'));
exec('./RMT release -n --confirm-first');
exec('hg tags', $tags);
$this->assertEquals(array('tip', '1'), static::cleanTags($tags));
$this->assertEquals(array('tip', 'v1'), static::cleanTags($tags));
}

public function testInitialVersionSemantic()
@@ -43,13 +51,13 @@ public function testInitialVersionSemantic()
public function testSimple()
{
$this->initHg();
exec('hg tag 1');
exec('hg tag 3');
exec('hg tag v1');
exec('hg tag v3');
exec('hg tag toto');
$this->createConfig('simple', 'vcs-tag', array('vcs' => 'hg'));
$this->createConfig('simple', array('name' => 'vcs-tag', 'tag-prefix' => 'v'), array('vcs' => 'hg'));
exec('./RMT release -n');
exec('hg tags', $tags);
$this->assertEquals(array('tip', '4', 'toto', '3', '1'), static::cleanTags($tags));
$this->assertEquals(array('tip', 'v4', 'toto', 'v3', 'v1'), static::cleanTags($tags));
}

public function testSemantic()
@@ -65,12 +73,12 @@ public function testSemantic()
public function testTagPrefix()
{
$this->initHg();
exec('hg tag 2');
exec('hg tag v2');
exec('hg tag v_1');
$this->createConfig('simple', array('name' => 'vcs-tag', 'tag-prefix' => 'v_'), array('vcs' => 'hg'));
exec('./RMT release -n');
exec('hg tags', $tags);
$this->assertEquals(array('tip', 'v_2', 'v_1', '2'), static::cleanTags($tags));
$this->assertEquals(array('tip', 'v_2', 'v_1', 'v2'), static::cleanTags($tags));
}

public function testTagPrefixWithBranchNamePlaceHolder()
47 changes: 47 additions & 0 deletions test/Liip/RMT/Tests/Unit/VCS/GitTest.php
Original file line number Diff line number Diff line change
@@ -60,6 +60,53 @@ public function testGetTags()
$this->assertEquals(array('1.0.0', '1.1.0'), $vcs->getTags());
}

/**
* @dataProvider invalidTagNames
* @expectedException \Liip\RMT\Exception\InvalidTagNameException
*/
public function testInvalidateTag($tag)
{
$vcs = new Git();
$vcs->validateTag($tag);
}

public function invalidTagNames()
{
return array(
array("test..test"),
array("test*test"),
array('test[test'),
array("test?test"),
array("test\ntest"),
array("test\rtest"),
array("test."),
array("@"),
array("\\"),
);
}

/**
* @dataProvider validTagNames
*/
public function testValidateTag($tag)
{
$vcs = new Git();
$vcs->validateTag($tag);
}

public function validTagNames()
{
return array(
array('test/test'),
array('test'),
array(2345),
array('1.2'),
array('1.2.3'),
array('v1.2'),
array('v1.2.3'),
);
}

public function testCreateTag()
{
$vcs = new Git();
41 changes: 41 additions & 0 deletions test/Liip/RMT/Tests/Unit/VCS/HgTest.php
Original file line number Diff line number Diff line change
@@ -60,6 +60,47 @@ public function testGetTags()
$this->assertEquals(array('tip', '1.1.0', '1.0.0'), $vcs->getTags());
}

/**
* @dataProvider invalidTagNames
* @expectedException \Liip\RMT\Exception\InvalidTagNameException
*/
public function testInvalidateTag($tag)
{
$vcs = new Hg();
$vcs->validateTag($tag);
}

public function invalidTagNames()
{
return array(
array(2345),
array('test:test'),
array("test\ntest"),
array("test\rtest"),
);
}

/**
* @dataProvider validTagNames
*/
public function testValidateTag($tag)
{
$vcs = new Hg();
$vcs->validateTag($tag);
}

public function validTagNames()
{
return array(
array('test/test'),
array('test'),
array('1.2'),
array('1.2.3'),
array('v1.2'),
array('v1.2.3'),
);
}

public function testCreateTag()
{
$vcs = new Hg();