Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Unified Diff: tools/isolate/isolate_test.py

Issue 9638020: Refactor isolate.py to be more functional and extensible (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More extensive testing, saner mode saving, etc Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tools/isolate/isolate_test.py
diff --git a/tools/isolate/isolate_test.py b/tools/isolate/isolate_test.py
index aa1dd69106fd4af0f5e44b00c5d925b3bd29916f..f2d9252d4f6fdb107c62f9faf30f2ae787635ef0 100755
--- a/tools/isolate/isolate_test.py
+++ b/tools/isolate/isolate_test.py
@@ -4,6 +4,8 @@
# found in the LICENSE file.
import hashlib
+import json
+import logging
import os
import re
import shutil
@@ -13,6 +15,7 @@ import tempfile
import unittest
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
+VERBOSE = False
class Isolate(unittest.TestCase):
@@ -23,27 +26,62 @@ class Isolate(unittest.TestCase):
self.isolate = isolate
self.tempdir = tempfile.mkdtemp()
self.result = os.path.join(self.tempdir, 'result')
+ if VERBOSE:
+ print
csharp 2012/03/08 21:53:39 Why print a new line?
M-A Ruel 2012/03/08 21:56:41 Makes reading the output much easier. It's only wh
def tearDown(self):
shutil.rmtree(self.tempdir)
+ def _expected_tree(self, files):
+ self.assertEquals(sorted(files), sorted(os.listdir(self.tempdir)))
+
+ def _expected_result(self, with_hash, files, args, read_only):
+ # 4 modes are supported, 0755 (rwx), 0644 (rw), 0555 (rx), 0444 (r)
+ min_mode = 0444
+ if not read_only:
+ min_mode |= 0200
+ def mode(filename):
+ return (min_mode | 0111) if filename.endswith('.py') else min_mode
+ expected = {
+ u'command':
+ [unicode(sys.executable), u'isolate_test.py'] +
+ [unicode(x) for x in args],
+ u'files': dict((unicode(f), {u'mode': mode(f)}) for f in files),
+ }
+ if with_hash:
+ for filename in expected[u'files']:
+ # Calculate our hash.
+ h = hashlib.sha1()
+ h.update(open(os.path.join(ROOT_DIR, filename), 'rb').read())
+ expected[u'files'][filename][u'sha-1'] = h.hexdigest()
+
+ actual = json.load(open(self.result, 'rb'))
+ self.assertEquals(expected, actual)
+ return expected
+
def _execute(self, args):
cmd = [
sys.executable, os.path.join(ROOT_DIR, 'isolate.py'),
'--root', ROOT_DIR,
'--result', self.result,
]
+ if VERBOSE:
+ cmd.extend(['-v'] * 3)
+ stdout = None
+ stderr = None
+ else:
+ stdout = subprocess.PIPE
+ stderr = subprocess.STDOUT
subprocess.check_call(
- cmd + args,
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
+ cmd + args, stdout=stdout, stderr=stderr, cwd=ROOT_DIR)
def test_help_modes(self):
# Check coherency in the help and implemented modes.
p = subprocess.Popen(
[sys.executable, os.path.join(ROOT_DIR, 'isolate.py'), '--help'],
stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
+ stderr=subprocess.STDOUT,
+ cwd=ROOT_DIR)
out = p.communicate()[0].splitlines()
self.assertEquals(0, p.returncode)
out = out[out.index('') + 1:]
@@ -53,6 +91,7 @@ class Isolate(unittest.TestCase):
self.assertEquals(self.isolate.VALID_MODES, modes)
for mode in modes:
self.assertTrue(hasattr(self, 'test_%s' % mode), mode)
+ self._expected_tree([])
def test_check(self):
cmd = [
@@ -60,7 +99,8 @@ class Isolate(unittest.TestCase):
'isolate_test.py',
]
self._execute(cmd)
- self.assertTrue(os.path.isfile(self.result))
+ self._expected_tree(['result'])
+ self._expected_result(False, ['isolate_test.py'], [], False)
def test_check_non_existant(self):
cmd = [
@@ -72,24 +112,33 @@ class Isolate(unittest.TestCase):
self.fail()
except subprocess.CalledProcessError:
pass
- self.assertFalse(os.path.isfile(self.result))
+ self._expected_tree([])
+
+ def test_check_directory_no_slash(self):
+ cmd = [
+ '--mode', 'check',
+ # Trailing slash missing.
+ 'data',
+ ]
+ try:
+ self._execute(cmd)
+ self.fail()
+ except subprocess.CalledProcessError:
+ pass
+ self._expected_tree([])
def test_hashtable(self):
cmd = [
'--mode', 'hashtable',
'--outdir', self.tempdir,
'isolate_test.py',
+ 'data/',
]
self._execute(cmd)
- # Calculate our hash.
- h = hashlib.sha1()
- h.update(open(__file__, 'rb').read())
- digest = h.hexdigest()
- self.assertEquals(
- '{"files": {"isolate_test.py": {"sha1": "%s"}}}' % digest,
- open(self.result, 'rb').read())
- self.assertEquals(
- sorted([digest, 'result']), sorted(os.listdir(self.tempdir)))
+ files = ['isolate_test.py', 'data/test_file1.txt', 'data/test_file2.txt']
+ data = self._expected_result(True, files, [], False)
+ self._expected_tree(
+ [f['sha-1'] for f in data['files'].itervalues()] + ['result'])
def test_remap(self):
cmd = [
@@ -98,9 +147,8 @@ class Isolate(unittest.TestCase):
'isolate_test.py',
]
self._execute(cmd)
- self.assertEquals('isolate_test.py\n', open(self.result, 'rb').read())
- self.assertEquals(
- ['isolate_test.py', 'result'], sorted(os.listdir(self.tempdir)))
+ self._expected_tree(['isolate_test.py', 'result'])
+ self._expected_result(False, ['isolate_test.py'], [], False)
def test_run(self):
cmd = [
@@ -110,7 +158,8 @@ class Isolate(unittest.TestCase):
sys.executable, 'isolate_test.py', '--ok',
]
self._execute(cmd)
- self.assertTrue(os.path.isfile(self.result))
+ self._expected_tree(['result'])
+ self._expected_result(False, ['isolate_test.py'], ['--ok'], False)
def test_run_fail(self):
cmd = [
@@ -124,10 +173,14 @@ class Isolate(unittest.TestCase):
self.fail()
except subprocess.CalledProcessError:
pass
- self.assertFalse(os.path.isfile(self.result))
+ self._expected_tree([])
def main():
+ global VERBOSE
+ VERBOSE = '-v' in sys.argv
+ level = logging.DEBUG if VERBOSE else logging.ERROR
+ logging.basicConfig(level=level)
if len(sys.argv) == 1:
unittest.main()
if sys.argv[1] == '--ok':

Powered by Google App Engine
This is Rietveld 408576698