Skip to content

Commit

Permalink
Fixes PathUtils tests for Linux
Browse files Browse the repository at this point in the history
The shortname functions and unicode path restrictions have no effect in linux.
Cleaned up the tests and ensured they work cross platform.

Also added convenience string constants to PlatformSpecificTestMethod

Work done for #196
  • Loading branch information
atruskie committed Mar 19, 2020
1 parent f952de3 commit 71ee22e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
5 changes: 4 additions & 1 deletion src/Acoustics.Shared/Meta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static class Meta

public const string Name = "AnalysisPrograms.exe";

public const string ProjectName = "AnalysisPrograms";

public static readonly int NowYear = DateTime.Now.Year;

private static Assembly[] ourAssemblies;
Expand All @@ -38,7 +40,8 @@ internal static Assembly[] QutAssemblies
{
get
{
if (ourAssemblies == null) {
if (ourAssemblies == null)
{
// warning: there are subtle runtime races that change which assemblies are
// currently loaded when this code runs. Caching it once with static
// intializer results in us returning only some of the assemblies
Expand Down
2 changes: 1 addition & 1 deletion src/Acoustics.Shared/PathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static bool HasUnicodeOrUnsafeChars(string path)
/// modification.
/// </remarks>
/// <param name="path">The path to convert.</param>
/// <exception cref="FileNotFoundException">If the requested file does not exist</exception>
/// <exception cref="FileNotFoundException">If the requested file does not exist.</exception>
/// <returns>An 8.3 filename extracted from kernel32.dll.</returns>
public static string GetShortFilename(string path)
{
Expand Down
42 changes: 21 additions & 21 deletions tests/Acoustics.Test/Shared/PathUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace Acoustics.Test.Shared
using System;
using System.IO;
using Acoustics.Shared;
using Acoustics.Test.TestHelpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelpers;
using static Acoustics.Test.TestHelpers.PlatformSpecificTestMethod;

[TestClass]
public class PathUtilsTests : OutputDirectoryTest
Expand All @@ -25,30 +26,15 @@ public void CanDetectUnicodePaths()
Assert.IsFalse(PathUtils.HasUnicodeOrUnsafeChars(path));
}

[TestMethod]
public void CanGetShortFileNames()
[PlatformSpecificTestMethod(Windows)]
public void CanGetShortFileNamesWindows()
{

var path = this.TestOutputDirectory.CombineFile("bad folder", "REC_C_20180616_145526😂.wav").Touch();

var actual = PathUtils.GetShortFilename(path.FullName);
string[] expected;
if (AppConfigHelper.IsWindows)
{
expected = new[]
{
this.TestOutputDirectory.Root.Name, "BADFOL~1", "REC_C_~1.WAV",
};
}
else if (AppConfigHelper.IsLinux || AppConfigHelper.IsMacOsX)
{
expected = string.Copy(actual).AsArray();
}
else
{
throw new InvalidOperationException();
}

// make sure each segment of the path is what we expect
string[] expected = new[] { this.TestOutputDirectory.Root.Name, "BADFOL~1", "REC_C_~1.WAV" };
foreach (var fragment in expected)
{
StringAssert.Contains(actual, fragment);
Expand All @@ -58,7 +44,21 @@ public void CanGetShortFileNames()
Assert.IsFalse(PathUtils.HasUnicodeOrUnsafeChars(actual));
}

[TestMethod]
[PlatformSpecificTestMethod(NotWindows)]
public void CanGetShortFileNamesOther()
{
var path = this.TestOutputDirectory.CombineFile("bad folder", "REC_C_20180616_145526😂.wav").Touch();

var actual = PathUtils.GetShortFilename(path.FullName);

Assert.That.FileExists(actual);

// path should be unchanged
Assert.IsTrue(PathUtils.HasUnicodeOrUnsafeChars(actual));
Assert.That.StringEqualWithDiff(path.FullName, actual);
}

[PlatformSpecificTestMethod(Windows)]
public void ShortFilenameValidatesFileExistence()
{
Assert.ThrowsException<FileNotFoundException>(
Expand Down
2 changes: 1 addition & 1 deletion tests/Acoustics.Test/TestHelpers/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal static void Initialize(TestContext context)
CodeBase = context.DeploymentDirectory;
TestResources = Path.Combine(SolutionRoot, "tests", "Fixtures");

AnalysisProgramsBuild = Path.Combine(SolutionRoot, "src", Meta.Name, "bin", "Debug", "netcoreapp3.1");
AnalysisProgramsBuild = Path.Combine(SolutionRoot, "src", Meta.ProjectName, "bin", "Debug", "netcoreapp3.1");

testContext = context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ namespace Acoustics.Test.TestHelpers
/// </summary>
public class PlatformSpecificTestMethod : TestMethodAttribute
{
public const string Windows = "WINDOWS";
public const string NotWindows = "!WINDOWS";
public const string OSX = "OSX";
public const string NotOSX = "!OSX";
public const string Linux = "LINUX";
public const string NotLinux = "!LINUX";

/// <summary>
/// Initializes a new instance of the <see cref="PlatformSpecificTestMethod"/> class.
/// </summary>
Expand Down

0 comments on commit 71ee22e

Please sign in to comment.