def test_internal_error(self): handler = log.StackfulStreamHandler() try: lineno = current_lineno() + 1 iterutils.first(None) except LookupError: record = logging.LogRecord('name', 'level', 'pathname', 1, 'msg', [], sys.exc_info()) with mock.patch.object(logging.StreamHandler, 'emit'): handler.emit(record) iterutils_file = iterutils.__file__.rstrip('c') self.assertEqual(record.full_stack, [ (this_file, lineno, 'test_internal_error', "iterutils.first(None)"), (iterutils_file, 47, 'first', 'raise LookupError()'), ]) self.assertEqual(record.stack_pre, '') self.assertEqual( record.stack, ('\n' + ' File "{}", line {}, in test_internal_error\n' + " iterutils.first(None)").format(this_file, lineno)) self.assertEqual(record.stack_post, ('\n' + ' File "{}", line 47, in first\n' + " raise LookupError()").format(iterutils_file)) self.assertEqual(record.user_pathname, this_file) self.assertEqual(record.user_lineno, lineno)
def test_no_user_stack(self): handler = log.StackfulStreamHandler() try: iterutils.first(None) except LookupError: exc = sys.exc_info() record = logging.LogRecord( 'name', 'level', 'pathname', 1, 'msg', [], exc[0:2] + (exc[2].tb_next,) ) with mock.patch.object(logging.StreamHandler, 'emit'), \ mock.patch('logging.root.handle'): # noqa handler.emit(record) iterutils_file = iterutils.__file__.rstrip('c') self.assertEqual(record.full_stack, [ (iterutils_file, 74, 'first', 'raise LookupError()'), ]) self.assertEqual(record.show_stack, False) self.assertEqual(record.stack_pre, ( '\n' + ' File "{}", line 74, in first\n' + " raise LookupError()" ).format(iterutils_file)) self.assertEqual(record.stack, '') self.assertEqual(record.stack_post, '') self.assertEqual(record.user_pathname, record.pathname) self.assertEqual(record.user_lineno, record.lineno)
def test_implicit(self): env = make_env(platform='linux', clear_variables=True) with mock.patch('bfg9000.tools.pkg_config.which') as mwhich, \ mock.patch('bfg9000.tools.pkg_config.check_which', lambda names, env: ([first(names)], True)), \ mock.patch('bfg9000.shell.which', return_value=['cc']), \ mock.patch('bfg9000.shell.execute', mock_execute_cc): self.assertEqual(PkgConfig(env).command, ['pkg-config']) mwhich.assert_not_called()
def test_guess_sibling_nonexist(self): env = make_env(platform='linux', clear_variables=True, variables={'CC': 'i686-w64-mingw32-gcc-99'}) with mock.patch('bfg9000.tools.pkg_config.which', lambda names, env: [first(names)]), \ mock.patch('bfg9000.tools.pkg_config.check_which') as mcwhich, \ mock.patch('bfg9000.shell.which', side_effect=IOError()), \ mock.patch('bfg9000.log.info'), \ mock.patch('warnings.warn'): self.assertEqual( PkgConfig(env).command, ['i686-w64-mingw32-pkg-config']) mcwhich.assert_not_called()
def test_guess_sibling(self): env = make_env(platform='linux', clear_variables=True, variables={'CC': 'i686-w64-mingw32-gcc-99'}) with mock.patch('bfg9000.tools.pkg_config.which', lambda names, env: [first(names)]), \ mock.patch('bfg9000.tools.pkg_config.check_which') as mcwhich, \ mock.patch('bfg9000.shell.which', return_value=['i686-w64-mingw32-gcc-99']), \ mock.patch('bfg9000.shell.execute', mock_execute_cc), \ mock.patch('bfg9000.log.info'): self.assertEqual( PkgConfig(env).command, ['i686-w64-mingw32-pkg-config']) mcwhich.assert_not_called()
def test_many(self): self.assertEqual(iterutils.first(['foo', 'bar']), 'foo')
def test_one(self): self.assertEqual(iterutils.first('foo'), 'foo')
def test_none_default(self): self.assertEqual(iterutils.first(None, default='foo'), 'foo')
def mock_which(names, *args, **kwargs): return [os.path.abspath('/' + first(first(names)))]