Skip to content

Commit 714963c

Browse files
committed
test for SoundFileSystemError instead of RuntimeError
check for backward-compatibility too, though (catching RuntimeError must still work)
1 parent 69c55d6 commit 714963c

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

tests/test_pysoundfile.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,12 @@ def test_read_mono_into_2d_out(file_mono_r):
232232

233233

234234
def test_read_non_existing_file():
235-
with pytest.raises(RuntimeError) as excinfo:
235+
with pytest.raises(sf.SoundFileError) as excinfo:
236236
sf.read("i_do_not_exist.wav")
237237
assert "Error opening 'i_do_not_exist.wav'" in str(excinfo.value)
238238

239239

240+
240241
# -----------------------------------------------------------------------------
241242
# Test write() function
242243
# -----------------------------------------------------------------------------
@@ -418,7 +419,7 @@ def test_blocks_wplus(sf_stereo_wplus):
418419

419420

420421
def test_blocks_write(sf_stereo_w):
421-
with pytest.raises(RuntimeError):
422+
with pytest.raises(sf.SoundFileError):
422423
list(sf_stereo_w.blocks(blocksize=2))
423424

424425

@@ -644,9 +645,9 @@ def test_seek_in_read_mode(sf_stereo_r):
644645
assert sf_stereo_r.tell() == 2
645646
assert sf_stereo_r.seek(2, sf.SEEK_CUR) == 4
646647
assert sf_stereo_r.seek(-2, sf.SEEK_END) == len(data_stereo) - 2
647-
with pytest.raises(RuntimeError):
648+
with pytest.raises(sf.SoundFileError):
648649
sf_stereo_r.seek(666)
649-
with pytest.raises(RuntimeError):
650+
with pytest.raises(sf.SoundFileError):
650651
sf_stereo_r.seek(-666)
651652

652653

@@ -685,8 +686,9 @@ def test_truncate(file_stereo_rplus, use_default):
685686
else:
686687
# file objects don't support truncate()
687688
with sf.SoundFile(file_stereo_rplus, 'r+', closefd=False) as f:
688-
with pytest.raises(RuntimeError) as excinfo:
689+
with pytest.raises(sf.SoundFileError) as excinfo:
689690
f.truncate()
691+
assert isinstance(excinfo.value, RuntimeError)
690692
assert "Error truncating" in str(excinfo.value)
691693

692694

@@ -696,7 +698,7 @@ def test_truncate(file_stereo_rplus, use_default):
696698

697699

698700
def test_read_write_only(sf_stereo_w):
699-
with pytest.raises(RuntimeError):
701+
with pytest.raises(sf.SoundFileError):
700702
sf_stereo_w.read(2)
701703

702704

@@ -796,7 +798,7 @@ def test_buffer_read_into(sf_stereo_r):
796798

797799

798800
def test_write_to_read_only_file_should_fail(sf_stereo_r):
799-
with pytest.raises(RuntimeError):
801+
with pytest.raises(sf.SoundFileError):
800802
sf_stereo_r.write(data_stereo)
801803

802804

@@ -887,8 +889,9 @@ def test_closing_should_close_file(file_stereo_r):
887889
def test_anything_on_closed_file(file_stereo_r):
888890
with sf.SoundFile(file_stereo_r) as f:
889891
pass
890-
with pytest.raises(RuntimeError) as excinfo:
892+
with pytest.raises(sf.SoundFileError) as excinfo:
891893
f.seek(0)
894+
assert isinstance(excinfo.value, RuntimeError)
892895
assert "closed" in str(excinfo.value)
893896

894897

@@ -1014,8 +1017,9 @@ def test_write_non_seekable_file(file_w):
10141017
f.write(data_mono)
10151018
assert f.frames == len(data_mono)
10161019

1017-
with pytest.raises(RuntimeError) as excinfo:
1020+
with pytest.raises(sf.SoundFileError) as excinfo:
10181021
f.seek(2)
1022+
assert isinstance(excinfo.value, RuntimeError)
10191023
assert "unseekable" in str(excinfo.value)
10201024

10211025
with sf.SoundFile(filename_new) as f:
@@ -1026,8 +1030,9 @@ def test_write_non_seekable_file(file_w):
10261030
data = f.read(666, dtype='int16')
10271031
assert np.all(data == data_mono[3:])
10281032

1029-
with pytest.raises(RuntimeError) as excinfo:
1033+
with pytest.raises(sf.SoundFileError) as excinfo:
10301034
f.seek(2)
1035+
assert isinstance(excinfo.value, RuntimeError)
10311036
assert "unseekable" in str(excinfo.value)
10321037

10331038
with pytest.raises(ValueError) as excinfo:
@@ -1041,3 +1046,32 @@ def test_write_non_seekable_file(file_w):
10411046
with pytest.raises(ValueError) as excinfo:
10421047
sf.read(filename_new, start=3)
10431048
assert "start is only allowed for seekable files" in str(excinfo.value)
1049+
1050+
1051+
# -----------------------------------------------------------------------------
1052+
# Test LibsndfileError
1053+
# -----------------------------------------------------------------------------
1054+
1055+
def test_libsndfile_error():
1056+
err = sf.LibsndfileError(2)
1057+
assert isinstance(err, sf.SoundFileError)
1058+
assert err.error_string # cannot assume exact message generated by libsndfile
1059+
assert str(err) == err.error_string
1060+
1061+
def test_libsndfile_error_with_prefix():
1062+
err = sf.LibsndfileError(2, prefix="XX ")
1063+
assert isinstance(err, sf.SoundFileError)
1064+
assert err.error_string # cannot assume exact message generated by libsndfile
1065+
assert str(err) == "XX " + err.error_string
1066+
1067+
def test_rasing_libsndfile_error():
1068+
with pytest.raises(sf.LibsndfileError) as excinfo:
1069+
sf.read("i_do_not_exist.wav")
1070+
assert isinstance(excinfo.value, sf.SoundFileError)
1071+
assert isinstance(excinfo.value, RuntimeError)
1072+
assert excinfo.value.code == 2 # SF_ERR_SYSTEM
1073+
if sys.version_info[0] == 2:
1074+
assert isinstance(excinfo.value.error_string, unicode)
1075+
else:
1076+
assert isinstance(excinfo.value.error_string, str)
1077+
assert excinfo.value.error_string in str(excinfo.value)

0 commit comments

Comments
 (0)