From 3783343e3567afdbc713854f50985af6125f6b9b Mon Sep 17 00:00:00 2001 From: Matthew Walker Date: Thu, 24 Dec 2020 17:13:11 +1300 Subject: [PATCH] Fixed test data file locations (#76) * Created test fixture pyogg_config. * Fixed location of test data files. They now are always stored in `tests/out` irrespective of the current working directory. * Added signed attribute to FlacFile --- pyogg/flac.py | 71 +++++++++++++++++++++++------ pyogg/flac_file.py | 10 +++- tests/config.py | 7 +++ tests/conftest.py | 45 ++++++++++++++++++ tests/test_flac_file.py | 20 ++++---- tests/test_flac_file_stream.py | 29 ++++++++---- tests/test_ogg_opus_writer.py | 54 ++++++++++++++++------ tests/test_opus_buffered_encoder.py | 2 - tests/test_opus_decoder.py | 2 - tests/test_opus_encoder.py | 2 - tests/test_opus_file.py | 46 +++++++++++++------ tests/test_opus_file_stream.py | 23 +++++++--- tests/test_vorbis_file.py | 59 ++++++++++++++++-------- tests/test_vorbis_file_stream.py | 23 +++++++--- 14 files changed, 292 insertions(+), 101 deletions(-) create mode 100644 tests/config.py create mode 100644 tests/conftest.py diff --git a/pyogg/flac.py b/pyogg/flac.py index db0e20f..d44509e 100644 --- a/pyogg/flac.py +++ b/pyogg/flac.py @@ -1372,21 +1372,62 @@ class FLAC__StreamDecoder(Structure): _fields_ = [("protected_", POINTER(FLAC__StreamDecoderProtected)), ("private_", POINTER(FLAC__StreamDecoderPrivate))] - FLAC__StreamDecoderReadCallback = CFUNCTYPE(FLAC__StreamDecoderReadStatus, POINTER(FLAC__StreamDecoder), POINTER(FLAC__byte*0), c_size_t_p, c_void_p) - - FLAC__StreamDecoderSeekCallback = CFUNCTYPE(FLAC__StreamDecoderSeekStatus, POINTER(FLAC__StreamDecoder), FLAC__uint64, c_void_p) - - FLAC__StreamDecoderTellCallback = CFUNCTYPE(FLAC__StreamDecoderTellStatus, POINTER(FLAC__StreamDecoder), FLAC__uint64_p, c_void_p) - - FLAC__StreamDecoderLengthCallback = CFUNCTYPE(FLAC__StreamDecoderLengthStatus, POINTER(FLAC__StreamDecoder), FLAC__uint64_p, c_void_p) - - FLAC__StreamDecoderEofCallback = CFUNCTYPE(FLAC__bool, POINTER(FLAC__StreamDecoder), c_void_p) - - FLAC__StreamDecoderWriteCallback = CFUNCTYPE(FLAC__StreamDecoderWriteStatus, POINTER(FLAC__StreamDecoder), POINTER(FLAC__Frame), POINTER(FLAC__int32_p*0), c_void_p)#FLAC__int32_p*0, c_void_p) - - FLAC__StreamDecoderMetadataCallback = CFUNCTYPE(None, POINTER(FLAC__StreamDecoder), POINTER(FLAC__StreamMetadata), c_void_p) - - FLAC__StreamDecoderErrorCallback = CFUNCTYPE(None, POINTER(FLAC__StreamDecoder), FLAC__StreamDecoderErrorStatus, c_void_p) + FLAC__StreamDecoderReadCallback = CFUNCTYPE( + FLAC__StreamDecoderReadStatus, + POINTER(FLAC__StreamDecoder), + POINTER(FLAC__byte*0), + c_size_t_p, + c_void_p + ) + + FLAC__StreamDecoderSeekCallback = CFUNCTYPE( + FLAC__StreamDecoderSeekStatus, + POINTER(FLAC__StreamDecoder), + FLAC__uint64, + c_void_p + ) + + FLAC__StreamDecoderTellCallback = CFUNCTYPE( + FLAC__StreamDecoderTellStatus, + POINTER(FLAC__StreamDecoder), + FLAC__uint64_p, + c_void_p + ) + + FLAC__StreamDecoderLengthCallback = CFUNCTYPE( + FLAC__StreamDecoderLengthStatus, + POINTER(FLAC__StreamDecoder), + FLAC__uint64_p, + c_void_p + ) + + FLAC__StreamDecoderEofCallback = CFUNCTYPE( + FLAC__bool, + POINTER(FLAC__StreamDecoder), + c_void_p + ) + + FLAC__StreamDecoderWriteCallback = CFUNCTYPE( + FLAC__StreamDecoderWriteStatus, + POINTER(FLAC__StreamDecoder), + POINTER(FLAC__Frame), + POINTER(FLAC__int32_p*0), + c_void_p + ) + + FLAC__StreamDecoderMetadataCallback = CFUNCTYPE( + None, + POINTER(FLAC__StreamDecoder), + POINTER(FLAC__StreamMetadata), + c_void_p + ) + + FLAC__StreamDecoderErrorCallback = CFUNCTYPE( + None, + POINTER(FLAC__StreamDecoder), + FLAC__StreamDecoderErrorStatus, + c_void_p + ) libflac.FLAC__stream_decoder_new.restype = POINTER(FLAC__StreamDecoder) diff --git a/pyogg/flac_file.py b/pyogg/flac_file.py index cf936ed..7e97ca7 100644 --- a/pyogg/flac_file.py +++ b/pyogg/flac_file.py @@ -82,7 +82,10 @@ def __init__(self, path): ) if init_status: # error - raise PyOggError("An error occured when trying to open '{}': {}".format(path, flac.FLAC__StreamDecoderInitStatusEnum[init_status])) + error = flac.FLAC__StreamDecoderInitStatusEnum[init_status] + raise PyOggError( + "An error occured when trying to open '{}': {}".format(path, error) + ) metadata_status = (flac.FLAC__stream_decoder_process_until_end_of_metadata(self.decoder)) if not metadata_status: # error @@ -100,9 +103,12 @@ def __init__(self, path): self.bytes_per_sample = ctypes.sizeof(flac.FLAC__int16) # See definition of Buffer in metadata_callback() # Cast buffer to one-dimensional array of chars - print("len(self.buffer):",len(self.buffer)) CharBuffer = ( ctypes.c_byte * (self.bytes_per_sample * len(self.buffer)) ) self.buffer = CharBuffer.from_buffer(self.buffer) + + # FLAC audio is always signed. See + # https://xiph.org/flac/api/group__flac__stream__decoder.html#gaf98a4f9e2cac5747da6018c3dfc8dde1 + self.signed = True diff --git a/tests/config.py b/tests/config.py new file mode 100644 index 0000000..3e11175 --- /dev/null +++ b/tests/config.py @@ -0,0 +1,7 @@ +import pathlib + +# Definition of class returned by PyTest fixture 'pyogg_config'. +class Config: + def __init__(self): + self.rootdir: pathlib.Path + self.outdir: pathlib.Path diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..cdf586a --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,45 @@ +import shutil +from typing import Any + +import pytest + +from config import Config + + +_config = Config() + + +# FIXME: Mypy: What is the correct type for 'config'? It is of type +# _pytest.config.Config (see +# https://docs.pytest.org/en/stable/_modules/_pytest/hookspec.html#pytest_configure) +# but this isn't part of the public API. Using 'Any' as a +# placeholder. +def pytest_configure(config: Any) -> None: + # Create an object to store the directories + + _config.rootdir = config.rootdir + _config.outdir = config.rootdir / "tests/out" + + # If the previous output directory exists, delete it + out_previous = config.rootdir / "tests/out_previous" + if out_previous.exists(): + try: + shutil.rmtree(out_previous) + except Exception as e: + raise Exception( + "Failed to remove previous output directory. "+ + "You will need to manually delete the directory "+ + f"'{out_previous}': "+str(e) + ) + + # If the output directory already exists, rename it + if _config.outdir.exists(): + _config.outdir.rename(config.rootdir / "tests/out_previous") + + # Create the output directory + _config.outdir.mkdir() + + +@pytest.fixture(scope="session") +def pyogg_config() -> Config: + return _config diff --git a/tests/test_flac_file.py b/tests/test_flac_file.py index e4f3fc9..47cb1b5 100644 --- a/tests/test_flac_file.py +++ b/tests/test_flac_file.py @@ -1,16 +1,16 @@ import pytest import pyogg -def test_error_in_filename(): +def test_error_in_filename() -> None: # Load a non-existant file filename = "does-not-exist.flac" with pytest.raises(pyogg.PyOggError): flac_file = pyogg.FlacFile(filename) -def test_as_array(): +def test_as_array(pyogg_config) -> None: # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.flac" + filename = str(pyogg_config.rootdir / "examples/left-right-demo-5s.flac") flac_file = pyogg.FlacFile(filename) # Test that the loaded file is indeed 5 seconds long (using @@ -25,9 +25,9 @@ def test_as_array(): assert duration_samples == expected_duration_samples -def test_as_bytes(): +def test_as_bytes(pyogg_config) -> None: # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.flac" + filename = str(pyogg_config.rootdir / "examples/left-right-demo-5s.flac") flac_file = pyogg.FlacFile(filename) # Test that the loaded file is indeed 5 seconds long (using @@ -46,14 +46,18 @@ def test_as_bytes(): assert duration_bytes == expected_duration_bytes -def test_output_via_wav(): +def test_output_via_wav(pyogg_config) -> None: # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.flac" + filename = str(pyogg_config.rootdir / "examples/left-right-demo-5s.flac") flac_file = pyogg.FlacFile(filename) import wave + out_filename = str( + pyogg_config.outdir + / "test_flac_file__test_output_via_wav.wav" + ) wave_out = wave.open( - "test_flac_file__test_output_via_wav.wav", + out_filename, "wb" ) wave_out.setnchannels(flac_file.channels) diff --git a/tests/test_flac_file_stream.py b/tests/test_flac_file_stream.py index 27db828..8614722 100644 --- a/tests/test_flac_file_stream.py +++ b/tests/test_flac_file_stream.py @@ -1,16 +1,21 @@ import pytest import pyogg -def test_error_in_filename(): +from config import Config + +def test_error_in_filename() -> None: # Load a non-existant file filename = "does-not-exist.flac" with pytest.raises(pyogg.PyOggError): flac_stream = pyogg.FlacFileStream(filename) -def test_total_length(): +def test_total_length(pyogg_config: Config) -> None: # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.flac" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.flac" + ) # Open the file using FlacFileStream, which does not read the entire # file immediately. @@ -39,10 +44,13 @@ def test_total_length(): assert duration_samples == expected_duration_samples -def test_same_data_as_flac_file(): +def test_same_data_as_flac_file(pyogg_config) -> None: # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.flac" - + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.flac" + ) + # Open the file using FlacFile to read the entire file into memory flac_file = pyogg.FlacFile(filename) @@ -68,12 +76,15 @@ def test_same_data_as_flac_file(): assert buf_all == bytes(flac_file.buffer) -def test_same_data_as_flac_file_using_as_array(): +def test_same_data_as_flac_file_using_as_array(pyogg_config): import numpy # type: ignore # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.flac" - + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.flac" + ) + # Open the file using FlacFile to read the entire file into memory flac_file = pyogg.FlacFile(filename) diff --git a/tests/test_ogg_opus_writer.py b/tests/test_ogg_opus_writer.py index b4a64c0..2c0d862 100644 --- a/tests/test_ogg_opus_writer.py +++ b/tests/test_ogg_opus_writer.py @@ -2,11 +2,14 @@ import pyogg import os -os.chdir(os.path.dirname(__file__)) +from config import Config -def test_zero_length_audio() -> None: +def test_zero_length_audio(pyogg_config: Config) -> None: # Save the audio using OggOpusWriter - filename = "test_ogg_opus_writer__test_zero_length_audio.opus" + filename = str( + pyogg_config.outdir + / "test_ogg_opus_writer__test_zero_length_audio.opus" + ) encoder = pyogg.OpusBufferedEncoder() encoder.set_application("audio") encoder.set_sampling_frequency(48000) @@ -27,9 +30,12 @@ def test_zero_length_audio() -> None: assert len(opus_file.buffer) == 0 -def test_one_frame_audio() -> None: +def test_one_frame_audio(pyogg_config: Config) -> None: # Save the audio using OggOpusWriter - filename = "test_ogg_opus_writer__test_one_frame_audio.opus" + filename = str( + pyogg_config.outdir + / "test_ogg_opus_writer__test_one_frame_audio.opus" + ) encoder = pyogg.OpusBufferedEncoder() encoder.set_application("audio") samples_per_second = 48000 @@ -57,12 +63,15 @@ def test_one_frame_audio() -> None: assert len(bytes(opus_file.buffer)) == bytes_per_sample * frame_size_samples -def test_n_frames_audio() -> None: +def test_n_frames_audio(pyogg_config: Config) -> None: # Number of frames to write n = 2 # Save the audio using OggOpusWriter - filename = f"test_ogg_opus_writer__test_{n}_frames_audio.opus" + filename = str( + pyogg_config.outdir + / f"test_ogg_opus_writer__test_{n}_frames_audio.opus" + ) encoder = pyogg.OpusBufferedEncoder() encoder.set_application("audio") samples_per_second = 48000 @@ -88,13 +97,19 @@ def test_n_frames_audio() -> None: assert len(bytes(opus_file.buffer)) == bytes_per_sample * frame_size_samples * n -def test_duplicate_audio() -> None: +def test_duplicate_audio(pyogg_config: Config) -> None: # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.opus" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.opus" + ) opus_file = pyogg.OpusFile(filename) # Save the audio using OggOpusWriter - out_filename = "test_ogg_opus_writer__test_duplicate_audio.opus" + out_filename = str( + pyogg_config.outdir + / "test_ogg_opus_writer__test_duplicate_audio.opus" + ) encoder = pyogg.OpusBufferedEncoder() encoder.set_application("audio") encoder.set_sampling_frequency(48000) @@ -105,13 +120,19 @@ def test_duplicate_audio() -> None: writer.write(memoryview(opus_file.buffer)) -def test_already_loaded_file() -> None: +def test_already_loaded_file(pyogg_config: Config) -> None: # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.opus" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.opus" + ) opus_file = pyogg.OpusFile(filename) # Save the audio using OggOpusWriter - out_filename = "test_ogg_opus_writer__test_already_loaded_file.opus" + out_filename = str( + pyogg_config.outdir + / "test_ogg_opus_writer__test_already_loaded_file.opus" + ) f = open(out_filename, "wb") encoder = pyogg.OpusBufferedEncoder() encoder.set_application("audio") @@ -126,9 +147,12 @@ def test_already_loaded_file() -> None: f.close() -def test_custom_pre_skip() -> None: +def test_custom_pre_skip(pyogg_config: Config) -> None: # Save the audio using OggOpusWriter - filename = "test_ogg_opus_writer__test_zero_length_audio.opus" + filename = str( + pyogg_config.outdir + / "test_ogg_opus_writer__test_custom_pre_skip.opus" + ) samples_of_pre_skip = 500 encoder = pyogg.OpusBufferedEncoder() encoder.set_application("audio") diff --git a/tests/test_opus_buffered_encoder.py b/tests/test_opus_buffered_encoder.py index 451fa6e..53274ae 100644 --- a/tests/test_opus_buffered_encoder.py +++ b/tests/test_opus_buffered_encoder.py @@ -5,8 +5,6 @@ import pytest import pyogg -os.chdir(os.path.dirname(__file__)) - # Function to create an encoder and encode a sample of silence def init_encoder(samples_per_second:int = 48000, application: str = "audio", diff --git a/tests/test_opus_decoder.py b/tests/test_opus_decoder.py index 787b183..58b74f4 100644 --- a/tests/test_opus_decoder.py +++ b/tests/test_opus_decoder.py @@ -2,8 +2,6 @@ import pyogg import os -os.chdir(os.path.dirname(__file__)) - # Function to create an encoded packet def get_encoded_packet(samples_per_second=48000, application="audio", diff --git a/tests/test_opus_encoder.py b/tests/test_opus_encoder.py index 15b2081..f100895 100644 --- a/tests/test_opus_encoder.py +++ b/tests/test_opus_encoder.py @@ -2,8 +2,6 @@ import pyogg import os -os.chdir(os.path.dirname(__file__)) - # Function to create an encoder and encode a sample of silence def init_encoder(samples_per_second: int = 48000, application: str = "audio", diff --git a/tests/test_opus_file.py b/tests/test_opus_file.py index 303b67d..c715f8b 100644 --- a/tests/test_opus_file.py +++ b/tests/test_opus_file.py @@ -2,23 +2,31 @@ import pyogg import os -os.chdir(os.path.dirname(__file__)) +from config import Config -def test_error_in_filename(): + +def test_error_in_filename() -> None: # Load a non-existant file filename = "does-not-exist.opus" with pytest.raises(pyogg.PyOggError): opus_file = pyogg.OpusFile(filename) -# This shouldn't be a source of error, but it currently is. -def test_unicode_filename(): - filename = "unicode filename 🎵.opus" - with pytest.raises(pyogg.PyOggError): - opus_file = pyogg.OpusFile(filename) +# FIXME: This shouldn't be a source of error, but it currently is. +# This works in macOS and probably Linux, but not Windows. +# def test_unicode_filename(pyogg_config: Config): +# filename = str( +# pyogg_config.rootdir +# / "examples/unicode filename 🎵.opus" +# ) +# opus_file = pyogg.OpusFile(filename) -def test_as_array(): +def test_as_array(pyogg_config: Config) -> None: # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.opus" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.opus" + ) + opus_file = pyogg.OpusFile(filename) # Test that the loaded file is indeed 5 seconds long (using @@ -33,9 +41,12 @@ def test_as_array(): assert duration_samples == expected_duration_samples -def test_as_bytes(): +def test_as_bytes(pyogg_config: Config) -> None: # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.opus" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.opus" + ) opus_file = pyogg.OpusFile(filename) # Test that the loaded file is indeed 5 seconds long (using @@ -54,14 +65,21 @@ def test_as_bytes(): assert duration_bytes == expected_duration_bytes -def test_output_via_wav(): +def test_output_via_wav(pyogg_config: Config) -> None: # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.opus" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.opus" + ) opus_file = pyogg.OpusFile(filename) import wave + out_filename = str( + pyogg_config.outdir + / "test_opus_file__test_output_via_wav.wav" + ) wave_out = wave.open( - "test_opus_file__test_output_via_wav.wav", + out_filename, "wb" ) wave_out.setnchannels(opus_file.channels) diff --git a/tests/test_opus_file_stream.py b/tests/test_opus_file_stream.py index 44e1f97..01fd02d 100644 --- a/tests/test_opus_file_stream.py +++ b/tests/test_opus_file_stream.py @@ -2,7 +2,7 @@ import pyogg import os -os.chdir(os.path.dirname(__file__)) +from config import Config def test_error_in_filename(): # Load a non-existant file @@ -11,9 +11,12 @@ def test_error_in_filename(): opus_stream = pyogg.OpusFileStream(filename) -def test_total_length(): +def test_total_length(pyogg_config: Config): # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.opus" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.opus" + ) # Open the file using OpusFileStream, which does not read the entire # file immediately. @@ -42,9 +45,12 @@ def test_total_length(): assert duration_samples == expected_duration_samples -def test_same_data_as_opus_file(): +def test_same_data_as_opus_file(pyogg_config: Config): # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.opus" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.opus" + ) # Open the file using OpusFile to read the entire file into memory opus_file = pyogg.OpusFile(filename) @@ -71,11 +77,14 @@ def test_same_data_as_opus_file(): assert buf_all == bytes(opus_file.buffer) -def test_same_data_as_opus_file_using_as_array(): +def test_same_data_as_opus_file_using_as_array(pyogg_config: Config): import numpy # type: ignore # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.opus" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.opus" + ) # Open the file using OpusFile to read the entire file into memory opus_file = pyogg.OpusFile(filename) diff --git a/tests/test_vorbis_file.py b/tests/test_vorbis_file.py index 6a51468..590f544 100644 --- a/tests/test_vorbis_file.py +++ b/tests/test_vorbis_file.py @@ -2,7 +2,7 @@ import pyogg import os -os.chdir(os.path.dirname(__file__)) +from config import Config def test_error_in_filename(): # Load a non-existant file @@ -11,9 +11,12 @@ def test_error_in_filename(): vorbis_file = pyogg.VorbisFile(filename) -def test_as_array(): +def test_as_array(pyogg_config: Config): # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.ogg" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.ogg" + ) vorbis_file = pyogg.VorbisFile(filename) # Test that the loaded file is indeed 5 seconds long (using @@ -28,9 +31,12 @@ def test_as_array(): assert duration_samples == expected_duration_samples -def test_as_bytes(): +def test_as_bytes(pyogg_config: Config): # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.ogg" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.ogg" + ) vorbis_file = pyogg.VorbisFile(filename) # Test that the loaded file is indeed 5 seconds long (using @@ -49,9 +55,12 @@ def test_as_bytes(): assert duration_bytes == expected_duration_bytes -def test_as_bytes_one_byte_per_sample(): +def test_as_bytes_one_byte_per_sample(pyogg_config: Config): # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.ogg" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.ogg" + ) vorbis_file = pyogg.VorbisFile(filename, bytes_per_sample=1) # Test that the loaded file is indeed 5 seconds long (using @@ -70,9 +79,12 @@ def test_as_bytes_one_byte_per_sample(): assert duration_bytes == expected_duration_bytes -def test_bytes_per_sample(): +def test_bytes_per_sample(pyogg_config: Config): # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.ogg" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.ogg" + ) vorbis_file_1 = pyogg.VorbisFile(filename, bytes_per_sample=1) vorbis_file_2 = pyogg.VorbisFile(filename, bytes_per_sample=2) @@ -80,14 +92,21 @@ def test_bytes_per_sample(): assert len(vorbis_file_2.buffer) == len(vorbis_file_1.buffer)*2 -def test_output_via_wav(): +def test_output_via_wav(pyogg_config: Config): # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.ogg" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.ogg" + ) vorbis_file = pyogg.VorbisFile(filename) import wave + out_filename = str( + pyogg_config.outdir + / "test_vorbis_file__test_output_via_wav.wav" + ) wave_out = wave.open( - "test_vorbis_file__test_output_via_wav.wav", + out_filename, "wb" ) wave_out.setnchannels(vorbis_file.channels) @@ -96,9 +115,12 @@ def test_output_via_wav(): wave_out.writeframes(vorbis_file.buffer) -def test_output_via_wav_one_byte_per_sample(): +def test_output_via_wav_one_byte_per_sample(pyogg_config: Config): # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.ogg" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.ogg" + ) vorbis_file = pyogg.VorbisFile( filename, bytes_per_sample = 1, @@ -106,14 +128,15 @@ def test_output_via_wav_one_byte_per_sample(): ) import wave + out_filename = str( + pyogg_config.outdir + / "test_vorbis_file__test_output_via_wav_one_byte_per_sample.wav" + ) wave_out = wave.open( - "test_vorbis_file__test_output_via_wav_one_byte_per_sample.wav", + out_filename, "wb" ) wave_out.setnchannels(vorbis_file.channels) wave_out.setsampwidth(vorbis_file.bytes_per_sample) wave_out.setframerate(vorbis_file.frequency) wave_out.writeframes(vorbis_file.buffer) - - - diff --git a/tests/test_vorbis_file_stream.py b/tests/test_vorbis_file_stream.py index 60f8d90..3d25c34 100644 --- a/tests/test_vorbis_file_stream.py +++ b/tests/test_vorbis_file_stream.py @@ -2,7 +2,7 @@ import pyogg import os -os.chdir(os.path.dirname(__file__)) +from config import Config def test_error_in_filename(): # Load a non-existant file @@ -11,9 +11,12 @@ def test_error_in_filename(): vorbis_stream = pyogg.VorbisFileStream(filename) -def test_total_length(): +def test_total_length(pyogg_config: Config): # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.ogg" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.ogg" + ) # Open the file using VorbisFileStream, which does not read the entire # file immediately. @@ -42,9 +45,12 @@ def test_total_length(): assert duration_samples == expected_duration_samples -def test_same_data_as_vorbis_file(): +def test_same_data_as_vorbis_file(pyogg_config: Config): # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.ogg" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.ogg" + ) # Open the file using VorbisFile to read the entire file into memory vorbis_file = pyogg.VorbisFile(filename) @@ -71,11 +77,14 @@ def test_same_data_as_vorbis_file(): assert buf_all == bytes(vorbis_file.buffer) -def test_same_data_as_vorbis_file_using_as_array(): +def test_same_data_as_vorbis_file_using_as_array(pyogg_config: Config): import numpy # type: ignore # Load the demonstration file that is exactly 5 seconds long - filename = "../examples/left-right-demo-5s.ogg" + filename = str( + pyogg_config.rootdir + / "examples/left-right-demo-5s.ogg" + ) # Open the file using VorbisFile to read the entire file into memory vorbis_file = pyogg.VorbisFile(filename)