brsh 2.5.1 → 2.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -149,3 +149,172 @@ module.exports = Hello;
149
149
  expect(output).toContain('hello from js bin');
150
150
  });
151
151
  });
152
+
153
+ // ---------------------------------------------------------------------------
154
+ // touch
155
+ // ---------------------------------------------------------------------------
156
+ describe('touch', () => {
157
+ let tmpDir;
158
+ let shell;
159
+
160
+ beforeEach(async () => {
161
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'brsh-touch-'));
162
+ shell = await createShell({ useRealFilesystem: true, cwd: tmpDir });
163
+ });
164
+ afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }); });
165
+
166
+ it('creates a new empty file', async () => {
167
+ const { exitCode } = await run(shell, 'touch newfile.txt');
168
+ expect(exitCode).toBe(0);
169
+ expect(fs.existsSync(path.join(tmpDir, 'newfile.txt'))).toBe(true);
170
+ expect(fs.readFileSync(path.join(tmpDir, 'newfile.txt'), 'utf8')).toBe('');
171
+ });
172
+
173
+ it('does not overwrite existing file content', async () => {
174
+ fs.writeFileSync(path.join(tmpDir, 'existing.txt'), 'keep me');
175
+ await run(shell, 'touch existing.txt');
176
+ expect(fs.readFileSync(path.join(tmpDir, 'existing.txt'), 'utf8')).toBe('keep me');
177
+ });
178
+
179
+ it('updates mtime of existing file', async () => {
180
+ const filePath = path.join(tmpDir, 'old.txt');
181
+ fs.writeFileSync(filePath, 'data');
182
+ const epoch = new Date('2000-01-01T00:00:00Z');
183
+ fs.utimesSync(filePath, epoch, epoch);
184
+ const before = fs.statSync(filePath).mtime;
185
+
186
+ await run(shell, 'touch old.txt');
187
+
188
+ const after = fs.statSync(filePath).mtime;
189
+ expect(after.getTime()).toBeGreaterThan(before.getTime());
190
+ });
191
+
192
+ it('-c does not create a missing file', async () => {
193
+ const { exitCode } = await run(shell, 'touch -c ghost.txt');
194
+ expect(exitCode).toBe(0);
195
+ expect(fs.existsSync(path.join(tmpDir, 'ghost.txt'))).toBe(false);
196
+ });
197
+
198
+ it('creates multiple files', async () => {
199
+ await run(shell, 'touch a.txt b.txt c.txt');
200
+ expect(fs.existsSync(path.join(tmpDir, 'a.txt'))).toBe(true);
201
+ expect(fs.existsSync(path.join(tmpDir, 'b.txt'))).toBe(true);
202
+ expect(fs.existsSync(path.join(tmpDir, 'c.txt'))).toBe(true);
203
+ });
204
+
205
+ it('works on virtual filesystem', async () => {
206
+ const vshell = await createShell({ filesystem: { home: {} } });
207
+ const { exitCode } = await run(vshell, 'touch /home/newfile.txt');
208
+ expect(exitCode).toBe(0);
209
+ expect(vshell.context.fs.exists('/home/newfile.txt')).toBe(true);
210
+ });
211
+ });
212
+
213
+ // ---------------------------------------------------------------------------
214
+ // ln
215
+ // ---------------------------------------------------------------------------
216
+ describe('ln', () => {
217
+ let tmpDir;
218
+ let shell;
219
+
220
+ beforeEach(async () => {
221
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'brsh-ln-'));
222
+ fs.writeFileSync(path.join(tmpDir, 'file.txt'), 'hello');
223
+ shell = await createShell({ useRealFilesystem: true, cwd: tmpDir });
224
+ });
225
+ afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }); });
226
+
227
+ it('creates a symlink with ln -s', async () => {
228
+ const { exitCode } = await run(shell, 'ln -s file.txt link.txt');
229
+ expect(exitCode).toBe(0);
230
+ expect(fs.lstatSync(path.join(tmpDir, 'link.txt')).isSymbolicLink()).toBe(true);
231
+ expect(fs.readlinkSync(path.join(tmpDir, 'link.txt'))).toBe('file.txt');
232
+ });
233
+
234
+ it('symlink target is readable', async () => {
235
+ await run(shell, 'ln -s file.txt link.txt');
236
+ const { output } = await run(shell, 'cat link.txt');
237
+ expect(output.join('')).toBe('hello');
238
+ });
239
+
240
+ it('creates a hard link', async () => {
241
+ const { exitCode } = await run(shell, 'ln file.txt hardlink.txt');
242
+ expect(exitCode).toBe(0);
243
+ const orig = fs.statSync(path.join(tmpDir, 'file.txt'));
244
+ const link = fs.statSync(path.join(tmpDir, 'hardlink.txt'));
245
+ expect(link.ino).toBe(orig.ino);
246
+ });
247
+
248
+ it('-f overwrites existing symlink', async () => {
249
+ fs.writeFileSync(path.join(tmpDir, 'other.txt'), 'other');
250
+ await run(shell, 'ln -s file.txt link.txt');
251
+ const { exitCode } = await run(shell, 'ln -sf other.txt link.txt');
252
+ expect(exitCode).toBe(0);
253
+ expect(fs.readlinkSync(path.join(tmpDir, 'link.txt'))).toBe('other.txt');
254
+ });
255
+
256
+ it('errors when source missing for hard link', async () => {
257
+ const { exitCode, errors } = await run(shell, 'ln ghost.txt link.txt');
258
+ expect(exitCode).toBe(1);
259
+ expect(errors.join('')).toMatch(/ghost\.txt/);
260
+ });
261
+
262
+ it('errors with missing operands', async () => {
263
+ const { exitCode, errors } = await run(shell, 'ln');
264
+ expect(exitCode).toBe(1);
265
+ expect(errors.join('')).toMatch(/missing/);
266
+ });
267
+ });
268
+
269
+ // ---------------------------------------------------------------------------
270
+ // stat
271
+ // ---------------------------------------------------------------------------
272
+ describe('stat', () => {
273
+ let tmpDir;
274
+ let shell;
275
+
276
+ beforeEach(async () => {
277
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'brsh-stat-'));
278
+ fs.writeFileSync(path.join(tmpDir, 'file.txt'), 'hello world');
279
+ fs.mkdirSync(path.join(tmpDir, 'subdir'));
280
+ shell = await createShell({ useRealFilesystem: true, cwd: tmpDir });
281
+ });
282
+ afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }); });
283
+
284
+ it('outputs file info for a regular file', async () => {
285
+ const { output, exitCode } = await run(shell, 'stat file.txt');
286
+ expect(exitCode).toBe(0);
287
+ const out = output.join('\n');
288
+ expect(out).toMatch(/file\.txt/);
289
+ expect(out).toMatch(/regular file/);
290
+ expect(out).toMatch(/\d+/); // size
291
+ });
292
+
293
+ it('outputs directory info', async () => {
294
+ const { output, exitCode } = await run(shell, 'stat subdir');
295
+ expect(exitCode).toBe(0);
296
+ expect(output.join('\n')).toMatch(/directory/);
297
+ });
298
+
299
+ it('reports symlink type without following', async () => {
300
+ fs.symlinkSync('file.txt', path.join(tmpDir, 'link.txt'));
301
+ const { output, exitCode } = await run(shell, 'stat link.txt');
302
+ expect(exitCode).toBe(0);
303
+ const out = output.join('\n');
304
+ expect(out).toMatch(/symbolic link/);
305
+ expect(out).toMatch(/-> file\.txt/);
306
+ });
307
+
308
+ it('errors for missing file', async () => {
309
+ const { exitCode, errors } = await run(shell, 'stat ghost.txt');
310
+ expect(exitCode).toBe(1);
311
+ expect(errors.join('')).toMatch(/No such file/);
312
+ });
313
+
314
+ it('works on virtual filesystem', async () => {
315
+ const vshell = await createShell({ filesystem: { 'readme.txt': 'hello' } });
316
+ const { output, exitCode } = await run(vshell, 'stat /readme.txt');
317
+ expect(exitCode).toBe(0);
318
+ expect(output.join('\n')).toMatch(/readme\.txt/);
319
+ });
320
+ });