~adam-disc0tech/ubuntu-autopilot-tests/fileroller

« back to all changes in this revision

Viewing changes to ubuntu_autopilot_tests/fileroller/test_fileroller.py

  • Committer: Adam Smith
  • Date: 2014-03-14 15:15:31 UTC
  • Revision ID: adam@disc0tech.com-20140314151531-lw2d438b6xpigx9f
Implemented fake home directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
from time import sleep
7
7
import logging
8
8
import os
 
9
try:
 
10
    from unittest import mock
 
11
except ImportError:
 
12
    import mock
9
13
import shutil
10
 
import subprocess
11
14
import tempfile
12
15
 
13
16
logger = logging.getLogger('__name__')
19
22
    def setUp(self):
20
23
        '''Set-up method'''
21
24
        super(FileRollerTests, self).setUp()
 
25
        #mock out the home dir
 
26
        self.home_dir = self._patch_home()
 
27
 
 
28
        #launch the app
22
29
        self.app = self.launch_test_application('file-roller')
23
30
 
24
31
        # only instantiate these once for multiple tests
38
45
                                                         name=u'New')
39
46
 
40
47
        #create symbolic link for fast test content location
41
 
        link_loc = os.getenv("HOME") + '/__000'
42
 
        link_cmd = 'ln -s /usr/share/example-content '
43
 
        p = subprocess.Popen(link_cmd + link_loc, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
44
 
        self.assertThat(p.wait(), Equals(0))
45
 
        self.addCleanup(os.remove, link_loc)
 
48
        src_dir = '/usr/share/example-content/Ubuntu_Free_Culture_Showcase/'
 
49
        files = os.listdir(src_dir)
 
50
        for f in files:
 
51
            shutil.copyfile(src_dir + str(f), os.getenv("HOME") + '/' + str(f))
46
52
 
47
53
    def test_create_archive(self):
48
54
        '''Creates an archive and adds sample data,
129
135
        self.add_files_dialog = self.app.select_single('FrFileSelectorDialog')
130
136
        self.assertThat(self.add_files_dialog.title,
131
137
                        Eventually(Equals('Add Files')))
132
 
 
133
138
        # This is a horrible hacky workaround to add sample
134
139
        # files from /usr/share/example-content/*
135
140
        # Need to find a better workaround :S Location GtkEntry
141
146
        self.keyboard.press_and_release("Alt+l")
142
147
        self.keyboard.press_and_release("Tab")
143
148
        self.keyboard.press_and_release("Tab")
144
 
        for x in range(0, 10):
145
 
            self.keyboard.press_and_release("Up")
146
 
 
147
 
        self.keyboard.press_and_release("Down")
148
 
        self.keyboard.press_and_release("Enter")
149
 
 
150
 
        #then select the sample content link
151
 
        self.keyboard.press_and_release("Alt+l")
152
 
        self.keyboard.press_and_release("Tab")
153
 
        self.keyboard.press_and_release("Tab")
154
 
        self.keyboard.press_and_release("Tab")
155
 
        self.keyboard.press_and_release("Right")
156
 
        self.keyboard.press_and_release("Enter")
157
 
        self.keyboard.press_and_release("Left")
158
 
        self.keyboard.press_and_release("Enter")
 
149
        self.keyboard.press_and_release("Tab")
 
150
 
 
151
        self.keyboard.press_and_release("Space")
 
152
        self.keyboard.press_and_release("Down")
 
153
        self.keyboard.press_and_release("Down")
 
154
        self.keyboard.press_and_release("Space")
159
155
 
160
156
        # We must be able to click the '_Add' button
161
157
        self.add_folder_button = self.app.select_single('GtkLabel',
201
197
 
202
198
        self.keyboard.press_and_release("Alt+l")
203
199
        self.keyboard.type(tempArchive)
 
200
        self.keyboard.press_and_release('Enter')
204
201
 
205
202
        self.extract_archive_button = self.app.select_single(
206
203
            'GtkLabel', label=u'_Extract')
225
222
 
226
223
        #check the files have actually been extracted
227
224
        self.assertThat(tempArchive, DirExists())
228
 
        srcdir = '/Ubuntu_Free_Culture_Showcase'
229
 
        self.assertThat(tempArchive + srcdir, DirExists())
230
 
        self.assertThat(tempArchive + srcdir + '/How fast.ogg', FileExists())
231
 
        self.assertThat(tempArchive + srcdir + '/Josh Woodward - Swansong.ogg', FileExists())
 
225
        self.assertThat(tempArchive + '/' + '/How fast.ogg', FileExists())
 
226
        self.assertThat(tempArchive + '/' + '/Josh Woodward - Swansong.ogg', FileExists())
232
227
 
233
228
        # This sleep is needed as we can't introspect
234
229
        # nautilus when it shows the files
277
272
        extract_path = os.path.join('/tmp/' + fileName)
278
273
        self.addCleanup(shutil.rmtree, extract_path)
279
274
 
 
275
    def _save_home(self):
 
276
            logger.debug('Saving HOME')
 
277
            home_dir = os.environ['HOME']
 
278
            backup_list = ('Music', )  # '.cache/mediascanner')
 
279
            backup_path = [os.path.join(home_dir, i) for i in backup_list]
 
280
            backups = [(i, '%s.bak' % i) for i in backup_path if os.path.exists(i)]
 
281
            for b in backups:
 
282
                logger.debug('backing up %s to %s' % b)
 
283
                try:
 
284
                    shutil.rmtree(b[1])
 
285
                except:
 
286
                    pass
 
287
                shutil.move(b[0], b[1])
 
288
                #self.addCleanup(shutil.move(b[1], b[0]))
 
289
            return home_dir
 
290
 
 
291
    def _patch_home(self):
 
292
        #make a temp dir
 
293
        temp_dir = tempfile.mkdtemp()
 
294
        #delete it, and recreate it to the length
 
295
        #required so our patching the db works
 
296
        #require a length of 25
 
297
        shutil.rmtree(temp_dir)
 
298
        temp_dir = temp_dir.ljust(25, 'X')
 
299
        os.mkdir(temp_dir)
 
300
        logger.debug("Created fake home directory " + temp_dir)
 
301
        self.addCleanup(shutil.rmtree, temp_dir)
 
302
        #if the Xauthority file is in home directory
 
303
        #make sure we copy it to temp home, otherwise do nothing
 
304
        xauth = os.path.expanduser(os.path.join('~', '.Xauthority'))
 
305
        if os.path.isfile(xauth):
 
306
            logger.debug("Copying .Xauthority to fake home " + temp_dir)
 
307
            shutil.copyfile(
 
308
                os.path.expanduser(os.path.join('~', '.Xauthority')),
 
309
                os.path.join(temp_dir, '.Xauthority'))
 
310
        patcher = mock.patch.dict('os.environ', {'HOME': temp_dir})
 
311
        patcher.start()
 
312
        logger.debug("Patched home to fake home directory " + temp_dir)
 
313
        self.addCleanup(patcher.stop)
 
314
        return temp_dir
 
315
 
280
316
 
281
317
#hardcoded global archiveName
282
318
def _tempArchiveName():