Skip to content

Commit 65c4c0c

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

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

test/common.js

Lines changed: 8 additions & 0 deletions
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

Lines changed: 5 additions & 2 deletions
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,11 @@ 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 ? ' +
26+
'LINES TERMINATED BY ? ' +
27+
'(id, title)';
2528

26-
connection.query(sql, [path, table, ','], function (err, result) {
29+
connection.query(sql, [path, table, ',', newline], function (err, result) {
2730
assert.ifError(err);
2831
assert.equal(result.affectedRows, 5);
2932
});

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

Lines changed: 8 additions & 5 deletions
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,13 @@ 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 ? ' +
25+
'LINES TERMINATED BY ? ' +
26+
'(id, title)';
2427

2528
var sql =
26-
connection.format(stmt, [path, table, ',']) + ';' +
27-
connection.format(stmt, [path, table, ',']) + ';';
29+
connection.format(stmt, [path, table, ',', newline]) + ';' +
30+
connection.format(stmt, [path, table, ',', newline]) + ';';
2831

2932
connection.query(sql, function (err, results) {
3033
assert.ifError(err);

0 commit comments

Comments
 (0)