|
1 |
| -const test = require('tape'); |
2 |
| -const { join, resolve, isAbsolute } = require('path'); |
| 1 | +const { test } = require('uvu'); |
| 2 | +const assert = require('uvu/assert'); |
| 3 | +const { join, isAbsolute } = require('path'); |
3 | 4 | const $ = require('../lib/util');
|
4 | 5 |
|
5 |
| -const cwd = join(__dirname, 'pg'); |
| 6 | +const cwd = join(__dirname, 'fixtures', 'pg'); |
6 | 7 | const dir = join(cwd, 'migrations');
|
7 | 8 |
|
8 |
| -test('(utils) glob', async t => { |
| 9 | +test('(utils) glob', async () => { |
9 | 10 | const out = await $.glob(dir);
|
10 |
| - t.true(Array.isArray(out), 'returns Promise<Array>'); |
11 |
| - t.is(out.length, 6, '~> has 6 items'); |
| 11 | + assert.ok(Array.isArray(out), 'returns Promise<Array>'); |
| 12 | + assert.is(out.length, 6, '~> has 6 items'); |
12 | 13 |
|
13 | 14 | const first = out[0];
|
14 |
| - t.is(typeof first, 'object', 'items are objects'); |
15 |
| - t.is(typeof first.name, 'string', '~> has "name" string'); |
16 |
| - t.is(typeof first.abs, 'string', '~> has "abs" string'); |
17 |
| - t.true(isAbsolute(first.abs), '~~> is absolute path'); |
| 15 | + assert.type(first, 'object', 'items are objects'); |
| 16 | + assert.type(first.name, 'string', '~> has "name" string'); |
| 17 | + assert.type(first.abs, 'string', '~> has "abs" string'); |
| 18 | + assert.ok(isAbsolute(first.abs), '~~> is absolute path'); |
18 | 19 |
|
19 | 20 | const names = out.map(x => x.name);
|
20 | 21 | const expects = ['001.js', '002.js', '003.js', '004.js', '005.js', '006.js'];
|
21 |
| - t.same(names, expects, '~> file order is expected'); |
22 |
| - |
23 |
| - t.end(); |
| 22 | + assert.equal(names, expects, '~> file order is expected'); |
24 | 23 | });
|
25 | 24 |
|
26 | 25 |
|
27 |
| -test('(utils) glob :: throws', async t => { |
28 |
| - t.plan(3); |
| 26 | +test('(utils) glob :: throws', async () => { |
| 27 | + let caught = false; |
29 | 28 |
|
30 | 29 | try {
|
31 | 30 | await $.glob(join(cwd, 'foobar'));
|
32 |
| - t.pass('should not run'); |
| 31 | + assert.unreachable('should not run'); |
33 | 32 | } catch (err) {
|
34 |
| - t.true(err instanceof Error, 'throws an Error'); |
35 |
| - t.is(err.code, 'ENOENT', '~> is "ENOENT" code'); |
36 |
| - t.true(err.stack.includes('no such file or directory'), '~> has detail'); |
| 33 | + assert.instance(err, Error, 'throws an Error'); |
| 34 | + assert.is(err.code, 'ENOENT', '~> is "ENOENT" code'); |
| 35 | + assert.ok(err.stack.includes('no such file or directory'), '~> has detail'); |
| 36 | + caught = true; |
37 | 37 | }
|
| 38 | + |
| 39 | + assert.ok(caught); |
38 | 40 | });
|
39 | 41 |
|
40 | 42 |
|
41 |
| -test('(utils) diff', t => { |
| 43 | +test('(utils) diff', () => { |
42 | 44 | const exists = ['001', '002', '003'].map(name => ({ name }));
|
43 | 45 | const locals = ['001', '002', '003', '004', '005'].map(name => ({ name }));
|
44 | 46 |
|
45 | 47 | const output = $.diff(exists, locals);
|
46 |
| - t.true(Array.isArray(output), 'returns an Array'); |
47 |
| - t.true(output.every(x => locals.includes(x)), '~> only returns items from `locals` list'); |
48 |
| - t.is(output.length, 2, '~> has 2 NEW items'); |
| 48 | + assert.ok(Array.isArray(output), 'returns an Array'); |
| 49 | + assert.ok(output.every(x => locals.includes(x)), '~> only returns items from `locals` list'); |
| 50 | + assert.is(output.length, 2, '~> has 2 NEW items'); |
49 | 51 |
|
50 | 52 | const names = output.map(x => x.name);
|
51 |
| - t.same(names, ['004', '005']); |
52 |
| - |
53 |
| - t.end(); |
| 53 | + assert.equal(names, ['004', '005']); |
54 | 54 | });
|
55 | 55 |
|
56 | 56 |
|
57 |
| -test('(utils) diff :: identical', t => { |
| 57 | +test('(utils) diff :: identical', () => { |
58 | 58 | const exists = ['001', '002', '003'].map(name => ({ name }));
|
59 | 59 | const locals = ['001', '002', '003'].map(name => ({ name }));
|
60 | 60 |
|
61 | 61 | const output = $.diff(exists, locals);
|
62 |
| - t.true(Array.isArray(output), 'returns an Array'); |
63 |
| - t.is(output.length, 0, '~> has 0 NEW items'); |
64 |
| - |
65 |
| - t.end(); |
| 62 | + assert.ok(Array.isArray(output), 'returns an Array'); |
| 63 | + assert.is(output.length, 0, '~> has 0 NEW items'); |
66 | 64 | });
|
67 | 65 |
|
68 | 66 |
|
69 |
| -test('(utils) diff :: throws sequence error', t => { |
70 |
| - t.plan(3); |
| 67 | +test('(utils) diff :: throws sequence error', () => { |
| 68 | + let caught = false; |
71 | 69 |
|
72 | 70 | try {
|
73 | 71 | const exists = ['001', '003'].map(name => ({ name }));
|
74 | 72 | const locals = ['001', '002', '003'].map(name => ({ name }));
|
75 | 73 |
|
76 | 74 | $.diff(exists, locals);
|
77 |
| - t.pass('SHOULD NOT REACH'); |
| 75 | + assert.unreachable('SHOULD NOT REACH'); |
78 | 76 | } catch (err) {
|
79 |
| - t.true(err instanceof Error, 'throws an Error'); |
80 |
| - t.is(err.message, `Cannot run "002" after "003" has been applied`); |
81 |
| - t.true(err.stack.length > err.message.length, '~> has "stack" details'); |
| 77 | + caught = true; |
| 78 | + assert.instance(err, Error, 'throws an Error'); |
| 79 | + assert.is(err.message, `Cannot run "002" after "003" has been applied`); |
| 80 | + assert.ok(err.stack.length > err.message.length, '~> has "stack" details'); |
82 | 81 | }
|
| 82 | + |
| 83 | + assert.ok(caught); |
83 | 84 | });
|
84 | 85 |
|
85 | 86 |
|
86 | 87 | // Note: Arrays are received in reverse
|
87 |
| -test('(utils) pluck', t => { |
| 88 | +test('(utils) pluck', () => { |
88 | 89 | // should return everything, in sync
|
89 | 90 | const foobar = ['003', '002', '001'];
|
90 | 91 |
|
91 | 92 | const exists = foobar.map(name => ({ name }));
|
92 | 93 | const locals = foobar.map(name => ({ name }));
|
93 | 94 |
|
94 | 95 | const output = $.pluck(exists, locals);
|
95 |
| - t.true(Array.isArray(output), 'returns an Array'); |
96 |
| - t.true(output.every(x => locals.includes(x)), '~> only returns items from `locals` list'); |
97 |
| - t.is(output.length, 3, '~> has 3 candidates'); |
| 96 | + assert.ok(Array.isArray(output), 'returns an Array'); |
| 97 | + assert.ok(output.every(x => locals.includes(x)), '~> only returns items from `locals` list'); |
| 98 | + assert.is(output.length, 3, '~> has 3 candidates'); |
98 | 99 |
|
99 | 100 | const names = output.map(x => x.name);
|
100 |
| - t.same(names, foobar); |
101 |
| - |
102 |
| - t.end(); |
| 101 | + assert.equal(names, foobar); |
103 | 102 | });
|
104 | 103 |
|
105 | 104 |
|
106 | 105 | // Note: Arrays are received in reverse
|
107 |
| -test('(utils) pluck :: locals >> exists', t => { |
| 106 | +test('(utils) pluck :: locals >> exists', () => { |
108 | 107 | // should NOT return items that don't exist yet
|
109 | 108 | const exists = ['003', '002', '001'].map(name => ({ name }));
|
110 | 109 | const locals = ['005', '004', '003', '002', '001'].map(name => ({ name }));
|
111 | 110 |
|
112 | 111 | const output = $.pluck(exists, locals);
|
113 |
| - t.true(Array.isArray(output), 'returns an Array'); |
114 |
| - t.true(output.every(x => locals.includes(x)), '~> only returns items from `locals` list'); |
115 |
| - t.is(output.length, 3, '~> has 3 candidates'); |
| 112 | + assert.ok(Array.isArray(output), 'returns an Array'); |
| 113 | + assert.ok(output.every(x => locals.includes(x)), '~> only returns items from `locals` list'); |
| 114 | + assert.is(output.length, 3, '~> has 3 candidates'); |
116 | 115 |
|
117 | 116 | const names = output.map(x => x.name);
|
118 |
| - t.same(names, ['003', '002', '001']); |
119 |
| - |
120 |
| - t.end(); |
| 117 | + assert.equal(names, ['003', '002', '001']); |
121 | 118 | });
|
122 | 119 |
|
123 | 120 |
|
124 | 121 | // Note: Arrays are received in reverse
|
125 |
| -test('(utils) pluck :: exists >> locals :: throws', t => { |
126 |
| - t.plan(3); |
| 122 | +test('(utils) pluck :: exists >> locals :: throws', () => { |
| 123 | + let caught = false; |
127 | 124 |
|
128 | 125 | try {
|
129 | 126 | // throws error because we don't have 005 definitions
|
130 | 127 | const exists = ['005', '004', '003', '002', '001'].map(name => ({ name }));
|
131 | 128 | const locals = ['003', '002', '001'].map(name => ({ name }));
|
132 | 129 |
|
133 | 130 | $.pluck(exists, locals);
|
134 |
| - t.pass('SHOULD NOT REACH'); |
| 131 | + assert.unreachable('SHOULD NOT REACH'); |
135 | 132 | } catch (err) {
|
136 |
| - t.true(err instanceof Error, 'throws an Error'); |
137 |
| - t.is(err.message, `Cannot find "005" migration file`); |
138 |
| - t.true(err.stack.length > err.message.length, '~> has "stack" details'); |
| 133 | + caught = true; |
| 134 | + assert.instance(err, Error, 'throws an Error'); |
| 135 | + assert.is(err.message, `Cannot find "005" migration file`); |
| 136 | + assert.ok(err.stack.length > err.message.length, '~> has "stack" details'); |
139 | 137 | }
|
| 138 | + |
| 139 | + assert.ok(caught); |
140 | 140 | });
|
141 | 141 |
|
142 | 142 |
|
143 |
| -test('(utils) exists :: success', t => { |
144 |
| - const output = $.exists('tape'); |
145 |
| - t.is(typeof output, 'string', 'returns string (success)'); |
146 |
| - t.is(output, require.resolve('tape')); |
147 |
| - t.end(); |
| 143 | +test('(utils) exists :: success', () => { |
| 144 | + const output = $.exists('uvu'); |
| 145 | + assert.type(output, 'string', 'returns string (success)'); |
| 146 | + assert.is(output, require.resolve('uvu')) |
148 | 147 | });
|
149 | 148 |
|
150 | 149 |
|
151 |
| -test('(utils) exists :: failure', t => { |
| 150 | +test('(utils) exists :: failure', () => { |
152 | 151 | const output = $.exists('foobar');
|
153 |
| - t.is(typeof output, 'boolean', 'returns boolean (fail)'); |
154 |
| - t.is(output, false); |
155 |
| - t.end(); |
| 152 | + assert.type(output, 'boolean', 'returns boolean (fail)'); |
| 153 | + assert.is(output, false) |
156 | 154 | });
|
157 | 155 |
|
158 | 156 |
|
159 |
| -test('(utils) exists :: failure :: relative', t => { |
| 157 | +test('(utils) exists :: failure :: relative', () => { |
160 | 158 | const output = $.exists('./foobar');
|
161 |
| - t.is(typeof output, 'boolean', 'returns boolean (fail)'); |
162 |
| - t.is(output, false); |
163 |
| - t.end(); |
| 159 | + assert.type(output, 'boolean', 'returns boolean (fail)'); |
| 160 | + assert.is(output, false) |
164 | 161 | });
|
165 | 162 |
|
166 | 163 |
|
167 |
| -test('(utils) detect', t => { |
| 164 | +test('(utils) detect', () => { |
168 | 165 | const ORDER = ['postgres', 'pg', 'mysql', 'mysql2', 'better-sqlite3'];
|
169 | 166 |
|
170 | 167 | const seen = [];
|
171 | 168 | const prev = $.exists;
|
172 | 169 |
|
| 170 | + // @ts-ignore |
173 | 171 | $.exists = x => {
|
174 | 172 | seen.push(x);
|
175 | 173 | }
|
176 | 174 |
|
177 | 175 | const foo = $.detect();
|
178 |
| - t.is(foo, undefined, 'returns undefined (failure)'); |
179 |
| - t.same(seen, ORDER, '~> looks for drivers (ordered)'); |
| 176 | + assert.is(foo, undefined, 'returns undefined (failure)'); |
| 177 | + assert.equal(seen, ORDER, '~> looks for drivers (ordered)'); |
180 | 178 |
|
| 179 | + // @ts-ignore |
181 | 180 | $.exists = x => x === 'pg';
|
182 | 181 | const bar = $.detect();
|
183 |
| - t.is(bar, 'pg', '~> found "pg" (mock)'); |
| 182 | + assert.is(bar, 'pg', '~> found "pg" (mock)'); |
184 | 183 |
|
185 | 184 | $.exists = prev;
|
186 |
| - t.end(); |
187 | 185 | });
|
188 | 186 |
|
189 | 187 |
|
190 |
| -test('(utils) local :: success', t => { |
| 188 | +test('(utils) local :: success', () => { |
191 | 189 | const output = $.local('index.js'); // root/index.js
|
192 |
| - t.is(typeof output, 'object', '~> returns _something_ if exists'); |
193 |
| - t.true(!!output.down, '~> had "down" export'); |
194 |
| - t.true(!!output.up, '~> had "up" export'); |
195 |
| - t.end(); |
| 190 | + assert.type(output, 'object', '~> returns _something_ if exists'); |
| 191 | + assert.type(output.down, 'function', '~> had "down" export'); |
| 192 | + assert.type(output.up, 'function', '~> had "up" export') |
196 | 193 | });
|
197 | 194 |
|
198 | 195 |
|
199 |
| -test('(utils) local :: success w/ cwd', t => { |
| 196 | +test('(utils) local :: success w/ cwd', () => { |
200 | 197 | const output = $.local('util.js', __dirname); // this file
|
201 |
| - t.is(typeof output, 'object', '~> returns _something_ if exists'); |
202 |
| - t.end(); |
| 198 | + assert.type(output, 'object', '~> returns _something_ if exists') |
203 | 199 | });
|
204 | 200 |
|
205 | 201 |
|
206 |
| -test('(utils) local :: failure', t => { |
| 202 | +test('(utils) local :: failure', () => { |
207 | 203 | const output = $.local('foobar.ts'); // root dir
|
208 |
| - t.is(output, false, '~> returns `false` if not found'); |
209 |
| - t.end(); |
| 204 | + assert.is(output, false, '~> returns `false` if not found') |
210 | 205 | });
|
211 | 206 |
|
212 | 207 |
|
213 |
| -test('(utils) local :: failure w/ cwd', t => { |
| 208 | +test('(utils) local :: failure w/ cwd', () => { |
214 | 209 | const output = $.local('index.js', dir); // => pg/migrations/index.js
|
215 |
| - t.is(output, false, '~> returns `false` if not found'); |
216 |
| - t.end(); |
| 210 | + assert.is(output, false, '~> returns `false` if not found') |
217 | 211 | });
|
218 | 212 |
|
219 | 213 |
|
220 |
| -test('(utils) MigrationError', t => { |
| 214 | +test('(utils) MigrationError', () => { |
221 | 215 | const original = new Error('hello world');
|
222 | 216 | const migration = { name: '000.js', abs: 'path/to/000.js' };
|
223 | 217 | Object.assign(original, { code: 42703, position: 8, foobar: undefined });
|
224 | 218 |
|
225 | 219 | const error = new $.MigrationError(original, migration);
|
226 |
| - t.true(error instanceof $.MigrationError, 'is MigrationError'); |
227 |
| - t.true(error instanceof Error, '~> still inherits Error class'); |
| 220 | + assert.instance(error, $.MigrationError, 'is MigrationError'); |
| 221 | + assert.instance(error, Error, '~> still inherits Error class'); |
228 | 222 |
|
229 |
| - t.true(error.stack.length > 0, 'has "stack" trace'); |
230 |
| - t.true(error.stack.length > original.stack.length, '~> is longer than original trace'); |
231 |
| - t.true(error.stack.includes(original.stack), '~> contains original trace'); |
| 223 | + assert.ok(error.stack.length > 0, 'has "stack" trace'); |
| 224 | + assert.ok(error.stack.length > original.stack.length, '~> is longer than original trace'); |
| 225 | + assert.ok(error.stack.includes(original.stack), '~> contains original trace'); |
232 | 226 |
|
233 |
| - t.is(error.code, original.code, 'inherits original "code" property (custom)'); |
234 |
| - t.is(error.foobar, original.foobar, 'inherits original "foobar" property (custom)'); |
235 |
| - t.is(error.position, original.position, 'inherits original "position" property (custom)'); |
| 227 | + assert.is(error.code, original.code, 'inherits original "code" property (custom)'); |
| 228 | + assert.is(error.foobar, original.foobar, 'inherits original "foobar" property (custom)'); |
| 229 | + assert.is(error.position, original.position, 'inherits original "position" property (custom)'); |
236 | 230 |
|
237 |
| - t.same(error.migration, migration, 'attaches custom "migration" key w/ file data'); |
238 |
| - |
239 |
| - t.end(); |
| 231 | + assert.equal(error.migration, migration, 'attaches custom "migration" key w/ file data'); |
240 | 232 | });
|
| 233 | + |
| 234 | +test.run(); |
0 commit comments