Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 35 additions & 28 deletions src/System.IO.FileSystem/tests/Base/BaseGetSetTimes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace System.IO.Tests
{
public abstract class BaseGetSetTimes<T> : FileSystemTest
{
private const string HFS = "hfs";
public delegate void SetTime(T item, DateTime time);
public delegate DateTime GetTime(T item);

Expand Down Expand Up @@ -71,40 +72,43 @@ public void CanGetAllTimesAfterCreation()
}

[Fact]
[PlatformSpecific(TestPlatforms.Linux)] // Windows tested below, and OSX does not currently support millisec granularity
public void TimesIncludeMillisecondPart_Linux()
[PlatformSpecific(TestPlatforms.AnyUnix)] // Windows tested below, and OSX HFS driver format does not support millisec granularity
public void TimesIncludeMillisecondPart_Unix()
{
T item = GetExistingItem();

string driveFormat = new DriveInfo(GetItemPath(item)).DriveFormat;

Assert.All(TimeFunctions(), (function) =>
if (!PlatformDetection.IsOSX ||
Copy link
Member

@danmoseley danmoseley Jul 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit, sometimes !(A && B) is easier to read than !A || !B. In this case I read "if it's not Mac or it's not HFS" whereas what a human would say is "if it's not HFS on Mac" so I believe

!(PlatformDetection.IsOSX && 
  driveFormat.Equals(HFS, StringComparison.InvariantCultureIgnoreCase)

may be easier to read.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to go back to fix, just general observation

!driveFormat.Equals(HFS, StringComparison.InvariantCultureIgnoreCase))
{
var msec = 0;
for (int i = 0; i < 5; i++)
Assert.All(TimeFunctions(), (function) =>
{
DateTime time = function.Getter(item);
msec = time.Millisecond;
var msec = 0;
for (int i = 0; i < 5; i++)
{
DateTime time = function.Getter(item);
msec = time.Millisecond;

if (msec != 0)
break;
if (msec != 0)
break;

// This case should only happen 1/1000 times, unless the OS/Filesystem does
// not support millisecond granularity.
// This case should only happen 1/1000 times, unless the OS/Filesystem does
// not support millisecond granularity.

// If it's 1/1000, or low granularity, this may help:
Thread.Sleep(1234);
// If it's 1/1000, or low granularity, this may help:
Thread.Sleep(1234);

// If it's the OS/Filesystem often returns 0 for the millisecond part, this may
// help prove it. This should only be written 1/1000 runs, unless the test is going to
// fail.
Console.WriteLine($"## TimesIncludeMillisecondPart got a file time of {time.ToString("o")} on {driveFormat}");
// If it's the OS/Filesystem often returns 0 for the millisecond part, this may
// help prove it. This should only be written 1/1000 runs, unless the test is going to
// fail.
Console.WriteLine($"## TimesIncludeMillisecondPart got a file time of {time.ToString("o")} on {driveFormat}");

item = GetExistingItem(); // try a new file/directory
}
item = GetExistingItem(); // try a new file/directory
}

Assert.NotEqual(0, msec);
});
Assert.NotEqual(0, msec);
});
}
}


Expand Down Expand Up @@ -137,17 +141,20 @@ public void TimesIncludeMillisecondPart_Windows()
}

[Fact]
// OSX does not currently support millisec granularity: use this test as a canary to flag
// if this ever changes so we can enable the actual test
[PlatformSpecific(TestPlatforms.OSX)]
public void TimesIncludeMillisecondPart_OSX()
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment above is now outdated

T item = GetExistingItem();
Assert.All(TimeFunctions(), (function) =>
string driveFormat = new DriveInfo(Path.GetTempPath()).DriveFormat;
if (driveFormat.Equals(HFS, StringComparison.InvariantCultureIgnoreCase))
{
DateTime time = function.Getter(item);
Assert.Equal(0, time.Millisecond);
});
// OSX HFS driver format does not support millisec granularity
Assert.All(TimeFunctions(), (function) =>
{
DateTime time = function.Getter(item);
Assert.Equal(0, time.Millisecond);
});
}
}

protected void ValidateSetTimes(T item, DateTime beforeTime, DateTime afterTime)
Expand Down