Skip to content

Commit

Permalink
Fix tests for Mac CI
Browse files Browse the repository at this point in the history
Geneally had to losen deltas on unit tests 😰 because ffprobe reports different values than sox.

Also changed the way log files are cleaned up in the hopes that it musl builds will now actually clean log files.

Fixed faulty timespan assertion - the formatting string in the failed case caused a format exception!

Adds a warning to CheckEnvironment for lacking MP3 support on MacOSX

Work done for #196
  • Loading branch information
atruskie committed Mar 30, 2020
1 parent 4432180 commit 7cf239a
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build/azure-pipelines-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ parameters:
pool: macos-latest
configurations: [debug, release]

- rid: ~ # framework dependent build
- rid: '' # framework dependent build
pool: ubuntu-latest
configurations: [release]
# cross-product not supported
Expand Down
16 changes: 11 additions & 5 deletions src/Acoustics.Shared/Logging/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Acoustics.Shared.Logging
using log4net.Util;
using static log4net.Appender.ManagedColoredConsoleAppender;

public class Logging : IDisposable
public class Logging : IDisposable, IAsyncDisposable
{
public const string CleanLogger = "CleanLogger";
public const string LogFileOnly = "LogFileOnly";
Expand All @@ -40,6 +40,7 @@ public class Logging : IDisposable
private readonly AppenderSkeleton standardConsoleAppender;
private readonly AppenderSkeleton cleanConsoleAppender;
private readonly Hierarchy repository;
private readonly ValueTask cleanFilesTask;

static Logging()
{
Expand Down Expand Up @@ -204,7 +205,7 @@ internal Logging(
// We'll never know if this fails or not, the exception is captured in the task
// that we do NOT await. We do however log any exceptions.
// ReSharper disable once AssignmentIsFullyDiscarded
_ = this.CleanLogs(this.LogFilePath);
this.cleanFilesTask = this.CleanLogs(this.LogFilePath);
}
}

Expand Down Expand Up @@ -276,18 +277,23 @@ public void TestLogging()
LoggedConsole.Log.Info("Clean log INFO");
}

public async ValueTask DisposeAsync()
{
await this.cleanFilesTask;
}

