Beispiel #1
0
        public MockFileStream(
            IMockFileDataAccessor mockFileDataAccessor,
            string path,
            FileMode mode,
            FileAccess access   = FileAccess.ReadWrite,
            FileOptions options = FileOptions.None)

        {
            this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
            this.path    = path;
            this.options = options;

            if (mockFileDataAccessor.FileExists(path))
            {
                if (mode.Equals(FileMode.CreateNew))
                {
                    throw CommonExceptions.FileAlreadyExists(path);
                }

                var fileData = mockFileDataAccessor.GetFile(path);
                fileData.CheckFileAccess(path, access);

                var existingContents     = fileData.Contents;
                var keepExistingContents =
                    existingContents?.Length > 0 &&
                    mode != FileMode.Truncate && mode != FileMode.Create;
                if (keepExistingContents)
                {
                    Write(existingContents, 0, existingContents.Length);
                    Seek(0, mode == FileMode.Append
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
            else
            {
                var directoryPath = mockFileDataAccessor.Path.GetDirectoryName(path);
                if (!string.IsNullOrEmpty(directoryPath) && !mockFileDataAccessor.Directory.Exists(directoryPath))
                {
                    throw CommonExceptions.CouldNotFindPartOfPath(path);
                }

                if (mode.Equals(FileMode.Open) || mode.Equals(FileMode.Truncate))
                {
                    throw CommonExceptions.FileNotFound(path);
                }

                mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
            }

            this.access = access;
        }
Beispiel #2
0
        public MockFileStream(
            IMockFileDataAccessor mockFileDataAccessor,
            string path,
            StreamType streamType,
            FileOptions options,
            FileMode fileMode = FileMode.Append)
        {
            this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
            this.path    = path;
            this.options = options;

            if (mockFileDataAccessor.FileExists(path))
            {
                if (fileMode.Equals(FileMode.CreateNew))
                {
                    throw CommonExceptions.FileAlreadyExists(path);
                }

                var fileData = mockFileDataAccessor.GetFile(path);
                fileData.CheckFileAccess(path, streamType != StreamType.READ ? FileAccess.Write : FileAccess.Read);

                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = fileData.Contents;
                if (data != null && data.Length > 0 && streamType != StreamType.TRUNCATE)
                {
                    Write(data, 0, data.Length);
                    Seek(0, StreamType.APPEND.Equals(streamType)
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
            else
            {
                if (!mockFileDataAccessor.Directory.Exists(mockFileDataAccessor.Path.GetDirectoryName(path)))
                {
                    throw CommonExceptions.CouldNotFindPartOfPath(path);
                }

                if (StreamType.READ.Equals(streamType) ||
                    fileMode.Equals(FileMode.Open) ||
                    fileMode.Equals(FileMode.Truncate))
                {
                    throw CommonExceptions.FileNotFound(path);
                }

                mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
            }

            canWrite = streamType != StreamType.READ;
        }
Beispiel #3
0
        /// <exception cref="System.IO.IOException"></exception>
        private void AssertEntry(FileMode type, bool entryIgnored, string pathName)
        {
            NUnit.Framework.Assert.IsTrue(walk.Next(), "walk has entry");
            NUnit.Framework.Assert.AreEqual(pathName, walk.PathString);
            NUnit.Framework.Assert.AreEqual(type, walk.GetFileMode(0));
            WorkingTreeIterator itr = walk.GetTree <WorkingTreeIterator>(0);

            NUnit.Framework.Assert.IsNotNull(itr, "has tree");
            NUnit.Framework.Assert.AreEqual(entryIgnored, itr.IsEntryIgnored(), "is ignored");
            if (D.Equals(type))
            {
                walk.EnterSubtree();
            }
        }
Beispiel #4
0
        /// <exception cref="System.IO.IOException"></exception>
        private void FormatHeader(ByteArrayOutputStream o, DiffEntry ent)
        {
            DiffEntry.ChangeType type = ent.GetChangeType();
            string   oldp             = ent.GetOldPath();
            string   newp             = ent.GetNewPath();
            FileMode oldMode          = ent.GetOldMode();
            FileMode newMode          = ent.GetNewMode();

            o.Write(Constants.EncodeASCII("diff --git "));
            o.Write(Constants.Encode(QuotePath(oldPrefix + (type == DiffEntry.ChangeType.ADD ?
                                                            newp : oldp))));
            o.Write(' ');
            o.Write(Constants.Encode(QuotePath(newPrefix + (type == DiffEntry.ChangeType.DELETE
                                           ? oldp : newp))));
            o.Write('\n');
            switch (type)
            {
            case DiffEntry.ChangeType.ADD:
            {
                o.Write(Constants.EncodeASCII("new file mode "));
                newMode.CopyTo(o);
                o.Write('\n');
                break;
            }

            case DiffEntry.ChangeType.DELETE:
            {
                o.Write(Constants.EncodeASCII("deleted file mode "));
                oldMode.CopyTo(o);
                o.Write('\n');
                break;
            }

            case DiffEntry.ChangeType.RENAME:
            {
                o.Write(Constants.EncodeASCII("similarity index " + ent.GetScore() + "%"));
                o.Write('\n');
                o.Write(Constants.Encode("rename from " + QuotePath(oldp)));
                o.Write('\n');
                o.Write(Constants.Encode("rename to " + QuotePath(newp)));
                o.Write('\n');
                break;
            }

            case DiffEntry.ChangeType.COPY:
            {
                o.Write(Constants.EncodeASCII("similarity index " + ent.GetScore() + "%"));
                o.Write('\n');
                o.Write(Constants.Encode("copy from " + QuotePath(oldp)));
                o.Write('\n');
                o.Write(Constants.Encode("copy to " + QuotePath(newp)));
                o.Write('\n');
                if (!oldMode.Equals(newMode))
                {
                    o.Write(Constants.EncodeASCII("new file mode "));
                    newMode.CopyTo(o);
                    o.Write('\n');
                }
                break;
            }

            case DiffEntry.ChangeType.MODIFY:
            {
                if (0 < ent.GetScore())
                {
                    o.Write(Constants.EncodeASCII("dissimilarity index " + (100 - ent.GetScore()) + "%"
                                                  ));
                    o.Write('\n');
                }
                break;
            }
            }
            if ((type == DiffEntry.ChangeType.MODIFY || type == DiffEntry.ChangeType.RENAME) &&
                !oldMode.Equals(newMode))
            {
                o.Write(Constants.EncodeASCII("old mode "));
                oldMode.CopyTo(o);
                o.Write('\n');
                o.Write(Constants.EncodeASCII("new mode "));
                newMode.CopyTo(o);
                o.Write('\n');
            }
            if (!ent.GetOldId().Equals(ent.GetNewId()))
            {
                FormatIndexLine(o, ent);
            }
        }