forked from mysqljs/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
167 lines (133 loc) · 4.81 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var common = exports;
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
common.lib = path.resolve(__dirname, '..', (process.env.TEST_COVERAGE || ''), 'lib');
common.fixtures = path.resolve(__dirname, 'fixtures');
// Useful for triggering ECONNREFUSED errors on connect()
common.bogusPort = 47378;
// Useful for triggering ER_ACCESS_DENIED_ERROR errors on connect()
common.bogusPassword = 'INVALID PASSWORD';
// Used for simulating a fake mysql server
common.fakeServerPort = 32893;
// Used for simulating a fake mysql server
common.fakeServerSocket = __dirname + '/fake_server.sock';
common.testDatabase = process.env.MYSQL_DATABASE || 'test';
// Export common modules
common.Charsets = require(common.lib + '/protocol/constants/charsets');
common.ClientConstants = require(common.lib + '/protocol/constants/client');
common.Connection = require(common.lib + '/Connection');
common.ConnectionConfig = require(common.lib + '/ConnectionConfig');
common.Errors = require(common.lib + '/protocol/constants/errors');
common.Packets = require(common.lib + '/protocol/packets');
common.PacketWriter = require(common.lib + '/protocol/PacketWriter');
common.Parser = require(common.lib + '/protocol/Parser');
common.PoolConfig = require(common.lib + '/PoolConfig');
common.PoolConnection = require(common.lib + '/PoolConnection');
common.SqlString = require(common.lib + '/protocol/SqlString');
common.Types = require(common.lib + '/protocol/constants/types');
// Setup coverage hook
if (process.env.TEST_COVERAGE) {
process.on('exit', function () {
writeCoverage(global.__coverage__ || {});
});
}
var Mysql = require(path.resolve(common.lib, '../index'));
var FakeServer = require('./FakeServer');
common.createConnection = function(config) {
config = mergeTestConfig(config);
return Mysql.createConnection(config);
};
common.createTestDatabase = function createTestDatabase(connection, callback) {
var database = common.testDatabase;
connection.query('CREATE DATABASE ??', [database], function (err) {
if (err && err.code !== 'ER_DB_CREATE_EXISTS') {
callback(err);
return;
}
callback(null, database);
});
};
common.createPool = function(config) {
config = mergeTestConfig(config);
config.connectionConfig = mergeTestConfig(config.connectionConfig);
return Mysql.createPool(config);
};
common.createPoolCluster = function(config) {
config = mergeTestConfig(config);
config.createConnection = common.createConnection;
return Mysql.createPoolCluster(config);
};
common.createFakeServer = function(options) {
return new FakeServer(common.extend({}, options));
};
common.extend = function extend(dest, src) {
for (var key in src) {
dest[key] = src[key];
}
return dest;
};
common.getTestConnection = function getTestConnection(config, callback) {
if (!callback && typeof config === 'function') {
callback = config;
config = {};
}
var connection = common.createConnection(config);
connection.connect(function (err) {
if (err && err.code === 'ECONNREFUSED') {
if (process.env.CI) {
throw err;
}
common.skipTest('cannot connect to MySQL server');
}
if (err) {
callback(err);
return;
}
callback(null, connection);
});
};
common.skipTest = function skipTest(message) {
var msg = 'skipping - ' + message + '\n';
try {
fs.writeSync(process.stdout.fd, msg);
fs.fsyncSync(process.stdout.fd);
} catch (e) {
// Ignore error
}
process.exit(0);
};
common.useTestDb = function(connection) {
common.createTestDatabase(connection, function (err) {
if (err) throw err;
});
connection.query('USE ' + common.testDatabase);
};
common.getTestConfig = function(config) {
return mergeTestConfig(config);
};
common.getSSLConfig = function() {
return {
ca : fs.readFileSync(path.join(common.fixtures, 'server.crt'), 'ascii'),
cert : fs.readFileSync(path.join(common.fixtures, 'server.crt'), 'ascii'),
ciphers : 'ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH',
key : fs.readFileSync(path.join(common.fixtures, 'server.key'), 'ascii')
};
};
function mergeTestConfig(config) {
config = common.extend({
host : process.env.MYSQL_HOST,
port : process.env.MYSQL_PORT,
user : process.env.MYSQL_USER,
password : process.env.MYSQL_PASSWORD
}, config);
return config;
}
function writeCoverage(coverage) {
var test = path.relative(__dirname, path.resolve(process.argv[1]));
var ext = path.extname(test);
var cov = test.substr(0, test.length - ext.length) + '.json';
var out = path.resolve(__dirname, '..', process.env.TEST_COVERAGE, 'test', cov);
mkdirp.sync(path.dirname(out));
fs.writeFileSync(out, JSON.stringify(coverage));
}