Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a utility for finding test fixtures that allows it to not exist i… #1880

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions com/win32comext/directsound/test/ds_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pywintypes
import win32event, win32api
import os
from pywin32_testutil import TestSkipped
from pywin32_testutil import TestSkipped, find_test_fixture
import win32com.directsound.directsound as ds
import pythoncom

Expand Down Expand Up @@ -301,22 +301,12 @@ def testCreate(self):

def testPlay(self):
"""Mesdames et Messieurs, la cour de Devin Dazzle"""
# look for the test file in various places
candidates = [
os.path.dirname(__file__),
os.path.dirname(sys.argv[0]),
# relative to 'testall.py' in the win32com test suite.
os.path.join(
os.path.dirname(sys.argv[0]), "../../win32comext/directsound/test"
),
".",
]
for candidate in candidates:
fname = os.path.join(candidate, "01-Intro.wav")
if os.path.isfile(fname):
break
else:
raise TestSkipped("Can't find test .wav file to play")
# relative to 'testall.py' in the win32com test suite.
extra = os.path.join(
os.path.dirname(sys.argv[0]), "../../win32comext/directsound/test"
)

fname = find_test_fixture("01-Intro.wav", extra)

with open(fname, "rb") as f:
hdr = f.read(WAV_HEADER_SIZE)
Expand Down
35 changes: 35 additions & 0 deletions win32/Lib/pywin32_testutil.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Utilities for the pywin32 tests
import site
import sys
import os
import unittest
import gc
import winerror
Expand Down Expand Up @@ -193,6 +195,39 @@ def check_is_admin():
return _is_admin


# Find a test "fixture" (eg, binary test file) expected to be very close to
# the test being run.
# If the tests are being run from the "installed" version, then these fixtures
# probably don't exist - the test is "skipped".
# But it's fatal if we think we might be running from a pywin32 source tree.
def find_test_fixture(basename, extra_dir="."):
# look for the test file in various places
candidates = [
os.path.dirname(sys.argv[0]),
extra_dir,
".",
]
for candidate in candidates:
fname = os.path.join(candidate, basename)
if os.path.isfile(fname):
return fname
else:
# Can't find it - see if this is expected or not.
# This module is typically always in the installed dir, so use argv[0]
this_file = os.path.normcase(os.path.abspath(sys.argv[0]))
dirs_to_check = site.getsitepackages()[:]
if site.USER_SITE:
dirs_to_check.append(site.USER_SITE)

for d in dirs_to_check:
d = os.path.normcase(d)
if os.path.commonprefix([this_file, d]) == d:
# looks like we are in an installed Python, so skip the text.
raise TestSkipped(f"Can't find test fixture '{fname}'")
# Looks like we are running from source, so this is fatal.
raise RuntimeError(f"Can't find test fixture '{fname}'")


# If this exception is raised by a test, the test is reported as a 'skip'
class TestSkipped(Exception):
pass
Expand Down