Skip to content

[wasm] Skip Assert.InRange in Copy.cs for Browser #41000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 20, 2020
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
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