forked from google/transitfeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request google#438 from TomGoBravo/master
update traceplus to work with Python2 and Python3, add example usage
- Loading branch information
Showing
4 changed files
with
128 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/python3 | ||
|
||
import argparse | ||
parser = argparse.ArgumentParser() | ||
|
||
def CrashOrNot(crash_commanded): | ||
some_value = 1 | ||
if crash_commanded: | ||
some_value -= 1 | ||
print("All good, you get {}".format(1 / some_value)) | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--crash", help="Do something that causes a crash", action="store_true") | ||
args = parser.parse_args() | ||
CrashOrNot(args.crash) | ||
|
||
# If the traceplus module is found use it, otherwise run main() directly. | ||
if __name__ == '__main__': | ||
try: | ||
import traceplus | ||
except ImportError: | ||
main() | ||
else: | ||
traceplus.RunWithExpandedTrace(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/python2.5 | ||
|
||
# Copyright (C) 2018 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Includes locals in the stacktrace when a failure occurs. | ||
# | ||
# Example use: | ||
# | ||
# if __name__ == '__main__': | ||
# try: | ||
# import traceplusunittest | ||
# except ImportError: | ||
# unittest.main() | ||
# else: | ||
# traceplusunittest.main() | ||
|
||
import unittest | ||
import traceplus | ||
import traceback | ||
import inspect | ||
|
||
|
||
class TextBigStackTestRunner(unittest.TextTestRunner): | ||
def _makeResult(self): | ||
return TextBigStackTestResult(self.stream, self.descriptions, self.verbosity) | ||
|
||
|
||
class TextBigStackTestResult(unittest._TextTestResult): | ||
def _exc_info_to_string(self, err, test): | ||
"""Converts a sys.exc_info()-style tuple of values into a string.""" | ||
exctype, value, tb = err | ||
# Skip test runner traceback levels | ||
while tb and self._is_relevant_tb_level(tb): | ||
tb = tb.tb_next | ||
if exctype is test.failureException: | ||
# Skip assert*() traceback levels | ||
length = self._count_relevant_tb_levels(tb) | ||
return ''.join(FormatException(exctype, value, tb, length)) | ||
return ''.join(FormatException(exctype, value, tb)) | ||
|
||
|
||
def FormatException(exctype, value, tb, length=None): | ||
frame_records = inspect.getinnerframes(tb, 3) | ||
|
||
dump = [] | ||
if length is None: | ||
dump.extend(traceplus.MakeExpandedTrace(frame_records)) | ||
else: | ||
dump.extend(traceplus.MakeExpandedTrace(frame_records[:length])) | ||
dump.extend(traceback.format_exception_only(exctype, value)) | ||
return ''.join(dump) | ||
|
||
|
||
def main(): | ||
unittest.main(testRunner=TextBigStackTestRunner()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import unittest | ||
|
||
class TestStringMethods(unittest.TestCase): | ||
|
||
def test_upper(self): | ||
self.assertEqual('foo'.upper(), 'FOO') | ||
|
||
def test_split(self): | ||
s = 'hello world' | ||
self.assertEqual(s.split(), ['hello', 'hello']) # This will fail | ||
|
||
if __name__ == '__main__': | ||
try: | ||
import traceplusunittest | ||
except ImportError: | ||
unittest.main() | ||
else: | ||
traceplusunittest.main() |