forked from knex/knex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knexfile-test.spec.js
192 lines (171 loc) · 6.13 KB
/
knexfile-test.spec.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';
const path = require('path');
const tildify = require('tildify');
const { FileTestHelper, execCommand } = require('cli-testlab');
const KNEX = path.normalize(__dirname + '/../../bin/cli.js');
describe('knexfile resolution', () => {
/**
* @type FileTestHelper
*/
let fileHelper;
beforeEach(() => {
fileHelper = new FileTestHelper(path.resolve(__dirname, '../jake-util'));
fileHelper.deleteFile('test.sqlite3');
fileHelper.registerForCleanup('test.sqlite3');
});
afterEach(() => {
fileHelper.cleanup();
});
before(() => {
process.env.KNEX_PATH = '../knex.js';
});
context('--cwd is NOT specified', function() {
context('and --knexfile is also NOT specified', function() {
it('Resolves default knexfile in working directory correctly', () => {
const path = process.cwd() + '/knexfile.js';
fileHelper.createFile(
path,
`
module.exports = {
client: 'sqlite3',
connection: {
filename: __dirname + '/test/jake-util/test.sqlite3',
},
migrations: {
directory: __dirname + '/test//jake-util/knexfile_migrations',
},
};
`,
{ isPathAbsolute: true }
);
return execCommand(
`node ${KNEX} migrate:latest --knexpath=../knex.js`,
{
expectedOutput: 'Batch 1 run: 1 migrations',
}
);
});
});
context('but --knexfile is specified', function() {
it('Run migrations with knexfile passed', () => {
return execCommand(
`node ${KNEX} migrate:latest --knexfile=test/jake-util/knexfile/knexfile.js --knexpath=../knex.js`,
{
expectedOutput: 'Batch 1 run: 1 migrations',
}
);
});
it('Run migrations with knexfile returning function passed', () => {
return execCommand(
`node ${KNEX} migrate:latest --knexfile=test/jake-util/knexfile/knexfile_func.js --knexpath=../knex.js`,
{
expectedOutput: 'Batch 1 run: 1 migrations',
}
);
});
it('Run migrations with knexfile with async passed', () => {
return execCommand(
`node ${KNEX} migrate:latest --knexfile=test/jake-util/knexfile/knexfile_async.js --knexpath=../knex.js`,
{
expectedOutput: 'Batch 1 run: 1 migrations',
}
);
});
it('Run migrations with knexfile with promise passed', () => {
return execCommand(
`node ${KNEX} migrate:latest --knexfile=test/jake-util/knexfile/knexfile_promise.js --knexpath=../knex.js`,
{
expectedOutput: 'Batch 1 run: 1 migrations',
}
);
});
it("changes the process's cwd to the directory that contains the knexfile", () => {
const knexfile = 'test/jake-util/knexfile-relative/knexfile.js';
const expectedCWD = tildify(path.resolve(path.dirname(knexfile)));
return execCommand(
`node ${KNEX} migrate:latest --knexfile=test/jake-util/knexfile-relative/knexfile.js --knexpath=../knex.js`,
{
expectedOutput: `Working directory changed to ${expectedCWD}`,
}
);
});
// This addresses the issue that was reported here:
//
// https://github.com/knex/knex/issues/3660
//
context(
'and the knexfile itself resolves paths relative to process.cwd()',
function() {
it("changes the process's cwd to the directory that contains the knexfile before opening the knexfile", () => {
const knexfile = 'test/jake-util/knexfile-relative/knexfile.js';
const expectedCWD = tildify(path.resolve(path.dirname(knexfile)));
return execCommand(
`node ${KNEX} migrate:latest --knexfile=test/jake-util/knexfile-relative/knexfile-with-resolve.js --knexpath=../knex.js`,
{
expectedOutput: `Working directory changed to ${expectedCWD}`,
}
);
});
}
);
// FYI: This is only true because the Knex CLI changes the CWD to
// the directory of the knexfile.
it('Resolves migrations relatively to knexfile', () => {
return execCommand(
`node ${KNEX} migrate:latest --knexfile=test/jake-util/knexfile-relative/knexfile.js --knexpath=../knex.js`,
{
expectedOutput: 'Batch 1 run: 2 migrations',
}
);
});
it('Throws informative error when no knexfile is found', () => {
return execCommand(
`node ${KNEX} migrate:latest --knexpath=../knex.js`,
{
expectedErrorMessage: 'No configuration file found',
}
);
});
});
});
context('--cwd is specified', function() {
context('and --knexfile is also specified', function() {
context('and --knexfile is a relative path', function() {
it('resolves --knexfile relative to --cwd', function() {
return execCommand(
`node ${KNEX} migrate:latest --cwd=test/jake-util/knexfile --knexfile=knexfile.js`,
{
expectedOutput: 'Batch 1 run: 1 migrations',
}
);
});
});
context('and --knexfile is an absolute path', function() {
it('uses the indicated knexfile', function() {
// Notice: the Knexfile is using Typescript. This means that Knex
// is pre-loading the appropriate Typescript modules before loading
// the Knexfile.
const knexfile = path.resolve(
'test/jake-util/knexfile-ts/custom-config.ts'
);
return execCommand(
`node ${KNEX} migrate:latest --cwd=test/jake-util/knexfile --knexfile=${knexfile}`,
{
expectedOutput: 'Batch 1 run: 4 migrations',
}
);
});
});
});
context('but --knexfile is NOT specified', function() {
it('resolves knexfile relative to the specified cwd', () => {
return execCommand(
`node ${KNEX} migrate:latest --cwd=test/jake-util/knexfile`,
{
expectedOutput: 'Batch 1 run: 1 migrations',
}
);
});
});
});
});