|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const chai = require('chai') |
| 5 | +chai.use(require('dirty-chai')) |
| 6 | +const expect = chai.expect |
| 7 | +const bufferStream = require('./fixtures/buffer-stream') |
| 8 | + |
| 9 | +const { |
| 10 | + createMfs |
| 11 | +} = require('./fixtures') |
| 12 | + |
| 13 | +const { |
| 14 | + FILE_SEPARATOR |
| 15 | +} = require('../src/core/utils') |
| 16 | + |
| 17 | +describe('rm', function () { |
| 18 | + this.timeout(30000) |
| 19 | + |
| 20 | + let mfs |
| 21 | + |
| 22 | + before(() => { |
| 23 | + return createMfs() |
| 24 | + .then(instance => { |
| 25 | + mfs = instance |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + after((done) => { |
| 30 | + mfs.node.stop(done) |
| 31 | + }) |
| 32 | + |
| 33 | + it('refuses to remove files without arguments', () => { |
| 34 | + return mfs.rm() |
| 35 | + .then(() => { |
| 36 | + throw new Error('No error was thrown for missing paths') |
| 37 | + }) |
| 38 | + .catch(error => { |
| 39 | + expect(error.message).to.contain('Please specify a path to remove') |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + it('refuses to remove the root path', () => { |
| 44 | + return mfs.rm(FILE_SEPARATOR) |
| 45 | + .then(() => { |
| 46 | + throw new Error('No error was thrown for missing paths') |
| 47 | + }) |
| 48 | + .catch(error => { |
| 49 | + expect(error.message).to.contain('Cannot delete root') |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + it('refuses to remove a directory without the recursive flag', () => { |
| 54 | + const path = `/directory-${Math.random()}` |
| 55 | + |
| 56 | + return mfs.mkdir(path) |
| 57 | + .then(() => mfs.rm(path)) |
| 58 | + .then(() => { |
| 59 | + throw new Error('No error was thrown for missing recursive flag') |
| 60 | + }) |
| 61 | + .catch(error => { |
| 62 | + expect(error.message).to.contain(`${path} is a directory, use -r to remove directories`) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('removes a file', () => { |
| 67 | + const file = `/some-file-${Math.random()}.txt` |
| 68 | + |
| 69 | + return mfs.write(file, bufferStream(100), { |
| 70 | + create: true, |
| 71 | + parents: true |
| 72 | + }) |
| 73 | + .then(() => mfs.rm(file, { |
| 74 | + recursive: true |
| 75 | + })) |
| 76 | + .then(() => mfs.stat(file)) |
| 77 | + .then(() => { |
| 78 | + throw new Error('File was not removed') |
| 79 | + }) |
| 80 | + .catch(error => { |
| 81 | + expect(error.message).to.contain(`Path ${file} did not exist`) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + it('removes a directory', () => { |
| 86 | + const directory = `/directory-${Math.random()}` |
| 87 | + |
| 88 | + return mfs.mkdir(directory) |
| 89 | + .then(() => mfs.rm(directory, { |
| 90 | + recursive: true |
| 91 | + })) |
| 92 | + .then(() => mfs.stat(directory)) |
| 93 | + .then(() => { |
| 94 | + throw new Error('Directory was not removed') |
| 95 | + }) |
| 96 | + .catch(error => { |
| 97 | + expect(error.message).to.contain(`Path ${directory} did not exist`) |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + it('recursively removes a directory', () => { |
| 102 | + const directory = `/directory-${Math.random()}` |
| 103 | + const subdirectory = `/directory-${Math.random()}` |
| 104 | + const path = `${directory}${subdirectory}` |
| 105 | + |
| 106 | + return mfs.mkdir(path) |
| 107 | + .then(() => mfs.rm(directory, { |
| 108 | + recursive: true |
| 109 | + })) |
| 110 | + .then(() => mfs.stat(subdirectory)) |
| 111 | + .then(() => { |
| 112 | + throw new Error('File was not removed') |
| 113 | + }) |
| 114 | + .catch(error => { |
| 115 | + expect(error.message).to.contain(`Path ${subdirectory} did not exist`) |
| 116 | + }) |
| 117 | + .then(() => mfs.stat(directory)) |
| 118 | + .then(() => { |
| 119 | + throw new Error('Directory was not removed') |
| 120 | + }) |
| 121 | + .catch(error => { |
| 122 | + expect(error.message).to.contain(`Path ${directory} did not exist`) |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + it('recursively removes a directory with files in', () => { |
| 127 | + const directory = `directory-${Math.random()}` |
| 128 | + const file = `/${directory}/some-file-${Math.random()}.txt` |
| 129 | + |
| 130 | + return mfs.write(file, bufferStream(100), { |
| 131 | + create: true, |
| 132 | + parents: true |
| 133 | + }) |
| 134 | + .then(() => mfs.rm(`/${directory}`, { |
| 135 | + recursive: true |
| 136 | + })) |
| 137 | + .then(() => mfs.stat(file)) |
| 138 | + .then(() => { |
| 139 | + throw new Error('File was not removed') |
| 140 | + }) |
| 141 | + .catch(error => { |
| 142 | + expect(error.message).to.contain(`Path ${file} did not exist`) |
| 143 | + }) |
| 144 | + .then(() => mfs.stat(`/${directory}`)) |
| 145 | + .then(() => { |
| 146 | + throw new Error('Directory was not removed') |
| 147 | + }) |
| 148 | + .catch(error => { |
| 149 | + expect(error.message).to.contain(`${directory} did not exist`) |
| 150 | + }) |
| 151 | + }) |
| 152 | +}) |
0 commit comments