def test_adduser_error(self): """ Test adding a user and encountering an error """ msg = "An unknown directory object was requested" error = pywintypes.com_error( -1234, "Exception occurred.", (0, None, msg, None, 0, -2147352567), None ) # Create mock group object with mocked Add function which raises the # exception we need in order to test the error case. class GroupObj(MockGroupObj): def Add(self, name): raise error obj_group_mock = MagicMock(return_value=GroupObj("foo", ["WinNT://HOST/steve"])) with patch.object( win_groupadd, "_get_group_object", obj_group_mock ), patch.object(salt.utils.win_functions, "get_sam_name", self.sam_mock): with TstSuiteLoggingHandler() as handler: self.assertFalse(win_groupadd.adduser("foo", "username")) expected = ( "ERROR:Failed to add HOST\\username to group foo. An unknown" " directory object was requested" ) self.assertIn(expected, handler.messages)
def init_windows(self): """Start Stata on Windows Until version 1.8.0, I included ```py from win32api import WinExec WinExec(config.get('stata_path')) ``` _before_ calling `win32com.client.Dispatch`. When opening a new Stata session using `win32com.client.Dispatch`, graph windows from the Stata GUI don't persist when opened. Originally, when developing `stata-exec`, I treated this as a bug, because that window was what a user looked at. However, for the purposes of `stata_kernel`, that's actually a helpful feature! - Though the graph window doesn't stay open, the _content_ of the graph is still in memory, so `graph export` still functions correctly! - This solves the problem of annoying graph windows popping up while using Jupyter Notebook. See #188 - But since I don't call `graph close`, it's still possible for a user to call `graph export` on the most recently opened graph. See: https://github.com/kylebarron/stata_kernel/commit/58c45636c4bfed24d36bf447f285f5bfb4b312da#commitcomment-31045398 """ try: self.stata = win32com.client.Dispatch("stata.StataOLEApp") except com_error: msg = """ The Stata Automation library is not enabled. Follow the instructions here and then try again: https://kylebarron.dev/stata_kernel/getting_started/#prerequisites """ raise com_error(dedent(msg)) self.automate(cmd_name='UtilShowStata', value=1) config.set('execution_mode', 'automation', permanent=True) self.start_log_aut()
def testStrangeArgsNone(self): try: raise pywintypes.com_error() self.fail("Expected exception") except pywintypes.com_error, exc: self.failUnlessEqual(exc.args, ()) self.failUnlessEqual(exc.hresult, None) self.failUnlessEqual(exc.strerror, None) self.failUnlessEqual(exc.argerror, None) self.failUnlessEqual(exc.excepinfo, None)
def testStrangeArgsNotEnough(self): try: raise pywintypes.com_error("foo") self.fail("Expected exception") except pywintypes.com_error as exc: self.failUnlessEqual(exc.args[0], "foo") self.failUnlessEqual(exc.hresult, "foo") self.failUnlessEqual(exc.strerror, None) self.failUnlessEqual(exc.excepinfo, None) self.failUnlessEqual(exc.argerror, None)
def testStrangeArgsNone(self): try: raise pywintypes.com_error() self.fail("Expected exception") except pywintypes.com_error as exc: assert exc.args == () assert exc.hresult == None assert exc.strerror == None assert exc.argerror == None assert exc.excepinfo == None
def testStrangeArgsNotEnough(self): try: raise pywintypes.com_error("foo") self.fail("Expected exception") except pywintypes.com_error as exc: assert exc.args[0] == "foo" assert exc.hresult == "foo" assert exc.strerror == None assert exc.excepinfo == None assert exc.argerror == None
def testStrangeArgsTooMany(self): try: raise pywintypes.com_error("foo", "bar", "you", "never", "kn", 0) self.fail("Expected exception") except pywintypes.com_error as exc: self.failUnlessEqual(exc.args[0], "foo") self.failUnlessEqual(exc.args[-1], 0) self.failUnlessEqual(exc.hresult, "foo") self.failUnlessEqual(exc.strerror, "bar") self.failUnlessEqual(exc.excepinfo, "you") self.failUnlessEqual(exc.argerror, "never")
def load_com(*names): result = None for name in names: try: result = gencache.EnsureDispatch(name) break except com_error: continue if result is None: raise com_error("Unable to load any of the provided com objects.") return result
def testStrangeArgsTooMany(self): try: raise pywintypes.com_error("foo", "bar", "you", "never", "kn", 0) self.fail("Expected exception") except pywintypes.com_error as exc: assert exc.args[0] == "foo" assert exc.args[-1] == 0 assert exc.hresult == "foo" assert exc.strerror == "bar" assert exc.excepinfo == "you" assert exc.argerror == "never"
def load_com(*names): global fixed if fixed==False: gencache._GetModule=patched_getmodule fixed=True result = None for name in names: try: result = gencache.EnsureDispatch(name) break except com_error: continue if result is None: raise com_error("Unable to load any of the provided com objects.") return result
def test_show_error_msg(self): """ Test the PowerpointDocument.show_error_msg() method gets called on com exception """ if is_win(): # GIVEN: A PowerpointDocument with mocked controller and presentation with patch('openlp.plugins.presentations.lib.powerpointcontroller.critical_error_message_box') as \ mocked_critical_error_message_box: instance = PowerpointDocument(self.mock_controller, self.mock_presentation) instance.presentation = MagicMock() instance.presentation.SlideShowWindow.View.GotoSlide = MagicMock( side_effect=pywintypes.com_error('1')) instance.index_map[42] = 42 # WHEN: Calling goto_slide which will throw an exception instance.goto_slide(42) # THEN: mocked_critical_error_message_box should have been called mocked_critical_error_message_box.assert_called_with( 'Error', 'An error occurred in the PowerPoint ' 'integration and the presentation will be stopped.' ' Restart the presentation if you wish to ' 'present it.')
def show_error_msg_test(self): """ Test the PowerpointDocument.show_error_msg() method gets called on com exception """ if is_win(): # GIVEN: A PowerpointDocument with mocked controller and presentation with patch('openlp.plugins.presentations.lib.powerpointcontroller.critical_error_message_box') as \ mocked_critical_error_message_box: instance = PowerpointDocument(self.mock_controller, self.mock_presentation) instance.presentation = MagicMock() instance.presentation.SlideShowWindow.View.GotoSlide = MagicMock(side_effect=pywintypes.com_error('1')) instance.index_map[42] = 42 # WHEN: Calling goto_slide which will throw an exception instance.goto_slide(42) # THEN: mocked_critical_error_message_box should have been called mocked_critical_error_message_box.assert_called_with('Error', 'An error occurred in the Powerpoint ' 'integration and the presentation will be stopped.' ' Restart the presentation if you wish to ' 'present it.')
import salt.modules.win_groupadd as win_groupadd import salt.utils.win_functions from tests.support.helpers import TstSuiteLoggingHandler from tests.support.mixins import LoaderModuleMockMixin from tests.support.mock import MagicMock, Mock, patch from tests.support.unit import TestCase, skipIf # pylint: disable=unused-import try: import pythoncom import pywintypes import win32com PYWINTYPES_ERROR = pywintypes.com_error( -1234, "Exception occurred.", (0, None, "C", None, 0, -2147352567), None ) HAS_WIN_LIBS = True except ImportError: HAS_WIN_LIBS = False # pylint: enable=unused-import class MockMember: def __init__(self, name): self.ADSPath = name class MockGroupObj: def __init__(self, ads_name, ads_users): self._members = [MockMember(x) for x in ads_users]
from tests.support.unit import TestCase, skipIf from tests.support.mock import (MagicMock, Mock, patch, NO_MOCK, NO_MOCK_REASON) # Import Salt Libs import salt.modules.win_groupadd as win_groupadd import salt.utils.win_functions # Import Other Libs # pylint: disable=unused-import try: import win32com import pythoncom import pywintypes PYWINTYPES_ERROR = pywintypes.com_error(-1234, 'Exception occurred.', (0, None, 'C', None, 0, -4321), None) HAS_WIN_LIBS = True except ImportError: HAS_WIN_LIBS = False # pylint: enable=unused-import class MockMember(object): def __init__(self, name): self.ADSPath = name class MockGroupObj(object): def __init__(self, ads_name, ads_users): self._members = [MockMember(x) for x in ads_users]