2015-12-15 01:39:29 +00:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using FluentAssertions.Execution;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Test.Utilities
|
|
|
|
|
{
|
|
|
|
|
public class DirectoryInfoAssertions
|
|
|
|
|
{
|
2016-04-05 00:51:36 +00:00
|
|
|
|
private DirectoryInfo _dirInfo;
|
2015-12-15 01:39:29 +00:00
|
|
|
|
|
|
|
|
|
public DirectoryInfoAssertions(DirectoryInfo dir)
|
|
|
|
|
{
|
2016-04-05 00:51:36 +00:00
|
|
|
|
_dirInfo = dir;
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-03 18:57:25 +00:00
|
|
|
|
public DirectoryInfo DirectoryInfo => _dirInfo;
|
|
|
|
|
|
2015-12-15 01:39:29 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> Exist()
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(_dirInfo.Exists)
|
|
|
|
|
.FailWith("Expected directory {0} does not exist.", _dirInfo.FullName);
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 08:58:37 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> HaveFile(string expectedFile, string because = "", params object[] reasonArgs)
|
2015-12-15 01:39:29 +00:00
|
|
|
|
{
|
|
|
|
|
var file = _dirInfo.EnumerateFiles(expectedFile, SearchOption.TopDirectoryOnly).SingleOrDefault();
|
2016-10-29 08:58:37 +00:00
|
|
|
|
|
|
|
|
|
Execute.Assertion
|
|
|
|
|
.ForCondition(file != null)
|
|
|
|
|
.BecauseOf(because, reasonArgs)
|
|
|
|
|
.FailWith($"Expected File {expectedFile} cannot be found in directory {_dirInfo.FullName}.");
|
|
|
|
|
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 09:40:26 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> HaveTextFile(
|
|
|
|
|
string expectedFile,
|
|
|
|
|
string expectedContents,
|
|
|
|
|
string because = "",
|
|
|
|
|
params object[] reasonArgs)
|
2016-10-29 08:58:37 +00:00
|
|
|
|
{
|
|
|
|
|
this.HaveFile(expectedFile, because, reasonArgs);
|
|
|
|
|
|
|
|
|
|
var file = _dirInfo.EnumerateFiles(expectedFile, SearchOption.TopDirectoryOnly).SingleOrDefault();
|
|
|
|
|
|
|
|
|
|
var contents = File.ReadAllText(file.FullName);
|
|
|
|
|
|
|
|
|
|
Execute.Assertion
|
|
|
|
|
.ForCondition(contents.Equals(expectedContents))
|
|
|
|
|
.BecauseOf(because, reasonArgs)
|
|
|
|
|
.FailWith($"Expected file {expectedFile} to contain \n\n{expectedContents}\n\nbut it contains\n\n{contents}\n");
|
|
|
|
|
|
2015-12-15 01:39:29 +00:00
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 09:40:26 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> NotHaveFile(
|
|
|
|
|
string expectedFile,
|
|
|
|
|
string because = "",
|
|
|
|
|
params object [] reasonArgs)
|
2015-12-15 01:39:29 +00:00
|
|
|
|
{
|
|
|
|
|
var file = _dirInfo.EnumerateFiles(expectedFile, SearchOption.TopDirectoryOnly).SingleOrDefault();
|
2017-01-06 09:40:26 +00:00
|
|
|
|
Execute.Assertion
|
|
|
|
|
.ForCondition(file == null)
|
|
|
|
|
.BecauseOf(because, reasonArgs)
|
2015-12-15 01:39:29 +00:00
|
|
|
|
.FailWith("File {0} should not be found in directory {1}.", expectedFile, _dirInfo.FullName);
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> HaveFiles(IEnumerable<string> expectedFiles)
|
2016-03-09 19:36:16 +00:00
|
|
|
|
{
|
2015-12-15 01:39:29 +00:00
|
|
|
|
foreach (var expectedFile in expectedFiles)
|
|
|
|
|
{
|
|
|
|
|
HaveFile(expectedFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 09:40:26 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> HaveTextFiles(
|
|
|
|
|
IDictionary<string, string> expectedFiles,
|
|
|
|
|
string because = "",
|
|
|
|
|
params object[] reasonArgs)
|
2016-10-29 08:58:37 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var expectedFile in expectedFiles)
|
|
|
|
|
{
|
|
|
|
|
HaveTextFile(expectedFile.Key, expectedFile.Value, because, reasonArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 09:40:26 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> HaveFilesMatching(
|
|
|
|
|
string expectedFilesSearchPattern,
|
|
|
|
|
SearchOption searchOption,
|
|
|
|
|
string because = "",
|
|
|
|
|
params object[] reasonArgs)
|
2016-10-04 17:29:07 +00:00
|
|
|
|
{
|
|
|
|
|
var matchingFileExists = _dirInfo.EnumerateFiles(expectedFilesSearchPattern, searchOption).Any();
|
2016-10-29 08:58:37 +00:00
|
|
|
|
|
|
|
|
|
Execute.Assertion
|
|
|
|
|
.ForCondition(matchingFileExists == true)
|
|
|
|
|
.BecauseOf(because, reasonArgs)
|
2016-10-04 17:29:07 +00:00
|
|
|
|
.FailWith("Expected directory {0} to contain files matching {1}, but no matching file exists.",
|
|
|
|
|
_dirInfo.FullName, expectedFilesSearchPattern);
|
2016-10-29 08:58:37 +00:00
|
|
|
|
|
2016-10-04 17:29:07 +00:00
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 09:40:26 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> NotHaveFiles(
|
|
|
|
|
IEnumerable<string> expectedFiles,
|
|
|
|
|
string because = "",
|
|
|
|
|
params object [] reasonArgs)
|
2016-04-11 18:01:41 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var expectedFile in expectedFiles)
|
|
|
|
|
{
|
2017-01-06 09:40:26 +00:00
|
|
|
|
NotHaveFile(expectedFile, because, reasonArgs);
|
2016-04-11 18:01:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:29:07 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> NotHaveFilesMatching(string expectedFilesSearchPattern, SearchOption searchOption)
|
|
|
|
|
{
|
|
|
|
|
var matchingFileCount = _dirInfo.EnumerateFiles(expectedFilesSearchPattern, searchOption).Count();
|
|
|
|
|
Execute.Assertion.ForCondition(matchingFileCount == 0)
|
|
|
|
|
.FailWith("Found {0} files that should not exist in directory {1}. No file matching {2} should exist.",
|
|
|
|
|
matchingFileCount, _dirInfo.FullName, expectedFilesSearchPattern);
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-15 01:39:29 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> HaveDirectory(string expectedDir)
|
|
|
|
|
{
|
|
|
|
|
var dir = _dirInfo.EnumerateDirectories(expectedDir, SearchOption.TopDirectoryOnly).SingleOrDefault();
|
|
|
|
|
Execute.Assertion.ForCondition(dir != null)
|
|
|
|
|
.FailWith("Expected directory {0} cannot be found inside directory {1}.", expectedDir, _dirInfo.FullName);
|
2016-03-09 19:36:16 +00:00
|
|
|
|
|
2015-12-15 01:39:29 +00:00
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(new DirectoryInfoAssertions(dir));
|
|
|
|
|
}
|
2016-04-05 00:51:36 +00:00
|
|
|
|
|
2016-12-27 21:28:24 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> HaveDirectories(IEnumerable<string> expectedDirs)
|
|
|
|
|
{
|
|
|
|
|
foreach (var expectedDir in expectedDirs)
|
|
|
|
|
{
|
|
|
|
|
HaveDirectory(expectedDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 23:46:55 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> NotHaveDirectory(string unexpectedDir)
|
|
|
|
|
{
|
|
|
|
|
var dir = _dirInfo.EnumerateDirectories(unexpectedDir, SearchOption.TopDirectoryOnly).SingleOrDefault();
|
|
|
|
|
Execute.Assertion.ForCondition(dir == null)
|
|
|
|
|
.FailWith("Directory {0} should not be found in directory {1}.", unexpectedDir, _dirInfo.FullName);
|
|
|
|
|
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(new DirectoryInfoAssertions(dir));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> NotHaveDirectories(IEnumerable<string> unexpectedDirs)
|
|
|
|
|
{
|
|
|
|
|
foreach (var unexpectedDir in unexpectedDirs)
|
|
|
|
|
{
|
|
|
|
|
NotHaveDirectory(unexpectedDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-05 00:51:36 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> OnlyHaveFiles(IEnumerable<string> expectedFiles)
|
|
|
|
|
{
|
|
|
|
|
var actualFiles = _dirInfo.EnumerateFiles("*", SearchOption.TopDirectoryOnly).Select(f => f.Name);
|
|
|
|
|
var missingFiles = Enumerable.Except(expectedFiles, actualFiles);
|
|
|
|
|
var extraFiles = Enumerable.Except(actualFiles, expectedFiles);
|
|
|
|
|
var nl = Environment.NewLine;
|
|
|
|
|
|
|
|
|
|
Execute.Assertion.ForCondition(!missingFiles.Any())
|
|
|
|
|
.FailWith($"Following files cannot be found inside directory {_dirInfo.FullName} {nl} {string.Join(nl, missingFiles)}");
|
|
|
|
|
|
|
|
|
|
Execute.Assertion.ForCondition(!extraFiles.Any())
|
|
|
|
|
.FailWith($"Following extra files are found inside directory {_dirInfo.FullName} {nl} {string.Join(nl, extraFiles)}");
|
|
|
|
|
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
2016-10-29 08:58:37 +00:00
|
|
|
|
|
2017-05-25 05:51:26 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> BeEmpty()
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(!_dirInfo.EnumerateFileSystemInfos().Any())
|
|
|
|
|
.FailWith($"The directory {_dirInfo.FullName} is not empty.");
|
|
|
|
|
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> NotBeEmpty()
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(_dirInfo.EnumerateFileSystemInfos().Any())
|
|
|
|
|
.FailWith($"The directory {_dirInfo.FullName} is empty.");
|
|
|
|
|
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 08:58:37 +00:00
|
|
|
|
public AndConstraint<DirectoryInfoAssertions> NotExist(string because = "", params object[] reasonArgs)
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion
|
|
|
|
|
.ForCondition(_dirInfo.Exists == false)
|
|
|
|
|
.BecauseOf(because, reasonArgs)
|
|
|
|
|
.FailWith($"Expected directory {_dirInfo.FullName} to not exist, but it does.");
|
|
|
|
|
|
|
|
|
|
return new AndConstraint<DirectoryInfoAssertions>(this);
|
|
|
|
|
}
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|