Skip to content

Commit

Permalink
[wasm][browser] Fix System.IO.FileSystem failing tests requiring nano…
Browse files Browse the repository at this point in the history
…second/millisecond granularity (dotnet#41288)

* Emscripten lacks ns/ms granularity which I included a switch for and added a browser platform check
* Emscripten takes maximum of utime and atime so I set utime and atime together for Browser
  • Loading branch information
tqiu8 authored Aug 26, 2020
1 parent c6da68c commit 6219594
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ private unsafe void SetAccessOrWriteTime(string path, DateTimeOffset time, bool
const long TicksPerSecond = TicksPerMillisecond * 1000;
long nanoseconds = (time.UtcDateTime.Ticks - DateTimeOffset.UnixEpoch.Ticks - seconds * TicksPerSecond) * NanosecondsPerTick;

#if TARGET_BROWSER
buf[0].TvSec = seconds;
buf[0].TvNsec = nanoseconds;
buf[1].TvSec = seconds;
buf[1].TvNsec = nanoseconds;
#else
if (isAccessTime)
{
buf[0].TvSec = seconds;
Expand All @@ -278,9 +284,8 @@ private unsafe void SetAccessOrWriteTime(string path, DateTimeOffset time, bool
buf[1].TvSec = seconds;
buf[1].TvNsec = nanoseconds;
}

#endif
Interop.CheckIo(Interop.Sys.UTimensat(path, buf), path, InitiallyDirectory);

_fileStatusInitialized = -1;
}

Expand All @@ -301,6 +306,7 @@ public void Refresh(ReadOnlySpan<char> path)
// lstat fails, as a broken symlink should still report info on exists, attributes, etc.
_isDirectory = false;
path = Path.TrimEndingDirectorySeparator(path);

int result = Interop.Sys.LStat(path, out _fileStatus);
if (result < 0)
{
Expand Down
17 changes: 9 additions & 8 deletions src/libraries/System.IO.FileSystem/tests/Base/BaseGetSetTimes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public abstract class BaseGetSetTimes<T> : FileSystemTest
private static string driveFormat = PlatformDetection.IsInAppContainer ? string.Empty : new DriveInfo(Path.GetTempPath()).DriveFormat;

protected static bool isHFS => driveFormat != null && driveFormat.Equals(HFS, StringComparison.InvariantCultureIgnoreCase);
protected static bool isNotHFS => !isHFS;

protected static bool LowTemporalResolution => PlatformDetection.IsBrowser || isHFS;
protected static bool HighTemporalResolution => !LowTemporalResolution;

protected abstract T GetExistingItem();
protected abstract T GetMissingItem();
Expand All @@ -41,15 +43,15 @@ public static TimeFunction Create(SetTime setter, GetTime getter, DateTimeKind k
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/40528", TestPlatforms.Browser)]
public void SettingUpdatesProperties()
{
T item = GetExistingItem();

Assert.All(TimeFunctions(requiresRoundtripping: true), (function) =>
{
// Checking that milliseconds are not dropped after setter.
DateTime dt = new DateTime(2014, 12, 1, 12, 3, 3, isHFS ? 0 : 321, function.Kind);
// Emscripten drops milliseconds in Browser
DateTime dt = new DateTime(2014, 12, 1, 12, 3, 3, LowTemporalResolution ? 0 : 321, function.Kind);
function.Setter(item, dt);
DateTime result = function.Getter(item);
Assert.Equal(dt, result);
Expand Down Expand Up @@ -77,8 +79,7 @@ public void CanGetAllTimesAfterCreation()
ValidateSetTimes(item, beforeTime, afterTime);
}

[ConditionalFact(nameof(isNotHFS))] // OSX HFS driver format does not support millisec granularity
[ActiveIssue("https://github.com/dotnet/runtime/issues/40530", TestPlatforms.Browser)]
[ConditionalFact(nameof(HighTemporalResolution))] // OSX HFS driver format and Browser platform do not support millisec granularity
public void TimesIncludeMillisecondPart()
{
T item = GetExistingItem();
Expand Down Expand Up @@ -110,11 +111,11 @@ public void TimesIncludeMillisecondPart()
});
}

[ConditionalFact(nameof(isHFS))]
public void TimesIncludeMillisecondPart_HFS()
[ConditionalFact(nameof(LowTemporalResolution))]
public void TimesIncludeMillisecondPart_LowTempRes()
{
T item = GetExistingItem();
// OSX HFS driver format does not support millisec granularity
// OSX HFS driver format and Browser do not support millisec granularity
Assert.All(TimeFunctions(), (function) =>
{
DateTime time = function.Getter(item);
Expand Down
3 changes: 1 addition & 2 deletions src/libraries/System.IO.FileSystem/tests/File/GetSetTimes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ public void SetLastWriteTimeTicks()
Assert.True(firstFileTicks <= secondFileTicks, $"First File Ticks\t{firstFileTicks}\nSecond File Ticks\t{secondFileTicks}");
}

[ConditionalFact(nameof(isNotHFS))] // OSX HFS driver format does not support nanosecond granularity.
[ActiveIssue("https://github.com/dotnet/runtime/issues/40532", TestPlatforms.Browser)]
[ConditionalFact(nameof(HighTemporalResolution))] // OSX HFS driver format/Browser Platform do not support nanosecond granularity.
public void SetUptoNanoseconds()
{
string file = GetTestFilePath();
Expand Down
21 changes: 9 additions & 12 deletions src/libraries/System.IO.FileSystem/tests/FileInfo/GetSetTimes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ public override IEnumerable<TimeFunction> TimeFunctions(bool requiresRoundtrippi
DateTimeKind.Utc);
}

[ConditionalFact(nameof(isNotHFS))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/40530", TestPlatforms.Browser)]
[ConditionalFact(nameof(HighTemporalResolution))]
public void CopyToMillisecondPresent()
{
FileInfo input = GetNonZeroMilliseconds();
Expand All @@ -118,8 +117,7 @@ public void CopyToMillisecondPresent()
Assert.NotEqual(0, output.LastWriteTime.Millisecond);
}

[ConditionalFact(nameof(isNotHFS))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/40530", TestPlatforms.Browser)]
[ConditionalFact(nameof(HighTemporalResolution))]
public void CopyToNanosecondsPresent()
{
FileInfo input = GetNonZeroNanoseconds();
Expand All @@ -135,8 +133,8 @@ public void CopyToNanosecondsPresent()
Assert.True(HasNonZeroNanoseconds(output.LastWriteTime));
}

[ConditionalFact(nameof(isHFS))]
public void CopyToNanosecondsPresent_HFS()
[ConditionalFact(nameof(LowTemporalResolution))]
public void CopyToNanosecondsPresent_LowTempRes()
{
FileInfo input = new FileInfo(GetTestFilePath());
input.Create().Dispose();
Expand All @@ -149,8 +147,8 @@ public void CopyToNanosecondsPresent_HFS()
Assert.False(HasNonZeroNanoseconds(output.LastWriteTime));
}

[ConditionalFact(nameof(isHFS))]
public void MoveToMillisecondPresent_HFS()
[ConditionalFact(nameof(LowTemporalResolution))]
public void MoveToMillisecondPresent_LowTempRes()
{
FileInfo input = new FileInfo(GetTestFilePath());
input.Create().Dispose();
Expand All @@ -161,8 +159,7 @@ public void MoveToMillisecondPresent_HFS()
Assert.Equal(0, output.LastWriteTime.Millisecond);
}

[ConditionalFact(nameof(isNotHFS))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/40530", TestPlatforms.Browser)]
[ConditionalFact(nameof(HighTemporalResolution))]
public void MoveToMillisecondPresent()
{
FileInfo input = GetNonZeroMilliseconds();
Expand All @@ -173,8 +170,8 @@ public void MoveToMillisecondPresent()
Assert.NotEqual(0, output.LastWriteTime.Millisecond);
}

[ConditionalFact(nameof(isHFS))]
public void CopyToMillisecondPresent_HFS()
[ConditionalFact(nameof(LowTemporalResolution))]
public void CopyToMillisecondPresent_LowTempRes()
{
FileInfo input = new FileInfo(GetTestFilePath());
input.Create().Dispose();
Expand Down

0 comments on commit 6219594

Please sign in to comment.