Skip to content

Commit

Permalink
Add unit tests for missing wavpack binary
Browse files Browse the repository at this point in the history
Work done for #196
  • Loading branch information
atruskie committed Mar 27, 2020
1 parent 6d12298 commit 0358b8c
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/Acoustics.Tools/Audio/AbstractUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,14 @@ protected void CheckExe(FileInfo file, string expectedFileName)

if (file == null)
{
throw new ArgumentNullException(nameof(file));
throw new ArgumentNullException(
nameof(file),
$"Expected path to binary with file name {expectedFileName} but was supplied with null.");
}

if (!File.Exists(file.FullName))
{
throw new FileNotFoundException("Could not find exe: " + file.FullName, file.FullName);
throw new FileNotFoundException("Could not find binary: " + file.FullName, file.FullName);
}

if (!file.Name.Contains(expectedFileName))
Expand Down
16 changes: 16 additions & 0 deletions src/Acoustics.Tools/Audio/AudioFormatNotSupportedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// <copyright file="AudioFormatNotSupportedException.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>

namespace Acoustics.Tools.Audio
{
using System;

public class AudioFormatNotSupportedException : NotSupportedException
{
public AudioFormatNotSupportedException(string message)
: base(message)
{
}
}
}
15 changes: 14 additions & 1 deletion src/Acoustics.Tools/Audio/MasterAudioUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ public override AudioUtilityInfo Info(FileInfo source)

