addons/isl-server/proxy/__tests__/rmtree.test.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import fs from 'node:fs';
b69ab319import os from 'node:os';
b69ab3110import path from 'node:path';
b69ab3111import {exists} from 'shared/fs';
b69ab3112import rmtree from '../rmtree';
b69ab3113
b69ab3114describe('rmtree', () => {
b69ab3115 let tmp: string;
b69ab3116
b69ab3117 beforeEach(async () => {
b69ab3118 tmp = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'rmtree-test'));
b69ab3119 });
b69ab3120
b69ab3121 it('does not complain about a non-existent file', async () => {
b69ab3122 await rmtree(path.join(tmp, 'foo'));
b69ab3123 });
b69ab3124
b69ab3125 it('removes a file', async () => {
b69ab3126 const file = path.join(tmp, 'foo');
b69ab3127 await fs.promises.writeFile(file, 'foobar');
b69ab3128 await rmtree(file);
b69ab3129
b69ab3130 expect(await exists(file)).toBe(false);
b69ab3131 });
b69ab3132
b69ab3133 it('removes an empty folder', async () => {
b69ab3134 const folder = path.join(tmp, 'foo');
b69ab3135 await fs.promises.mkdir(folder);
b69ab3136 await rmtree(folder);
b69ab3137
b69ab3138 expect(await exists(folder)).toBe(false);
b69ab3139 });
b69ab3140
b69ab3141 it('removes a folder with files', async () => {
b69ab3142 const folder = path.join(tmp, 'foo');
b69ab3143 await fs.promises.mkdir(folder);
b69ab3144 await fs.promises.writeFile(path.join(folder, '1'), '1');
b69ab3145 await fs.promises.writeFile(path.join(folder, '2'), '2');
b69ab3146 await fs.promises.writeFile(path.join(folder, '3'), '3');
b69ab3147 await fs.promises.writeFile(path.join(folder, '4'), '4');
b69ab3148 await rmtree(folder);
b69ab3149
b69ab3150 expect(await exists(folder)).toBe(false);
b69ab3151 });
b69ab3152
b69ab3153 it('removes a deeper tree of folders and files', async () => {
b69ab3154 const folder = path.join(tmp, 'tree');
b69ab3155 await fs.promises.mkdir(path.join(folder, '1/2/3/4/5'), {recursive: true});
b69ab3156 await fs.promises.writeFile(path.join(folder, '1/A'), 'A');
b69ab3157 await fs.promises.writeFile(path.join(folder, '1/2/B'), 'B');
b69ab3158 await fs.promises.writeFile(path.join(folder, '1/2/B'), 'B');
b69ab3159 await fs.promises.writeFile(path.join(folder, '1/2/3/C'), 'C');
b69ab3160 await fs.promises.writeFile(path.join(folder, '1/2/3/4/D'), 'D');
b69ab3161 await fs.promises.writeFile(path.join(folder, '1/2/3/4/5/E'), 'E');
b69ab3162 await rmtree(folder);
b69ab3163
b69ab3164 expect(await exists(folder)).toBe(false);
b69ab3165 });
b69ab3166
b69ab3167 it('does not follow argument if it is a symlink', async () => {
b69ab3168 const target = path.join(tmp, 'target');
b69ab3169 const link = path.join(tmp, 'link');
b69ab3170 await fs.promises.writeFile(target, 'target file');
b69ab3171 await fs.promises.symlink(target, link);
b69ab3172 expect(await fs.promises.readFile(link, {encoding: 'utf8'})).toBe('target file');
b69ab3173 expect(await exists(link)).toBe(true);
b69ab3174 await rmtree(link);
b69ab3175
b69ab3176 expect(await exists(link)).toBe(false);
b69ab3177 expect(await exists(target)).toBe(true);
b69ab3178 });
b69ab3179
b69ab3180 it('does not follow symlink in the tree', async () => {
b69ab3181 const target = path.join(tmp, 'target');
b69ab3182 await fs.promises.writeFile(target, 'target file');
b69ab3183
b69ab3184 const folder = path.join(tmp, 'folder');
b69ab3185 await fs.promises.mkdir(folder);
b69ab3186 const link = path.join(folder, 'link');
b69ab3187 await fs.promises.symlink(target, link);
b69ab3188 expect(await fs.promises.readFile(link, {encoding: 'utf8'})).toBe('target file');
b69ab3189 await rmtree(folder);
b69ab3190
b69ab3191 expect(await exists(folder)).toBe(false);
b69ab3192 expect(await exists(target)).toBe(true);
b69ab3193 });
b69ab3194});