-
Notifications
You must be signed in to change notification settings - Fork 19
/
bin.test.js
307 lines (285 loc) · 13.7 KB
/
bin.test.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const async = require('async');
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const yargs = require('yargs');
const database = require('./src/database');
const yargsOptions = require('./lib/yargsOptions');
const { Batch } = require('./src/batch');
const { History } = require('./src/history');
const { Options } = require('./src/options');
const { Favorites } = require('./src/favorites');
jest.setTimeout(30000);
let SEQUELIZE;
beforeAll(async () => {
// remove test directory
await fs.remove('./test');
// create test files/directories
await fs.ensureDir('test');
await fs.ensureDir('test/another-dir');
await async.times(31, async (i) => {
if (i === 0) return;
let num = inWords(i);
let dir = `${i < 20 ? 'test/' : 'test/another-dir/'}`;
let fileName = `${num.trim().replace(' ', '-')}.txt`;
await fs.writeFile(`${dir}${fileName}`, `file ${num.trim()}`, 'utf8');
});
SEQUELIZE = await database.initTest();
});
describe('Rename a single file: rename test/one.txt test/one-renamed.txt', () => {
const oldFiles = ['test/one.txt'];
const newFiles = ['test/one-renamed.txt'];
let originalContent;
beforeAll(async () => {
originalContent = await fs.readFile('test/one.txt', 'utf8');
await runCommand('rename test/one.txt test/one-renamed.txt');
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
test(`New file has correct content`, async () => {
const result = await fs.readFile('test/one-renamed.txt', 'utf8');
expect(result).toBe(originalContent);
});
});
describe('Rename multiple files the same thing with appended index: rename test/f*.txt test/multiple', () => {
const oldFiles = ['test/four.txt', 'test/five.txt', 'test/fourteen.txt', 'test/fifteen.txt'];
const newFiles = ['test/multiple1.txt', 'test/multiple2.txt', 'test/multiple3.txt', 'test/multiple4.txt'];
beforeAll(async () => {
await runCommand('rename test/f*.txt test/multiple');
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
});
describe('Rename multiple files the same thing with appended index and file extension specified and sort option: rename test/multiple* test/twelve.txt test/multiple.log', () => {
const oldFiles = ['test/multiple1.txt', 'test/multiple2.txt', 'test/multiple3.txt', 'test/multiple4.txt', 'test/twelve.txt'];
const newFiles = ['test/multiple1.log', 'test/multiple2.log', 'test/multiple3.log', 'test/multiple4.log', 'test/multiple5.log'];
let originalContent;
beforeAll(async () => {
originalContent = await fs.readFile('test/twelve.txt', 'utf8');
await runCommand('rename --sort reverse-alphabet test/multiple* test/twelve.txt test/multiple.log');
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
test(`New file has correct content`, async () => {
const result = await fs.readFile('test/multiple1.log', 'utf8');
expect(result).toBe(originalContent);
});
});
describe('Rename with variables and filters: rename test/two.txt "{{p}}/{{f|upper}}.{{\'testing-stuff\'|camel}}"', () => {
const oldFiles = ['test/two.txt'];
const newFiles = ['test/TWO.testingStuff'];
beforeAll(async () => {
await runCommand(`rename test/two.txt "{{p}}/{{f|upper}}.{{'testing-stuff'|camel}}"`);
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
test(`New files has correct case`, async () => {
const files = await fs.readdir('test');
expect(files.indexOf('TWO.testingStuff')).toBeGreaterThan(-1);
});
});
describe('Force multiple files to be renamed the same: rename test/th* test/same --noindex -force', () => {
const oldFiles = ['test/three.txt', 'test/thirteen.txt'];
const newFiles = ['test/same.txt'];
beforeAll(async () => {
await runCommand('rename test/th* test/same --noindex -force');
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
test(`New file has correct content`, async () => {
const result = await fs.readFile('test/same.txt', 'utf8');
expect(result).toMatch(/^file three.*/);
});
});
describe('Multiple files to be renamed the same but with keep option: rename test/six* test/keep --noindex -k', () => {
const oldFiles = ['test/six.txt', 'test/sixteen.txt'];
const newFiles = ['test/keep.txt', 'test/keep-1.txt'];
beforeAll(async () => {
await runCommand('rename test/six* test/keep --noindex -k');
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
});
describe('Move a file to a new directory: rename test/one-renamed.txt "test/another-dir/{{os.platform}}"', () => {
const oldFiles = ['test/one-renamed.txt'];
const newFiles = [`test/another-dir/${os.platform()}.txt`];
beforeAll(async () => {
await runCommand('rename test/one-renamed.txt "test/another-dir/{{os.platform}}"');
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
});
describe(`Don't move a file to a new directory: rename test/eight.txt "test/another-dir/{{f}}-notmoved" --nomove`, () => {
const oldFiles = ['test/eight.txt'];
const newFiles = ['test/eight-notmoved.txt'];
beforeAll(async () => {
await runCommand('rename test/eight.txt "test/another-dir/{{f}}-notmoved" --nomove');
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
});
describe(`Rename multiple files to the same date and append index: rename --nomove test/seven* "{{ date.current | date('yyyy-MM-dd') }}"`, () => {
const now = new Date();
const nowFormatted = `${now.getFullYear()}-${now.getMonth() < 9 ? '0' : ''}${now.getMonth() + 1}-${now.getDate() < 10 ? '0' : ''}${now.getDate()}`;
const oldFiles = ['test/seven.txt', 'test/seventeen.txt'];
const newFiles = [`test/${nowFormatted}1.txt`, `test/${nowFormatted}2.txt`];
beforeAll(async () => {
await runCommand(`rename --nomove test/seven* "{{ date.current | date('yyyy-MM-dd') }}"`);
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
});
describe(`Test --noext option: rename test/ten.txt "test/asdf{{os.user}}" --noext`, () => {
const oldFiles = ['test/ten.txt'];
const newFiles = [`test/asdf${os.userInfo().username}`];
beforeAll(async () => {
await runCommand('rename test/ten.txt "test/asdf{{os.user}}" --noext', true);
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
});
describe(`Test undo last rename via History class`, () => {
const newFiles = ['test/ten.txt'];
const oldFiles = [`test/asdf${os.userInfo().username}`];
beforeAll(async () => {
let history = new History(SEQUELIZE, 1, false);
await history.getBatches();
await history.undoBatch(0);
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
});
describe(`Rename a mp3 file: rename test/music.mp3 --createdirs "test/{{id3.year}}/{{id3.artist}}/{{id3.track|padNumber(2)}} - {{id3.title}}.{{ext}}"`, () => {
const oldFiles = ['test/music.mp3'];
const newFiles = ['test/2019/Scott Holmes/04 - Upbeat Party.mp3'];
beforeAll(async () => {
//await fs.writeFile('test/music.mp3', await download('https://files.freemusicarchive.org/storage-freemusicarchive-org/music/no_curator/Scott_Holmes/Inspiring__Upbeat_Music/Scott_Holmes_-_04_-_Upbeat_Party.mp3'));
await fs.copyFile('test-files/Scott_Holmes_-_04_-_Upbeat_Party.mp3', 'test/music.mp3');
await runCommand('rename test/music.mp3 --createdirs "test/{{id3.year}}/{{id3.artist}}/{{id3.track|padNumber(2)}} - {{id3.title}}{{ext}}"');
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
});
describe(`Run a favorited command`, () => {
const oldFiles = ['test/ten.txt'];
const newFiles = [`test/testten.txt`];
let favorite;
beforeAll(async () => {
let command = 'rename --nomove test/ten.txt {{p}}{{f}}';
favorite = SEQUELIZE.models.Favorites.build({ command: JSON.stringify(command.split(' ')), alias: 'test'});
await favorite.save();
await runFavorite('--favorites test');
});
test(`Old files don't exist`, async () => {
const result = await async.every(oldFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(false);
});
test(`New files do exist`, async () => {
const result = await async.every(newFiles, async (f) => { return await fs.pathExists(path.resolve(f)); });
await expect(result).toBe(true);
});
afterAll(async () => {
if (favorite) await favorite.destroy();
});
});
// HELPER FUNCTIONS
async function runCommand(command, undo) {
undo = undo || false;
let argv = yargs.options(yargsOptions).parse(`${command.replace(/^rename /, '')}${!undo ? ' --noundo' : ''}`);
let batch = new Batch(argv, null, SEQUELIZE);
await batch.complete();
}
async function runFavorite(command) {
let argv = yargs.options(yargsOptions).parse(command.replace(/^rename /, ''));
const options = new Options(argv);
const favorites = new Favorites(SEQUELIZE, options);
await favorites.get();
if (options.favorites) await favorites.run();
}
/* eslint-disable eqeqeq */
function inWords (num) {
let a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
let b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
if ((num = num.toString()).length > 9) return 'overflow';
let n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
if (!n) return;
let str = '';
str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) : '';
return str;
}