Skip to content
This repository has been archived by the owner on May 27, 2019. It is now read-only.

Commit

Permalink
Refactor Ysfo, move IsPathValid method to PathHelper class
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspiller committed Aug 27, 2009
1 parent 927e9f9 commit d785ca2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 29 deletions.
32 changes: 32 additions & 0 deletions src/Ysfo.Core/Loaders/PathHelper.cs
Expand Up @@ -82,5 +82,37 @@ public static String ConvertLstEntryToNative(String lstEntry, char directorySepa
throw new ArgumentException("Unknown DirectorySeparatorChar `" + directorySeparatorChar + "'.");
}
}

public const String WindowsExecutable = "fsmain.exe";
public const String LinuxExecutable = "ysflight";

/// <summary>
/// Checks whether <paramref name="ysPath"/> is a valid YsFlight directory.
/// </summary>
/// <param name="ysPath">The path to the YsFlight base directory.</param>
/// <returns><code>true</code> if <paramref name="ysPath"/> is valid.</returns>
public static Boolean IsPathValid(String ysPath)
{
// check path is not null
if (ysPath == null)
{
return false;
}

// check directory
if (!Directory.Exists(ysPath))
{
return false;
}

// check ysflight exectutable
if (!File.Exists(Path.Combine(ysPath, WindowsExecutable)) && !File.Exists(Path.Combine(ysPath, LinuxExecutable)))
{
return false;
}

// yay
return true;
}
}
}
28 changes: 3 additions & 25 deletions src/Ysfo.Core/Ysfo.cs
Expand Up @@ -19,34 +19,12 @@ public void Dispose()
}

/// <summary>
/// Checks whether Path is a valid ysflight directory.
/// Checks whether Path is a valid YsFlight directory.
/// </summary>
/// <returns>true is Path is valid.</returns>
/// <returns><code>true</code> if Path is valid.</returns>
public Boolean IsPathValid()
{
// check path is not null
if (Path == null)
{
return false;
}

// check directory
if (!Directory.Exists(Path))
{
return false;
}

String windowsYsFlight = "fsmain.exe";
String linuxYsFlight = "ysflight";

// check ysflight exectutable
if (!File.Exists(System.IO.Path.Combine(Path, windowsYsFlight)) && !File.Exists(System.IO.Path.Combine(Path, linuxYsFlight)))
{
return false;
}

// yay
return true;
return Loaders.PathHelper.IsPathValid(Path);
}

/// <summary>
Expand Down
44 changes: 40 additions & 4 deletions tests/Ysfo.Tests.Core/Loaders/PathHelperFixture.cs
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Ysfo.Core.Loaders;

Expand Down Expand Up @@ -32,7 +29,7 @@ public void GetFullPathThrowsExceptionIfRelPathIsNull()
[ExpectedException(typeof(DirectoryNotFoundException))]
public void GetFullPathThrowsExceptionIfYsPathIsInvalid()
{
String invalidYsPath = System.IO.Path.Combine(_validYsPath, "invaliddir");
String invalidYsPath = Path.Combine(_validYsPath, "invaliddir");

PathHelper.GetFullPath(invalidYsPath, _validLstPath);
}
Expand Down Expand Up @@ -72,5 +69,44 @@ public void ConvertLstEntryIsCorrectForBackSlashDirectorySeperatorChar()

Assert.AreEqual("aircraft\\aircraft.lst", result);
}

[Test]
public void IsPathValidReturnsFalseIfYsPathIsNull()
{
Boolean result = PathHelper.IsPathValid(null);

Assert.IsFalse(result);
}

[Test]
public void IsPathValidReturnsFalseIfYsFlightExecutableDoesntExist()
{
Boolean result = PathHelper.IsPathValid(_validYsPath);

Assert.IsFalse(result);
}

[Test]
public void IsPathValidReturnsTrueIfValid()
{
String ysExecutable = Path.Combine(_validYsPath, PathHelper.WindowsExecutable);

// create file
File.Create(ysExecutable).Close();
Assert.IsTrue(File.Exists(ysExecutable), "Temporary executable not created!");

Boolean result = PathHelper.IsPathValid(_validYsPath);

try
{
Assert.IsTrue(result);
}
finally
{
// cleanup
File.Delete(ysExecutable);
Assert.IsFalse(File.Exists(ysExecutable), "Temporary executable not removed!");
}
}
}
}

0 comments on commit d785ca2

Please sign in to comment.