if (mediaType == MediaTypes.MediaTypeWavpack)
{
if (this.wvunpackUtility == null)
{
throw new AudioFormatNotSupportedException(WavPackAudioUtility.MissingBinary);
}

info = this.Combine(this.wvunpackUtility.Info(source), this.ffmpegUtility.Info(source));
}
else if (mediaType == MediaTypes.MediaTypeMp3 || mediaType == MediaTypes.MediaTypeWav)
Expand Down Expand Up @@ -399,11 +404,19 @@ protected override AudioUtilityInfo GetInfo(FileInfo source, ProcessRunner proce
/// </param>
protected override void CheckRequestValid(FileInfo source, string sourceMimeType, FileInfo output, string outputMediaType, AudioUtilityRequest request)
{
// no restrictions
if (this.wvunpackUtility == null)
{
throw new AudioFormatNotSupportedException(WavPackAudioUtility.MissingBinary);
}
}

private FileInfo SegmentWavpackToWav(FileInfo source, AudioUtilityRequest request)
{
if (this.wvunpackUtility == null)
{
throw new AudioFormatNotSupportedException(WavPackAudioUtility.MissingBinary);
}

// use a temp file for wvunpack.
var wavunpackTempFile = TempFileHelper.NewTempFile(this.TemporaryFilesDirectory, MediaTypes.GetExtension(MediaTypes.MediaTypeWav));

Expand Down
16 changes: 13 additions & 3 deletions src/Acoustics.Tools/Audio/WavPackAudioUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class WavPackAudioUtility : AbstractAudioUtility, IAudioUtility
// -w = regenerate .wav header (ignore RIFF data in file)
*/

public const string MissingBinary = "Converting from WavPack is not supported because we cannot find a wvunpack binary.";

private const string ArgsDefault = " -m -q -w ";
private const string ArgsSkip = " --skip={0} ";
private const string ArgsUtil = " --until={0}{1} ";
Expand All @@ -53,10 +55,14 @@ public class WavPackAudioUtility : AbstractAudioUtility, IAudioUtility
/// The wav Unpack.
/// </param>
/// <exception cref="FileNotFoundException">
/// If the provided binary does not exist.
/// </exception>
/// <exception cref="ArgumentNullException">
/// If <paramref name="wavUnpack"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// If <paramref name="wavUnpack"/> does contain the string "wavUnpack".
/// </exception>
/// <exception cref="ArgumentException">wavUnpack</exception>
public WavPackAudioUtility(FileInfo wavUnpack)
{
this.CheckExe(wavUnpack, "wvunpack");
Expand All @@ -73,10 +79,14 @@ public WavPackAudioUtility(FileInfo wavUnpack)
/// The wav Unpack.
/// </param>
/// <exception cref="FileNotFoundException">
/// If the provided binary does not exist.
/// </exception>
/// <exception cref="ArgumentNullException">
/// If <paramref name="wavUnpack"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// If <paramref name="wavUnpack"/> does contain the string "wavUnpack".
/// </exception>
/// <exception cref="ArgumentException">wavUnpack</exception>
public WavPackAudioUtility(FileInfo wavUnpack, DirectoryInfo temporaryFilesDirectory)
{
this.CheckExe(wavUnpack, "wvunpack");
Expand Down Expand Up @@ -128,7 +138,7 @@ protected override string ConstructModifyArgs(FileInfo source, FileInfo output,
// only deals with start and end, does not do anything with sampling, channels or bit rate.
if (request.OffsetStart.HasValue || request.OffsetEnd.HasValue)
{

if (request.OffsetStart.HasValue && request.OffsetStart.Value > TimeSpan.Zero)
{
sb.AppendFormat(ArgsSkip, FormatTimeSpan(request.OffsetStart.Value));
Expand Down
11 changes: 11 additions & 0 deletions src/AnalysisPrograms/CheckEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class CheckEnvironment
private int Execute(Arguments arguments)
{
var errors = new List<string>();
var warnings = new List<string>();

Log.Info("Checking required executables and libraries can be found and loaded");

Expand All @@ -49,6 +50,11 @@ private int Execute(Arguments arguments)
errors.Add(ex.Message);
}

if (AppConfigHelper.WvunpackExe == null)
{
warnings.Add("Cannot find wvunpack - we'll be unable to process any wavpack files.");
}

if (MainEntry.CheckForDataAnnotations() is string message)
{
errors.Add(message);
Expand All @@ -60,6 +66,11 @@ private int Execute(Arguments arguments)
errors.Add($"We no longer use Mono with ${Meta.Name}. DO NOT prefix the {Meta.Name} prefix with `mono`.");
}

foreach (var warning in warnings)
{
Log.Warn(warning);
}

// don't have much more to check at the current time
if (errors.Count == 0)
{
Expand Down
37 changes: 34 additions & 3 deletions tests/Acoustics.Test/Tools/MasterAudioUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public void ValidatesNonExistingExePaths()
public void ValidatesNullExePaths()
{
TestHelper.ExceptionMatches<ArgumentNullException>(
() => new FfmpegAudioUtility(null,null), "Value cannot be null");
() => new FfmpegAudioUtility(null, null), "Value cannot be null");

//TestHelper.ExceptionMatches<ArgumentNullException>(
// () => new Mp3SpltAudioUtility(null), "Value cannot be null");
Expand All @@ -358,13 +358,44 @@ public void ValidatesNullExePaths()
public void MasterAudioUtilityAllowsOptionalSupportForMp3splt()
{
// creation should normally fail but MasterAudioUtility was changed so that Mp3Splt was optional
var utility = new MasterAudioUtility(
new MasterAudioUtility(
(FfmpegAudioUtility)TestHelper.GetAudioUtilityFfmpeg(),
(WavPackAudioUtility)TestHelper.GetAudioUtilityWavunpack(),
(SoxAudioUtility)TestHelper.GetAudioUtilitySox(),
(FfmpegRawPcmAudioUtility)TestHelper.GetAudioUtilityFfmpegRawPcm());
}

[TestMethod]
public void MasterAudioUtilityAllowsOptionalSupportFoWavPack()
{
// creation should normally fail but MasterAudioUtility was changed so that WavPack was optional
new MasterAudioUtility(
(FfmpegAudioUtility)TestHelper.GetAudioUtilityFfmpeg(),
null,
(SoxAudioUtility)TestHelper.GetAudioUtilitySox(),
(FfmpegRawPcmAudioUtility)TestHelper.GetAudioUtilityFfmpegRawPcm());
}

[TestMethod]
public void MasterAudioUtilityCheckRequestValidFailsWhenWavpackIsMissing()
{
var utility = new MasterAudioUtility(
(FfmpegAudioUtility)TestHelper.GetAudioUtilityFfmpeg(),
null,
(SoxAudioUtility)TestHelper.GetAudioUtilitySox(),
(FfmpegRawPcmAudioUtility)TestHelper.GetAudioUtilityFfmpegRawPcm());

var source = PathHelper.GetTestAudioFile("Raw_audio_id_cd6e8ba1-11b4-4724-9562-f6ec893110aa.wv");

Assert.ThrowsException<AudioFormatNotSupportedException>(
() => utility.Info(source),
"Converting from WavPack is not supported because we cannot find a wvunpack binary.");

Assert.ThrowsException<AudioFormatNotSupportedException>(
() => utility.Modify(source, MediaTypes.MediaTypeWavpack, PathHelper.GetTempFile(MediaTypes.ExtWav), MediaTypes.MediaTypeWav, new AudioUtilityRequest()),
"Converting from WavPack is not supported because we cannot find a wvunpack binary.");
}

private static FileInfo GetAudioUtilityExe(string name)
{
var baseResourceDir = PathHelper.GetResourcesBaseDir();
Expand Down Expand Up @@ -498,7 +529,7 @@ private static string GetDurationInfo(AudioUtilityInfo info)
private static void SegmentsCorrectly(
string filename, string mimetype, TimeSpan start, TimeSpan end, TimeSpan maxVariance)
{
foreach (var util in new[] { TestHelper.GetAudioUtility()})
foreach (var util in new[] { TestHelper.GetAudioUtility() })
{
var dir = PathHelper.GetTempDir();

Expand Down

0 comments on commit 0358b8c

Please sign in to comment.