diff --git a/src/Renci.SshNet/ISftpClient.cs b/src/Renci.SshNet/ISftpClient.cs index b7ca2f4f8..af6d31b94 100644 --- a/src/Renci.SshNet/ISftpClient.cs +++ b/src/Renci.SshNet/ISftpClient.cs @@ -454,12 +454,8 @@ public interface ISftpClient : IBaseClient /// The specified path is invalid, or its directory was not found on the remote host. /// The method was called after the client was disposed. /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// + /// If the file specified by does not exist, it is created. + /// If the file does exist, its contents are replaced. /// StreamWriter CreateText(string path); @@ -476,12 +472,8 @@ public interface ISftpClient : IBaseClient /// The specified path is invalid, or its directory was not found on the remote host. /// The method was called after the client was disposed. /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// + /// If the file specified by does not exist, it is created. + /// If the file does exist, its contents are replaced. /// StreamWriter CreateText(string path, Encoding encoding); @@ -1151,12 +1143,8 @@ public interface ISftpClient : IBaseClient /// The specified path is invalid, or its directory was not found on the remote host. /// The method was called after the client was disposed. /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// + /// If the file specified by does not exist, it is created. + /// If the file does exist, its contents are replaced. /// void WriteAllBytes(string path, byte[] bytes); @@ -1174,10 +1162,8 @@ public interface ISftpClient : IBaseClient /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM). /// /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. + /// If the file specified by does not exist, it is created. + /// If the file does exist, its contents are replaced. /// /// void WriteAllLines(string path, IEnumerable contents); @@ -1193,12 +1179,8 @@ public interface ISftpClient : IBaseClient /// The specified path is invalid, or its directory was not found on the remote host. /// The method was called after the client was disposed. /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// + /// If the file specified by does not exist, it is created. + /// If the file does exist, its contents are replaced. /// void WriteAllLines(string path, IEnumerable contents, Encoding encoding); @@ -1216,10 +1198,8 @@ public interface ISftpClient : IBaseClient /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM). /// /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. + /// If the file specified by does not exist, it is created. + /// If the file does exist, its contents are replaced. /// /// void WriteAllLines(string path, string[] contents); @@ -1235,12 +1215,8 @@ public interface ISftpClient : IBaseClient /// The specified path is invalid, or its directory was not found on the remote host. /// The method was called after the client was disposed. /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// + /// If the file specified by does not exist, it is created. + /// If the file does exist, its contents are replaced. /// void WriteAllLines(string path, string[] contents, Encoding encoding); @@ -1258,10 +1234,8 @@ public interface ISftpClient : IBaseClient /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM). /// /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. + /// If the file specified by does not exist, it is created. + /// If the file does exist, its contents are replaced. /// /// void WriteAllText(string path, string contents); @@ -1277,12 +1251,8 @@ public interface ISftpClient : IBaseClient /// The specified path is invalid, or its directory was not found on the remote host. /// The method was called after the client was disposed. /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// + /// If the file specified by does not exist, it is created. + /// If the file does exist, its contents are replaced. /// void WriteAllText(string path, string contents, Encoding encoding); } diff --git a/src/Renci.SshNet/SftpClient.cs b/src/Renci.SshNet/SftpClient.cs index 2d3ac574b..949c64b3e 100644 --- a/src/Renci.SshNet/SftpClient.cs +++ b/src/Renci.SshNet/SftpClient.cs @@ -1468,56 +1468,18 @@ public SftpFileStream Create(string path, int bufferSize) return SftpFileStream.Open(_sftpSession, path, FileMode.Create, FileAccess.ReadWrite, bufferSize); } - /// - /// Creates or opens a file for writing UTF-8 encoded text. - /// - /// The file to be opened for writing. - /// - /// A that writes text to a file using UTF-8 encoding without - /// a Byte-Order Mark (BOM). - /// - /// is . - /// Client is not connected. - /// The specified path is invalid, or its directory was not found on the remote host. - /// The method was called after the client was disposed. - /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// - /// + /// public StreamWriter CreateText(string path) { return CreateText(path, Utf8NoBOM); } - /// - /// Creates or opens a file for writing text using the specified encoding. - /// - /// The file to be opened for writing. - /// The character encoding to use. - /// - /// A that writes to a file using the specified encoding. - /// - /// is . - /// Client is not connected. - /// The specified path is invalid, or its directory was not found on the remote host. - /// The method was called after the client was disposed. - /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// - /// + /// public StreamWriter CreateText(string path, Encoding encoding) { CheckDisposed(); - return new StreamWriter(OpenWrite(path), encoding); + return new StreamWriter(Open(path, FileMode.Create, FileAccess.Write), encoding); } /// @@ -1903,99 +1865,27 @@ public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc) SetAttributes(path, attributes); } - /// - /// Writes the specified byte array to the specified file, and closes the file. - /// - /// The file to write to. - /// The bytes to write to the file. - /// is . - /// Client is not connected. - /// The specified path is invalid, or its directory was not found on the remote host. - /// The method was called after the client was disposed. - /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// - /// + /// public void WriteAllBytes(string path, byte[] bytes) { - using (var stream = OpenWrite(path)) - { - stream.Write(bytes, 0, bytes.Length); - } + ThrowHelper.ThrowIfNull(bytes); + + UploadFile(new MemoryStream(bytes), path); } - /// - /// Writes a collection of strings to the file using the UTF-8 encoding, and closes the file. - /// - /// The file to write to. - /// The lines to write to the file. - /// is . - /// Client is not connected. - /// The specified path is invalid, or its directory was not found on the remote host. - /// The method was called after the client was disposed. - /// - /// - /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM). - /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// - /// + /// public void WriteAllLines(string path, IEnumerable contents) { WriteAllLines(path, contents, Utf8NoBOM); } - /// - /// Write the specified string array to the file using the UTF-8 encoding, and closes the file. - /// - /// The file to write to. - /// The string array to write to the file. - /// is . - /// Client is not connected. - /// The specified path is invalid, or its directory was not found on the remote host. - /// The method was called after the client was disposed. - /// - /// - /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM). - /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// - /// + /// public void WriteAllLines(string path, string[] contents) { WriteAllLines(path, contents, Utf8NoBOM); } - /// - /// Writes a collection of strings to the file using the specified encoding, and closes the file. - /// - /// The file to write to. - /// The lines to write to the file. - /// The character encoding to use. - /// is . - /// Client is not connected. - /// The specified path is invalid, or its directory was not found on the remote host. - /// The method was called after the client was disposed. - /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// - /// + /// public void WriteAllLines(string path, IEnumerable contents, Encoding encoding) { using (var stream = CreateText(path, encoding)) @@ -2007,55 +1897,13 @@ public void WriteAllLines(string path, IEnumerable contents, Encoding en } } - /// - /// Writes the specified string array to the file by using the specified encoding, and closes the file. - /// - /// The file to write to. - /// The string array to write to the file. - /// An object that represents the character encoding applied to the string array. - /// is . - /// Client is not connected. - /// The specified path is invalid, or its directory was not found on the remote host. - /// The method was called after the client was disposed. - /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// - /// + /// public void WriteAllLines(string path, string[] contents, Encoding encoding) { - using (var stream = CreateText(path, encoding)) - { - foreach (var line in contents) - { - stream.WriteLine(line); - } - } + WriteAllLines(path, (IEnumerable)contents, encoding); } - /// - /// Writes the specified string to the file using the UTF-8 encoding, and closes the file. - /// - /// The file to write to. - /// The string to write to the file. - /// is . - /// Client is not connected. - /// The specified path is invalid, or its directory was not found on the remote host. - /// The method was called after the client was disposed. - /// - /// - /// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM). - /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// - /// + /// public void WriteAllText(string path, string contents) { using (var stream = CreateText(path)) @@ -2064,24 +1912,7 @@ public void WriteAllText(string path, string contents) } } - /// - /// Writes the specified string to the file using the specified encoding, and closes the file. - /// - /// The file to write to. - /// The string to write to the file. - /// The encoding to apply to the string. - /// is . - /// Client is not connected. - /// The specified path is invalid, or its directory was not found on the remote host. - /// The method was called after the client was disposed. - /// - /// - /// If the target file already exists, it is overwritten. It is not first truncated to zero bytes. - /// - /// - /// If the target file does not exist, it is created. - /// - /// + /// public void WriteAllText(string path, string contents, Encoding encoding) { using (var stream = CreateText(path, encoding)) diff --git a/test/Renci.SshNet.IntegrationTests/Common/ArrayBuilder`1.cs b/test/Renci.SshNet.IntegrationTests/Common/ArrayBuilder`1.cs deleted file mode 100644 index 1720c19c9..000000000 --- a/test/Renci.SshNet.IntegrationTests/Common/ArrayBuilder`1.cs +++ /dev/null @@ -1,32 +0,0 @@ -namespace Renci.SshNet.IntegrationTests.Common -{ - public class ArrayBuilder - { - private readonly List _buffer; - - public ArrayBuilder() - { - _buffer = new List(); - } - - public ArrayBuilder Add(T[] array) - { - return Add(array, 0, array.Length); - } - - public ArrayBuilder Add(T[] array, int index, int length) - { - for (var i = 0; i < length; i++) - { - _buffer.Add(array[index + i]); - } - - return this; - } - - public T[] Build() - { - return _buffer.ToArray(); - } - } -} diff --git a/test/Renci.SshNet.IntegrationTests/SftpTests.cs b/test/Renci.SshNet.IntegrationTests/SftpTests.cs index 481b82630..3c65bb0c3 100644 --- a/test/Renci.SshNet.IntegrationTests/SftpTests.cs +++ b/test/Renci.SshNet.IntegrationTests/SftpTests.cs @@ -1058,8 +1058,7 @@ public void Sftp_CreateText_NoEncoding_ExistingFile() const string initialContent = "\u0100ert & Ann"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); const string newContent = "\u0116ver"; - const string expectedContent = "\u0116ver" + " & Ann"; - var expectedContentBytes = GetBytesWithPreamble(expectedContent, encoding); + var newContentBytes = GetBytesWithPreamble(newContent, encoding); using (var client = new SftpClient(_connectionInfoFactory.Create())) { @@ -1076,26 +1075,21 @@ public void Sftp_CreateText_NoEncoding_ExistingFile() { client.WriteAllText(remoteFile, initialContent); - using (client.CreateText(remoteFile)) - { - } - - // verify that original content is left untouched - var actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(initialContentBytes, actualBytes); + Assert.AreEqual(initialContentBytes.Length, client.Get(remoteFile).Length); - // write content that is less bytes than original content using (var sw = client.CreateText(remoteFile)) { sw.Write(newContent); } - // verify that original content is only partially overwritten + // verify that the file was truncated + Assert.AreEqual(newContentBytes.Length, client.Get(remoteFile).Length); + var text = client.ReadAllText(remoteFile); - Assert.AreEqual(expectedContent, text); + Assert.AreEqual(newContent, text); - actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedContentBytes, actualBytes); + var actualBytes = client.ReadAllBytes(remoteFile); + CollectionAssert.AreEqual(newContentBytes, actualBytes); } finally { @@ -1206,8 +1200,7 @@ public void Sftp_CreateText_Encoding_ExistingFile() var initialContent = "\u0100ert & Ann"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); var newContent = "\u0116ver"; - var expectedContent = "\u0116ver" + " & Ann"; - var expectedContentBytes = GetBytesWithPreamble(expectedContent, encoding); + var newContentBytes = GetBytesWithPreamble(newContent, encoding); using (var client = new SftpClient(_connectionInfoFactory.Create())) { @@ -1224,26 +1217,21 @@ public void Sftp_CreateText_Encoding_ExistingFile() { client.WriteAllText(remoteFile, initialContent, encoding); - using (client.CreateText(remoteFile)) - { - } - - // verify that original content is left untouched - var actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(initialContentBytes, actualBytes); + Assert.AreEqual(initialContentBytes.Length, client.Get(remoteFile).Length); - // write content that is less bytes than original content using (var sw = client.CreateText(remoteFile, encoding)) { sw.Write(newContent); } - // verify that original content is only partially overwritten - var text = client.ReadAllText(remoteFile, encoding); - Assert.AreEqual(expectedContent, text); + // verify that the file was truncated + Assert.AreEqual(newContentBytes.Length, client.Get(remoteFile).Length); - actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedContentBytes, actualBytes); + var text = client.ReadAllText(remoteFile); + Assert.AreEqual(newContent, text); + + var actualBytes = client.ReadAllBytes(remoteFile); + CollectionAssert.AreEqual(newContentBytes, actualBytes); } finally { @@ -1942,9 +1930,6 @@ public void Sftp_WriteAllBytes_ExistingFile() { var initialContent = GenerateRandom(size: 13); var newContent1 = GenerateRandom(size: 5); - var expectedContent1 = new ArrayBuilder().Add(newContent1) - .Add(initialContent, newContent1.Length, initialContent.Length - newContent1.Length) - .Build(); var newContent2 = GenerateRandom(size: 50000); using (var client = new SftpClient(_connectionInfoFactory.Create())) @@ -1965,14 +1950,14 @@ public void Sftp_WriteAllBytes_ExistingFile() fs.Write(initialContent, offset: 0, initialContent.Length); } - #region Write less bytes than the current content, overwriting part of that content + #region Write less bytes than the current content, overwriting that content client.WriteAllBytes(remoteFile, newContent1); var actualContent1 = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedContent1, actualContent1); + CollectionAssert.AreEqual(newContent1, actualContent1); - #endregion Write less bytes than the initial content, overwriting part of that content + #endregion #region Write more bytes than the current content, overwriting and appending to that content @@ -1981,7 +1966,7 @@ public void Sftp_WriteAllBytes_ExistingFile() var actualContent2 = client.ReadAllBytes(remoteFile); CollectionAssert.AreEqual(newContent2, actualContent2); - #endregion Write less bytes than the initial content, overwriting part of that content + #endregion } finally { @@ -2068,19 +2053,12 @@ public void Sftp_WriteAllLines_IEnumerable_NoEncoding_ExistingFile() { var encoding = new UTF8Encoding(false, true); var initialContent = "\u0100ert & Ann Forever & Ever Lisa & Sofie"; - var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); IEnumerable linesToWrite1 = new[] { "Forever", "&", "\u0116ver" }; var linesToWrite1Bytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite1) + Environment.NewLine, encoding); - var expectedBytes1 = new ArrayBuilder().Add(linesToWrite1Bytes) - .Add(initialContentBytes, - linesToWrite1Bytes.Length, - initialContentBytes.Length - linesToWrite1Bytes.Length) - .Build(); IEnumerable linesToWrite2 = new[] { "Forever", "&", "\u0116ver", "Gert & Ann", "Lisa + Sofie" }; var linesToWrite2Bytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite2) + Environment.NewLine, encoding); - var expectedBytes2 = linesToWrite2Bytes; using (var client = new SftpClient(_connectionInfoFactory.Create())) { @@ -2098,21 +2076,21 @@ public void Sftp_WriteAllLines_IEnumerable_NoEncoding_ExistingFile() // create initial content client.WriteAllText(remoteFile, initialContent); - #region Write less bytes than the current content, overwriting part of that content + #region Write less bytes than the current content, overwriting that content client.WriteAllLines(remoteFile, linesToWrite1); var actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes1, actualBytes); + CollectionAssert.AreEqual(linesToWrite1Bytes, actualBytes); - #endregion Write less bytes than the current content, overwriting part of that content + #endregion #region Write more bytes than the current content, overwriting and appending to that content client.WriteAllLines(remoteFile, linesToWrite2); actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes2, actualBytes); + CollectionAssert.AreEqual(linesToWrite2Bytes, actualBytes); #endregion Write more bytes than the current content, overwriting and appending to that content } @@ -2203,18 +2181,11 @@ public void Sftp_WriteAllLines_IEnumerable_Encoding_ExistingFile() { var encoding = GetRandomEncoding(); const string initialContent = "\u0100ert & Ann Forever & Ever Lisa & Sofie"; - var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); IEnumerable linesToWrite1 = new[] { "Forever", "&", "\u0116ver" }; var linesToWrite1Bytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite1) + Environment.NewLine, encoding); - var expectedBytes1 = new ArrayBuilder().Add(linesToWrite1Bytes) - .Add(initialContentBytes, - linesToWrite1Bytes.Length, - initialContentBytes.Length - linesToWrite1Bytes.Length) - .Build(); IEnumerable linesToWrite2 = new[] { "Forever", "&", "\u0116ver", "Gert & Ann", "Lisa + Sofie" }; var linesToWrite2Bytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite2) + Environment.NewLine, encoding); - var expectedBytes2 = linesToWrite2Bytes; using (var client = new SftpClient(_connectionInfoFactory.Create())) { @@ -2232,21 +2203,21 @@ public void Sftp_WriteAllLines_IEnumerable_Encoding_ExistingFile() // create initial content client.WriteAllText(remoteFile, initialContent, encoding); - #region Write less bytes than the current content, overwriting part of that content + #region Write less bytes than the current content, overwriting that content client.WriteAllLines(remoteFile, linesToWrite1, encoding); var actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes1, actualBytes); + CollectionAssert.AreEqual(linesToWrite1Bytes, actualBytes); - #endregion Write less bytes than the current content, overwriting part of that content + #endregion #region Write more bytes than the current content, overwriting and appending to that content client.WriteAllLines(remoteFile, linesToWrite2, encoding); actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes2, actualBytes); + CollectionAssert.AreEqual(linesToWrite2Bytes, actualBytes); #endregion Write more bytes than the current content, overwriting and appending to that content } @@ -2336,15 +2307,10 @@ public void Sftp_WriteAllLines_Array_NoEncoding_ExistingFile() { var encoding = new UTF8Encoding(false, true); const string initialContent = "\u0100ert & Ann Forever & Ever Lisa & Sofie"; - var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); var linesToWrite1 = new[] { "Forever", "&", "\u0116ver" }; var linesToWrite1Bytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite1) + Environment.NewLine, encoding); - var expectedBytes1 = new ArrayBuilder().Add(linesToWrite1Bytes) - .Add(initialContentBytes, linesToWrite1Bytes.Length, initialContentBytes.Length - linesToWrite1Bytes.Length) - .Build(); var linesToWrite2 = new[] { "Forever", "&", "\u0116ver", "Gert & Ann", "Lisa + Sofie" }; var linesToWrite2Bytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite2) + Environment.NewLine, encoding); - var expectedBytes2 = linesToWrite2Bytes; using (var client = new SftpClient(_connectionInfoFactory.Create())) { @@ -2362,21 +2328,21 @@ public void Sftp_WriteAllLines_Array_NoEncoding_ExistingFile() // create initial content client.WriteAllText(remoteFile, initialContent); - #region Write less bytes than the current content, overwriting part of that content + #region Write less bytes than the current content, overwriting that content client.WriteAllLines(remoteFile, linesToWrite1); var actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes1, actualBytes); + CollectionAssert.AreEqual(linesToWrite1Bytes, actualBytes); - #endregion Write less bytes than the current content, overwriting part of that content + #endregion #region Write more bytes than the current content, overwriting and appending to that content client.WriteAllLines(remoteFile, linesToWrite2); actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes2, actualBytes); + CollectionAssert.AreEqual(linesToWrite2Bytes, actualBytes); #endregion Write more bytes than the current content, overwriting and appending to that content } @@ -2468,15 +2434,10 @@ public void Sftp_WriteAllLines_Array_Encoding_ExistingFile() const string initialContent = "\u0100ert & Ann Forever & Ever Lisa & Sofie"; var encoding = GetRandomEncoding(); - var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); var linesToWrite1 = new[] { "Forever", "&", "\u0116ver" }; var linesToWrite1Bytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite1) + Environment.NewLine, encoding); - var expectedBytes1 = new ArrayBuilder().Add(linesToWrite1Bytes) - .Add(initialContentBytes, linesToWrite1Bytes.Length, initialContentBytes.Length - linesToWrite1Bytes.Length) - .Build(); var linesToWrite2 = new[] { "Forever", "&", "\u0116ver", "Gert & Ann", "Lisa + Sofie" }; var linesToWrite2Bytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite2) + Environment.NewLine, encoding); - var expectedBytes2 = linesToWrite2Bytes; using (var client = new SftpClient(_connectionInfoFactory.Create())) { @@ -2494,23 +2455,23 @@ public void Sftp_WriteAllLines_Array_Encoding_ExistingFile() // create initial content client.WriteAllText(remoteFile, initialContent, encoding); - #region Write less bytes than the current content, overwriting part of that content + #region Write less bytes than the current content, overwriting that content client.WriteAllLines(remoteFile, linesToWrite1, encoding); var actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes1, actualBytes); + CollectionAssert.AreEqual(linesToWrite1Bytes, actualBytes); - #endregion Write less bytes than the current content, overwriting part of that content + #endregion #region Write more bytes than the current content, overwriting and appending to that content client.WriteAllLines(remoteFile, linesToWrite2, encoding); actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes2, actualBytes); + CollectionAssert.AreEqual(linesToWrite2Bytes, actualBytes); - #endregion Write more bytes than the current content, overwriting and appending to that content + #endregion } finally { @@ -2601,14 +2562,9 @@ public void Sftp_WriteAllText_NoEncoding_ExistingFile() const string newContent1 = "For\u0116ver & Ever"; var encoding = new UTF8Encoding(false, true); - var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); var newContent1Bytes = GetBytesWithPreamble(newContent1, encoding); - var expectedBytes1 = new ArrayBuilder().Add(newContent1Bytes) - .Add(initialContentBytes, newContent1Bytes.Length, initialContentBytes.Length - newContent1Bytes.Length) - .Build(); var newContent2 = "Sofie & Lisa For\u0116ver & Ever with \u0100ert & Ann"; var newContent2Bytes = GetBytesWithPreamble(newContent2, encoding); - var expectedBytes2 = newContent2Bytes; using (var client = new SftpClient(_connectionInfoFactory.Create())) { @@ -2625,21 +2581,21 @@ public void Sftp_WriteAllText_NoEncoding_ExistingFile() { client.WriteAllText(remoteFile, initialContent); - #region Write less bytes than the current content, overwriting part of that content + #region Write less bytes than the current content, overwriting that content client.WriteAllText(remoteFile, newContent1); var actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes1, actualBytes); + CollectionAssert.AreEqual(newContent1Bytes, actualBytes); - #endregion Write less bytes than the current content, overwriting part of that content + #endregion #region Write more bytes than the current content, overwriting and appending to that content client.WriteAllText(remoteFile, newContent2); actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes2, actualBytes); + CollectionAssert.AreEqual(newContent2Bytes, actualBytes); #endregion Write more bytes than the current content, overwriting and appending to that content } @@ -2734,13 +2690,8 @@ public void Sftp_WriteAllText_Encoding_ExistingFile() const string newContent2 = "Sofie & Lisa For\u0116ver & Ever with \u0100ert & Ann"; var encoding = GetRandomEncoding(); - var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); var newContent1Bytes = GetBytesWithPreamble(newContent1, encoding); - var expectedBytes1 = new ArrayBuilder().Add(newContent1Bytes) - .Add(initialContentBytes, newContent1Bytes.Length, initialContentBytes.Length - newContent1Bytes.Length) - .Build(); var newContent2Bytes = GetBytesWithPreamble(newContent2, encoding); - var expectedBytes2 = newContent2Bytes; using (var client = new SftpClient(_connectionInfoFactory.Create())) { @@ -2757,21 +2708,21 @@ public void Sftp_WriteAllText_Encoding_ExistingFile() { client.WriteAllText(remoteFile, initialContent, encoding); - #region Write less bytes than the current content, overwriting part of that content + #region Write less bytes than the current content, overwriting that content client.WriteAllText(remoteFile, newContent1, encoding); var actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes1, actualBytes); + CollectionAssert.AreEqual(newContent1Bytes, actualBytes); - #endregion Write less bytes than the current content, overwriting part of that content + #endregion #region Write more bytes than the current content, overwriting and appending to that content client.WriteAllText(remoteFile, newContent2, encoding); actualBytes = client.ReadAllBytes(remoteFile); - CollectionAssert.AreEqual(expectedBytes2, actualBytes); + CollectionAssert.AreEqual(newContent2Bytes, actualBytes); #endregion Write more bytes than the current content, overwriting and appending to that content }