view tests/test_utility_commands.py @ 717:ae5968ffe6fe

svnwrap: fix handling of quotable URLs (fixes #197, refs #132) The way hgsubversion handles URLs that may or may not be quoted is somewhat fragile. As part of fixing issue 132 in 925ff8c5989c, the path component of URLs was always quoted. The URL has been attempted encoded since the initial check-in. The fix from 925ff8c5989c was incomplete; reverting it allows us to clone a URL with a '~' in it.[1] Encoding the URL as UTF-8 seldom works as expected, as the default string encoding is ASCII, causing Python to be unable to decode any URL containing an 8-bit character. The core problem here is that we don't know whether the URL specified by the user is quoted or not. Rather than trying to deal with this ourselves, we pass the problem on to Subversion. Then, we obtain the URL from the RA instance, where it is always quoted. (It's worth noting that the editor interface, on the other hand, always deals with unquoted paths...) Thus, the following invariants should apply to SubversionRepo attributes: - svn_url and root will always be quoted. - subdir will always be unquoted. Tests are added that verify that it won't affect the conversion whether a URL is specified in quoted or unquoted form. Furthermore, a test fixture for this is added *twice*, so that we can thoroughly test both quoted and unquoted URLs. I'm not adding a test dedicated to tildes in URLs; it doesn't seem necessary. [1] Such as <https://svn.kenai.com/svn/winsw~subversion>.
author Dan Villiom Podlaski Christiansen <danchr@gmail.com>
date Mon, 04 Oct 2010 21:00:36 -0500
parents d2ef7220a079
children 045120d3a110
line wrap: on
line source

import test_util

import os
import unittest

from hgext import rebase
from mercurial import hg
from mercurial import revlog
from mercurial import context
from mercurial import node
from mercurial import commands

from hgsubversion import util
from hgsubversion import svncommands
from hgsubversion import wrappers

expected_info_output = '''URL: %(repourl)s/%(branch)s
Repository Root: %(repourl)s
Repository UUID: df2126f7-00ab-4d49-b42c-7e981dde0bcf
Revision: %(rev)s
Node Kind: directory
Last Changed Author: durin
Last Changed Rev: %(rev)s
Last Changed Date: %(date)s
'''

class UtilityTests(test_util.TestBase):
    @property
    def repourl(self):
        return util.normalize_url(test_util.fileurl(self.repo_path))

    def test_info_output(self):
        self._load_fixture_and_fetch('two_heads.svndump')
        hg.update(self.repo, 'the_branch')
        u = self.ui()
        u.pushbuffer()
        svncommands.info(u, self.repo)
        actual = u.popbuffer()
        expected = (expected_info_output %
                    {'date': '2008-10-08 01:39:05 +0000 (Wed, 08 Oct 2008)',
                     'repourl': self.repourl,
                     'branch': 'branches/the_branch',
                     'rev': 5,
                     })
        self.assertEqual(actual, expected)
        hg.update(self.repo, 'default')
        u.pushbuffer()
        svncommands.info(u, self.repo)
        actual = u.popbuffer()
        expected = (expected_info_output %
                    {'date': '2008-10-08 01:39:29 +0000 (Wed, 08 Oct 2008)',
                     'repourl': self.repourl,
                     'branch': 'trunk',
                     'rev': 6,
                     })
        self.assertEqual(actual, expected)
        hg.update(self.repo, 'default')
        u.pushbuffer()
        svncommands.info(u, self.repo, rev=3)
        actual = u.popbuffer()
        expected = (expected_info_output %
                    {'date': '2008-10-08 01:39:05 +0000 (Wed, 08 Oct 2008)',
                     'repourl': self.repourl,
                     'branch': 'branches/the_branch',
                     'rev': 5,
                     })
        self.assertEqual(actual, expected)

    def test_info_single(self):
        self._load_fixture_and_fetch('two_heads.svndump', subdir='trunk')
        hg.update(self.repo, 'tip')
        u = self.ui()
        u.pushbuffer()
        svncommands.info(u, self.repo)
        actual = u.popbuffer()
        expected = (expected_info_output %
                    {'date': '2008-10-08 01:39:29 +0000 (Wed, 08 Oct 2008)',
                     'repourl': self.repourl,
                     'branch': 'trunk',
                     'rev': 6,
                     })
        self.assertStringEqual(expected, actual)

    def test_parent_output(self):
        self._load_fixture_and_fetch('two_heads.svndump')
        u = self.ui()
        u.pushbuffer()
        parents = (self.repo['the_branch'].node(), revlog.nullid, )
        def filectxfn(repo, memctx, path):
            return context.memfilectx(path=path,
                                      data='added',
                                      islink=False,
                                      isexec=False,
                                      copied=False)
        ctx = context.memctx(self.repo,
                             parents,
                             'automated test',
                             ['added_bogus_file', 'other_added_file', ],
                             filectxfn,
                             'testy',
                             '2008-12-21 16:32:00 -0500',
                             {'branch': 'localbranch', })
        new = self.repo.commitctx(ctx)
        hg.update(self.repo, new)
        wrappers.parents(lambda x, y: None, u, self.repo, svn=True)
        actual = u.popbuffer()
        self.assertEqual(actual, '3:4e256962fc5d\n')

        hg.update(self.repo, 'default')

        # Make sure styles work
        u.pushbuffer()
        wrappers.parents(lambda x, y: None, u, self.repo, svn=True, style='compact')
        actual = u.popbuffer()
        self.assertEqual(actual, '4:1083037b18d8\n')

        # custom templates too
        u.pushbuffer()
        wrappers.parents(lambda x, y: None, u, self.repo, svn=True, template='{node}\n')
        actual = u.popbuffer()
        self.assertEqual(actual, '1083037b18d85cd84fa211c5adbaeff0fea2cd9f\n')

        u.pushbuffer()
        wrappers.parents(lambda x, y: None, u, self.repo, svn=True)
        actual = u.popbuffer()
        self.assertEqual(actual, '4:1083037b18d8\n')

    def test_outgoing_output(self):
        self._load_fixture_and_fetch('two_heads.svndump')
        u = self.ui()
        parents = (self.repo['the_branch'].node(), revlog.nullid, )
        def filectxfn(repo, memctx, path):
            return context.memfilectx(path=path,
                                      data='added',
                                      islink=False,
                                      isexec=False,
                                      copied=False)
        ctx = context.memctx(self.repo,
                             parents,
                             'automated test',
                             ['added_bogus_file', 'other_added_file', ],
                             filectxfn,
                             'testy',
                             '2008-12-21 16:32:00 -0500',
                             {'branch': 'localbranch', })
        new = self.repo.commitctx(ctx)
        hg.update(self.repo, new)
        u.pushbuffer()
        commands.outgoing(u, self.repo, self.repourl)
        actual = u.popbuffer()
        self.assertTrue(node.hex(self.repo['localbranch'].node())[:8] in actual)
        self.assertEqual(actual.strip(), '5:6de15430fa20')
        hg.update(self.repo, 'default')
        u.pushbuffer()
        commands.outgoing(u, self.repo, self.repourl)
        actual = u.popbuffer()
        self.assertEqual(actual, '')

    def test_rebase(self):
        self._load_fixture_and_fetch('two_revs.svndump')
        parents = (self.repo[0].node(), revlog.nullid, )
        def filectxfn(repo, memctx, path):
            return context.memfilectx(path=path,
                                      data='added',
                                      islink=False,
                                      isexec=False,
                                      copied=False)
        ctx = context.memctx(self.repo,
                             parents,
                             'automated test',
                             ['added_bogus_file', 'other_added_file', ],
                             filectxfn,
                             'testy',
                             '2008-12-21 16:32:00 -0500',
                             {'branch': 'localbranch', })
        self.repo.commitctx(ctx)
        self.assertEqual(self.repo['tip'].branch(), 'localbranch')
        beforerebasehash = self.repo['tip'].node()
        hg.update(self.repo, 'tip')
        wrappers.rebase(rebase.rebase, self.ui(), self.repo, svn=True)
        self.assertEqual(self.repo['tip'].branch(), 'localbranch')
        self.assertEqual(self.repo['tip'].parents()[0].parents()[0], self.repo[0])
        self.assertNotEqual(beforerebasehash, self.repo['tip'].node())

    def test_genignore(self):
        """ Test generation of .hgignore file. """
        test_util.load_fixture_and_fetch('ignores.svndump', self.repo_path,
                                         self.wc_path, noupdate=False)
        u = self.ui()
        u.pushbuffer()
        svncommands.genignore(u, self.repo, self.wc_path)
        self.assertEqual(open(os.path.join(self.wc_path, '.hgignore')).read(),
                         '.hgignore\nsyntax:glob\nblah\notherblah\nbaz/magic\n')

    def test_genignore_single(self):
        self._load_fixture_and_fetch('ignores.svndump', subdir='trunk')
        hg.update(self.repo, 'tip')
        u = self.ui()
        u.pushbuffer()
        svncommands.genignore(u, self.repo, self.wc_path)
        self.assertStringEqual(open(os.path.join(self.wc_path, '.hgignore')).read(),
                               '.hgignore\nsyntax:glob\nblah\notherblah\nbaz/magic\n')

    def test_list_authors(self):
        test_util.load_svndump_fixture(self.repo_path,
                                       'replace_trunk_with_branch.svndump')
        u = self.ui()
        u.pushbuffer()
        svncommands.listauthors(u,
                                     args=[test_util.fileurl(self.repo_path)],
                                     authors=None)
        actual = u.popbuffer()
        self.assertEqual(actual, 'Augie\nevil\n')


    def test_list_authors_map(self):
        test_util.load_svndump_fixture(self.repo_path,
                                       'replace_trunk_with_branch.svndump')
        author_path = os.path.join(self.repo_path, 'authors')
        svncommands.listauthors(self.ui(),
                                     args=[test_util.fileurl(self.repo_path)],
                                     authors=author_path)
        self.assertEqual(open(author_path).read(), 'Augie=\nevil=\n')


def suite():
    all = [unittest.TestLoader().loadTestsFromTestCase(UtilityTests),
          ]
    return unittest.TestSuite(all)