Inheritance: MockFileData
Exemple #1
0
        public void AddDirectory(string path)
        {
            var fixedPath = FixPath(path);

            lock (files)
            {
                throwExceptionWhenReadonly(path, fixedPath);
                files[fixedPath] = new MockDirectoryData();
            }
        }
Exemple #2
0
        public void AddDirectory(string path)
        {
            var fixedPath = FixPath(path, true);
            var separator = XFS.Separator();

            lock (files)
            {
                if (FileExists(fixedPath) &&
                    (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ACCESS_TO_THE_PATH_IS_DENIED, fixedPath));
                }

                if (forbiddenDirectories.Exists(directory => fixedPath.StartsWith(directory, StringComparison.OrdinalIgnoreCase)))
                {
                    throw new DirectoryNotFoundException(string.Format("The parent directory of {0} cannot be written to.", fixedPath));
                }

                var lastIndex = 0;

                bool isUnc =
                    fixedPath.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase) ||
                    fixedPath.StartsWith(@"//", StringComparison.OrdinalIgnoreCase);

                if (isUnc)
                {
                    //First, confirm they aren't trying to create '\\server\'
                    lastIndex = fixedPath.IndexOf(separator, 2, StringComparison.OrdinalIgnoreCase);
                    if (lastIndex < 0)
                    {
                        throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path");
                    }

                    /*
                     * Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles.
                     * See PR https://github.com/tathamoddie/System.IO.Abstractions/pull/90 for conversation
                     */
                }

                while ((lastIndex = fixedPath.IndexOf(separator, lastIndex + 1, StringComparison.OrdinalIgnoreCase)) > -1)
                {
                    var segment = fixedPath.Substring(0, lastIndex + 1);
                    if (!directory.Exists(segment))
                    {
                        files[segment] = new MockDirectoryData();
                    }
                }

                var s = fixedPath.EndsWith(separator, StringComparison.OrdinalIgnoreCase) ? fixedPath : fixedPath + separator;
                files[s] = new MockDirectoryData();
            }
        }
Exemple #3
0
        public void AddDirectory(string path)
        {
            var fixedPath = FixPath(path);

            lock (files)
            {
                if (FileExists(path) &&
                    (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    throw new UnauthorizedAccessException(string.Format("Access to the path '{0}' is denied.", path));
                }

                files[fixedPath] = new MockDirectoryData();
            }
        }
Exemple #4
0
        public void AddDirectory(string path)
        {
            var fixedPath = FixPath(path);
            var separator = XFS.Separator();

            lock (files)
            {
                if (FileExists(path) &&
                    (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path));
                }

                var lastIndex = 0;

                bool isUnc =
                    path.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase) ||
                    path.StartsWith(@"//", StringComparison.OrdinalIgnoreCase);

                if (isUnc)
                {
                    //First, confirm they aren't trying to create '\\server\'
                    lastIndex = path.IndexOf(separator, 2);
                    if (lastIndex < 0)
                    {
                        throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path");
                    }

                    /*
                     * Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles.
                     * See PR https://github.com/tathamoddie/System.IO.Abstractions/pull/90 for conversation
                     */
                }

                while ((lastIndex = path.IndexOf(separator, lastIndex + 1)) > -1)
                {
                    var segment = path.Substring(0, lastIndex + 1);
                    if (!directory.Exists(segment))
                    {
                        files[segment] = new MockDirectoryData();
                    }
                }

                var s = path.EndsWith(separator, StringComparison.OrdinalIgnoreCase) ? path : path + separator;
                files[s] = new MockDirectoryData();
            }
        }
        public void AddDirectory(string path)
        {
            var fixedPath = FixPath(path);
            var separator = XFS.Separator();

            lock (files)
            {
                if (FileExists(path) &&
                    (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path));

                var lastIndex = 0;

                bool isUnc =
                    path.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase) ||
                    path.StartsWith(@"//", StringComparison.OrdinalIgnoreCase);

                if (isUnc)
                {

                    //First, confirm they aren't trying to create '\\server\'
                    lastIndex = path.IndexOf(separator, 2, StringComparison.OrdinalIgnoreCase);
                    if (lastIndex < 0)
                        throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path");

                    /* 
                     * Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles.
                     * See PR https://github.com/tathamoddie/System.IO.Abstractions/pull/90 for conversation
                     */
                }

                while ((lastIndex = path.IndexOf(separator, lastIndex + 1, StringComparison.OrdinalIgnoreCase)) > -1)
                {
                    var segment = path.Substring(0, lastIndex + 1);
                    if (!directory.Exists(segment))
                    {
                        files[segment] = new MockDirectoryData();
                    }
                }

                var s = path.EndsWith(separator, StringComparison.OrdinalIgnoreCase) ? path : path + separator;
                files[s] = new MockDirectoryData();
            }
        }
        public void AddDirectory(string path)
        {
            var fixedPath = FixPath(path);

            lock (files)
            {
                if (FileExists(path) &&
                    (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path));

                var lastIndex = 0;
                while ((lastIndex = path.IndexOf('\\', lastIndex + 1)) > -1)
                {
                    var segment = path.Substring(0, lastIndex + 1);
                    if (!directory.Exists(segment))
                    {
                        files[segment] = new MockDirectoryData();
                    }
                }

                var s = path.EndsWith("\\", StringComparison.OrdinalIgnoreCase) ? path : path + "\\";
                files[s] = new MockDirectoryData();
            }
        }
        public void AddDirectory(string path)
        {
            var fixedPath = FixPath(path);

            lock (files)
            {
                if (FileExists(path) &&
                    (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    throw new UnauthorizedAccessException(string.Format("Access to the path '{0}' is denied.", path));

                files[fixedPath] = new MockDirectoryData();
                directory.CreateDirectory(fixedPath);
            }
        }