~bzr/bzr-webserve/webserve-dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# test_webserve.py - test suite for the bazaar web interface
#
# Copyright 2006 Goffredo Baroncelli <kreijack@inwind.it>
#
# This file is part of bazaar-webserve.
#
# bazaar-webserve is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# bazaar-webserve is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with bazaar-webserve; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import sys
import StringIO
import os
import re

import bzrlib
from bzrlib.tests.blackbox import ExternalBase
from bzrlib.tests import TestCaseInTempDir
from bzrlib.workingtree import WorkingTree

import hgweb
from templater import (
        templater,
        write2,
)


class TestWebserveBase(ExternalBase):

    format=None

    def make_branch_and_tree(self, relpath):
        #print "***** format=", self.format
        return super(TestWebserveBase, self).make_branch_and_tree(relpath, self.format)

    def do_test(self, url, repo=None):
        """Call the webserve engine, with the url "url"
           If repo==Null, the repo is in the current working dir
        """

        def url2args(url):
            d = dict()
            if len(url)<1:
                return d
            if url[0].find("?")>=0:
                url = url[url.find("?")+1:]

            for i in url.split(";"):
                key, value = i.split("=")
                d[key]=(value,)

            return d

        if repo is None:
            repo = os.getcwd()

        s = StringIO.StringIO()
        oldstdout = sys.stdout
        sys.stdout = s
        hg = hgweb.hgweb(repo, "TEST")

        self.update_hgweb(hg)

        hg.do_cmd(url, url2args(url))
        sys.stdout = oldstdout

        return s.getvalue()

    def update_hgweb(self, hg):
        pass

    def do_test_text(self, url, repo=None):
        s = self.do_test(url, repo)

        start = s.find("<")
        while start >=0:
            end = s.find(">", start)
            if end < 0: break
            s = s[:start] + s[end+1:]
            start = s.find("<")

        s = re.sub("[ \t]+", " ", s)
        while s.find("\n \n")>=0:
            s = s.replace("\n \n", "\n")
        return s

    def assertContains(self, haystack, needle_re):
        """Assert that a contains something matching a regular expression."""
        if haystack.find(needle_re)<0:
            raise AssertionError('string "%s" not found in "%s"'
                    % (needle_re, haystack))

    def assertContainsReMultiline(self, haystack, needle_re):
        """Assert that a contains something matching a regular expression."""
        if not re.search(needle_re, haystack, re.DOTALL):
            raise AssertionError('pattern "%s" not found in "%s"'
                    % (needle_re, haystack))

    def assertNotContainsReMultiline(self, haystack, needle_re):
        """Assert that a does not match a regular expression"""
        if re.search(needle_re, haystack, re.DOTALL):
            raise AssertionError('pattern "%s" found in "%s"'
                    % (needle_re, haystack))

    def appendFile(self, path, content):
        file = open(path, "ab")
        file.write(content)
        file.close()