cleanup_test.py 3.65 KB
Newer Older
1 2
# -*- coding: utf-8 -*-

3
from unittest import TestCase
Romain Bignon's avatar
Romain Bignon committed
4 5
from assnet.cli import CLI
from assnet.storage import Storage
6 7 8 9 10 11 12 13 14
from tempfile import mkdtemp
import shutil
import os
import sys
from StringIO import StringIO


class CleanupTest(TestCase):
    def setUp(self):
Romain Bignon's avatar
Romain Bignon committed
15
        self.root = mkdtemp(prefix='assnet_test_root')
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
        self.storage = Storage.create(self.root)
        self.app = CLI(self.root)

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

    def beginCapture(self, with_stderr=False):
        self.stdout = sys.stdout
        # begin capture
        sys.stdout = StringIO()
        if with_stderr:
            self.stderr = sys.stderr
            sys.stderr = sys.stdout
        elif not hasattr(self, 'stderr'):
            self.stderr = None

    def endCapture(self):
        captured = sys.stdout.getvalue()
        # end capture
        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

    def test_simpleRun(self):
        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
46
        assert self.app.main(['assnet_test', 'cleanup']) in (0, None)
47 48 49 50
        output = self.endCapture()
        assert output.strip() == ''

        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
51
        assert self.app.main(['assnet_test', 'cleanup', '--gc']) in (0, None)
52 53 54 55 56 57 58 59
        output = self.endCapture()
        assert output.strip() == ''

    def test_invalidPaths(self):
        with open(os.path.join(self.storage.path, 'files', 'hello'), 'w') as f:
            f.write('')

        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
60
        assert self.app.main(['assnet_test', 'cleanup']) in (0, None)
61 62 63 64
        output = self.endCapture()
        assert output.strip() == 'files/hello has an invalid path: None.'

        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
65
        assert self.app.main(['assnet_test', 'cleanup', '--gc']) in (0, None)
66 67 68 69 70 71
        output = self.endCapture()
        assert output.strip() == 'files/hello has an invalid path: None.\nRemoved files/hello.'
        with open(os.path.join(self.storage.path, 'files', 'c93c3312483174a3170ebe7395612c404a0620d0'), 'w') as f:
            f.write('[info]\npath = /hello')

        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
72
        assert self.app.main(['assnet_test', 'cleanup']) in (0, None)
73 74 75 76
        output = self.endCapture()
        assert output.strip() == 'files/c93c3312483174a3170ebe7395612c404a0620d0 has an invalid path: /hello.'

        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
77
        assert self.app.main(['assnet_test', 'cleanup', '--gc']) in (0, None)
78 79
        output = self.endCapture()
        assert output.strip() == 'files/c93c3312483174a3170ebe7395612c404a0620d0 has an invalid path: /hello.\nRemoved files/c93c3312483174a3170ebe7395612c404a0620d0.'
80 81 82 83 84 85

    def test_oldStorageBug(self):
        with open(os.path.join(self.storage.path, 'files', '3be00feb429b32b7705b689475e3ab8bdf16733f'), 'w') as f:
            f.write('[info]\npath = /hello\nview = None')

        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
86
        assert self.app.main(['assnet_test', 'cleanup', '--gc']) in (0, None)
87
        output = self.endCapture()
88
        assert output.strip() == 'files/3be00feb429b32b7705b689475e3ab8bdf16733f: fixed empty view.'
89 90 91 92

        f = self.storage.get_file('/hello')
        assert f.data['info'].get('view') is None
        assert f.view is None
93 94 95 96 97 98

    def test_unicode(self):
        with open(os.path.join(self.storage.path, 'files', 'eed3d4c0aac6375a40412d0c4e689d3da85c44d0'), 'w') as f:
            f.write('[info]\npath = /héy')

        self.beginCapture()
Romain Bignon's avatar
Romain Bignon committed
99
        assert self.app.main(['assnet_test', 'cleanup', '--gc']) in (0, None)
100 101
        output = self.endCapture()
        assert output.strip() == ''