Exemple #1
0
 public FakeDirectory(FakeFileSystem fileSystem, DirectoryPath path, bool creatable)
 {
     _fileSystem = fileSystem;
     _path = path;
     _exist = false;
     _creatable = creatable;
 }
Exemple #2
0
 public void DeleteDirectory(DirectoryPath path)
 {
     if (Directories.ContainsKey(path))
     {
         Directories[path].Exists = false;
     }
 }
Exemple #3
0
 private IDirectory GetDirectory(DirectoryPath path, bool creatable)
 {
     if (!Directories.ContainsKey(path))
     {
         Directories.Add(path, new FakeDirectory(this, path, creatable));
     }
     return Directories[path];
 }
            public void Should_Combine_Paths(string first, string second, string expected)
            {
                // Given
                var path = new DirectoryPath(first);

                // When
                var result = path.Combine(new DirectoryPath(second));

                // Then
                Assert.Equal(expected, result.FullPath);
            }
            public void Should_Throw_If_Path_Is_Null()
            {
                // Given
                var path = new DirectoryPath("assets");

                // When
                var result = Record.Exception(() => path.CombineWithFilePath(null));

                // Then
                Assert.IsType<ArgumentNullException>(result);
                Assert.Equal("path", ((ArgumentNullException)result).ParamName);
            }
            public void Can_Not_Combine_Directory_Path_With_Absolute_File_Path()
            {
                // Given
                var path = new DirectoryPath("assets");

                // When
                var result = Record.Exception(() => path.CombineWithFilePath(new FilePath("/other/asset.txt")));

                // Then
                Assert.IsType<InvalidOperationException>(result);
                Assert.Equal("Cannot combine a directory path with an absolute file path.", result.Message);
            }
Exemple #7
0
 /// <summary>
 /// Combines the current path with another <see cref="DirectoryPath"/>.
 /// The provided <see cref="DirectoryPath"/> must be absolute.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <returns>A combination of the current path and the provided <see cref="DirectoryPath"/>.</returns>
 public DirectoryPath Combine(DirectoryPath path)
 {
     if (path == null)
     {
         throw new ArgumentNullException("path");
     }
     if (!path.IsRelative)
     {
         throw new InvalidOperationException("Cannot combine a directory path with an absolute directory path.");
     }
     var combinedPath = System.IO.Path.Combine(FullPath, path.FullPath);
     return new DirectoryPath(combinedPath);
 }
Exemple #8
0
 /// <summary>
 /// Makes the path absolute to another (absolute) path.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <returns>An absolute path.</returns>
 public DirectoryPath MakeAbsolute(DirectoryPath path)
 {
     if (path == null)
     {
         throw new ArgumentNullException("path");
     }
     if (path.IsRelative)
     {
         throw new PathException("The provided path cannot be relative.");
     }
     return IsRelative
         ? path.Combine(this)
         : new DirectoryPath(FullPath);
 }
Exemple #9
0
 public Directory(DirectoryPath path)
 {
     _path = path;
     _directory = new DirectoryInfo(_path.FullPath);
 }
Exemple #10
0
 public Directory(DirectoryPath path)
 {
     _path      = path;
     _directory = new DirectoryInfo(_path.FullPath);
 }
Exemple #11
0
 public IDirectory GetDirectory(DirectoryPath path)
 {
     return GetDirectory(path, creatable: true);
 }
Exemple #12
0
                public void Should_Create_New_Absolute_Path_Identical_To_The_Path()
                {
                    // Given
                    var path = new DirectoryPath("/assets");

                    // When
                    var result = path.MakeAbsolute("/absolute");

                    // Then
                    Assert.Equal("/assets", result.FullPath);
                }
Exemple #13
0
                public void Should_Create_New_Absolute_Path_When_Path_Is_Relative()
                {
                    // Given
                    var path = new DirectoryPath("assets");

                    // When
                    var result = path.MakeAbsolute("/absolute");

                    // Then
                    Assert.Equal("/absolute/assets", result.FullPath);
                }
