From 780267978d9c7a9908be888edf8711e297e12289 Mon Sep 17 00:00:00 2001 From: Thomas Keller Date: Wed, 30 Jun 2010 00:42:09 +0200 Subject: [PATCH] basic test setup works; added tests for isAvailable(), getBranches(), getTags(), inTags(), inBranches(), getTree() and isValidRevision() --- src/IDF/Tests/TestMonotone.php | 117 ++++++++++++++++++++++++++++++--- 1 file changed, 107 insertions(+), 10 deletions(-) diff --git a/src/IDF/Tests/TestMonotone.php b/src/IDF/Tests/TestMonotone.php index 8071cc4..ff01e79 100644 --- a/src/IDF/Tests/TestMonotone.php +++ b/src/IDF/Tests/TestMonotone.php @@ -28,10 +28,12 @@ require_once("simpletest/autorun.php"); */ class IDF_Tests_TestMonotone extends UnitTestCase { - private $tmpdir, $dbfile; + private $tmpdir, $dbfile, $mtnInstance; private function mtnCall($args, $stdin = null, $dir = null) { + // if you have an SSH agent running for key caching, + // please disable it $cmdline = array("mtn", "--confdir", $this->tmpdir, "--db", $this->dbfile, @@ -47,10 +49,11 @@ class IDF_Tests_TestMonotone extends UnitTestCase ); $pipes = array(); + $dir = !empty($dir) ? $dir : $this->tmpdir; $process = proc_open(implode(" ", $cmdline), $descriptorspec, $pipes, - empty($dir) ? $this->tmpdir : $dir); + $dir); if (!is_resource($process)) { @@ -83,8 +86,15 @@ class IDF_Tests_TestMonotone extends UnitTestCase parent::__construct("Test the monotone class."); $this->tmpdir = sys_get_temp_dir() . "/mtn-test"; - echo "test root is {$this->tmpdir}\n"; $this->dbfile = "{$this->tmpdir}/test.mtn"; + + set_include_path(get_include_path() . ":../../../pluf-master/src"); + require_once("Pluf.php"); + + Pluf::start(dirname(__FILE__)."/../conf/idf.php"); + + // Pluf::f() mocking + $GLOBALS['_PX_config']['mtn_repositories'] = "{$this->tmpdir}/%s.mtn"; } private static function deleteRecursive($dirname) @@ -127,23 +137,110 @@ class IDF_Tests_TestMonotone extends UnitTestCase $workspaceRoot = "{$this->tmpdir}/test-workspace"; mkdir($workspaceRoot); - $this->mtnCall(array("setup", "-b", "testbranch", "blafoo"), null, $workspaceRoot); + $this->mtnCall(array("setup", "-b", "testbranch", "."), null, $workspaceRoot); - file_put_contents("$workspaceRoot/foo", "blafoo"); + file_put_contents("$workspaceRoot/foo", "blubber"); $this->mtnCall(array("add", "foo"), null, $workspaceRoot); $this->mtnCall(array("commit", "-m", "initial"), null, $workspaceRoot); - file_put_contents("$workspaceRoot/foo", "bla"); - file_put_contents("$workspaceRoot/bar", "blafoo"); - $this->mtnCall(array("add", "bar"), null, $workspaceRoot); + mkdir("$workspaceRoot/subdir"); + file_put_contents("$workspaceRoot/subdir/bla", "blabla"); + $this->mtnCall(array("add", "-R", "--unknown"), null, $workspaceRoot); $this->mtnCall(array("commit", "-m", "second"), null, $workspaceRoot); + + $rev = $this->mtnCall(array("au", "get_base_revision_id"), null, $workspaceRoot); + $this->mtnCall(array("tag", rtrim($rev), "release-1.0")); + + $project = new IDF_Project(); + $project->shortname = "test"; + $this->mtnInstance = new IDF_Scm_Monotone($project); } - public function testBranches() + public function testIsAvailable() { - $this->assertTrue(false); + $this->assertTrue($this->mtnInstance->isAvailable()); + } + + public function testGetBranches() + { + $branches = $this->mtnInstance->getBranches(); + $this->assertEqual(1, count($branches)); + list($key, $value) = each($branches); + $this->assertEqual("h:testbranch", $key); + $this->assertEqual("testbranch", $value); + } + + public function testGetTags() + { + $tags = $this->mtnInstance->getTags(); + $this->assertEqual(1, count($tags)); + list($key, $value) = each($tags); + $this->assertEqual("t:release-1.0", $key); + $this->assertEqual("release-1.0", $value); + } + + public function testInBranches() + { + $revOut = $this->mtnCall(array("au", "select", "b:testbranch")); + $revs = preg_split('/\n/', $revOut, -1, PREG_SPLIT_NO_EMPTY); + + $branches = $this->mtnInstance->inBranches($revs[0], null); + $this->assertEqual(1, count($branches)); + $this->assertEqual("h:testbranch", $branches[0]); + + $branches = $this->mtnInstance->inBranches("t:release-1.0", null); + $this->assertEqual(1, count($branches)); + $this->assertEqual("h:testbranch", $branches[0]); + } + + public function testInTags() + { + $rev = $this->mtnCall(array("au", "select", "t:release-1.0")); + $tags = $this->mtnInstance->inTags(rtrim($rev), null); + $this->assertEqual(1, count($tags)); + $this->assertEqual("t:release-1.0", $tags[0]); + + // pick the first (root) revisions in this database + $rev = $this->mtnCall(array("au", "roots")); + $tags = $this->mtnInstance->inTags(rtrim($rev), null); + $this->assertEqual(0, count($tags)); + } + + public function testGetTree() + { + $files = $this->mtnInstance->getTree("t:release-1.0"); + $this->assertEqual(3, count($files)); + + $this->assertEqual("bar", $files[0]->file); + $this->assertEqual("blob", $files[0]->type); + $this->assertEqual(6, $files[0]->size); // "blafoo" + $this->assertEqual("second\n", $files[0]->log); + + $this->assertEqual("foo", $files[1]->file); + $this->assertEqual("blob", $files[1]->type); + $this->assertEqual(7, $files[1]->size); // "blubber" + $this->assertEqual("initial\n", $files[1]->log); + + $this->assertEqual("subdir", $files[2]->file); + $this->assertEqual("tree", $files[2]->type); + $this->assertEqual(0, $files[2]->size); + + $files = $this->mtnInstance->getTree("t:release-1.0", "subdir"); + $this->assertEqual(1, count($files)); + + $this->assertEqual("bla", $files[0]->file); + $this->assertEqual("subdir/bla", $files[0]->fullpath); + $this->assertEqual("blob", $files[0]->type); + $this->assertEqual(6, $files[0]->size); // "blabla" + $this->assertEqual("second\n", $files[0]->log); + } + + public function testIsValidRevision() + { + $this->assertTrue($this->mtnInstance->isValidRevision("t:release-1.0")); + $this->assertFalse($this->mtnInstance->isValidRevision("abcdef12345")); } }