basecli_test.py 2.43 KB
Newer Older
Laurent Bachelier's avatar
Laurent Bachelier committed
1
from unittest import TestCase
Romain Bignon's avatar
Romain Bignon committed
2 3 4
from assnet.cli import CLI
from assnet.storage import Storage
from assnet.users import User
Laurent Bachelier's avatar
Laurent Bachelier committed
5
from tempfile import mkdtemp
6 7
import os
import re
Laurent Bachelier's avatar
Laurent Bachelier committed
8
import shutil
9 10
import sys
from StringIO import StringIO
Laurent Bachelier's avatar
Laurent Bachelier committed
11

Laurent Bachelier's avatar
Laurent Bachelier committed
12

Laurent Bachelier's avatar
Laurent Bachelier committed
13
class BaseCLITest(TestCase):
Laurent Bachelier's avatar
Laurent Bachelier committed
14
    def setUp(self):
Romain Bignon's avatar
Romain Bignon committed
15
        self.root = mkdtemp(prefix='assnet_test_root')
Laurent Bachelier's avatar
Laurent Bachelier committed
16 17 18 19 20 21
        self.app = CLI(self.root)

    def tearDown(self):
        if self.root:
            shutil.rmtree(self.root)

Laurent Bachelier's avatar
Laurent Bachelier committed
22
    def beginCapture(self, with_stderr=False):
23 24 25
        self.stdout = sys.stdout
        # begin capture
        sys.stdout = StringIO()
26 27 28 29 30
        if with_stderr:
            self.stderr = sys.stderr
            sys.stderr = sys.stdout
        elif not hasattr(self, 'stderr'):
            self.stderr = None
31 32

    def endCapture(self):
33
        captured = sys.stdout.getvalue()
34
        # end capture
35 36 37 38 39 40 41
        if self.stdout is not None:
            sys.stdout = self.stdout
            self.stdout = None
        if self.stderr is not None:
            sys.stderr = self.stderr
            self.stderr = None
        return captured
42

Laurent Bachelier's avatar
Laurent Bachelier committed
43
    def test_init(self):
44
        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
45
        assert self.app.main(['assnet_test', 'init']) in (0, None)
46
        output = self.endCapture()
Romain Bignon's avatar
Romain Bignon committed
47
        assert output.strip() == "assnet working directory created."
Laurent Bachelier's avatar
Laurent Bachelier committed
48

49
        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
50
        assert self.app.main(['assnet_test', 'tree']) in (0, None)
51
        output = self.endCapture()
52
        assert re.match(re.escape(r'-/') + r'\s+' + re.escape(r'all(irl-)'), output, re.S)
Romain Bignon's avatar
Romain Bignon committed
53
        assert re.match(".+" + re.escape(r'|-.assnet/') + r'\s+' + re.escape(r'all(----)'), output, re.S)
54 55 56

    def test_findRoot(self):
        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
57
        assert self.app.main(['assnet_test', 'init']) in (0, None)
58 59 60 61 62
        self.endCapture()

        subdir = os.path.join(self.root, "penguins")
        os.mkdir(subdir)
        subapp = CLI(subdir)
Romain Bignon's avatar
Romain Bignon committed
63
        assert subapp.main(['assnet_test', 'tree']) in (0, None)
64 65 66

    def test_genKey(self):
        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
67
        assert self.app.main(['assnet_test', 'init']) in (0, None)
68 69 70 71 72 73 74 75 76
        self.endCapture()

        storage = Storage.lookup(self.root)
        user = User(storage, 'penguin')
        user.realname = 'Penguin'
        user.password = 'monkey1'
        user.save()

        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
77
        assert self.app.main(['assnet_test', 'contacts', 'genkey', 'penguin']) in (0, None)
78 79
        output = self.endCapture()
        assert 'Key of user penguin set' in output