Skip to content

Commit

Permalink
Update tests for SetLastAccessTime(Utc) to also verify the time compo…
Browse files Browse the repository at this point in the history
…nent and the Kind of the DateTime value returned by GetLastAccessTime(Utc). (#1198)
  • Loading branch information
drieseng authored Oct 9, 2023
1 parent 70f58b7 commit 1c7166a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
11 changes: 11 additions & 0 deletions src/Renci.SshNet.IntegrationTests/Common/DateTimeAssert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Renci.SshNet.IntegrationTests.Common
{
public static class DateTimeAssert
{
public static void AreEqual(DateTime expected, DateTime actual)
{
Assert.AreEqual(expected, actual, $"Expected {expected:o}, but was {actual:o}.");
Assert.AreEqual(expected.Kind, actual.Kind);
}
}
}
12 changes: 12 additions & 0 deletions src/Renci.SshNet.IntegrationTests/Common/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Renci.SshNet.IntegrationTests.Common
{
public static class DateTimeExtensions
{
public static DateTime TruncateToWholeSeconds(this DateTime dateTime)
{
return dateTime.AddMilliseconds(-dateTime.Millisecond)
.AddMicroseconds(-dateTime.Microsecond)
.AddTicks(-(dateTime.Nanosecond / 100));
}
}
}
42 changes: 20 additions & 22 deletions src/Renci.SshNet.IntegrationTests/SftpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6153,15 +6153,15 @@ public void Sftp_SetLastAccessTime()
try
{
var time = client.GetLastAccessTime(testFilePath);
Assert.AreEqual(currentTime.Year, time.Year);
Assert.AreEqual(currentTime.Month, time.Month);
Assert.AreEqual(currentTime.Day, time.Day);

var newTime = new DateTime(1986, 03, 15, 01, 02, 03);
DateTimeAssert.AreEqual(currentTime.TruncateToWholeSeconds(), time);

var newTime = new DateTime(1986, 03, 15, 01, 02, 03, 123, DateTimeKind.Local);

client.SetLastAccessTime(testFilePath, newTime);
time = client.GetLastAccessTime(testFilePath);
Assert.AreEqual(newTime, time);

DateTimeAssert.AreEqual(newTime.TruncateToWholeSeconds(), time);
}
finally
{
Expand All @@ -6185,16 +6185,15 @@ public void Sftp_SetLastAccessTimeUtc()
try
{
var time = client.GetLastAccessTimeUtc(testFilePath);
Assert.AreEqual(currentTime.Year, time.Year);
Assert.AreEqual(currentTime.Month, time.Month);
Assert.AreEqual(currentTime.Day, time.Day);

var newTime = new DateTime(1986, 03, 15, 01, 02, 03);
DateTime.SpecifyKind(newTime, DateTimeKind.Utc);
DateTimeAssert.AreEqual(currentTime.TruncateToWholeSeconds(), time);

var newTime = new DateTime(1986, 03, 15, 01, 02, 03, 123, DateTimeKind.Utc);

client.SetLastAccessTimeUtc(testFilePath, newTime);
time = client.GetLastAccessTimeUtc(testFilePath);
Assert.AreEqual(newTime, time);

DateTimeAssert.AreEqual(newTime.TruncateToWholeSeconds(), time);
}
finally
{
Expand All @@ -6217,15 +6216,15 @@ public void Sftp_SetLastWriteTime()
try
{
var time = client.GetLastWriteTime(testFilePath);
Assert.AreEqual(currentTime.Year, time.Year);
Assert.AreEqual(currentTime.Month, time.Month);
Assert.AreEqual(currentTime.Day, time.Day);

var newTime = new DateTime(1986, 03, 15, 01, 02, 03);
DateTimeAssert.AreEqual(currentTime.TruncateToWholeSeconds(), time);

var newTime = new DateTime(1986, 03, 15, 01, 02, 03, 123, DateTimeKind.Local);

client.SetLastWriteTime(testFilePath, newTime);
time = client.GetLastWriteTime(testFilePath);
Assert.AreEqual(newTime, time);

DateTimeAssert.AreEqual(newTime.TruncateToWholeSeconds(), time);
}
finally
{
Expand All @@ -6248,16 +6247,15 @@ public void Sftp_SetLastWriteTimeUtc()
try
{
var time = client.GetLastWriteTimeUtc(testFilePath);
Assert.AreEqual(currentTime.Year, time.Year);
Assert.AreEqual(currentTime.Month, time.Month);
Assert.AreEqual(currentTime.Day, time.Day);

var newTime = new DateTime(1986, 03, 15, 01, 02, 03);
DateTime.SpecifyKind(newTime, DateTimeKind.Utc);
DateTimeAssert.AreEqual(currentTime.TruncateToWholeSeconds(), time);

var newTime = new DateTime(1986, 03, 15, 01, 02, 03, 123, DateTimeKind.Utc);

client.SetLastWriteTimeUtc(testFilePath, newTime);
time = client.GetLastWriteTimeUtc(testFilePath);
Assert.AreEqual(newTime, time);

DateTimeAssert.AreEqual(newTime.TruncateToWholeSeconds(), time);
}
finally
{
Expand Down
6 changes: 6 additions & 0 deletions src/Renci.SshNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Renci.SshNet.IntegrationTes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Renci.SshNet.TestTools.OpenSSH", "Renci.SshNet.TestTools.OpenSSH\Renci.SshNet.TestTools.OpenSSH.csproj", "{78239046-2019-494E-B6EC-240AF787E4D0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E8A42832-1183-4E66-9141-DEBA662374DF}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -158,6 +163,7 @@ Global
{94EE3919-19FA-4D9B-8DA9-249050B15232} = {2D6CAE62-D053-476F-9BDD-2B1F27FA9C5D}
{A6C3FFD3-16A5-44D3-8C1F-3613D6DD17D1} = {2D6CAE62-D053-476F-9BDD-2B1F27FA9C5D}
{D21A4D03-0AC2-4613-BB6D-74D2D16A72CC} = {04E8CC26-116E-4116-9558-7ED542548E70}
{E8A42832-1183-4E66-9141-DEBA662374DF} = {04E8CC26-116E-4116-9558-7ED542548E70}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BAD6019D-4AF7-4E15-99A0-8036E16FC0E5}
Expand Down

0 comments on commit 1c7166a

Please sign in to comment.