Skip to content

Commit 7fdf803

Browse files
MrDesjardinsclaude
andcommitted
Fix flaky test in CI: mock subprocess.run in retry tests
The test_transcribe_audio_retries_on_failure test was failing in CI with 5 sleep calls instead of the expected 2. This was caused by unmocked subprocess.run calls (ffprobe) inside the retry loop, which behaved differently in the CI environment. Changes: - Added @patch for subprocess.run in both retry test methods - Properly configured Path mock to handle .stat().st_size calls - Mock ffprobe to return consistent results This makes the tests more isolated and deterministic across different environments. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent f1407a2 commit 7fdf803

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

tests/services/test_transcription.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ def test_transcribe_audio_compression_fails(
302302
transcribe_audio("/path/to/audio.mp3")
303303

304304
@patch("services.transcription.expand_path")
305+
@patch("services.transcription.subprocess.run")
305306
@patch("services.transcription.compress_audio_for_whisper")
306307
@patch("services.transcription.Path")
307308
@patch("services.transcription.os.path.getsize")
@@ -318,6 +319,7 @@ def test_transcribe_audio_retries_on_failure(
318319
mock_getsize,
319320
mock_path,
320321
mock_compress,
322+
mock_subprocess,
321323
mock_expand_path,
322324
):
323325
"""Test transcription retries on failure."""
@@ -332,11 +334,18 @@ def test_transcribe_audio_retries_on_failure(
332334
mock_expanded_path.stat.return_value = mock_stat
333335
mock_expand_path.return_value = mock_expanded_path
334336

335-
# Mock Path(compressed_path) for cleanup
337+
# Mock Path(compressed_path) for cleanup and stat calls
336338
mock_compressed_path_instance = Mock()
337339
mock_compressed_path_instance.exists.return_value = True
340+
mock_compressed_path_instance.stat.return_value = mock_stat
338341
mock_path.return_value = mock_compressed_path_instance
339342

343+
# Mock subprocess.run for ffprobe call
344+
mock_ffprobe_result = Mock()
345+
mock_ffprobe_result.returncode = 0
346+
mock_ffprobe_result.stdout = "120.5"
347+
mock_subprocess.return_value = mock_ffprobe_result
348+
340349
mock_config.return_value = config
341350

342351
mock_getsize.return_value = 5 * 1024 * 1024
@@ -363,6 +372,7 @@ def test_transcribe_audio_retries_on_failure(
363372
assert mock_sleep.call_count == 2
364373

365374
@patch("services.transcription.expand_path")
375+
@patch("services.transcription.subprocess.run")
366376
@patch("services.transcription.compress_audio_for_whisper")
367377
@patch("services.transcription.Path")
368378
@patch("services.transcription.os.path.getsize")
@@ -379,6 +389,7 @@ def test_transcribe_audio_fails_after_retries(
379389
mock_getsize,
380390
mock_path,
381391
mock_compress,
392+
mock_subprocess,
382393
mock_expand_path,
383394
):
384395
"""Test transcription fails after all retries."""
@@ -394,11 +405,18 @@ def test_transcribe_audio_fails_after_retries(
394405
mock_expanded_path.stat.return_value = mock_stat
395406
mock_expand_path.return_value = mock_expanded_path
396407

397-
# Mock Path(compressed_path) for cleanup
408+
# Mock Path(compressed_path) for cleanup and stat calls
398409
mock_compressed_path_instance = Mock()
399410
mock_compressed_path_instance.exists.return_value = True
411+
mock_compressed_path_instance.stat.return_value = mock_stat
400412
mock_path.return_value = mock_compressed_path_instance
401413

414+
# Mock subprocess.run for ffprobe call
415+
mock_ffprobe_result = Mock()
416+
mock_ffprobe_result.returncode = 0
417+
mock_ffprobe_result.stdout = "120.5"
418+
mock_subprocess.return_value = mock_ffprobe_result
419+
402420
mock_getsize.return_value = 5 * 1024 * 1024
403421
mock_compress.return_value = "/tmp/compressed.mp3"
404422
mock_exists.return_value = True

0 commit comments

Comments
 (0)