Skip to content

Commit

Permalink
More write tests, amend how line ending mismatch on append is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Jul 13, 2022
1 parent ce8ea04 commit 75ebfdc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,19 @@ write_file table format file on_existing_file match_columns on_problems =
If the file does not exist or is empty, it acts like a regular overwrite.
append_to_file : Table -> File_Format.Delimited -> File -> Match_Columns -> Problem_Behavior -> Any
append_to_file table format file match_columns on_problems =
Column_Name_Mismatch.handle_java_exception <| Column_Count_Mismatch.handle_java_exception <|
metadata = Delimited_Reader.detect_metadata file format
Column_Name_Mismatch.handle_java_exception <| Column_Count_Mismatch.handle_java_exception <| Panic.recover Illegal_Argument_Error <|
inferring_format = format.with_line_endings Infer
metadata = Delimited_Reader.detect_metadata file inferring_format
preexisting_headers = metadata.headers
case format.line_endings of
Infer -> Nothing
other_ending_style ->
selected_separator = line_separator_sequence other_ending_style
existing_separator = metadata.line_separator
if selected_separator != existing_separator then
Panic.throw <| Illegal_Argument_Error <|
# Ensure that these are properly escaped once `to_text` meaning is changed.
"The explicitly provided line endings (" + selected_separator.to_text + ") do not match the line endings in the file (" + existing_separator.to_text + ")."
effective_line_separator = metadata.line_separator
reordered_java_table = case preexisting_headers of
Nothing -> table.java_table
Expand Down
29 changes: 24 additions & 5 deletions test/Table_Tests/src/Delimited_Write_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,30 @@ spec =
File.read_text nonexistent_file . should_equal expected_text

Test.specify "should use the existing line ending style when appending to a file consisting of only comments" <|
# TODO
Nothing
initial_lines = ["# comment 1", "# comment 2"]
table_to_append = Table.new [["a", ["x", "y"]], ["b", ["z", "w"]]]
expected_lines = initial_lines + ["a,b", "x,z", "y,w"]
file = (enso_project.data / "transient" / "endings_comments_only.csv")
line_ending_pairs.each setting->
separator=setting.second
file.delete_if_exists
(initial_lines.join separator suffix=separator).write file
format = File_Format.Delimited ',' . with_comments
table_to_append.write file format on_existing_file=Existing_File_Behavior.Append . should_equal Nothing
text = File.read_text file
expected_text = expected_lines.join separator suffix=separator
text.should_equal expected_text
file.delete

Test.specify "should use the explicitly specified line ending style regardless of the detected one" <|
# TODO write in one style than append in other
Nothing
Test.specify "should fail if explicitly provided line endings do not match line endings in the file when appending" <|
initial_table = Table.new [["a", [1, 2]]]
table_to_append = Table.new [["a", ["x", "y"]]]
file = (enso_project.data / "transient" / "endings_mismatch.csv")
file.delete_if_exists
initial_table.write file (File_Format.Delimited ',' line_endings=Classic_Mac_Line_Endings)
result = table_to_append.write file (File_Format.Delimited ',' line_endings=Unix_Line_Endings) on_existing_file=Existing_File_Behavior.Append match_columns=Match_Columns.By_Position
result . should_fail_with Illegal_Argument_Error
result.catch.message . should_equal "The explicitly provided line endings ('\n') do not match the line endings in the file ('\r')."
file.delete

main = Test.Suite.run_main spec

0 comments on commit 75ebfdc

Please sign in to comment.