Exemple #14
0
                public void Should_Throw_If_Provided_Path_Is_Relative()
                {
                    // Given
                    var path = new DirectoryPath("assets");

                    // When
                    var result = Record.Exception(() => path.MakeAbsolute("Working"));

                    // Then
                    Assert.IsType<PathException>(result);
                    Assert.Equal("The provided path cannot be relative.", result.Message);
                }
Exemple #15
0
                public void Should_Throw_If_Provided_Path_Is_Null()
                {
                    // Given
                    var path = new DirectoryPath("assets");

                    // When
                    var result = Record.Exception(
                        () => path.MakeAbsolute((DirectoryPath)null));

                    // Then
                    Assert.IsType<ArgumentNullException>(result);
                    Assert.Equal("path", ((ArgumentNullException)result).ParamName);
                }
Exemple #16
0
                public void Should_Create_New_Absolute_Path_Identical_To_The_Path()
                {
                    // Given
                    var environment = Substitute.For<IEnvironment>();
                    var path = new DirectoryPath("/assets");

                    // When
                    var result = path.MakeAbsolute(environment);

                    // Then
                    Assert.Equal("/assets", result.FullPath);
                }
Exemple #17
0
                public void Should_Create_New_Absolute_Path_When_Path_Is_Relative()
                {
                    // Given
                    var environment = Substitute.For<IEnvironment>();
                    environment.GetApplicationRoot().Returns("/Working");
                    var path = new DirectoryPath("assets");

                    // When
                    var result = path.MakeAbsolute(environment);

                    // Then
                    Assert.Equal("/Working/assets", result.FullPath);
                }
        public static void LoadExtensions(this IContainer container, DirectoryPath path)
        {
            var fileSystem = container.Resolve<IFileSystem>();
            var finder = container.Resolve<IAssemblyNameFinder>();
            var logger = container.Resolve<ILogger<HadoukenService>>();
            
            var files = fileSystem.GetDirectory(path)
                .GetFiles("*.dll", SearchScope.Current)
                .Select(f => f.Path.FullPath);

            // Search through the path for assemblies to register
            var assemblyNames = finder.GetAssemblyNames<IExtension>(files);

            // The container builder which later updates the provided container
            var builder = new ContainerBuilder();

            foreach (var assemblyName in assemblyNames)
            {
                var filePath = new FilePath(assemblyName.CodeBase);
                logger.Info("Loading extension {FileName}.", filePath.GetFilename());

                var assembly = Assembly.Load(assemblyName);

                // Find extensions, services and custom components
                var extensions = assembly.GetTypesAssignableFrom<IExtension>();
                var services = assembly.GetTypesAssignableFrom<IJsonRpcService>();
                var components = (from type in assembly.GetTypes()
                    where !type.IsAbstract
                    let cattr = type.GetCustomAttribute<ComponentAttribute>()
                    where cattr != null
                    select type);

                // Register extensions
                foreach (var extension in extensions)
                {
                    var attr = extension.GetCustomAttribute<ExtensionAttribute>();
                    if (attr == null) continue;

                    builder.RegisterType(extension)
                        .AsImplementedInterfaces()
                        .SingleInstance();
                }

                // Register JSONRPC services
                foreach (var service in services)
                {
                    builder.RegisterType(service)
                        .As<IJsonRpcService>()
                        .SingleInstance();
                }

                // Register custom components
                foreach (var component in components)
                {
                    var attr = component.GetCustomAttribute<ComponentAttribute>();
                    var registration = builder.RegisterType(component).AsImplementedInterfaces();

                    switch (attr.Lifestyle)
                    {
                        case ComponentLifestyle.Singleton:
                            registration.SingleInstance();
                            break;
                        case ComponentLifestyle.Transient:
                            registration.InstancePerDependency();
                            break;
                    }
                }
            }

            // Update the container
            builder.Update(container);
        }
Exemple #19
0
 public IDirectory GetCreatedDirectory(DirectoryPath path)
 {
     var directory = GetDirectory(path, creatable: true);
     directory.Create();
     return directory;
 }
