summaryrefslogtreecommitdiff
path: root/tests/functional-tests/miner/miner_testcase.py
blob: 1c9d748dee8d3b6b895e7cc25e6bda39098da20d (plain)
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
121
122
123
124
125
# Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


from gi.repository import GLib

import shutil
import os
import unittest

from itertools import chain

import common.helpers

from common.utils import configuration as cfg


MINER_TMP_DIR = cfg.TEST_MONITORED_TMP_DIR


def path(filename):
    return os.path.join(MINER_TMP_DIR, filename)


def uri(filename):
    return "file://" + os.path.join(MINER_TMP_DIR, filename)


DEFAULT_TEXT = "Some stupid content, to have a test file"

index_dirs = [os.path.join(MINER_TMP_DIR, "test-monitored")]
CONF_OPTIONS = {
    cfg.DCONF_MINER_SCHEMA: {
        'index-recursive-directories': GLib.Variant.new_strv(index_dirs),
        'index-single-directories': GLib.Variant.new_strv([]),
        'index-optical-discs': GLib.Variant.new_boolean(False),
        'index-removable-devices': GLib.Variant.new_boolean(False),
        'throttle': GLib.Variant.new_int32(5),
    }
}


class MinerTestCase (unittest.TestCase):
    def prepare_directories(self):
        #
        #     ~/test-monitored/
        #                     /file1.txt
        #                     /dir1/
        #                          /file2.txt
        #                          /dir2/
        #                               /file3.txt
        #
        #
        #     ~/test-no-monitored/
        #                        /file0.txt
        #

        monitored_files = [
            'test-monitored/file1.txt',
            'test-monitored/dir1/file2.txt',
            'test-monitored/dir1/dir2/file3.txt'
        ]

        unmonitored_files = [
            'test-no-monitored/file0.txt'
        ]

        def ensure_dir_exists(dirname):
            if not os.path.exists(dirname):
                os.makedirs(dirname)

        for tf in chain(monitored_files, unmonitored_files):
            testfile = path(tf)
            ensure_dir_exists(os.path.dirname(testfile))
            with open(testfile, 'w') as f:
                f.write(DEFAULT_TEXT)

        for tf in monitored_files:
            self.tracker.await_resource_inserted(
                'nfo:TextDocument', url=uri(tf))

    def setUp(self):
        for d in ['test-monitored', 'test-no-monitored']:
            dirname = path(d)
            if os.path.exists(dirname):
                shutil.rmtree(dirname)
            os.makedirs(dirname)

        self.sandbox = common.sandbox.TrackerSandbox(CONF_OPTIONS)
        self.store = common.helpers.StoreHelper()
        self.store.start(self.sandbox)

        self.extractor = common.helpers.ExtractorHelper()
        self.extractor.start(self.sandbox)

        self.miner_fs = common.helpers.MinerFsHelper()
        self.miner_fs.start(self.sandbox)

        self.tracker = self.store

        try:
            self.prepare_directories()
            self.tracker.reset_graph_updates_tracking()
        except Exception:
            self.tearDown()
            raise

    def tearDown(self):
        self.extractor.stop()
        self.miner_fs.stop()
        self.store.stop()