Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
XUnit test for missing files
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Xunit;
namespace Isas.Shared.Tests
{
/// <summary>
/// The following test class was adapted from the following link:
/// http://www.codeproject.com/Articles/335584/Unit-Test-looking-for-missing-files-in-your-projec
/// </summary>
public class MissingFileTest
{
[Fact]
public void CheckingThatAllFilesAreInSolution()
{
var solutionDirectory = GetSolutionDirectory("your solution directory name goes here");
var solutionSubdirectoriesToSearch = new[] { "directory1", "directory2", "directory3", "directory4" };
var solutionSubdirectoriesToIgnore = new[] { "ignore1", "ignore2", "ignore3" };
var solutionProjectFiles = new List<string>();
foreach (var s in solutionSubdirectoriesToSearch)
{
solutionProjectFiles.AddRange(Directory.GetFileSystemEntries(Path.Combine(solutionDirectory.FullName, s), "*.csproj",
SearchOption.AllDirectories));
}
var errorCollection = new Dictionary<string, List<string>>();
foreach (var solutionProjectFile in solutionProjectFiles)
{
var missingFiles = RetrieveMissingFilesList(solutionProjectFile, solutionSubdirectoriesToIgnore);
if (missingFiles.Any())
{
errorCollection.Add(solutionProjectFile, missingFiles);
}
}
var result = new StringBuilder();
foreach (var error in errorCollection)
{
result.AppendFormat("{0} excludes the following files:\r\n{1}\r\n", error.Key, string.Join(Environment.NewLine, error.Value));
}
Assert.True(!errorCollection.Any(), result.ToString());
}
private DirectoryInfo GetSolutionDirectory(string solutionDirectoryName)
{
var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
while (currentDirectory != null &&
currentDirectory.GetDirectories("*", SearchOption.TopDirectoryOnly).All(f => f.Name.ToLower() != solutionDirectoryName.ToLower()))
{
currentDirectory = currentDirectory.Parent;
}
return new DirectoryInfo(currentDirectory.FullName + "\\" + solutionDirectoryName);
}
/// <summary>
/// Replacing Directory.GetFiles with Directory.EnumerateFiles (and other enhancements) came from
/// http://stackoverflow.com/questions/163162/can-you-call-directory-getfiles-with-multiple-filters
/// </summary>
/// <param name="solutionProjectFile"></param>
/// <param name="excludedFolders"></param>
/// <returns></returns>
private List<string> RetrieveMissingFilesList(string solutionProjectFile, IEnumerable<string> excludedFolders)
{
var spfInfo = new DirectoryInfo(solutionProjectFile);
var projectDirectory = spfInfo.Parent.FullName;
var allFiles = Directory.EnumerateFiles(projectDirectory, "*.*", SearchOption.AllDirectories)
.Where(f => f.EndsWith(".cs") || f.EndsWith(".cshtml")).ToList();
var filesToIgnore = (from mf in allFiles from ef in excludedFolders where mf.ToLower().Contains(ef.ToLower()) select mf).ToList();
var projFiles = allFiles.Except(filesToIgnore);
var csprojDocument = XDocument.Load(solutionProjectFile);
var csFileElements = GetCsFileElements(csprojDocument);
var contentFileElements = GetContentFileElements(csprojDocument);
var allFileElements = csFileElements.Union(contentFileElements);
return (from file in projFiles
let stripFileName = file.Replace(projectDirectory, "")
where allFileElements.All(x => x.Attribute("Include").Value.ToLower() != file.Replace(projectDirectory + "\\", "").ToLower())
select stripFileName).ToList();
}
private static IEnumerable<XElement> GetContentFileElements(XDocument csprojDocument)
{
return GetFileElements(csprojDocument, "Content");
}
private static IEnumerable<XElement> GetCsFileElements(XDocument csprojDocument)
{
return GetFileElements(csprojDocument, "Compile");
}
private static IEnumerable<XElement> GetFileElements(XDocument csprojDocument, string elementType)
{
return csprojDocument.Elements()
.Where(x => x.Name.LocalName == "Project").Elements()
.Where(x => x.Name.LocalName == "ItemGroup" &&
x.HasElements &&
x.Elements().Any(t => t.Name.LocalName == elementType)).Elements();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment