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

Side by Side Diff: third_party/typ/typ/test_case.py

Issue 627763002: Add new 'typ' python testing framework to third_party/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: upload to typ v0.8.1, update README.chromium Created 6 years, 2 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 unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2014 Google Inc. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import shlex
16 import unittest
17
18
19 def convert_newlines(msg):
20 """A routine that mimics Python's universal_newlines conversion."""
21 return msg.replace('\r\n', '\n').replace('\r', '\n')
22
23
24 class TestCase(unittest.TestCase):
25 child = None
26 context = None
27 maxDiff = 80 * 66
28
29
30 class MainTestCase(TestCase):
31 prog = None
32
33 def _write_files(self, host, files):
34 for path, contents in list(files.items()):
35 dirname = host.dirname(path)
36 if dirname:
37 host.maybe_mkdir(dirname)
38 host.write_text_file(path, contents)
39
40 def _read_files(self, host, tmpdir):
41 out_files = {}
42 for f in host.files_under(tmpdir):
43 key = f.replace(host.sep, '/')
44 out_files[key] = host.read_text_file(tmpdir, f)
45 return out_files
46
47 def assert_files(self, expected_files, actual_files, files_to_ignore=None):
48 files_to_ignore = files_to_ignore or []
49 for k, v in expected_files.items():
50 self.assertMultiLineEqual(expected_files[k], v)
51 interesting_files = set(actual_files.keys()).difference(
52 files_to_ignore)
53 self.assertEqual(interesting_files, set(expected_files.keys()))
54
55 def make_host(self):
56 # If we are ever called by unittest directly, and not through typ,
57 # this will probably fail.
58 assert(self.child)
59
60 return self.child.host
61
62 def call(self, host, argv, stdin, env):
63 return host.call(argv, stdin=stdin, env=env)
64
65 def check(self, cmd=None, stdin=None, env=None, aenv=None, files=None,
66 prog=None, cwd=None, host=None,
67 ret=None, out=None, rout=None, err=None, rerr=None,
68 exp_files=None,
69 files_to_ignore=None, universal_newlines=True):
70 # Too many arguments pylint: disable=R0913
71 prog = prog or self.prog or []
72 host = host or self.make_host()
73 argv = shlex.split(cmd) if isinstance(cmd, str) else cmd or []
74
75 tmpdir = None
76 orig_wd = host.getcwd()
77 try:
78 tmpdir = host.mkdtemp()
79 host.chdir(tmpdir)
80 if files:
81 self._write_files(host, files)
82 if cwd:
83 host.chdir(cwd)
84 if aenv:
85 env = host.env.copy()
86 env.update(aenv)
87
88 result = self.call(host, prog + argv, stdin=stdin, env=env)
89
90 actual_ret, actual_out, actual_err = result
91 actual_files = self._read_files(host, tmpdir)
92 finally:
93 if tmpdir:
94 host.rmtree(tmpdir)
95 host.chdir(orig_wd)
96
97 if universal_newlines:
98 actual_out = convert_newlines(actual_out)
99 if universal_newlines:
100 actual_err = convert_newlines(actual_err)
101
102 if ret is not None:
103 self.assertEqual(ret, actual_ret)
104 if out is not None:
105 self.assertMultiLineEqual(out, actual_out)
106 if rout is not None:
107 self.assertRegexpMatches(actual_out, rout)
108 if err is not None:
109 self.assertMultiLineEqual(err, actual_err)
110 if rerr is not None:
111 self.assertRegexpMatches(actual_err, rerr)
112 if exp_files:
113 self.assert_files(exp_files, actual_files, files_to_ignore)
114
115 return actual_ret, actual_out, actual_err, actual_files
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698