/// <summary>
/// Rolling log file appender has no concept of cleaning up logs with a date stamp in their name.
/// This we have to clean them manually.
/// </summary>
/// <returns>A task.</returns>
private async Task CleanLogs(string logFilePath)
private async ValueTask CleanLogs(string logFilePath)
{
Contract.RequiresNotNull(logFilePath);
const int threshold = 60;
int target = 50;

void CleanFiles()
async ValueTask CleanFilesAsync()
{
var logsPath = Path.GetDirectoryName(logFilePath) ??
throw new InvalidOperationException("Could not resolve logs directory path: " + logFilePath);
Expand Down Expand Up @@ -347,7 +353,7 @@ void CleanFiles()
try
{
// ReSharper disable once RedundantCast
await Task.Run((Action)CleanFiles);
await CleanFilesAsync();
}
catch (Exception ex)
{
Expand Down
10 changes: 3 additions & 7 deletions src/Acoustics.Tools/Audio/MasterAudioUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ namespace Acoustics.Tools.Audio
using System;
using System.Collections.Generic;
using System.IO;
using Shared;
using Shared.Contracts;
using Acoustics.Shared;
using Acoustics.Shared.Contracts;

/// <summary>
/// Combined audio utility that makes use of the most appropriate audio utility for the task.
/// </summary>
public class MasterAudioUtility : AbstractAudioUtility, IAudioUtility
{
private static bool missingMp3SpltWarned = false;

private readonly WavPackAudioUtility wvunpackUtility;

private readonly FfmpegAudioUtility ffmpegUtility;
Expand Down Expand Up @@ -67,13 +65,11 @@ public MasterAudioUtility(DirectoryInfo temporaryFilesDirectory)
/// </summary>
/// <param name="ffmpegUtility">ffmpeg utility.
/// </param>
/// <param name="mp3SpltUtility">mp3splt utility.
/// </param>
/// <param name="wvunpackUtility">wxunpack utility.
/// </param>
/// <param name="soxUtility">sox utility.
/// </param>
/// <param name="ffmpegRawPcmUtility">The ffmpeg utility for converting raw PCM data</param>
/// <param name="ffmpegRawPcmUtility">The ffmpeg utility for converting raw PCM data.</param>
/// <param name="temporaryFilesDirectory">Directory for temporary files.</param>
public MasterAudioUtility(
FfmpegAudioUtility ffmpegUtility,
Expand Down
6 changes: 6 additions & 0 deletions src/AnalysisPrograms/CheckEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace AnalysisPrograms
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Acoustics.Shared;
Expand Down Expand Up @@ -55,6 +56,11 @@ private int Execute(Arguments arguments)
warnings.Add("Cannot find wvunpack - we'll be unable to process any wavpack files.");
}

if (!new SoxAudioUtility(new FileInfo(AppConfigHelper.SoxExe)).SupportsMp3)
{
warnings.Add(SoxAudioUtility.Mp3NotSupportedOnOSX);
}

if (MainEntry.CheckForDataAnnotations() is string message)
{
errors.Add(message);
Expand Down
5 changes: 2 additions & 3 deletions tests/Acoustics.Test/AnalysisBase/FileSegmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Acoustics.Test.AnalysisBase
{
using System;
using Acoustics.Test.TestHelpers;
using global::AnalysisBase;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelpers;

[TestClass]
public class FileSegmentTests
Expand Down Expand Up @@ -70,11 +70,10 @@ public void SecondConstructorAutomaticallyExtractsInfo()
Assert.AreEqual(null, s.SegmentEndOffset);
Assert.AreEqual(0, s.StartOffsetSeconds);
Assert.AreEqual(info.Duration.Value.TotalSeconds, s.EndOffsetSeconds, 1.0);
Assert.That.AreEqual(info.Duration.Value, s.TargetFileDuration.Value, TimeSpan.FromMilliseconds(0.050));
Assert.That.AreEqual(info.Duration.Value, s.TargetFileDuration.Value, TimeSpan.FromMilliseconds(0.1));
Assert.AreEqual(info.SampleRate, s.TargetFileSampleRate);
Assert.AreEqual("Currawongs_curlew_West_Knoll_Bees_20091102-183000", s.SourceMetadata.Identifier);
Assert.IsNull(s.TargetFileStartDate);

}
}
}
8 changes: 7 additions & 1 deletion tests/Acoustics.Test/AnalysisPrograms/MainEntryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Acoustics.Test.AnalysisPrograms
using System.Threading.Tasks;
using Acoustics.Shared;
using Acoustics.Test.TestHelpers;
using Acoustics.Tools.Audio;
using global::AnalysisPrograms;
using global::AnalysisPrograms.Production.Arguments;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -110,7 +111,12 @@ public async Task CheckEnvironmentWorks(string rid)

this.AssertContainsCopyright(console.Lines);
this.AssertContainsGitHashAndVersion(console.Lines);
StringAssert.Contains(console.Lines[4], "SUCCESS - Valid environment");
if (IsMacOsX)
{
StringAssert.Contains(console.Lines[4], SoxAudioUtility.Mp3NotSupportedOnOSX);
}

StringAssert.Contains(console.Lines[^2], "SUCCESS - Valid environment");
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private static void AssertSegmentsAreEqual(ISegment<FileInfo>[] acutal, Range<do

Assert.IsTrue(actual.IsSegmentSet);
Assert.AreEqual(expectedStart, actual.SegmentStartOffset.Value.TotalSeconds);
Assert.AreEqual(expectedEnd, actual.SegmentEndOffset.Value.TotalSeconds);
Assert.AreEqual(expectedEnd, actual.SegmentEndOffset.Value.TotalSeconds, 0.1);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Acoustics.Test/Shared/LoggingTests/LoggingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ public async Task TestLogFilesAreCleaned()
// its cleaning logic
do
{
new Logging(false, Level.Info, quietConsole: false).Dispose();
using (new Logging(false, Level.Info, quietConsole: false))
{
}

delta--;
}
while (delta > 0);
Expand Down
10 changes: 5 additions & 5 deletions tests/Acoustics.Test/TestHelpers/Assertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void AreEqual(this Assert assert, TimeSpan expected, TimeSpan actu
{
message = message == null ? string.Empty : message + "\n";
Assert.Fail(
$"{message}Actual delta ({actualDelta}) between expected value ({expected:O}) and actual value ({actual:O}) was not less than {delta}");
$"{message}Actual delta ({actualDelta}) between expected value ({expected}) and actual value ({actual}) was not less than {delta}");
}
}

Expand Down Expand Up @@ -217,8 +217,8 @@ public static void DirectoryExists(this Assert assert, string path)
var exists = Directory.Exists(Path.GetFullPath(path));
if (!exists)
{
PathDiagnostics.PathExistsOrDiff(path, out var diff);
Assert.Fail($"Expected path {path} to exist but it could not be found. Path diagnostics:\n{diff.Messages}");
PathDiagnostics.PathExistsOrDiff(path, out var diff);
Assert.Fail($"Expected path {path} to exist but it could not be found. Path diagnostics:\n{diff.Messages}");
}
}

Expand All @@ -232,8 +232,8 @@ public static void FileExists(this Assert assert, string path)
var exists = File.Exists(Path.GetFullPath(path));
if (!exists)
{
PathDiagnostics.PathExistsOrDiff(path, out var diff);
Assert.Fail($"Expected path {path} to exist but it could not be found. Path diagnostics:\n{diff.Messages}");
PathDiagnostics.PathExistsOrDiff(path, out var diff);
Assert.Fail($"Expected path {path} to exist but it could not be found. Path diagnostics:\n{diff.Messages}");
}
}

Expand Down
24 changes: 19 additions & 5 deletions tests/Acoustics.Test/Tools/AudioUtilityInfoTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// <copyright file="AudioUtilityInfoTests.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.Test.Tools
{
using System;
Expand All @@ -6,9 +10,9 @@ namespace Acoustics.Test.Tools
using System.Linq;
using System.Threading;
using Acoustics.Shared;
using Acoustics.Test.TestHelpers;
using Acoustics.Tools.Audio;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelpers;

[TestClass]
public class AudioUtilityInfoTests
Expand Down Expand Up @@ -213,14 +217,24 @@ public void InfoFailsForShnTool(string file)
[DataRow("4min test.mp3")]
public void InfoWorksSoxTool(string file)
{
var util = TestHelper.GetAudioUtilitySox();
var util = TestHelper.GetAudioUtilitySox() as SoxAudioUtility;

var source = TestHelper.GetAudioFile(file);
var info = util.Info(source);

var expected = TestHelper.AudioDetails[file];
if (util.SupportsMp3)
{
var info = util.Info(source);

TestHelper.CheckAudioUtilityInfo(expected, info);
var expected = TestHelper.AudioDetails[file];

TestHelper.CheckAudioUtilityInfo(expected, info);
}
else
{
Assert.ThrowsException<AudioFormatNotSupportedException>(
() => util.Info(source),
"cannot be processed. Valid formats are: wav (audio/wav), flac (audio/flac).");
}
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion tests/Acoustics.Test/Tools/AudioUtilityMp3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void SegmentsMp3NotAvailableOnOsxWithSox()

Assert.ThrowsException<AudioFormatNotSupportedException>(
() => util.Info(source),
"Working with MP3 in SoX is not supported on OSX.");
"cannot be processed. Valid formats are: wav (audio/wav), flac (audio/flac).");

Assert.ThrowsException<AudioFormatNotSupportedException>(
() => util.Modify(source, MediaTypes.GetMediaType(source.Extension), output, MediaTypes.GetMediaType(output.Extension), request),
Expand Down

0 comments on commit 7cf239a

Please sign in to comment.