[Test] public void Contents()
        {
            RepositoryFile file = new RepositoryFile(repository, CONTENTS_PATH, RepositoryStatus.Unchanged, RepositoryStatus.Unchanged);

            using (Stream stream = file.GetContents())
                using (TextReader reader = new StreamReader(stream))
                {
                    Assert.AreEqual("This is the first line", reader.ReadLine());
                    Assert.AreEqual("Second Line...", reader.ReadLine());
                    Assert.AreEqual("And the third", reader.ReadLine());
                    Assert.AreEqual("", reader.ReadLine());
                    Assert.AreEqual("Final line!", reader.ReadLine());
                    Assert.AreEqual(null, reader.ReadLine());
                }
        }
        [Test] public void BinaryWithContents()
        {
            RepositoryFile file = new RepositoryFile(repository, BINARY_WITH_CONTENTS_PATH, RepositoryStatus.Unchanged, RepositoryStatus.Unchanged);

            Stream stream = file.GetContents();
        }
Ejemplo n.º 3
0
        public Error[] PreCommit(RepositoryFile file)
        {
            // Check files that have the proper extension
            // but skip the files that are deleted
            if (file.ContentsStatus == RepositoryStatus.Deleted ||
                Array.IndexOf(extensions, file.Extension) == -1)
            {
                return(Error.NoErrors);
            }

            // Skip AssemblyInfo.cs, it is a special
            // case file and should not be necessary
            // to add the header to. I think?
            if (file.FileName == "AssemblyInfo.cs")
            {
                return(Error.NoErrors);
            }

            ArrayList errors = new ArrayList();

            using (Stream stream = file.GetContents())
                using (TextReader reader = new StreamReader(stream))
                {
                    String line;
                    int    index = 0;
                    while ((line = reader.ReadLine()) != null &&
                           index < LINES.Length)
                    {
                        line = Trim(line);

                        if (line == String.Empty)
                        {
                            if (index != 0)
                            {
                                AddError(errors, file, "Blank lines are not allowed in the Apache License 2.0 header");
                            }

                            continue;
                        }

                        if (line != LINES[index])
                        {
                            if (index == 0)
                            {
                                AddError(errors, file, "No text or code is allowed before the Apache License 2.0 header");
                                continue;
                            }
                            AddError(errors, file, "Apache License 2.0 header has errors on line " + (index + 1));
                        }

                        index++;
                    }

                    // if index is still at 0 we havent found the
                    // header at all, clear the errors and add
                    // that as error.
                    if (index == 0)
                    {
                        errors.Clear();
                        AddError(errors, file, "Apache License 2.0 header is missing or there are errors on the first line");
                    }
                }

            return((Error[])errors.ToArray(typeof(Error)));
        }