From 99da050752a324c23d7bc2d882e1aecf46159389 Mon Sep 17 00:00:00 2001 From: Eric Joanis Date: Thu, 7 Nov 2024 13:27:02 -0500 Subject: [PATCH] fix(tests): silence test_anchors and test_force_align suites includes using silence_c_stderr to hide SoundSwallower logs --- test/test_anchors.py | 33 +++++++++++++++++++-------------- test/test_force_align.py | 34 +++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/test/test_anchors.py b/test/test_anchors.py index 7d43bec6..be7045c4 100755 --- a/test/test_anchors.py +++ b/test/test_anchors.py @@ -3,9 +3,11 @@ """Unit testing for the anchors functionality in readalongs align""" import os +from contextlib import redirect_stderr +from io import StringIO from unittest import main -from basic_test_case import BasicTestCase +from basic_test_case import BasicTestCase, silence_c_stderr from readalongs.align import align_audio from readalongs.log import LOGGER @@ -18,10 +20,11 @@ def test_anchors_inner_only(self): """Test aligning with anchors only between existing text""" # ej-fra-anchors has anchors between words/sentences only - results = align_audio( - os.path.join(self.data_dir, "ej-fra-anchors.readalong"), - os.path.join(self.data_dir, "ej-fra.m4a"), - ) + with redirect_stderr(StringIO()): + results = align_audio( + os.path.join(self.data_dir, "ej-fra-anchors.readalong"), + os.path.join(self.data_dir, "ej-fra.m4a"), + ) words = results["words"] # The input text file has 99 words, so should the aligned segments. self.assertEqual(len(words), 99) @@ -39,11 +42,12 @@ def test_anchors_outer_too(self): # ej-fra-anchors2 also has anchors before the first word and after the last word save_temps_prefix = os.path.join(self.tempdir, "anchors2-temps") - results = align_audio( - os.path.join(self.data_dir, "ej-fra-anchors2.readalong"), - os.path.join(self.data_dir, "ej-fra.m4a"), - save_temps=save_temps_prefix, - ) + with redirect_stderr(StringIO()): + results = align_audio( + os.path.join(self.data_dir, "ej-fra-anchors2.readalong"), + os.path.join(self.data_dir, "ej-fra.m4a"), + save_temps=save_temps_prefix, + ) words = results["words"] # The input text file has 99 words, so should the aligned segments. self.assertEqual(len(words), 99) @@ -83,10 +87,11 @@ def test_anchors_align_modes(self): with open(xml_file, "wt", encoding="utf8") as f: print(xml_with_anchors, file=f) with self.assertLogs(LOGGER, level="INFO") as cm: - results = align_audio( - xml_file, - os.path.join(self.data_dir, "noise.mp3"), - ) + with silence_c_stderr(), redirect_stderr(StringIO()): + results = align_audio( + xml_file, + os.path.join(self.data_dir, "noise.mp3"), + ) words = results["words"] self.assertEqual(len(words), 10) logger_output = "\n".join(cm.output) diff --git a/test/test_force_align.py b/test/test_force_align.py index d27e2d4c..2334ca11 100755 --- a/test/test_force_align.py +++ b/test/test_force_align.py @@ -8,9 +8,11 @@ import shutil import unittest import wave +from contextlib import redirect_stderr +from io import StringIO from tempfile import TemporaryDirectory -from basic_test_case import BasicTestCase +from basic_test_case import BasicTestCase, silence_c_stderr from lxml import etree from soundswallower import get_model_path @@ -32,7 +34,8 @@ def test_align(self): """Basic alignment test case with XML input""" xml_path = os.path.join(self.data_dir, "ej-fra.readalong") wav_path = os.path.join(self.data_dir, "ej-fra.m4a") - results = align_audio(xml_path, wav_path, unit="w", debug_aligner=True) + with silence_c_stderr(), redirect_stderr(StringIO()): + results = align_audio(xml_path, wav_path, unit="w", debug_aligner=True) # Verify that the same IDs are in the output converted_path = os.path.join(self.data_dir, "ej-fra-converted.readalong") @@ -50,7 +53,8 @@ def test_align_text(self): _, temp_fn = create_input_ras( input_file_name=txt_path, text_languages=("fra",), save_temps=None ) - results = align_audio(temp_fn, wav_path, unit="w", save_temps=None) + with redirect_stderr(StringIO()): + results = align_audio(temp_fn, wav_path, unit="w", save_temps=None) # Verify that the same IDs are in the output converted_path = os.path.join(self.data_dir, "ej-fra-converted.readalong") @@ -131,14 +135,22 @@ def test_align_switch_am(self): with open(os.path.join(custom_am_path, "noisedict"), "at") as fh: fh.write(";; here is a comment\n") fh.write("[BOGUS] SIL\n") - results = align_audio( - xml_path, wav_path, unit="w", config={"acoustic_model": custom_am_path} - ) + with redirect_stderr(StringIO()): + results = align_audio( + xml_path, + wav_path, + unit="w", + config={"acoustic_model": custom_am_path}, + ) # Try with no noisedict os.remove(os.path.join(custom_am_path, "noisedict")) - results = align_audio( - xml_path, wav_path, unit="w", config={"acoustic_model": custom_am_path} - ) + with redirect_stderr(StringIO()): + results = align_audio( + xml_path, + wav_path, + unit="w", + config={"acoustic_model": custom_am_path}, + ) # Verify that the same IDs are in the output converted_path = os.path.join(self.data_dir, "ej-fra-converted.readalong") xml = load_xml(converted_path) @@ -157,11 +169,11 @@ def test_align_fail(self): writer.setsampwidth(2) writer.setframerate(16000) writer.writeframes(b"\x00\x00") - with self.assertRaises(RuntimeError): + with self.assertRaises(RuntimeError), redirect_stderr(StringIO()): _ = align_audio(xml_path, tf.name, unit="w") def test_bad_align_mode(self): - with self.assertRaises(AssertionError): + with self.assertRaises(AssertionError), redirect_stderr(StringIO()): _ = align_audio( os.path.join(self.data_dir, "ej-fra.readalong"), os.path.join(self.data_dir, "noise.mp3"),