public void Encoding()
        {
            var file = FileUtilities.GetTemporaryFile();

            try
            {
                // Write default encoding: UTF8
                var a = new WriteLinesToFile
                {
                    BuildEngine = new MockEngine(_output),
                    File        = new TaskItem(file),
                    Lines       = new ITaskItem[] { new TaskItem("\uBDEA") }
                };
                Assert.True(a.Execute());

                var r = new ReadLinesFromFile
                {
                    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
                {
                    BuildEngine = new MockEngine(_output),
                    File        = new TaskItem(file),
                    Lines       = new ITaskItem[] { new TaskItem("\uBDEA") },
                    Encoding    = "ASCII"
                };
                Assert.True(a.Execute());

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

                Assert.NotEqual("\uBDEA", r.Lines[0].ItemSpec);
            }
            finally
            {
                File.Delete(file);
            }
        }
Example #2
0
        public void ReadNoAccess()
        {
            if (NativeMethodsShared.IsUnixLike)
            {
                return; // "The security API is not the same under Unix"
            }

            var file = FileUtilities.GetTemporaryFile();

            try
            {
                // Start with a missing file.
                File.Delete(file);

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

                // Remove all File access to the file to current user
                var fSecurity   = File.GetAccessControl(file);
                var 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.
                var r       = new ReadLinesFromFile();
                var mEngine = new MockEngine();
                r.BuildEngine = mEngine;
                r.File        = new TaskItem(file);
                Assert.False(r.Execute());
            }
            finally
            {
                var fSecurity   = File.GetAccessControl(file);
                var 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);
            }
        }
Example #3
0
        public void Escaping()
        {
            var file = FileUtilities.GetTemporaryFile();

            try
            {
                // Start with a missing file.
                File.Delete(file);

                // Append one line to the file.
                var a = new WriteLinesToFile
                {
                    File  = new TaskItem(file),
                    Lines = new ITaskItem[] { new TaskItem("Line1_%253b_") }
                };
                Assert.True(a.Execute());

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

                Assert.Single(r.Lines);
                Assert.Equal("Line1_%3b_", 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_%3b_", r.Lines[0].ItemSpec);
                Assert.Equal("Line2", r.Lines[1].ItemSpec);
                Assert.Equal("Line3", r.Lines[2].ItemSpec);
            }
            finally
            {
                File.Delete(file);
            }
        }
Example #4
0
        public void IgnoreBlankLines()
        {
            var file = FileUtilities.GetTemporaryFile();

            try
            {
                // Start with a missing file.
                File.Delete(file);

                // Append one line to the file.
                var a = new WriteLinesToFile
                {
                    File  = new TaskItem(file),
                    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.True(a.Execute());

                // Read the line from the file.
                var r = new ReadLinesFromFile
                {
                    File = new TaskItem(file)
                };
                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);
            }
        }
Example #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.IsTrue(a.Execute());

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

                Assert.IsTrue("\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.IsTrue(a.Execute());

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

                Assert.IsTrue("\uBDEA" != r.Lines[0].ItemSpec);
            }
            finally
            {
                File.Delete(file);
            }
        }
Example #6
0
        public void WriteLinesToFileDoesCreateDirectory()
        {
            using (var testEnv = TestEnvironment.Create())
            {
                var directory = testEnv.CreateFolder(folderPath: null, createFolder: false);
                var file      = Path.Combine(directory.Path, $"{Guid.NewGuid().ToString("N")}.tmp");

                var WriteLinesToFile = new WriteLinesToFile
                {
                    BuildEngine = new MockEngine(_output),
                    File        = new TaskItem(file),
                    Lines       = new ITaskItem[] { new TaskItem("WriteLinesToFileDoesCreateDirectory Test") }
                };

                // Verify that the diretory doesn't exist. Otherwise the test would pass - even it should not.
                Directory.Exists(directory.Path).ShouldBeFalse();

                WriteLinesToFile.Execute().ShouldBeTrue();

                Directory.Exists(directory.Path).ShouldBeTrue();
            }
        }
Example #7
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);
            }
        }
Example #8
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);
            }
        }
        private void WriteLinesToFileDeleteFileCore(ITaskItem[] lines)
        {
            using (var testEnv = TestEnvironment.Create())
            {
                var file = testEnv.CreateFile();

                var WriteLinesToFile = new WriteLinesToFile
                {
                    Overwrite   = true,
                    BuildEngine = new MockEngine(_output),
                    File        = new TaskItem(file.Path),
                    Lines       = lines
                };

                // Verify that the file does exist. Otherwise the test would pass - even it should not.
                File.Exists(file.Path).ShouldBeTrue();

                WriteLinesToFile.Execute().ShouldBeTrue();

                File.Exists(file.Path).ShouldBeFalse();
            }
        }
Example #10
0
        public void WriteLinesWriteOnlyWhenDifferentTest()
        {
            string file = FileUtilities.GetTemporaryFile();

            try
            {
                // Write an initial file.
                WriteLinesToFile a = new WriteLinesToFile
                {
                    Overwrite              = true,
                    BuildEngine            = new MockEngine(),
                    File                   = new TaskItem(file),
                    WriteOnlyWhenDifferent = true,
                    Lines                  = new ITaskItem[] { new TaskItem("File contents1") }
                };

                Assert.True(a.Execute());

                // Verify contents
                ReadLinesFromFile r = new ReadLinesFromFile {
                    File = new TaskItem(file)
                };
                Assert.True(r.Execute());
                Assert.Equal("File contents1", r.Lines[0].ItemSpec);

                var creationTime = File.GetCreationTime(file);
                var writeTime    = File.GetLastWriteTime(file);

                if (!NativeMethodsShared.IsWindows)
                {
                    // Non-Windows file systems do not have sub-second timestamps. Unfortunately we need
                    // to sleep to determine if the file is written or not.
                    Thread.Sleep(1000);
                }

                // Write the same contents to the file, timestamps should match.
                WriteLinesToFile a2 = new WriteLinesToFile
                {
                    Overwrite              = true,
                    BuildEngine            = new MockEngine(),
                    File                   = new TaskItem(file),
                    WriteOnlyWhenDifferent = true,
                    Lines                  = new ITaskItem[] { new TaskItem("File contents1") }
                };
                Assert.True(a2.Execute());
                Assert.Equal(creationTime, File.GetCreationTime(file));
                Assert.Equal(writeTime, File.GetLastWriteTime(file));

                if (!NativeMethodsShared.IsWindows)
                {
                    // Non-Windows file systems do not have sub-second timestamps. Unfortunately we need
                    // to sleep to determine if the file is written or not.
                    Thread.Sleep(1000);
                }

                // Write different contents to the file, last write time should differ.
                WriteLinesToFile a3 = new WriteLinesToFile
                {
                    Overwrite              = true,
                    BuildEngine            = new MockEngine(),
                    File                   = new TaskItem(file),
                    WriteOnlyWhenDifferent = true,
                    Lines                  = new ITaskItem[] { new TaskItem("File contents2") }
                };
                Assert.True(a3.Execute());
                Assert.Equal(creationTime, File.GetCreationTime(file));
                Assert.NotEqual(writeTime, File.GetLastWriteTime(file));
            }
            finally
            {
                File.Delete(file);
            }
        }