-
-
Notifications
You must be signed in to change notification settings - Fork 5
Add polyfill for File.Move(..., overwrite)
#41
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| using System.IO; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.NetCore30; | ||
|
|
||
| public class FileTests | ||
| { | ||
| [Fact] | ||
| public void Move_Overwrite_TargetDoesNotExist_Test() | ||
| { | ||
| // Arrange | ||
| var sourceFileName = Path.GetTempFileName(); | ||
| var destFileName = Path.GetTempFileName(); | ||
|
|
||
| try | ||
| { | ||
| // Path.GetTempFileName() pre-creates the file, so delete it to simulate non-existence | ||
| File.Delete(destFileName); | ||
|
|
||
| // Act | ||
| File.Move(sourceFileName, destFileName, true); | ||
|
|
||
| // Assert | ||
| File.Exists(sourceFileName).Should().BeFalse(); | ||
| File.Exists(destFileName).Should().BeTrue(); | ||
| } | ||
| finally | ||
| { | ||
| try | ||
| { | ||
| File.Delete(sourceFileName); | ||
| File.Delete(destFileName); | ||
| } | ||
| catch | ||
| { | ||
| // Ignore | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Move_Overwrite_TargetExists_Test() | ||
| { | ||
| // Arrange | ||
| var sourceFileName = Path.GetTempFileName(); | ||
| var destFileName = Path.GetTempFileName(); | ||
|
|
||
| try | ||
| { | ||
| // Act | ||
| File.Move(sourceFileName, destFileName, true); | ||
|
|
||
| // Assert | ||
| File.Exists(sourceFileName).Should().BeFalse(); | ||
| File.Exists(destFileName).Should().BeTrue(); | ||
| } | ||
| finally | ||
| { | ||
| try | ||
| { | ||
| File.Delete(sourceFileName); | ||
| File.Delete(destFileName); | ||
| } | ||
| catch | ||
| { | ||
| // Ignore | ||
| } | ||
|
Comment on lines
+65
to
+68
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,28 @@ | ||||||||||||||||||||||||||||||
| // The following comment is required to instruct analyzers to skip this file | ||||||||||||||||||||||||||||||
| // <auto-generated/> | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #if (NETCOREAPP && !NETCOREAPP3_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) | ||||||||||||||||||||||||||||||
| #nullable enable | ||||||||||||||||||||||||||||||
| // ReSharper disable RedundantUsingDirective | ||||||||||||||||||||||||||||||
| // ReSharper disable CheckNamespace | ||||||||||||||||||||||||||||||
| // ReSharper disable InconsistentNaming | ||||||||||||||||||||||||||||||
| // ReSharper disable PartialTypeWithSinglePart | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| using System.IO; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| internal static partial class PolyfillExtensions | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| // No file I/O on .NET Standard prior to 1.3 | ||||||||||||||||||||||||||||||
| #if !NETSTANDARD || NETSTANDARD1_3_OR_GREATER | ||||||||||||||||||||||||||||||
| extension(File) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| // https://learn.microsoft.com/dotnet/api/system.io.file.move#system-io-file-move(system-string-system-string-system-boolean) | ||||||||||||||||||||||||||||||
| public static void Move(string sourceFileName, string destFileName, bool overwrite) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| File.Copy(sourceFileName, destFileName, overwrite); | ||||||||||||||||||||||||||||||
| File.Delete(sourceFileName); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+23
to
+24
|
||||||||||||||||||||||||||||||
| File.Delete(sourceFileName); | |
| } | |
| // WARNING: This polyfill is not atomic. If File.Delete fails after File.Copy succeeds, | |
| // the file may exist in both locations. The standard File.Move is atomic on most filesystems. | |
| try | |
| { | |
| File.Delete(sourceFileName); | |
| } | |
| catch (IOException ex) | |
| { | |
| // Optionally, handle or log the error here. | |
| // For now, rethrow to preserve original behavior. | |
| throw; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generic catch clause.