Skip to content

Commit

Permalink
[wasm] Skip Assert.InRange in Copy.cs for Browser (#41000)
Browse files Browse the repository at this point in the history
* Revert "[Wasm] Enable CopyWithData FileSystem test (#40663)"

This reverts commit 1821567.

* Provide context for skipping Assert in Copy.cs

Co-authored-by: Mitchell Hwang <mitchell.hwang@microsoft.com>
  • Loading branch information
mdh1418 and Mitchell Hwang authored Aug 20, 2020
1 parent 982e275 commit 2e68b6e
Showing 1 changed file with 12 additions and 34 deletions.
46 changes: 12 additions & 34 deletions src/libraries/System.IO.FileSystem/tests/File/Copy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ protected virtual void Copy(string source, string dest)
File.Copy(source, dest);
}

protected virtual void CopyReadOnlyFile(string source, string dest)
{
byte[] bits = File.ReadAllBytes(source);
File.WriteAllBytes(dest, bits);
File.SetAttributes(dest, FileAttributes.ReadOnly);
}

#region UniversalTests

[Fact]
Expand Down Expand Up @@ -115,7 +108,6 @@ public static IEnumerable<object[]> CopyFileWithData_MemberData()

[Theory]
[MemberData(nameof(CopyFileWithData_MemberData))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/40867", TestPlatforms.Browser)]
public void CopyFileWithData(char[] data, bool readOnly)
{
string testFileSource = GetTestFilePath();
Expand All @@ -127,21 +119,8 @@ public void CopyFileWithData(char[] data, bool readOnly)
stream.Write(data, 0, data.Length);
}

DateTime lastWriteTime;
if (PlatformDetection.IsBrowser)
{
// For browser, there is technically only 1 time. It's the max
// of LastWrite and LastAccess. Setting to a date/time in the future
// is a way of making this test work similarly.
//
// https://emscripten.org/docs/api_reference/Filesystem-API.html#FS.utime
//
lastWriteTime = DateTime.UtcNow.Add(TimeSpan.FromHours(1));
}
else
{
lastWriteTime = DateTime.UtcNow.Subtract(TimeSpan.FromHours(1));
}
// Set the last write time of the source file to something a while ago
DateTime lastWriteTime = DateTime.UtcNow.Subtract(TimeSpan.FromHours(1));
File.SetLastWriteTime(testFileSource, lastWriteTime);

if (readOnly)
Expand All @@ -150,16 +129,7 @@ public void CopyFileWithData(char[] data, bool readOnly)
}

// Copy over the data
//
// For browser, work around limitation of File.Copy, which
// fails when trying to open the dest file
if (PlatformDetection.IsBrowser && readOnly)
{
CopyReadOnlyFile(testFileSource, testFileDest);
File.SetLastWriteTime(testFileDest, lastWriteTime);
}
else
Copy(testFileSource, testFileDest);
Copy(testFileSource, testFileDest);

// Ensure copy transferred written data
using (StreamReader stream = new StreamReader(File.OpenRead(testFileDest)))
Expand All @@ -170,7 +140,15 @@ public void CopyFileWithData(char[] data, bool readOnly)
}

// Ensure last write/access time on the new file is appropriate
Assert.InRange(File.GetLastWriteTimeUtc(testFileDest), lastWriteTime.AddSeconds(-1), lastWriteTime.AddSeconds(1));
//
// For browser, there is technically only 1 time. It's the max
// of LastWrite and LastAccess. On browser, File.SetLastWriteTime
// overwrites LastWrite and LastAccess, and File.Copy
// overwrites LastWrite , so this check doesn't apply.
if (PlatformDetection.IsNotBrowser)
{
Assert.InRange(File.GetLastWriteTimeUtc(testFileDest), lastWriteTime.AddSeconds(-1), lastWriteTime.AddSeconds(1));
}

Assert.Equal(readOnly, (File.GetAttributes(testFileDest) & FileAttributes.ReadOnly) != 0);
if (readOnly)
Expand Down

0 comments on commit 2e68b6e

Please sign in to comment.