Source

heechee / heechee / tests / hg.py

Full commit
"""
Mercurial backend unit tests.
"""

import unittest
from heechee.tests.base import HeecheeTest

class MercurialTests(HeecheeTest):
    
    url_prefix = ""
    
    def init_repo(self):
        self.source_cmds([
            "hg init",
            "echo a > a",
            "hg ci -Am adda",
            "hg branch branch1",
            "echo b > b",
            "hg ci -Am addb",
            "mkdir dir",
            "echo c > dir/c",
            "hg ci -Am addc",
            "hg tag tag1",
        ])
    
    def test_checkout_all(self):
        # Checkout the whole repo
        self.target_cmds([
            "svn co http://localhost:8080/%s checkout" % self.url_prefix
        ])
        # Check things are there
        self.assert_contents("checkout/trunk/a", "a\n")
        self.assert_contents("checkout/branches/branch1/b", "b\n")
        self.assert_contents("checkout/branches/branch1/dir/c", "c\n")
        # Check there's no default branch (it's trunk)
        self.assert_not_exists("checkout/branches/default/a")
    
    def test_checkout_trunk(self):
        # Checkout a subdir
        self.target_cmds([
            "svn co http://localhost:8080/%strunk/ checkout" % self.url_prefix
        ])
        # Make sure it only has file a
        self.assert_contents("checkout/a", "a\n")
        self.assert_not_exists("checkout/b")
        self.assert_not_exists("checkout/c")
    
    def test_checkout_tag(self):
        # Checkout the tag
        self.target_cmds([
            "svn co http://localhost:8080/%stags/tag1/ checkout" % self.url_prefix
        ])
        # Make sure the files are there
        self.assert_contents("checkout/a", "a\n")
        self.assert_contents("checkout/b", "b\n")
        self.assert_contents("checkout/dir/c", "c\n")
    
    def test_checkout_subdir(self):
        self.target_cmds([
            "svn co http://localhost:8080/%sbranches/branch1/dir/ checkout" % self.url_prefix
        ])
        self.assert_contents("checkout/c", "c\n")
        self.assert_not_exists("checkout/a")
        self.assert_not_exists("checkout/b")
    
    def test_ls(self):
        self.assert_equal(
            self.target_cmds("svn ls http://localhost:8080/%s" % self.url_prefix),
            "branches/\ntags/\ntrunk/\n",
        )
        self.assert_equal(
            self.target_cmds("svn ls http://localhost:8080/%strunk/" % self.url_prefix),
            "a\n",
        )

    def test_cat(self):
        self.assert_equal(
            self.target_cmds("svn cat http://localhost:8080/%strunk/a" % self.url_prefix),
            "a\n",
        )    
    
    def test_update(self):
        # First, get a checkout.
        self.target_cmds("svn co http://localhost:8080/%strunk/ checkout" % self.url_prefix)
        # Make sure there's no file d (or b)
        self.assert_not_exists("checkout/b")
        self.assert_not_exists("checkout/d")
        # Add one and commit to the source repo
        self.source_cmds([
            "hg up -C default",
            "echo d > d",
            "hg ci -Am addd",
        ])
        # Update
        self.target_cmds("svn up checkout")
        # Check d has appeared
        self.assert_not_exists("checkout/b")
        self.assert_contents("checkout/d", "d\n")
    
    def test_log(self):
        # Get the log directly
        log = self.target_cmds("svn log http://localhost:8080/%s" % self.url_prefix)
        # Check it contains certain vital things
        self.assert_contains(log, "r1 |")
        self.assert_contains(log, "r2 |")
        self.assert_contains(log, "r3 |")
        self.assert_contains(log, "r4 |")
        self.assert_not_contains(log, "r5 |")
        self.assert_contains(log, "\nadda\n")
        self.assert_contains(log, "\naddb\n")
        self.assert_contains(log, "\naddc\n")
        self.assert_contains(log, "\nAdded tag tag1")
        # Get the log for trunk (only had one commit)
        log = self.target_cmds("svn log http://localhost:8080/%strunk/" % self.url_prefix)
        self.assert_contains(log, "r1 |")
        self.assert_not_contains(log, "r2 |")
        # Check out the code, get the log from there (well, server still...)
        self.target_cmds("svn co http://localhost:8080/%s checkout" % self.url_prefix)
        log = self.target_cmds("svn log checkout")
        # Check it contains certain vital things
        self.assert_contains(log, "r1 |")
        self.assert_contains(log, "r2 |")
        self.assert_contains(log, "r3 |")
        self.assert_contains(log, "r4 |")
        self.assert_not_contains(log, "r5 |")
        self.assert_contains(log, "\nadda\n")
        self.assert_contains(log, "\naddb\n")
        self.assert_contains(log, "\naddc\n")
        self.assert_contains(log, "\nAdded tag tag1")
    
    def test_commit(self):
        "Tests committing to the fake SVN repo"
        # Check out a copy of trunk, create a new file, add it, and commit it
        self.target_cmds([
            "svn co http://localhost:8080/%strunk/ checkout" % self.url_prefix,
            "echo e > checkout/e",
            "echo aa > checkout/a",
            "svn add checkout/e",
            "svn ci checkout -m 'Commit from SVN'",
        ])
        # Update the mercurial repo to default, make sure it's there
        self.source_cmds("hg up -C default")
        self.assert_exists_source("e")
        self.assert_contents_source("e", "e\n")
        self.assert_contents_source("a", "aa\n")
        # Remove that new file, make sure it goes.
        self.target_cmds([
            "svn rm checkout/e",
            "svn ci checkout -m 'Second commit from SVN'",
        ])
        self.source_cmds("hg up -C default")
        self.assert_not_exists_source("e")
        self.assert_contents_source("a", "aa\n")
    
    def test_commit_directories(self):
        "Tests committing directories. Mercurial should ignore these."
        # Check out a copy of trunk, create a new file, add it, and commit it
        self.target_cmds([
            "svn co http://localhost:8080/%strunk/ checkout" % self.url_prefix,
            "svn mkdir checkout/newdir",
            "svn ci checkout -m 'Commit from SVN'",
        ])
        # Update the mercurial repo to default, make sure it's appeared
        self.source_cmds("hg up -C default")
        self.assert_exists_source("newdir")
        # Add a file into it, make sure that appears
        self.target_cmds([
            "echo ff > checkout/newdir/f",
            "svn add checkout/newdir/f",
            "svn ci checkout -m 'Commit 2 from SVN'",
        ])
        self.source_cmds("hg up -C default")
        self.assert_contents_source("newdir/f", "ff\n")
        # Now, delete the directory, and make sure it deleted
        self.target_cmds([
            "svn rm checkout/newdir/",
            "svn ci checkout -m 'Commit 3 from SVN'",
        ])
        self.source_cmds("hg up -C default")
        self.assert_not_exists_source("newdir/f")
        self.assert_not_exists_source("newdir")


class MercurialHydraTests(MercurialTests):
    
    use_hydra = True
    url_prefix = "subdir/"


class MercurialCacheTests(MercurialTests):
    
    use_cache = True


if __name__ == '__main__':
    unittest.main()