From 7257dd3ec0b3ebf112437be52b12d2c29db82bf5 Mon Sep 17 00:00:00 2001 From: Xavier Claessens <xavier.claessens@collabora.com> Date: Wed, 23 Sep 2015 14:15:40 -0400 Subject: [PATCH] tracker: factor out a base class for python tests Reviewers: smcv, pwith Differential Revision: https://phabricator.apertis.org/D542 --- apertis_tests_lib/__init__.py | 37 +++++++++++++++++++++++++ apertis_tests_lib/tracker.py | 32 +-------------------- tracker/automated/test-tracker.py | 7 +++-- tracker/manual/test-removable-device.py | 5 ++-- 4 files changed, 45 insertions(+), 36 deletions(-) diff --git a/apertis_tests_lib/__init__.py b/apertis_tests_lib/__init__.py index 9e2f0e7..109589c 100644 --- a/apertis_tests_lib/__init__.py +++ b/apertis_tests_lib/__init__.py @@ -7,3 +7,40 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. +import unittest +import shutil +import os + +MEDIADIR = '/usr/lib/apertis-tests/resources/media' + +# define this here to make lint happy with too long lines +LONG_JPEG_NAME = '320px-European_Common_Frog_Rana_temporaria.jpg' + + +class ApertisTest(unittest.TestCase): + def copy_medias(self, dst): + """ + Copy media resources from apertis-tests to @dst with per media type + subdirectory corresponding to the default subdirectories in $HOME. + """ + print("ApertisTest: copying medias to " + dst) + self.__copytree(MEDIADIR + '/audio', dst + '/Music') + self.__copytree(MEDIADIR + '/documents', dst + '/Documents') + self.__copytree(MEDIADIR + '/images', dst + '/Pictures') + self.__copytree(MEDIADIR + '/playlists', dst + '/Music') + self.__copytree(MEDIADIR + '/videos', dst + '/Videos') + + def __copytree(self, root_src_dir, root_dst_dir): + # This is our own version of shutil.copytree. The difference is that + # if the destination directory already exists it adds files into it. If + # files already exists they are overwritten. + for src_dir, dirs, files in os.walk(root_src_dir): + dst_dir = src_dir.replace(root_src_dir, root_dst_dir) + if not os.path.exists(dst_dir): + os.mkdir(dst_dir) + for file_ in files: + src_file = os.path.join(src_dir, file_) + dst_file = os.path.join(dst_dir, file_) + if os.path.exists(dst_file): + os.remove(dst_file) + shutil.copy2(src_file, dst_dir) diff --git a/apertis_tests_lib/tracker.py b/apertis_tests_lib/tracker.py index b8ebce5..4913393 100644 --- a/apertis_tests_lib/tracker.py +++ b/apertis_tests_lib/tracker.py @@ -15,10 +15,7 @@ from gi.repository import GLib from gi.repository import TrackerControl from gi.repository import Tracker -MEDIADIR = '/usr/lib/apertis-tests/resources/media' - -# define this here to make lint happy with too long lines -LONG_JPEG_NAME = '320px-European_Common_Frog_Rana_temporaria.jpg' +from . import LONG_JPEG_NAME class TrackerIndexer(): @@ -33,33 +30,6 @@ class TrackerIndexer(): self.loop = GLib.MainLoop.new(None, False) - def copy_medias(self, dst): - """ - Copy media resources from apertis-tests to @dst with per media type - subdirectory corresponding to the default subdirectories in $HOME. - """ - print("TrackerIndexer: copying medias to " + dst) - self.copytree(MEDIADIR + '/audio', dst + '/Music') - self.copytree(MEDIADIR + '/documents', dst + '/Documents') - self.copytree(MEDIADIR + '/images', dst + '/Pictures') - self.copytree(MEDIADIR + '/playlists', dst + '/Music') - self.copytree(MEDIADIR + '/videos', dst + '/Videos') - - def copytree(self, root_src_dir, root_dst_dir): - # This is our own version of shutil.copytree. The difference is that - # if the destination directory already exists it adds files into it. If - # files already exists they are overwritten. - for src_dir, dirs, files in os.walk(root_src_dir): - dst_dir = src_dir.replace(root_src_dir, root_dst_dir) - if not os.path.exists(dst_dir): - os.mkdir(dst_dir) - for file_ in files: - src_file = os.path.join(src_dir, file_) - dst_file = os.path.join(dst_dir, file_) - if os.path.exists(dst_file): - os.remove(dst_file) - shutil.copy2(src_file, dst_dir) - def miner_progress_cb(self, manager, miner, status, progress, remaining_time): # Ignore signal if status didn't change diff --git a/tracker/automated/test-tracker.py b/tracker/automated/test-tracker.py index f823da4..44e4777 100755 --- a/tracker/automated/test-tracker.py +++ b/tracker/automated/test-tracker.py @@ -21,15 +21,16 @@ from gi.repository import Grl sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) from apertis_tests_lib.tracker import TrackerIndexer -from apertis_tests_lib.tracker import LONG_JPEG_NAME +from apertis_tests_lib import LONG_JPEG_NAME +from apertis_tests_lib import ApertisTest -class TrackerTest(unittest.TestCase): +class TrackerTest(ApertisTest): def setUp(self): self.loop = GLib.MainLoop.new(None, False) self.homedir = os.path.expanduser("~") self.indexer = TrackerIndexer() - self.indexer.copy_medias(self.homedir) + self.copy_medias(self.homedir) # Monitor thumbnail creation to know when it's done. The DBus API # doesn't have a method to query the initial state but we can be diff --git a/tracker/manual/test-removable-device.py b/tracker/manual/test-removable-device.py index 5f412e3..03ba80a 100755 --- a/tracker/manual/test-removable-device.py +++ b/tracker/manual/test-removable-device.py @@ -19,9 +19,10 @@ from gi.repository import Gio sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) from apertis_tests_lib.tracker import TrackerIndexer +from apertis_tests_lib import ApertisTest -class TestRemovableDevice(unittest.TestCase): +class TestRemovableDevice(ApertisTest): def setUp(self): self.loop = GLib.MainLoop.new(None, False) self.monitor = Gio.VolumeMonitor.get() @@ -41,7 +42,7 @@ class TestRemovableDevice(unittest.TestCase): # Copy our medias and start indexing self.indexer = TrackerIndexer() - self.indexer.copy_medias(self.path) + self.copy_medias(self.path) self.indexer.start() self.indexer.assert_all_indexed(self.path) -- GitLab