Skip to content

Commit 69c8371

Browse files
committed
tests: fix load data infile tests to match on-disk line endings
fixes #1437
1 parent 36b1927 commit 69c8371

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

test/common.js

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ common.createFakeServer = function(options) {
7676
return new FakeServer(common.extend({}, options));
7777
};
7878

79+
common.detectNewline = function detectNewline(path) {
80+
var newlines = fs.readFileSync(path, 'utf8').match(/(?:\r?\n)/g) || [];
81+
var crlf = newlines.filter(function (nl) { return nl === '\r\n'; }).length;
82+
var lf = newlines.length - crlf;
83+
84+
return crlf > lf ? '\r\n' : '\n';
85+
};
86+
7987
common.extend = function extend(dest, src) {
8088
for (var key in src) {
8189
dest[key] = src[key];

test/integration/connection/test-load-data-infile.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var fs = require('fs');
55
var badPath = common.fixtures + '/does_not_exist.csv';
66
var path = common.fixtures + '/data.csv';
77
var table = 'load_data_test';
8+
var newline = common.detectNewline(path);
89

910
common.getTestConnection(function (err, connection) {
1011
assert.ifError(err);
@@ -21,9 +22,10 @@ common.getTestConnection(function (err, connection) {
2122

2223
var sql =
2324
'LOAD DATA LOCAL INFILE ? INTO TABLE ?? CHARACTER SET utf8 ' +
24-
'FIELDS TERMINATED BY ? (id, title)';
25+
'FIELDS TERMINATED BY ? (id, title)' +
26+
'LINES TERMINATED BY ?';
2527

26-
connection.query(sql, [path, table, ','], function (err, result) {
28+
connection.query(sql, [path, table, ',', newline], function (err, result) {
2729
assert.ifError(err);
2830
assert.equal(result.affectedRows, 5);
2931
});

test/integration/connection/test-multiple-statements-load-data-infile.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ var assert = require('assert');
22
var common = require('../../common');
33
var fs = require('fs');
44

5-
var path = common.fixtures + '/data.csv';
6-
var table = 'multi_load_data_test';
5+
var path = common.fixtures + '/data.csv';
6+
var table = 'multi_load_data_test';
7+
var newline = common.detectNewline(path);
78

89
common.getTestConnection({multipleStatements: true}, function (err, connection) {
910
assert.ifError(err);
@@ -20,11 +21,12 @@ common.getTestConnection({multipleStatements: true}, function (err, connection)
2021

2122
var stmt =
2223
'LOAD DATA LOCAL INFILE ? INTO TABLE ?? CHARACTER SET utf8 ' +
23-
'FIELDS TERMINATED BY ? (id, title)';
24+
'FIELDS TERMINATED BY ? (id, title)' +
25+
'LINES TERMINATED BY ?';
2426

2527
var sql =
26-
connection.format(stmt, [path, table, ',']) + ';' +
27-
connection.format(stmt, [path, table, ',']) + ';';
28+
connection.format(stmt, [path, table, ',', newline]) + ';' +
29+
connection.format(stmt, [path, table, ',', newline]) + ';';
2830

2931
connection.query(sql, function (err, results) {
3032
assert.ifError(err);

0 commit comments

Comments
 (0)