コード例 #1
0
        public void ANSINonASCII()
        {
            string file = FileUtilities.GetTemporaryFile();
            try
            {
                // Start with a missing file.
                File.Delete(file);

                // Append one line to the file.
                WriteLinesToFile a = new WriteLinesToFile();
                a.File = new TaskItem(file);
                a.Lines = new ITaskItem[] { new TaskItem("My special character is \u00C3") };
                Assert.True(a.Execute());

                // Read the line from the file.
                ReadLinesFromFile r = new ReadLinesFromFile();
                r.File = new TaskItem(file);
                Assert.True(r.Execute());

                Assert.Equal(1, r.Lines.Length);
                Assert.Equal("My special character is \u00C3", r.Lines[0].ItemSpec);
            }
            finally
            {
                File.Delete(file);
            }
        }
コード例 #2
0
        public void Basic()
        {
            string file = FileUtilities.GetTemporaryFile();
            try
            {
                // Start with a missing file.
                File.Delete(file);

                // Append one line to the file.
                WriteLinesToFile a = new WriteLinesToFile();
                a.File = new TaskItem(file);
                a.Lines = new ITaskItem[] { new TaskItem("Line1") };
                Assert.True(a.Execute());

                // Read the line from the file.
                ReadLinesFromFile r = new ReadLinesFromFile();
                r.File = new TaskItem(file);
                Assert.True(r.Execute());

                Assert.Equal(1, r.Lines.Length);
                Assert.Equal("Line1", r.Lines[0].ItemSpec);

                // Write two more lines to the file.
                a.Lines = new ITaskItem[] { new TaskItem("Line2"), new TaskItem("Line3") };
                Assert.True(a.Execute());

                // Read all of the lines and verify them.
                Assert.True(r.Execute());
                Assert.Equal(3, r.Lines.Length);
                Assert.Equal("Line1", r.Lines[0].ItemSpec);
                Assert.Equal("Line2", r.Lines[1].ItemSpec);
                Assert.Equal("Line3", r.Lines[2].ItemSpec);
            }
            finally
            {
                File.Delete(file);
            }
        }
コード例 #3
0
        public void ReadNoAccess()
        {
            string file = FileUtilities.GetTemporaryFile();
            try
            {
                // Start with a missing file.
                File.Delete(file);

                // Append one line to the file.
                WriteLinesToFile a = new WriteLinesToFile();
                a.File = new TaskItem(file);
                a.Lines = new ITaskItem[] { new TaskItem("This is a new line") };
                Assert.True(a.Execute());

                // Remove all File access to the file to current user
                FileSecurity fSecurity = File.GetAccessControl(file);
                string userAccount = string.Format(@"{0}\{1}", System.Environment.UserDomainName, System.Environment.UserName);
                fSecurity.AddAccessRule(new FileSystemAccessRule(userAccount, FileSystemRights.ReadData, AccessControlType.Deny));
                File.SetAccessControl(file, fSecurity);

                // Attempt to Read lines from the file.
                ReadLinesFromFile r = new ReadLinesFromFile();
                MockEngine mEngine = new MockEngine();
                r.BuildEngine = mEngine;
                r.File = new TaskItem(file);
                Assert.False(r.Execute());
            }
            finally
            {
                FileSecurity fSecurity = File.GetAccessControl(file);
                string userAccount = string.Format(@"{0}\{1}", System.Environment.UserDomainName, System.Environment.UserName);
                fSecurity.AddAccessRule(new FileSystemAccessRule(userAccount, FileSystemRights.ReadData, AccessControlType.Allow));
                File.SetAccessControl(file, fSecurity);

                // Delete file
                File.Delete(file);
            }
        }
コード例 #4
0
        public void ReadMissing()
        {
            string file = FileUtilities.GetTemporaryFile();
            File.Delete(file);

            // Read the line from the file.
            ReadLinesFromFile r = new ReadLinesFromFile();
            r.File = new TaskItem(file);
            Assert.True(r.Execute());

            Assert.Equal(0, r.Lines.Length);
        }
コード例 #5
0
        public void Encoding()
        {
            string file = FileUtilities.GetTemporaryFile();
            try
            {
                // Write default encoding: UTF8
                WriteLinesToFile a = new WriteLinesToFile();
                a.BuildEngine = new MockEngine();
                a.File = new TaskItem(file);
                a.Lines = new ITaskItem[] { new TaskItem("\uBDEA") };
                Assert.True(a.Execute());

                ReadLinesFromFile r = new ReadLinesFromFile();
                r.File = new TaskItem(file);
                Assert.True(r.Execute());

                Assert.Equal("\uBDEA", r.Lines[0].ItemSpec);

                File.Delete(file);

                // Write ANSI .. that won't work!
                a = new WriteLinesToFile();
                a.BuildEngine = new MockEngine();
                a.File = new TaskItem(file);
                a.Lines = new ITaskItem[] { new TaskItem("\uBDEA") };
                a.Encoding = "ASCII";
                Assert.True(a.Execute());

                // Read the line from the file.
                r = new ReadLinesFromFile();
                r.File = new TaskItem(file);
                Assert.True(r.Execute());

                Assert.NotEqual("\uBDEA", r.Lines[0].ItemSpec);
            }
            finally
            {
                File.Delete(file);
            }
        }
コード例 #6
0
        public void IgnoreBlankLines()
        {
            string file = FileUtilities.GetTemporaryFile();
            try
            {
                // Start with a missing file.
                File.Delete(file);

                // Append one line to the file.
                WriteLinesToFile a = new WriteLinesToFile();
                a.File = new TaskItem(file);
                a.Lines = new ITaskItem[]
                {
                    new TaskItem("Line1"),
                    new TaskItem("  "),
                    new TaskItem("Line2"),
                    new TaskItem(""),
                    new TaskItem("Line3"),
                    new TaskItem("\0\0\0\0\0\0\0\0\0")
                };
                Assert.IsTrue(a.Execute());

                // Read the line from the file.
                ReadLinesFromFile r = new ReadLinesFromFile();
                r.File = new TaskItem(file);
                Assert.IsTrue(r.Execute());

                Assert.AreEqual(3, r.Lines.Length);
                Assert.AreEqual("Line1", r.Lines[0].ItemSpec);
                Assert.AreEqual("Line2", r.Lines[1].ItemSpec);
                Assert.AreEqual("Line3", r.Lines[2].ItemSpec);
            }
            finally
            {
                File.Delete(file);
            }
        }