]> source.dussan.org Git - redmine.git/commitdiff
Auto-detection of field wrapper type when importing CSV file (#39511).
authorGo MAEDA <maeda@farend.jp>
Thu, 16 Nov 2023 11:13:39 +0000 (11:13 +0000)
committerGo MAEDA <maeda@farend.jp>
Thu, 16 Nov 2023 11:13:39 +0000 (11:13 +0000)
Patch by Go MAEDA.

git-svn-id: https://svn.redmine.org/redmine/trunk@22440 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/import.rb
test/unit/issue_import_test.rb

index fe3f24fab29e0118bef09e51272a44b2706eec9e..2674606bb5ed94699a85c39440a5b6a5a2f5b73b 100644 (file)
@@ -65,12 +65,14 @@ class Import < ActiveRecord::Base
 
   def set_default_settings(options={})
     separator = lu(user, :general_csv_separator)
+    wrapper = '"'
     encoding = lu(user, :general_csv_encoding)
     if file_exists?
       begin
         content = File.read(filepath, 256)
 
         separator = [',', ';'].max_by {|sep| content.count(sep)}
+        wrapper = ['"', "'"].max_by {|quote_char| content.count(quote_char)}
 
         guessed_encoding = Redmine::CodesetUtil.guess_encoding(content)
         encoding =
@@ -81,7 +83,6 @@ class Import < ActiveRecord::Base
       rescue => e
       end
     end
-    wrapper = '"'
 
     date_format = lu(user, "date.formats.default", :default => "foo")
     date_format = DATE_FORMATS.first unless DATE_FORMATS.include?(date_format)
index d68f0e44c90d5f44617cd5ebd413bd6e082c3e2d..00c13e0bb063b34794239818d64d3925d0910d11 100644 (file)
@@ -463,4 +463,19 @@ class IssueImportTest < ActiveSupport::TestCase
       assert_equal 'CP932', guessed_encoding
     end
   end
+
+  def test_set_default_settings_should_detect_field_wrapper
+    to_test = {
+      'import_issues.csv' => '"',
+      'import_issues_single_quotation.csv' => "'",
+      # Use '"' as a wrapper for CSV file with no wrappers
+      'import_dates.csv' => '"',
+    }
+
+    to_test.each do |file, expected|
+      import = generate_import(file)
+      import.set_default_settings
+      assert_equal expected, import.settings['wrapper']
+    end
+  end
 end