Skip to content
Browse files

Remove ReplacementFileSystem. All tests working.

  • Loading branch information...
1 parent 1db809b commit 4ebc75ddc2c29289243d073baa1674f892923147 @BenPhegan committed Mar 30, 2012
View
6 NuGet.Extras.Tests/NuGet.Extras.Tests.csproj
@@ -48,10 +48,6 @@
<Reference Include="pnunit.framework">
<HintPath>..\packages\NUnit.2.5.10.11092\lib\pnunit.framework.dll</HintPath>
</Reference>
- <Reference Include="ReplacementFileSystem, Version=0.1.4421.32307, Culture=neutral, processorArchitecture=MSIL">
- <SpecificVersion>False</SpecificVersion>
- <HintPath>..\packages\ReplacementFileSystem.0.4\lib\net40\ReplacementFileSystem.dll</HintPath>
- </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Security" />
@@ -71,7 +67,7 @@
<Compile Include="Repositories\RepositoryGroupManagerTests.cs" />
<Compile Include="Repositories\RepositoryManagerTests.cs" />
<Compile Include="TestData\PackageResolverTestObject.cs" />
- <Compile Include="TestObjects\NugetMockFileSystem.cs" />
+ <Compile Include="TestObjects\MockFileSystem.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="TestData\PackageReferenceSetResolverTestData.xml">
View
7 NuGet.Extras.Tests/Packages/PackageAggregatorTests.cs
@@ -8,7 +8,6 @@
using NuGet;
using System.Collections.Generic;
using NuGet.Extras.Tests.TestObjects;
-using ReplacementFileSystem;
using NuGet.Extras.Comparers;
using NuGet.Extras.PackageReferences;
@@ -43,8 +42,8 @@ public void ConstructorTest()
//TODO Not much to test here really....
[TestCase(2, 0, Description = "Using version stated")]
public void AggregateCount(int expectedCanResolveCount, int expectedCannotResolveCount)
- {
- _packageAggregator.Compute((s, s1) => Console.WriteLine(@"{0}{1}", s, s1), PackageReferenceEqualityComparer.Id, new PackageReferenceSetResolver());
+ {
+ _packageAggregator.Compute((s, s1) => { }, PackageReferenceEqualityComparer.Id, new PackageReferenceSetResolver());
Assert.AreEqual(expectedCanResolveCount, _packageAggregator.Packages.Count());
Assert.AreEqual(expectedCannotResolveCount, _packageAggregator.PackageResolveFailures.Count());
}
@@ -82,7 +81,7 @@ private static List<PackageReferenceFile> GetPackageReferenceFileList()
<package id='Package2' version='2.0' />
</packages>";
- var mfs = new Mock<NugetMockFileSystem>();
+ var mfs = new Mock<MockFileSystem>();
mfs.Setup(m => m.OpenFile("x1")).Returns(x1.AsStream());
mfs.Setup(m => m.OpenFile("x2")).Returns(x2.AsStream());
mfs.Setup(m => m.FileExists(It.IsAny<string>())).Returns(true);
View
9 NuGet.Extras.Tests/Packages/PackageEnumeratorTests.cs
@@ -4,7 +4,6 @@
using System.Text;
using NUnit.Framework;
using NuGet.Extras.Packages;
-using ReplacementFileSystem;
using NuGet.Extras.Tests.TestObjects;
using NuGet.Extras.Comparers;
using System.IO;
@@ -19,7 +18,7 @@ public class PackageEnumeratorTests
public void TestStream()
{
Stream stream1 = "test".AsStream();
- IFileSystem fs = new NugetMockFileSystem();
+ IFileSystem fs = new MockFileSystem();
fs.AddFile(@"c:\test.config",stream1);
//Stream stream2 = fs.OpenFile();
@@ -28,7 +27,7 @@ public void TestStream()
[Test]
public void CanEnumerateOverSet()
{
- IFileSystem fs = new NugetMockFileSystem();
+ IFileSystem fs = new MockFileSystem();
var test1 = @"c:\test1\packages.config";
var test2 = @"c:\test1\packages.config";
@@ -54,10 +53,10 @@ public void CanEnumerateOverSet()
PackageEnumerator enumerator = new PackageEnumerator();
- var idOnly = enumerator.GetPackageReferences(list, (a, b) => Console.WriteLine(a + " - " + b), PackageReferenceEqualityComparer.Id);
+ var idOnly = enumerator.GetPackageReferences(list, (a, b) => { }, PackageReferenceEqualityComparer.Id);
Assert.AreEqual(1, idOnly.Count());
- var idAndVersion = enumerator.GetPackageReferences(list, (a, b) => Console.WriteLine(a + " - " + b), PackageReferenceEqualityComparer.IdAndVersion);
+ var idAndVersion = enumerator.GetPackageReferences(list, (a, b) => { }, PackageReferenceEqualityComparer.IdAndVersion);
Assert.AreEqual(3, idAndVersion.Count());
View
41 NuGet.Extras.Tests/Repositories/RepositoryGroupManagerTests.cs
@@ -4,32 +4,31 @@
using NuGet.Extras.Repositories;
using NuGet.Extras.Tests.TestObjects;
using System.IO;
-using ReplacementFileSystem;
-using IFileSystem = ReplacementFileSystem.IFileSystem;
-using ItemType = ReplacementFileSystem.ItemType;
namespace NuGet.Extras.Tests.Repositories
{
[TestFixture]
public class RepositoryGroupManagerTests
{
- #region Global Constructor Tests
string baseRepositoriesConfig = @"<?xml version='1.0' encoding='utf-8'?>
<repositories>
<repository path='..\Project1\packages.config' />
<repository path='..\Project2\packages.config' />
<repository path='..\Project3\packages.config' />
</repositories>";
- NugetMockFileSystem mfs;
+ MockFileSystem mfs;
[TestFixtureSetUp]
public void Setup()
{
- mfs = new NugetMockFileSystem();
- mfs.AddMockFile(new MockFileSystemInfo(ItemType.File,@"c:\files\TestSolution\packages\repositories.config",DateTime.Now,baseRepositoriesConfig),createDirectoryTree: true);
- mfs.AddMockFile(new MockFileSystemInfo(ItemType.File,@"c:\files\TestSolution\repositories.config",DateTime.Now, baseRepositoriesConfig), createDirectoryTree: false);
- mfs.AddMockDirectoryStructure(@"c:\random\empty");
+ mfs = new MockFileSystem();
+ mfs.CreateDirectory(@"c:\files");
+ mfs.CreateDirectory(@"c:\files\TestSolution");
+ mfs.CreateDirectory(@"c:\files\TestSolution\packages");
+ mfs.AddFile(@"c:\files\TestSolution\packages\repositories.config", baseRepositoriesConfig);
+ mfs.AddFile(@"c:\files\TestSolution\repositories.config", baseRepositoriesConfig);
+ mfs.CreateDirectory(@"c:\random\empty");
}
[TestCase(@"c:\files\TestSolution\packages\repositories.config", 1)]
@@ -48,25 +47,11 @@ public void ConstructParser(string repositoryConfigPath,int repositoryCount)
[ExpectedException(typeof(System.ArgumentOutOfRangeException))]
public void ConstructorException(string repositoryConfigPath)
{
- var mfs = new NugetMockFileSystem();
- mfs.Info.Add(MockFileSystemInfo.CreateFileObject(repositoryConfigPath));
+ var mfs = new MockFileSystem();
+ mfs.AddFile(repositoryConfigPath);
new RepositoryGroupManager(repositoryConfigPath, mfs);
}
- #endregion
-
- private readonly string _repositoryConfigPath;
- private RepositoryGroupManager _repositoryManager;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="RepositoryManagerTests"/> class.
- /// </summary>
- /// <param name="repositoryConfigPath">The repository config path.</param>
- //public RepositoryGroupManagerTests(string repositoryConfigPath)
- //{
- // _repositoryConfigPath = repositoryConfigPath;
- //}
-
[TestCase(@"c:\files\TestSolution\packages\repositories.config")]
public void CanCleanPackageFolders(string repositoryConfig)
{
@@ -75,15 +60,17 @@ public void CanCleanPackageFolders(string repositoryConfig)
foreach (var repositoryManager in repositoryGroupManager.RepositoryManagers)
{
if (repositoryManager.RepositoryConfig.Directory != null)
- repositoryManager.RepositoryConfig.Directory.CreateSubdirectory("Test");
+ {
+ mfs.CreateDirectory(Path.Combine(repositoryManager.RepositoryConfig.Directory.Name, "Test"));
+ }
}
repositoryGroupManager.CleanPackageFolders();
foreach (var repositoryManager in repositoryGroupManager.RepositoryManagers)
{
if (repositoryManager.RepositoryConfig.Directory != null)
- Assert.AreEqual(0, repositoryManager.RepositoryConfig.Directory.GetDirectories().Count());
+ Assert.AreEqual(0, mfs.GetDirectories(repositoryManager.RepositoryConfig.Directory.Name).Count());
}
}
}
View
29 NuGet.Extras.Tests/Repositories/RepositoryManagerTests.cs
@@ -7,7 +7,6 @@
using NuGet.Extras.Tests.TestObjects;
using System.IO;
using NuGet.Extras.Packages;
-using ReplacementFileSystem;
namespace NuGet.Extras.Tests.Repositories
{
@@ -18,19 +17,19 @@ public class RepositoryManagerTests
public void ConstructParser()
{
- var mfs = new NugetMockFileSystem();
+ var mfs = new MockFileSystem();
var correctConfig = @"<?xml version='1.0' encoding='utf-8'?>
<repositories>
<repository path='..\Project1\packages.config' />
<repository path='..\Project2\packages.config' />
<repository path='..\Project3\packages.config' />
</repositories>";
- mfs.AddMockFile(MockFileSystemInfo.CreateFileObject(@"c:\packages\repositories.config", correctConfig));
- mfs.AddMockFile(MockFileSystemInfo.CreateFileObject(@"c:\project1\packages.config", correctConfig));
- mfs.AddMockFile(MockFileSystemInfo.CreateFileObject(@"c:\project2\packages.config", correctConfig));
- mfs.AddMockFile(MockFileSystemInfo.CreateFileObject(@"c:\project3\packages.config", correctConfig));
- mfs.AddMockFile(MockFileSystemInfo.CreateFileObject(@"c:\packages\repositories.config", correctConfig));
+ mfs.AddFile(@"c:\packages\repositories.config", correctConfig);
+ mfs.AddFile(@"c:\project1\packages.config", correctConfig);
+ mfs.AddFile(@"c:\project2\packages.config", correctConfig);
+ mfs.AddFile(@"c:\project3\packages.config", correctConfig);
+ mfs.AddFile(@"c:\packages\repositories.config", correctConfig);
var repositoryManager = new RepositoryManager(@"c:\packages\repositories.config", new RepositoryEnumerator(mfs), mfs);
Assert.IsNotNull(repositoryManager);
@@ -47,21 +46,21 @@ public void ConstructParser()
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ConstructorException(string repositoryConfigPath, Boolean fileExists)
{
- var mfs = new Mock<NugetMockFileSystem>();
+ var mfs = new Mock<MockFileSystem>();
mfs.Setup(m => m.FileExists(It.IsAny<string>())).Returns(fileExists);
new RepositoryManager(repositoryConfigPath, new RepositoryEnumerator(mfs.Object), mfs.Object);
}
#endregion
[TestCase]
public void CanCleanPackageFolders()
- {
- var mfs = new NugetMockFileSystem();
-
-
- mfs.AddMockFile(MockFileSystemInfo.CreateFileObject("c:\\packages\\Component\\test.txt"));
- mfs.AddMockFile(MockFileSystemInfo.CreateFileObject("c:\\packages\\Component\\test.dll"));
- mfs.AddMockFile(MockFileSystemInfo.CreateFileObject("c:\\packages\\repositories.config"));
+ {
+ var mfs = new MockFileSystem();
+
+ mfs.CreateDirectory("c:\\packages\\Component");
+ mfs.AddFile("c:\\packages\\Component\\test.txt", "blah");
+ mfs.AddFile("c:\\packages\\Component\\test.dll","blah");
+ mfs.AddFile("c:\\packages\\repositories.config","blah");
var re = new Mock<IRepositoryEnumerator>();
re.Setup(r => r.GetPackageReferenceFiles(It.IsAny<FileInfo>())).Returns(new List<PackageReferenceFile>());
View
182 NuGet.Extras.Tests/TestObjects/MockFileSystem.cs
@@ -0,0 +1,182 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text.RegularExpressions;
+using NuGet.Extras.ExtensionMethods;
+
+namespace NuGet.Extras.Tests.TestObjects
+{
+ public class MockFileSystem : IFileSystem
+ {
+ private ILogger _logger;
+
+ public MockFileSystem()
+ : this(@"C:\MockFileSystem\")
+ {
+
+ }
+
+ public MockFileSystem(string root)
+ {
+ Root = root;
+ Paths = new Dictionary<string, Func<Stream>>(StringComparer.OrdinalIgnoreCase);
+ Deleted = new HashSet<string>();
+ }
+
+ public virtual ILogger Logger
+ {
+ get
+ {
+ return _logger ?? NullLogger.Instance;
+ }
+ set
+ {
+ _logger = value;
+ }
+ }
+
+ public virtual string Root
+ {
+ get;
+ private set;
+ }
+
+ public virtual IDictionary<string, Func<Stream>> Paths
+ {
+ get;
+ private set;
+ }
+
+ public virtual HashSet<string> Deleted
+ {
+ get;
+ private set;
+ }
+
+ public virtual void CreateDirectory(string path)
+ {
+ Paths.Add(path, null);
+ }
+
+ public virtual void DeleteDirectory(string path, bool recursive = false)
+ {
+ foreach (var file in Paths.Keys.ToList())
+ {
+ if (file.StartsWith(path))
+ {
+ Paths.Remove(file);
+ }
+ }
+ Deleted.Add(path);
+ }
+
+ public virtual string GetFullPath(string path)
+ {
+ return Path.Combine(Root, path);
+ }
+
+ public virtual IEnumerable<string> GetFiles(string path)
+ {
+ return Paths.Select(f => f.Key)
+ .Where(f => Path.GetDirectoryName(f).Equals(path, StringComparison.OrdinalIgnoreCase));
+ }
+
+ public virtual IEnumerable<string> GetFiles(string path, string filter)
+ {
+ Regex matcher = FindFilesPatternToRegex.Convert(filter);
+
+ return GetFiles(path).Where(f => matcher.IsMatch(Path.GetFileName(f)));
+ }
+
+ private static Regex GetFilterRegex(string wildcard)
+ {
+ string pattern = String.Join(String.Empty, wildcard.Split('.').Select(GetPattern));
+ return new Regex(pattern, RegexOptions.IgnoreCase);
+ }
+
+ private static string GetPattern(string token)
+ {
+ return token == "*" ? @"(.*)" : @"(" + token + ")";
+ }
+
+ public virtual void DeleteFile(string path)
+ {
+ Paths.Remove(path);
+ Deleted.Add(path);
+ }
+
+ public virtual bool FileExists(string path)
+ {
+ return Paths.ContainsKey(path);
+ }
+
+ public virtual Stream OpenFile(string path)
+ {
+ Func<Stream> factory;
+ if (!Paths.TryGetValue(path, out factory))
+ {
+ throw new FileNotFoundException(path + " not found.");
+ }
+ return factory();
+ }
+
+ public string ReadAllText(string path)
+ {
+ return OpenFile(path).ReadToEnd();
+ }
+
+ public virtual bool DirectoryExists(string path)
+ {
+ return Paths.Select(file => file.Key)
+ .Any(file => Path.GetDirectoryName(file).Equals(path, StringComparison.OrdinalIgnoreCase));
+ }
+
+ public virtual IEnumerable<string> GetDirectories(string path)
+ {
+ return Paths.GroupBy(f => Path.GetDirectoryName(f.Key))
+ .SelectMany(g => IFileSystemExtensions.GetDirectories(g.Key))
+ .Where(f => Path.GetDirectoryName(f) != null && !String.IsNullOrEmpty(f) &&
+ Path.GetDirectoryName(f).Equals(path, StringComparison.OrdinalIgnoreCase))
+ .Distinct();
+ }
+
+ public virtual void AddFile(string path)
+ {
+ AddFile(path, new MemoryStream());
+ }
+
+ public void AddFile(string path, string content)
+ {
+ AddFile(path, content.AsStream());
+ }
+
+ public virtual void AddFile(string path, Stream stream, bool overrideIfExists)
+ {
+ var ms = new MemoryStream((int)stream.Length);
+ stream.CopyTo(ms);
+ byte[] buffer = ms.ToArray();
+ Paths[path] = () => new MemoryStream(buffer);
+ }
+
+ public virtual void AddFile(string path, Stream stream)
+ {
+ AddFile(path, stream, overrideIfExists: true);
+ }
+
+ public virtual void AddFile(string path, Func<Stream> getStream)
+ {
+ Paths[path] = getStream;
+ }
+
+ public virtual DateTimeOffset GetLastModified(string path)
+ {
+ return DateTime.UtcNow;
+ }
+
+ public virtual DateTimeOffset GetCreated(string path)
+ {
+ return DateTime.UtcNow;
+ }
+ }
+}
View
67 NuGet.Extras.Tests/TestObjects/NugetMockFileSystem.cs
@@ -1,67 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using ReplacementFileSystem;
-using System.IO;
-using NuGet;
-
-namespace NuGet.Extras.Tests.TestObjects
-{
- public class NugetMockFileSystem : MockFileSystem, IFileSystem
- {
- public void AddFile(string path, System.IO.Stream stream)
- {
- var file = MockFileSystemInfo.CreateFileObject(path, stream);
- this.AddMockFile(file);
- }
-
- public DateTimeOffset GetCreated(string path)
- {
- throw new NotImplementedException();
- }
-
- public new IEnumerable<string> GetDirectories(string path)
- {
- return base.GetDirectories(path);
- }
-
- IEnumerable<string> IFileSystem.GetFiles(string path, string filter)
- {
- return base.GetFiles(path, filter).AsEnumerable();
- }
-
- public DateTimeOffset GetLastModified(string path)
- {
- throw new NotImplementedException();
- }
-
- public ILogger Logger
- {
- get
- {
- throw new NotImplementedException();
- }
- set
- {
- throw new NotImplementedException();
- }
- }
-
- public virtual Stream OpenFile(string path)
- {
- return base.OpenFile(path,FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
- }
-
- public string Root
- {
- get { throw new NotImplementedException(); }
- }
-
-
- public new IEnumerable<string> GetFiles(string path, string filter)
- {
- throw new NotImplementedException();
- }
- }
-}
View
1 NuGet.Extras.Tests/packages.config
@@ -2,5 +2,4 @@
<packages>
<package id="Moq" version="4.0.10827" />
<package id="NUnit" version="2.5.10.11092" />
- <package id="ReplacementFileSystem" version="0.4" />
</packages>
View
24 NuGet.Extras/ExtensionMethods/IFilesSystemExtensions.cs
@@ -4,7 +4,7 @@
namespace NuGet.Extras.ExtensionMethods
{
- public static class IFilesSystemExtensions
+ public static class IFileSystemExtensions
{
/// <summary>
/// Gets files for a particular pattern recursively.
@@ -36,5 +36,27 @@ private static IEnumerable<string> GetFilesRecursive(this IFileSystem fileSystem
return files.Distinct();
}
+ public static IEnumerable<string> GetDirectories(string path)
+ {
+ foreach (var index in IndexOfAll(path, Path.DirectorySeparatorChar))
+ {
+ yield return path.Substring(0, index);
+ }
+ yield return path;
+ }
+
+ private static IEnumerable<int> IndexOfAll(string value, char ch)
+ {
+ int index = -1;
+ do
+ {
+ index = value.IndexOf(ch, index + 1);
+ if (index >= 0)
+ {
+ yield return index;
+ }
+ }
+ while (index >= 0);
+ }
}
}
View
59 NuGet.Extras/ExtensionMethods/IVersionSpecExtensions.cs
@@ -0,0 +1,59 @@
+using System;
+
+namespace NuGet.Extras.ExtensionMethods
+{
+ public static class IVersionExtensions
+ {
+ public static Func<IPackage, bool> ToDelegate(this IVersionSpec versionInfo)
+ {
+ return versionInfo.ToDelegate<IPackage>(p => p.Version);
+ }
+
+ public static Func<T, bool> ToDelegate<T>(this IVersionSpec versionInfo, Func<T, SemanticVersion> extractor)
+ {
+ return p =>
+ {
+ SemanticVersion version = extractor(p);
+ bool condition = true;
+ if (versionInfo.MinVersion != null)
+ {
+ if (versionInfo.IsMinInclusive)
+ {
+ condition = condition && version >= versionInfo.MinVersion;
+ }
+ else
+ {
+ condition = condition && version > versionInfo.MinVersion;
+ }
+ }
+
+ if (versionInfo.MaxVersion != null)
+ {
+ if (versionInfo.IsMaxInclusive)
+ {
+ condition = condition && version <= versionInfo.MaxVersion;
+ }
+ else
+ {
+ condition = condition && version < versionInfo.MaxVersion;
+ }
+ }
+
+ return condition;
+ };
+ }
+
+ /// <summary>
+ /// Determines if the specified version is within the version spec
+ /// </summary>
+ public static bool Satisfies(this IVersionSpec versionSpec, SemanticVersion version)
+ {
+ // The range is unbounded so return true
+ if (versionSpec == null)
+ {
+ return true;
+ }
+ return versionSpec.ToDelegate<SemanticVersion>(v => v)(version);
+ }
+ }
+}
View
1 NuGet.Extras/packages.config
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
- <package id="ReplacementFileSystem" version="0.4" />
</packages>
View
BIN packages/ReplacementFileSystem.0.4/ReplacementFileSystem.0.4.nupkg
Binary file not shown.
View
BIN packages/ReplacementFileSystem.0.4/lib/net40/ReplacementFileSystem.dll
Binary file not shown.
View
BIN packages/ReplacementFileSystem.0.4/lib/net40/ReplacementFileSystem.pdb
Binary file not shown.

0 comments on commit 4ebc75d

Please sign in to comment.
Something went wrong with that request. Please try again.