view tests/test_fetch_mappings.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 841399d10c79
children 467b95348e6a
line wrap: on
line source

"""Tests for author maps and file maps.
"""
import test_util

import os
import unittest

from mercurial import commands
from mercurial import hg
from mercurial import node
from mercurial import util as hgutil

from hgsubversion import maps
from hgsubversion import svncommands

class MapTests(test_util.TestBase):
    @property
    def authors(self):
        return os.path.join(self.tmpdir, 'authormap')

    @property
    def filemap(self):
        return os.path.join(self.tmpdir, 'filemap')

    @property
    def branchmap(self):
        return os.path.join(self.tmpdir, 'branchmap')

    def test_author_map(self, stupid=False):
        test_util.load_svndump_fixture(self.repo_path, 'replace_trunk_with_branch.svndump')
        authormap = open(self.authors, 'w')
        authormap.write('Augie=Augie Fackler <durin42@gmail.com> # stuffy\n')
        authormap.write("Augie Fackler <durin42@gmail.com>\n")
        authormap.close()
        ui = self.ui(stupid)
        ui.setconfig('hgsubversion', 'authormap', self.authors)
        commands.clone(ui, test_util.fileurl(self.repo_path),
                       self.wc_path, authors=self.authors)
        self.assertEqual(self.repo[0].user(),
                         'Augie Fackler <durin42@gmail.com>')
        self.assertEqual(self.repo['tip'].user(),
                        'evil@5b65bade-98f3-4993-a01f-b7a6710da339')

    def test_author_map_stupid(self):
        self.test_author_map(True)

    def test_author_map_closing_author(self, stupid=False):
        test_util.load_svndump_fixture(self.repo_path, 'replace_trunk_with_branch.svndump')
        authormap = open(self.authors, 'w')
        authormap.write("evil=Testy <test@test>")
        authormap.close()
        ui = self.ui(stupid)
        ui.setconfig('hgsubversion', 'authormap', self.authors)
        commands.clone(ui, test_util.fileurl(self.repo_path),
                       self.wc_path, authors=self.authors)
        self.assertEqual(self.repo[0].user(),
                         'Augie@5b65bade-98f3-4993-a01f-b7a6710da339')
        self.assertEqual(self.repo['tip'].user(),
                        'Testy <test@test>')

    def test_author_map_closing_author_stupid(self):
        self.test_author_map_closing_author(True)

    def test_author_map_no_overwrite(self):
        cwd = os.path.dirname(__file__)
        orig = os.path.join(cwd, 'fixtures', 'author-map-test.txt')
        new = open(self.authors, 'w')
        new.write(open(orig).read())
        new.close()
        test = maps.AuthorMap(self.ui(), self.authors)
        fromself = set(test)
        test.load(orig)
        all = set(test)
        self.assertEqual(fromself.symmetric_difference(all), set())

    def test_file_map(self, stupid=False):
        test_util.load_svndump_fixture(self.repo_path, 'replace_trunk_with_branch.svndump')
        filemap = open(self.filemap, 'w')
        filemap.write("include alpha\n")
        filemap.close()
        ui = self.ui(stupid)
        ui.setconfig('hgsubversion', 'filemap', self.filemap)
        commands.clone(ui, test_util.fileurl(self.repo_path),
                       self.wc_path, filemap=self.filemap)
        self.assertEqual(node.hex(self.repo[0].node()), '88e2c7492d83e4bf30fbb2dcbf6aa24d60ac688d')
        self.assertEqual(node.hex(self.repo['default'].node()), 'e524296152246b3837fe9503c83b727075835155')

    def test_file_map_stupid(self):
        self.test_file_map(True)

    def test_file_map_exclude(self, stupid=False):
        test_util.load_svndump_fixture(self.repo_path, 'replace_trunk_with_branch.svndump')
        filemap = open(self.filemap, 'w')
        filemap.write("exclude alpha\n")
        filemap.close()
        ui = self.ui(stupid)
        ui.setconfig('hgsubversion', 'filemap', self.filemap)
        commands.clone(ui, test_util.fileurl(self.repo_path),
                       self.wc_path, filemap=self.filemap)
        self.assertEqual(node.hex(self.repo[0].node()), '2c48f3525926ab6c8b8424bcf5eb34b149b61841')
        self.assertEqual(node.hex(self.repo['default'].node()), 'b37a3c0297b71f989064d9b545b5a478bbed7cc1')

    def test_file_map_exclude_stupid(self):
        self.test_file_map_exclude(True)

    def test_branchmap(self, stupid=False):
        test_util.load_svndump_fixture(self.repo_path, 'branchmap.svndump')
        branchmap = open(self.branchmap, 'w')
        branchmap.write("badname = good-name # stuffy\n")
        branchmap.write("feature = default\n")
        branchmap.close()
        ui = self.ui(stupid)
        ui.setconfig('hgsubversion', 'branchmap', self.branchmap)
        commands.clone(ui, test_util.fileurl(self.repo_path),
                       self.wc_path, branchmap=self.branchmap)
        branches = set(self.repo[i].branch() for i in self.repo)
        self.assert_('badname' not in branches)
        self.assert_('good-name' in branches)
        self.assertEquals(self.repo[2].branch(), 'default')

    def test_branchmap_stupid(self):
        self.test_branchmap(True)

    def test_branchmap_tagging(self, stupid=False):
        '''test tagging a renamed branch, which used to raise an exception'''
        test_util.load_svndump_fixture(self.repo_path, 'commit-to-tag.svndump')
        branchmap = open(self.branchmap, 'w')
        branchmap.write("magic = art\n")
        branchmap.close()
        ui = self.ui(stupid)
        ui.setconfig('hgsubversion', 'branchmap', self.branchmap)
        commands.clone(ui, test_util.fileurl(self.repo_path),
                       self.wc_path, branchmap=self.branchmap)
        branches = set(self.repo[i].branch() for i in self.repo)
        self.assertEquals(sorted(branches), ['art', 'closeme'])

    def test_branchmap_tagging_stupid(self):
        self.test_branchmap_tagging(True)

    def test_branchmap_empty_commit(self, stupid=False):
        '''test mapping an empty commit on a renamed branch'''
        test_util.load_svndump_fixture(self.repo_path, 'propset-branch.svndump')
        branchmap = open(self.branchmap, 'w')
        branchmap.write("the-branch = bob\n")
        branchmap.close()
        ui = self.ui(stupid)
        ui.setconfig('hgsubversion', 'branchmap', self.branchmap)
        commands.clone(ui, test_util.fileurl(self.repo_path),
                       self.wc_path, branchmap=self.branchmap)
        branches = set(self.repo[i].branch() for i in self.repo)
        self.assertEquals(sorted(branches), ['bob', 'default'])

    def test_branchmap_empty_commit_stupid(self):
        '''test mapping an empty commit on a renamed branch (stupid)'''
        self.test_branchmap_empty_commit(True)

    def test_branchmap_combine(self, stupid=False):
        '''test combining two branches, but retaining heads'''
        test_util.load_svndump_fixture(self.repo_path, 'branchmap.svndump')
        branchmap = open(self.branchmap, 'w')
        branchmap.write("badname = default\n")
        branchmap.write("feature = default\n")
        branchmap.close()
        ui = self.ui(stupid)
        ui.setconfig('hgsubversion', 'branchmap', self.branchmap)
        commands.clone(ui, test_util.fileurl(self.repo_path),
                       self.wc_path, branchmap=self.branchmap)
        branches = set(self.repo[i].branch() for i in self.repo)
        self.assertEquals(sorted(branches), ['default'])
        self.assertEquals(len(self.repo.heads()), 2)
        self.assertEquals(len(self.repo.branchheads('default')), 2)

        # test that the mapping does not affect branch info
        branches = self.repo.svnmeta().branches
        self.assertEquals(sorted(branches.keys()),
                          [None, 'badname', 'feature'])

    def test_branchmap_combine_stupid(self):
        '''test combining two branches, but retaining heads (stupid)'''
        self.test_branchmap_combine(True)

    def test_branchmap_rebuildmeta(self, stupid=False):
        '''test rebuildmeta on a branchmapped clone'''
        test_util.load_svndump_fixture(self.repo_path, 'branchmap.svndump')
        branchmap = open(self.branchmap, 'w')
        branchmap.write("badname = dit\n")
        branchmap.write("feature = dah\n")
        branchmap.close()
        ui = self.ui(stupid)
        ui.setconfig('hgsubversion', 'branchmap', self.branchmap)
        commands.clone(ui, test_util.fileurl(self.repo_path),
                       self.wc_path, branchmap=self.branchmap)
        originfo = self.repo.svnmeta().branches

        # clone & rebuild
        ui = self.ui(stupid)
        src, dest = hg.clone(ui, self.wc_path, self.wc_path + '_clone',
                             update=False)
        svncommands.rebuildmeta(ui, dest,
                                args=[test_util.fileurl(self.repo_path)])

        # just check the keys; assume the contents are unaffected by the branch
        # map and thus properly tested by other tests
        self.assertEquals(sorted(src.svnmeta().branches),
                          sorted(dest.svnmeta().branches))

    def test_branchmap_rebuildmeta_stupid(self):
        '''test rebuildmeta on a branchmapped clone (stupid)'''
        self.test_branchmap_rebuildmeta(True)

    def test_branchmap_verify(self, stupid=False):
        '''test verify on a branchmapped clone'''
        test_util.load_svndump_fixture(self.repo_path, 'branchmap.svndump')
        branchmap = open(self.branchmap, 'w')
        branchmap.write("badname = dit\n")
        branchmap.write("feature = dah\n")
        branchmap.close()
        ui = self.ui(stupid)
        ui.setconfig('hgsubversion', 'branchmap', self.branchmap)
        commands.clone(ui, test_util.fileurl(self.repo_path),
                       self.wc_path, branchmap=self.branchmap)
        repo = self.repo

        for r in repo:
            self.assertEquals(svncommands.verify(ui, repo, rev=r), 0)

    def test_branchmap_verify_stupid(self):
        '''test verify on a branchmapped clone (stupid)'''
        self.test_branchmap_verify(True)

    def test_branchmap_no_replacement(self):
        '''
        test that empty mappings are rejected

        Empty mappings are lines like 'this ='. The most sensible thing to do
        is to not convert the 'this' branches. Until we can do that, we settle
        with aborting.
        '''
        test_util.load_svndump_fixture(self.repo_path, 'propset-branch.svndump')
        branchmap = open(self.branchmap, 'w')
        branchmap.write("closeme =\n")
        branchmap.close()
        self.assertRaises(hgutil.Abort,
                          maps.BranchMap, self.ui(), self.branchmap)

def suite():
    return unittest.TestLoader().loadTestsFromTestCase(MapTests)