diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/System/File.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/System/File.enso index a0e44eda6e2a9..47a26123ffba0 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/System/File.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/System/File.enso @@ -86,7 +86,8 @@ type File if temp_path.is_nothing then Error.throw (File_Error.IO_Error "Unable to create a temporary file.") else temp = File.new temp_path if self.exists && copy_original then - self.copy_to temp replace_existing=True + Context.Output.with_enabled <| + self.copy_to temp replace_existing=True temp ## ALIAS Current Directory diff --git a/test/Tests/src/System/File_Spec.enso b/test/Tests/src/System/File_Spec.enso index 95f8cb343585e..7ad847cee1586 100644 --- a/test/Tests/src/System/File_Spec.enso +++ b/test/Tests/src/System/File_Spec.enso @@ -1,5 +1,6 @@ from Standard.Base import all import Standard.Base.Errors.Common.Forbidden_Operation +import Standard.Base.Errors.Common.Dry_Run_Operation import Standard.Base.Errors.Encoding_Error.Encoding_Error import Standard.Base.Errors.File_Error.File_Error import Standard.Base.Errors.Illegal_Argument.Illegal_Argument @@ -364,6 +365,19 @@ spec = f.delete f.exists.should_be_false + Test.specify "should perform a dry run write of text to a new file and return this file's descriptor on success" <| + f = transient / "work.txt" + f.delete_if_exists + f.exists.should_be_false + + Context.Output.with_disabled <| + r = "line 1!".write f + Problems.expect_only_warning Dry_Run_Operation r + f.exists.should_be_false + r.exists.should_be_true + + Context.Output.with_enabled <| r.delete_if_exists + Test.specify "should allow appending text to a file" <| f = transient / "work.txt" f.delete_if_exists @@ -373,6 +387,40 @@ spec = f.delete f.exists.should_be_false + Test.specify "should perform a dry run append text to a file" <| + f = transient / "work.txt" + f.delete_if_exists + "line 1!".write f on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error . should_succeed . should_equal f + + Context.Output.with_disabled <| + r = '\nline 2!'.write f on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error + Problems.expect_only_warning Dry_Run_Operation r + r.exists.should_be_true + r.read_text.should_equal 'line 1!\nline 2!' + f.read_text.should_equal 'line 1!' + + Context.Output.with_enabled <| r.delete_if_exists + + Test.specify "should perform a dry run create and append text to a file" <| + f = transient / "dry_append.txt" + f.delete_if_exists + + Context.Output.with_disabled <| + r = "line 1!".write f on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error + Problems.expect_only_warning Dry_Run_Operation r + r.exists.should_be_true + + s = '\nline 2!'.write f on_existing_file=Existing_File_Behavior.Append on_problems=Report_Error + Problems.expect_only_warning Dry_Run_Operation s + s.exists.should_be_true + + s.read_text.should_equal 'line 1!\nline 2!' + s.should_equal r + + f.exists.should_be_false + + Context.Output.with_enabled <| r.delete_if_exists + Test.specify "should allow to overwrite files" <| f = transient / "work.txt" f.delete_if_exists @@ -385,6 +433,20 @@ spec = f.delete f.exists.should_be_false + Test.specify "should not overwrite original file in dry run mode" <| + f = transient / "work.txt" + f.delete_if_exists + f.exists.should_be_false + "line 1!".write f on_existing_file=Existing_File_Behavior.Overwrite on_problems=Report_Error . should_succeed . should_equal f + f.exists.should_be_true + + Context.Output.with_disabled <| + "line 2!".write f on_existing_file=Existing_File_Behavior.Overwrite + f.read_text.should_equal "line 1!" + + f.delete + f.exists.should_be_false + Test.specify "should fail if a file already exists, depending on the settings" <| f = transient / "work.txt" f.delete_if_exists