From 2e68b6e642fef547c3a6c7a4e665994e7c8aefae Mon Sep 17 00:00:00 2001 From: Mitchell Hwang Date: Thu, 20 Aug 2020 08:40:12 -0400 Subject: [PATCH] [wasm] Skip Assert.InRange in Copy.cs for Browser (#41000) * Revert "[Wasm] Enable CopyWithData FileSystem test (#40663)" This reverts commit 1821567ab60e156101f2a5bdcd5dab6d96dc3c71. * Provide context for skipping Assert in Copy.cs Co-authored-by: Mitchell Hwang --- .../System.IO.FileSystem/tests/File/Copy.cs | 46 +++++-------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/src/libraries/System.IO.FileSystem/tests/File/Copy.cs b/src/libraries/System.IO.FileSystem/tests/File/Copy.cs index ee362cda28339..8fd9ea4183e16 100644 --- a/src/libraries/System.IO.FileSystem/tests/File/Copy.cs +++ b/src/libraries/System.IO.FileSystem/tests/File/Copy.cs @@ -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] @@ -115,7 +108,6 @@ public static IEnumerable 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(); @@ -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) @@ -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))) @@ -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)