Skip to content

Commit

Permalink
- Add some tests for Text.write.
Browse files Browse the repository at this point in the history
- Fix issue with appending to dry run.
  • Loading branch information
jdunkerley committed May 2, 2023
1 parent 8facc20 commit 0f84e73
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions test/Tests/src/System/File_Spec.enso
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0f84e73

Please sign in to comment.