Skip to content

Commit

Permalink
Catch com errors, skip on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Sep 10, 2024
1 parent 3716e4c commit df359b3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ jobs:
- name: Run tests
run: |
python -m pytest
# Disable the fault handler to fix https://stackoverflow.com/questions/57523762/pytest-windows-fatal-exception-code-0x8001010d
pytest -p no:faulthandler
# ------------------------------------------------------------
# Build the distribution and publish (on release tag).
Expand Down
13 changes: 10 additions & 3 deletions rhodium/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
# along with Rhodium. If not, see <http://www.gnu.org/licenses/>.
import win32com.client
from win32com.universal import com_error
from .model import Model
from .model import Model, RhodiumError

class ExcelHelper:

def __init__(self, filename, sheet=1, visible=False):
self.xl = win32com.client.Dispatch("Excel.Application")
self.wb = self.xl.Workbooks.Open(filename)
try:
self.xl = win32com.client.Dispatch("Excel.Application")
except com_error as e:
raise RhodiumError("Failed to load Excel application", e)

try:
self.wb = self.xl.Workbooks.Open(filename)
except com_error as e:
raise RhodiumError("Failed to open Excel file", e)

# ensure auto-calculations is enabled
sheets = self.xl.Worksheets
Expand Down
27 changes: 26 additions & 1 deletion rhodium/test/excel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,27 @@
import os
import sys
import unittest
from ..model import Parameter, Response, IntegerUncertainty, \
from ..model import Parameter, Response, RhodiumError, IntegerUncertainty, \
UniformUncertainty
from ..optimization import evaluate
from ..sampling import sample_lhs

# Since Excel is typically not installed on hosted CI, skip test failures.
def skipErrorsOnCI(test):
def wrapper(self):
try:
test(self)
except RhodiumError as e:
if os.getenv("CI"):
self.skipTest(f"Excel test failed, ignoring. Reason: {e}")
else:
raise
return wrapper

class TestExcelHelper(unittest.TestCase):

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
@skipErrorsOnCI
def testGetItem(self):
from ..excel import ExcelHelper
file = os.path.join(os.path.dirname(__file__), "TestGetItem.xlsx")
Expand All @@ -42,6 +55,7 @@ def testGetItem(self):
self.assertEqual(u"sheet 2", helper["B2"])

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
@skipErrorsOnCI
def testSetItem(self):
from ..excel import ExcelHelper
file = os.path.join(os.path.dirname(__file__), "TestSetItem.xlsx")
Expand All @@ -62,10 +76,20 @@ def testSetItem(self):
helper["B2"] = "world"
helper.set_sheet(2)
self.assertEqual(u"hello", helper["B2"])

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
@skipErrorsOnCI
def testInvalidFile(self):
from ..excel import ExcelHelper
file = os.path.join(os.path.dirname(__file__), "Missing.xlsx")
with self.assertRaises(RhodiumError) as context:
with ExcelHelper(file) as helper:
pass

class TestExcelModel(unittest.TestCase):

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
@skipErrorsOnCI
def testEvaluate(self):
from ..excel import ExcelModel
file = os.path.join(os.path.dirname(__file__), "TestModel.xlsx")
Expand All @@ -79,6 +103,7 @@ def testEvaluate(self):
self.assertEqual(8, result["Y"])

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
@skipErrorsOnCI
def testSample(self):
from ..excel import ExcelModel
file = os.path.join(os.path.dirname(__file__), "TestModel.xlsx")
Expand Down

0 comments on commit df359b3

Please sign in to comment.