[GRASS-SVN] r61418 - grass/trunk/lib/python/gunittest
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Jul 26 21:02:34 PDT 2014
Author: wenzeslaus
Date: 2014-07-26 21:02:34 -0700 (Sat, 26 Jul 2014)
New Revision: 61418
Modified:
grass/trunk/lib/python/gunittest/invoker.py
grass/trunk/lib/python/gunittest/main.py
grass/trunk/lib/python/gunittest/reporters.py
Log:
gunittest: functions to remove full paths from standard output and other files
Modified: grass/trunk/lib/python/gunittest/invoker.py
===================================================================
--- grass/trunk/lib/python/gunittest/invoker.py 2014-07-27 01:53:12 UTC (rev 61417)
+++ grass/trunk/lib/python/gunittest/invoker.py 2014-07-27 04:02:34 UTC (rev 61418)
@@ -25,7 +25,8 @@
from .loader import GrassTestLoader, discover_modules
from .reporters import (GrassTestFilesMultiReporter,
GrassTestFilesTextReporter, GrassTestFilesHtmlReporter,
- TestsuiteDirReporter, get_svn_path_authors)
+ TestsuiteDirReporter, get_svn_path_authors,
+ NoopFileAnonymizer)
from .utils import silent_rmtree, ensure_dir
import grass.script.setup as gsetup
@@ -87,7 +88,7 @@
# we can also save only failed tests, or generate only if assert fails
def __init__(self, start_dir,
clean_mapsets=True, clean_outputs=True, clean_before=True,
- testsuite_dir='testsuite'):
+ testsuite_dir='testsuite', file_anonymizer=None):
"""
:param bool clean_mapsets: if the mapsets should be removed
@@ -106,6 +107,10 @@
self.reporter = None
self.testsuite_dirs = None
+ if file_anonymizer is None:
+ self._file_anonymizer = NoopFileAnonymizer()
+ else:
+ self._file_anonymizer = file_anonymizer
def _create_mapset(self, gisdbase, location, module):
"""Create mapset according to informations in module.
@@ -164,6 +169,8 @@
returncode = p.wait()
stdout.close()
stderr.close()
+ self._file_anonymizer.anonymize([stdout_path, stderr_path])
+
test_summary = update_keyval_file(
os.path.join(cwd, 'test_keyvalue_result.txt'),
module=module, returncode=returncode)
@@ -186,7 +193,8 @@
self.reporter = GrassTestFilesMultiReporter(
reporters=[
GrassTestFilesTextReporter(stream=sys.stderr),
- GrassTestFilesHtmlReporter(),
+ GrassTestFilesHtmlReporter(
+ file_anonymizer=self._file_anonymizer),
])
self.testsuite_dirs = collections.defaultdict(list) # reset list of dirs each time
# TODO: move constants out of loader class or even module
Modified: grass/trunk/lib/python/gunittest/main.py
===================================================================
--- grass/trunk/lib/python/gunittest/main.py 2014-07-27 01:53:12 UTC (rev 61417)
+++ grass/trunk/lib/python/gunittest/main.py 2014-07-27 04:02:34 UTC (rev 61418)
@@ -22,6 +22,7 @@
TextTestResult, KeyValueTestResult)
from .invoker import GrassTestFilesInvoker
from .utils import silent_rmtree
+from .reporters import FileAnonymizer
import grass.script.core as gcore
@@ -142,7 +143,12 @@
results_dir = 'testreport'
silent_rmtree(results_dir) # TODO: too brute force?
- invoker = GrassTestFilesInvoker(start_dir='.')
+ start_dir = '.'
+ abs_start_dir = os.path.abspath(start_dir)
+ invoker = GrassTestFilesInvoker(
+ start_dir=start_dir,
+ file_anonymizer=FileAnonymizer(paths_to_remove=[abs_start_dir]))
+ # TODO: remove also results dir from files
# we can just iterate over all locations available in database
# but the we don't know the right location label/shortcut
invoker.run_in_location(gisdbase=gisdbase,
Modified: grass/trunk/lib/python/gunittest/reporters.py
===================================================================
--- grass/trunk/lib/python/gunittest/reporters.py 2014-07-27 01:53:12 UTC (rev 61417)
+++ grass/trunk/lib/python/gunittest/reporters.py 2014-07-27 04:02:34 UTC (rev 61418)
@@ -19,11 +19,63 @@
import subprocess
import StringIO
import collections
+import re
+import grass.script as gscript
+
from .utils import ensure_dir
from .checkers import text_to_keyvalue
+def replace_in_file(file_path, pattern, repl):
+ """
+
+ :param repl: a repl paramter of ``re.sub()`` function
+ """
+ # using tmp file to store the replaced content
+ tmp_file_path = file_path + '.tmp'
+ old_file = open(file_path, 'r')
+ new_file = open(tmp_file_path, 'w')
+ for line in old_file:
+ new_file.write(re.sub(pattern=pattern, string=line, repl=repl))
+ new_file.close()
+ old_file.close()
+ # remove old file since it must not exist for rename/move
+ os.remove(file_path)
+ # replace old file by new file
+ os.rename(tmp_file_path, file_path)
+
+
+class NoopFileAnonymizer(object):
+ def anonymize(self, filenames):
+ pass
+
+
+class FileAnonymizer(object):
+ def __init__(self, paths_to_remove, remove_gisbase=True,
+ remove_gisdbase=False):
+ self._paths_to_remove = []
+ if remove_gisbase:
+ gisbase = os.environ['GISBASE']
+ self._paths_to_remove.append(gisbase)
+ if remove_gisdbase:
+ gisdbase = gscript.gis.get['GISDBASE']
+ self._paths_to_remove.append(gisdbase)
+ if paths_to_remove:
+ self._paths_to_remove.extend(paths_to_remove)
+
+ def anonymize(self, filenames):
+ # besides GISBASE and test recursion start directory (which is
+ # supposed to be source root directory or similar) we can also try
+ # to remove user home directory and GISDBASE
+ # we suppuse that we run in standard grass session
+ # TODO: provide more effective implementation
+ for path in self._paths_to_remove:
+ for filename in filenames:
+ path_end = r'[\\/]?'
+ replace_in_file(filename, path + path_end, '')
+
+
def get_source_url(path, revision, line=None):
"""
@@ -410,9 +462,10 @@
unknown_number = UNKNOWN_NUMBER_HTML
- def __init__(self):
+ def __init__(self, file_anonymizer):
super(GrassTestFilesHtmlReporter, self).__init__()
self.main_index = None
+ self._file_anonymizer = file_anonymizer
def start(self, results_dir):
super(GrassTestFilesHtmlReporter, self).start(results_dir)
@@ -613,6 +666,14 @@
supplementary_files = test_summary.get('supplementary_files', None)
if supplementary_files:
+ # this is something we might want to do once for all and not
+ # risk that it will be done twice or rely that somebody else
+ # will do it for use
+ # the solution is perhaps do the multi reporter more grass-specific
+ # and do all common things, so that other can rely on it and
+ # moreover something can be shared with other explicity
+ # using constructors as seems advantageous for counting
+ self._file_anonymizer.anonymize(supplementary_files)
for f in supplementary_files:
file_index.write('<li><a href="{f}">{f}</a></li>'.format(f=f))
More information about the grass-commit
mailing list