Exemple #20
0
        /// <summary>
        /// Returns <see cref="Path" /> instances matching the specified pattern.
        /// </summary>
        /// <param name="pattern">The pattern to match.</param>
        /// <returns>
        ///   <see cref="Path" /> instances matching the specified pattern.
        /// </returns>
        public IEnumerable<Path> Match(string pattern)
        {
            var scanner = new Scanner(pattern);
            var parser = new Parser(scanner, _environment);
            var path = parser.Parse();

            var rootNodes = new List<Node>();
            while (path.Count > 0)
            {
                // Pop the first path item.
                var segment = path[0];
                path.RemoveAt(0);

                if (segment.IsWildcard)
                {
                    path.Insert(0, segment);
                    break;
                }
                rootNodes.Add(segment);
            }

            // Fix up the tree.
            var newRoot = FixRootNode(rootNodes);
            if (newRoot != null)
            {
                rootNodes[0] = newRoot;
            }

            // Ge the root.
            var rootDirectory = new DirectoryPath(string.Join("/", rootNodes.Select(x => x.Render())));

            // Nothing left in the path?
            if (path.Count == 0)
            {
                // We have an absolute path with no wild cards.
                return new Path[] { rootDirectory };
            }

            // Walk the root and return the unique results.
            var segments = new Stack<Node>(((IEnumerable<Node>)path).Reverse());
            var results = Walk(rootDirectory, segments);
            return new HashSet<Path>(results, new PathComparer(_environment.IsUnix())).ToArray();
        }
Exemple #21
0
        private List<Path> Walk(DirectoryPath rootPath, Stack<Node> segments)
        {
            var results = new List<Path>();
            var segment = segments.Pop();

            var expression = new Regex("^" + segment.Render() + "$", _options);
            var isDirectoryWildcard = false;

            if (segment is WildcardSegmentNode)
            {
                segments.Push(segment);
                isDirectoryWildcard = true;
            }

            // Get all files and folders.
            var root = _fileSystem.GetDirectory(rootPath);
            if (!root.Exists)
            {
                return results;
            }

            foreach (var directory in root.GetDirectories("*", SearchScope.Current))
            {
                var part = directory.Path.FullPath.Substring(root.Path.FullPath.Length + 1);
                var pathTest = expression.IsMatch(part);

                var subWalkCount = 0;

                if (isDirectoryWildcard)
                {
                    // Walk recursivly down the segment.
                    var nextSegments = new Stack<Node>(segments.Reverse());
                    var subwalkResult = Walk(directory.Path, nextSegments);
                    if (subwalkResult.Count > 0)
                    {
                        results.AddRange(subwalkResult);
                    }

                    subWalkCount++;
                }

                // Check without directory wildcard.
                if (segments.Count > subWalkCount && (subWalkCount == 1 || pathTest))
                {
                    // Walk the next segment in the list.
                    var nextSegments = new Stack<Node>(segments.Skip(subWalkCount).Reverse());
                    var subwalkResult = Walk(directory.Path, nextSegments);
                    if (subwalkResult.Count > 0)
                    {
                        results.AddRange(subwalkResult);
                    }
                }

                // Got a match?
                if (pathTest && segments.Count == 0)
                {
                    results.Add(directory.Path);
                }
            }

            foreach (var file in root.GetFiles("*", SearchScope.Current))
            {
                var part = file.Path.FullPath.Substring(root.Path.FullPath.Length + 1);
                var pathTest = expression.IsMatch(part);

                // Got a match?
                if (pathTest && segments.Count == 0)
                {
                    results.Add(file.Path);
                }
                else if(pathTest)
                {
                    /////////////////////////////////////////////////////////////B
                    // We got a match, but we still have segments left.
                    // Is the next part a directory wild card?
                    /////////////////////////////////////////////////////////////

                    var nextNode = segments.Peek();
                    if (nextNode is WildcardSegmentNode)
                    {
                        var nextSegments = new Stack<Node>(segments.Skip(1).Reverse());
                        var subwalkResult = Walk(root.Path, nextSegments);
                        if (subwalkResult.Count > 0)
                        {
                            results.AddRange(subwalkResult);
                        }
                    }
                }
            }
            return results;
        }
Exemple #22
0
 public IDirectory GetNonCreatableDirectory(DirectoryPath path)
 {
     return GetDirectory(path, creatable: false);
 }