Commit b0a01c59 authored by Gabriel Monnerat's avatar Gabriel Monnerat

add functions to create a folder to run the application and save temporary files

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@37867 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent be2c867b
...@@ -27,9 +27,9 @@ ...@@ -27,9 +27,9 @@
############################################################################## ##############################################################################
import unittest import unittest
from sys import path, argv import sys
from ConfigParser import ConfigParser from ConfigParser import ConfigParser
from os.path import join, exists, dirname from os import path, mkdir
from os import environ, putenv from os import environ, putenv
from cloudooo.application.xvfb import xvfb from cloudooo.application.xvfb import xvfb
from cloudooo.application.openoffice import openoffice from cloudooo.application.openoffice import openoffice
...@@ -37,7 +37,31 @@ from cloudooo.utils import waitStartDaemon ...@@ -37,7 +37,31 @@ from cloudooo.utils import waitStartDaemon
from cloudooo.mimemapper import mimemapper from cloudooo.mimemapper import mimemapper
config = ConfigParser() config = ConfigParser()
testcase_path = dirname(__file__) testcase_path = path.dirname(__file__)
def check_folder(path_dir_run_cloudooo, tmp_dir_path):
if not path.exists(path_dir_run_cloudooo):
mkdir(path_dir_run_cloudooo)
if not path.exists(tmp_dir_path):
mkdir(tmp_dir_path)
def extract_cloudooo_script():
from sys import executable
import pkg_resources
cloudooo_resources = pkg_resources.get_distribution('cloudooo')
console_scripts = cloudooo_resources.get_entry_map()['console_scripts']
openoffice_tester_bin_path = path.join(path.dirname(executable),
console_scripts["openoffice_tester.py"].name)
unomimemapper_bin_path = path.join(path.dirname(executable),
console_scripts["unomimemapper.py"].name)
unoconverter_bin_path = path.join(path.dirname(executable),
console_scripts["unoconverter.py"].name)
return dict(unoconverter_bin=unoconverter_bin_path,
unomimemapper_bin=unomimemapper_bin_path,
openoffice_tester_bin=openoffice_tester_bin_path)
def make_suite(test_case): def make_suite(test_case):
"""Function is used to run all tests together""" """Function is used to run all tests together"""
...@@ -50,8 +74,8 @@ def loadConfig(path): ...@@ -50,8 +74,8 @@ def loadConfig(path):
def startFakeEnvironment(start_openoffice=True, conf_path=None): def startFakeEnvironment(start_openoffice=True, conf_path=None):
"""Create a fake environment""" """Create a fake environment"""
if not conf_path and len(argv) >=1: if not conf_path and len(sys.argv) >=1:
conf_path = argv[1] conf_path = sys.argv[1]
loadConfig(conf_path) loadConfig(conf_path)
uno_path = config.get("app:main", "uno_path") uno_path = config.get("app:main", "uno_path")
path_dir_run_cloudooo = config.get("app:main", "path_dir_run_cloudooo") path_dir_run_cloudooo = config.get("app:main", "path_dir_run_cloudooo")
...@@ -60,8 +84,8 @@ def startFakeEnvironment(start_openoffice=True, conf_path=None): ...@@ -60,8 +84,8 @@ def startFakeEnvironment(start_openoffice=True, conf_path=None):
hostname = config.get("server:main", "host") hostname = config.get("server:main", "host")
openoffice_port = int(config.get("app:main", "openoffice_port")) openoffice_port = int(config.get("app:main", "openoffice_port"))
office_bin_path = config.get("app:main", "office_bin_path") office_bin_path = config.get("app:main", "office_bin_path")
openoffice_tester_bin = config.get("app:main", "openoffice_tester_bin") tmp_dir = path.join(path_dir_run_cloudooo, 'tmp')
check_folder(path_dir_run_cloudooo, tmp_dir)
if not environ.get('uno_path'): if not environ.get('uno_path'):
environ['uno_path'] = uno_path environ['uno_path'] = uno_path
...@@ -69,11 +93,11 @@ def startFakeEnvironment(start_openoffice=True, conf_path=None): ...@@ -69,11 +93,11 @@ def startFakeEnvironment(start_openoffice=True, conf_path=None):
if not environ.get('office_bin_path'): if not environ.get('office_bin_path'):
environ['office_bin_path'] = office_bin_path environ['office_bin_path'] = office_bin_path
if not uno_path in path: if uno_path not in sys.path:
path.append(uno_path) sys.path.append(uno_path)
fundamentalrc_file = '%s/fundamentalrc' % office_bin_path fundamentalrc_file = '%s/fundamentalrc' % office_bin_path
if exists(fundamentalrc_file) and \ if path.exists(fundamentalrc_file) and \
not environ.has_key('URE_BOOTSTRAP'): not environ.has_key('URE_BOOTSTRAP'):
putenv('URE_BOOTSTRAP','vnd.sun.star.pathname:%s' % fundamentalrc_file) putenv('URE_BOOTSTRAP','vnd.sun.star.pathname:%s' % fundamentalrc_file)
...@@ -84,6 +108,11 @@ def startFakeEnvironment(start_openoffice=True, conf_path=None): ...@@ -84,6 +108,11 @@ def startFakeEnvironment(start_openoffice=True, conf_path=None):
virtual_screen='1') virtual_screen='1')
xvfb.start() xvfb.start()
waitStartDaemon(xvfb, 10) waitStartDaemon(xvfb, 10)
cloudooo_script_dict = extract_cloudooo_script()
openoffice_tester_bin = cloudooo_script_dict["openoffice_tester_bin"]
unomimemapper_bin = cloudooo_script_dict["unomimemapper_bin"]
if start_openoffice: if start_openoffice:
openoffice.loadSettings(hostname, openoffice.loadSettings(hostname,
openoffice_port, openoffice_port,
...@@ -95,8 +124,7 @@ def startFakeEnvironment(start_openoffice=True, conf_path=None): ...@@ -95,8 +124,7 @@ def startFakeEnvironment(start_openoffice=True, conf_path=None):
openoffice.start() openoffice.start()
openoffice.acquire() openoffice.acquire()
hostname, port = openoffice.getAddress() hostname, port = openoffice.getAddress()
kw = dict(python_path=config.get("app:main", "python_path"), kw = dict(unomimemapper_bin=unomimemapper_bin,
unomimemapper_bin=config.get("app:main", "unomimemapper_bin"),
uno_path=config.get("app:main", "uno_path"), uno_path=config.get("app:main", "uno_path"),
office_bin_path=config.get("app:main", "office_bin_path")) office_bin_path=config.get("app:main", "office_bin_path"))
if not mimemapper.isLoaded(): if not mimemapper.isLoaded():
...@@ -119,19 +147,23 @@ class cloudoooTestCase(unittest.TestCase): ...@@ -119,19 +147,23 @@ class cloudoooTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
"""Creates a environment to run the tests. Is called always before the """Creates a environment to run the tests. Is called always before the
tests.""" tests."""
cloudooo_script_dict = extract_cloudooo_script()
self.openoffice_tester_bin = cloudooo_script_dict["openoffice_tester_bin"]
self.unomimemapper_bin = cloudooo_script_dict["unomimemapper_bin"]
self.unoconverter_bin = cloudooo_script_dict["unoconverter_bin"]
self.hostname = config.get("server:main", "host") self.hostname = config.get("server:main", "host")
self.cloudooo_port = config.get("server:main", "port") self.cloudooo_port = config.get("server:main", "port")
self.openoffice_port = config.get("app:main", "openoffice_port") self.openoffice_port = config.get("app:main", "openoffice_port")
self.office_bin_path = config.get("app:main", "office_bin_path") self.office_bin_path = config.get("app:main", "office_bin_path")
self.python_path = sys.executable
self.path_dir_run_cloudooo = config.get("app:main", "path_dir_run_cloudooo") self.path_dir_run_cloudooo = config.get("app:main", "path_dir_run_cloudooo")
self.tmp_url = join(self.path_dir_run_cloudooo, "tmp") self.tmp_url = path.join(self.path_dir_run_cloudooo, "tmp")
check_folder(self.path_dir_run_cloudooo, self.tmp_url)
self.uno_path = config.get("app:main", "uno_path") self.uno_path = config.get("app:main", "uno_path")
self.unomimemapper_bin = config.get("app:main", "unomimemapper_bin")
self.unoconverter_bin = config.get("app:main", "unoconverter_bin")
self.python_path = config.get("app:main", "python_path")
self.virtual_display_id = config.get("app:main", "virtual_display_id") self.virtual_display_id = config.get("app:main", "virtual_display_id")
self.virtual_display_port_int = config.get("app:main", "virtual_display_port") self.virtual_display_port_int = config.get("app:main", "virtual_display_port")
self.openoffice_tester_bin = config.get("app:main", "openoffice_tester_bin")
self.afterSetUp() self.afterSetUp()
def afterSetUp(self): def afterSetUp(self):
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
############################################################################## ##############################################################################
import unittest import unittest
from os import path
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from base64 import encodestring, decodestring from base64 import encodestring, decodestring
from cloudoooTestCase import cloudoooTestCase from cloudoooTestCase import cloudoooTestCase
...@@ -43,7 +44,7 @@ class TestOOHandler(cloudoooTestCase): ...@@ -43,7 +44,7 @@ class TestOOHandler(cloudoooTestCase):
def _assert_document_output(self, document_output_url, msg): def _assert_document_output(self, document_output_url, msg):
"""Check if the document was created correctly""" """Check if the document was created correctly"""
stdout, stderr = Popen("file %s" % document_output_url, stdout, stderr = Popen("file -b %s" % document_output_url,
shell=True, shell=True,
stdout=PIPE).communicate() stdout=PIPE).communicate()
self.assertEquals(msg in stdout, self.assertEquals(msg in stdout,
...@@ -63,7 +64,7 @@ class TestOOHandler(cloudoooTestCase): ...@@ -63,7 +64,7 @@ class TestOOHandler(cloudoooTestCase):
'odt', 'odt',
**self.kw) **self.kw)
doc_exported = handler.convert("doc") doc_exported = handler.convert("doc")
document_output_url = "output/testExport.doc" document_output_url = path.join(self.tmp_url, "testExport.doc")
self._save_document(document_output_url, doc_exported) self._save_document(document_output_url, doc_exported)
msg = 'Microsoft Office Document' msg = 'Microsoft Office Document'
self._assert_document_output(document_output_url, msg) self._assert_document_output(document_output_url, msg)
...@@ -76,9 +77,9 @@ class TestOOHandler(cloudoooTestCase): ...@@ -76,9 +77,9 @@ class TestOOHandler(cloudoooTestCase):
'doc', 'doc',
**self.kw) **self.kw)
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
document_output_url = "output/testConvert.odt" document_output_url = path.join(self.tmp_url, "testConvert.odt")
self._save_document(document_output_url, doc_exported) self._save_document(document_output_url, doc_exported)
msg = 'output/testConvert.odt: OpenDocument Text\n' msg = 'OpenDocument Text\n'
self._assert_document_output(document_output_url, msg) self._assert_document_output(document_output_url, msg)
def testGetMetadata(self): def testGetMetadata(self):
...@@ -121,9 +122,9 @@ class TestOOHandler(cloudoooTestCase): ...@@ -121,9 +122,9 @@ class TestOOHandler(cloudoooTestCase):
'doc', 'doc',
**self.kw) **self.kw)
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
document_output_url = "output/testConvert.odt" document_output_url = path.join(self.tmp_url, "testConvert.odt")
self._save_document(document_output_url, doc_exported) self._save_document(document_output_url, doc_exported)
msg = 'output/testConvert.odt: OpenDocument Text\n' msg = 'OpenDocument Text\n'
self._assert_document_output(document_output_url, msg) self._assert_document_output(document_output_url, msg)
def testGetMetadataWithOpenOfficeStopped(self): def testGetMetadataWithOpenOfficeStopped(self):
......
...@@ -93,8 +93,7 @@ class TestUnoConverter(cloudoooTestCase): ...@@ -93,8 +93,7 @@ class TestUnoConverter(cloudoooTestCase):
"--document_url=%s" % self.document.getUrl(), "--document_url=%s" % self.document.getUrl(),
"--destination_format=%s" % "doc", "--destination_format=%s" % "doc",
"--source_format=%s" % "odt", "--source_format=%s" % "odt",
"--unomimemapper_bin=%s" % self.unomimemapper_bin, "--unomimemapper_bin=%s" % self.unomimemapper_bin]
"--python_path=%s" % self.python_path]
stdout, stderr = Popen(' '.join(command), shell=True, stdout, stderr = Popen(' '.join(command), shell=True,
stdout=PIPE, stderr=PIPE).communicate() stdout=PIPE, stderr=PIPE).communicate()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment