├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test ├── async.js └── sync.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/move-file 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }} 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | os: 15 | - ubuntu-latest 16 | - macos-latest 17 | steps: 18 | - uses: actions/checkout@v2 19 | - uses: actions/setup-node@v2 20 | with: 21 | node-version: ${{ matrix.node-version }} 22 | - run: npm install 23 | - run: npm test 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type Options = { 2 | /** 3 | Overwrite existing destination file. 4 | 5 | @default true 6 | */ 7 | readonly overwrite?: boolean; 8 | 9 | /** 10 | The working directory to find source files. 11 | 12 | The source and destination path are relative to this. 13 | 14 | @default process.cwd() 15 | */ 16 | readonly cwd?: string; 17 | 18 | /** 19 | [Permissions](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) for created directories. 20 | 21 | It has no effect on Windows. 22 | 23 | @default 0o777 24 | */ 25 | readonly directoryMode?: number; 26 | }; 27 | 28 | /** 29 | Move a file asynchronously. 30 | 31 | @param sourcePath - The file you want to move. 32 | @param destinationPath - Where you want the file moved. 33 | @returns A `Promise` that resolves when the file has been moved. 34 | 35 | @example 36 | ``` 37 | import {moveFile} from 'move-file'; 38 | 39 | await moveFile('source/unicorn.png', 'destination/unicorn.png'); 40 | console.log('The file has been moved'); 41 | ``` 42 | */ 43 | export function moveFile(sourcePath: string, destinationPath: string, options?: Options): Promise; 44 | 45 | /** 46 | Move a file synchronously. 47 | 48 | @param sourcePath - The file you want to move. 49 | @param destinationPath - Where you want the file moved. 50 | 51 | @example 52 | ``` 53 | import {moveFileSync} from 'move-file'; 54 | 55 | moveFileSync('source/unicorn.png', 'destination/unicorn.png'); 56 | console.log('The file has been moved'); 57 | ``` 58 | */ 59 | export function moveFileSync(sourcePath: string, destinationPath: string, options?: Options): void; 60 | 61 | /** 62 | Rename a file asynchronously. 63 | 64 | @param source - The file you want to rename. 65 | @param destination - The name of the renamed file. 66 | @returns A `Promise` that resolves when the file has been renamed. 67 | 68 | @example 69 | ``` 70 | import {renameFile} from 'move-file'; 71 | 72 | await renameFile('unicorn.png', 'unicorns.png', {cwd: 'source'}); 73 | console.log('The file has been renamed'); 74 | ``` 75 | */ 76 | export function renameFile(source: string, destination: string, options?: Options): Promise; 77 | 78 | /** 79 | Rename a file synchronously. 80 | 81 | @param source - The file you want to rename. 82 | @param destination - The name of the renamed file. 83 | 84 | @example 85 | ``` 86 | import {renameFileSync} from 'move-file'; 87 | 88 | renameFileSync('unicorn.png', 'unicorns.png', {cwd: 'source'}); 89 | console.log('The file has been renamed'); 90 | ``` 91 | */ 92 | export function renameFileSync(source: string, destination: string, options?: Options): void; 93 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import path from 'node:path'; 3 | import fs, {promises as fsP} from 'node:fs'; 4 | import {pathExists} from 'path-exists'; 5 | 6 | const resolvePath = (cwd, sourcePath, destinationPath) => { 7 | sourcePath = path.resolve(cwd, sourcePath); 8 | destinationPath = path.resolve(cwd, destinationPath); 9 | 10 | return { 11 | sourcePath, 12 | destinationPath, 13 | }; 14 | }; 15 | 16 | const validatePathsExist = (sourcePath, destinationPath, suffix = 'Path') => { 17 | if (!sourcePath || !destinationPath) { 18 | throw new TypeError(`\`source${suffix}\` and \`destination${suffix}\` required`); 19 | } 20 | }; 21 | 22 | const validateSameDirectory = (source, destination) => { 23 | if (path.dirname(source) !== path.dirname(destination)) { 24 | throw new Error('`source` and `destination` must be in the same directory'); 25 | } 26 | }; 27 | 28 | const _moveFile = async (sourcePath, destinationPath, {overwrite = true, cwd = process.cwd(), directoryMode, validateDirectory = false} = {}) => { 29 | if (cwd) { 30 | ({sourcePath, destinationPath} = resolvePath(cwd, sourcePath, destinationPath)); 31 | } 32 | 33 | if (validateDirectory) { 34 | validateSameDirectory(sourcePath, destinationPath); 35 | } 36 | 37 | if (!overwrite && await pathExists(destinationPath)) { 38 | throw new Error(`The destination file exists: ${destinationPath}`); 39 | } 40 | 41 | await fsP.mkdir(path.dirname(destinationPath), { 42 | recursive: true, 43 | mode: directoryMode, 44 | }); 45 | 46 | try { 47 | await fsP.rename(sourcePath, destinationPath); 48 | } catch (error) { 49 | if (error.code === 'EXDEV') { 50 | await fsP.copyFile(sourcePath, destinationPath); 51 | await fsP.unlink(sourcePath); 52 | } else { 53 | throw error; 54 | } 55 | } 56 | }; 57 | 58 | const _moveFileSync = (sourcePath, destinationPath, {overwrite = true, cwd = process.cwd(), directoryMode, validateDirectory = false} = {}) => { 59 | if (cwd) { 60 | ({sourcePath, destinationPath} = resolvePath(cwd, sourcePath, destinationPath)); 61 | } 62 | 63 | if (validateDirectory) { 64 | validateSameDirectory(sourcePath, destinationPath); 65 | } 66 | 67 | if (!overwrite && fs.existsSync(destinationPath)) { 68 | throw new Error(`The destination file exists: ${destinationPath}`); 69 | } 70 | 71 | fs.mkdirSync(path.dirname(destinationPath), { 72 | recursive: true, 73 | mode: directoryMode, 74 | }); 75 | 76 | try { 77 | fs.renameSync(sourcePath, destinationPath); 78 | } catch (error) { 79 | if (error.code === 'EXDEV') { 80 | fs.copyFileSync(sourcePath, destinationPath); 81 | fs.unlinkSync(sourcePath); 82 | } else { 83 | throw error; 84 | } 85 | } 86 | }; 87 | 88 | export async function moveFile(sourcePath, destinationPath, options) { 89 | validatePathsExist(sourcePath, destinationPath); 90 | return _moveFile(sourcePath, destinationPath, options); 91 | } 92 | 93 | export function moveFileSync(sourcePath, destinationPath, options) { 94 | validatePathsExist(sourcePath, destinationPath); 95 | return _moveFileSync(sourcePath, destinationPath, options); 96 | } 97 | 98 | export async function renameFile(source, destination, options = {}) { 99 | validatePathsExist(source, destination, ''); 100 | return _moveFile(source, destination, {...options, validateDirectory: true}); 101 | } 102 | 103 | export function renameFileSync(source, destination, options = {}) { 104 | validatePathsExist(source, destination, ''); 105 | return _moveFileSync(source, destination, {...options, validateDirectory: true}); 106 | } 107 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectError, expectType} from 'tsd'; 2 | import {moveFile, moveFileSync, renameFile, renameFileSync} from './index.js'; 3 | 4 | expectType>( 5 | moveFile('source/unicorn.png', 'destination/unicorn.png'), 6 | ); 7 | expectType>( 8 | moveFile('source/unicorn.png', 'destination/unicorn.png', { 9 | overwrite: false, 10 | }), 11 | ); 12 | expectType>( 13 | moveFile('unicorn.png', '../destination/unicorn.png', { 14 | cwd: 'source', 15 | }), 16 | ); 17 | expectType>( 18 | moveFile('source/unicorn.png', 'destination/unicorn.png', { 19 | directoryMode: 0o700, 20 | }), 21 | ); 22 | expectError( 23 | await moveFile('source/unicorn.png', 'destination/unicorn.png', { 24 | directoryMode: '700', 25 | }), 26 | ); 27 | expectType( 28 | moveFileSync('source/unicorn.png', 'destination/unicorn.png'), 29 | ); 30 | expectType( 31 | moveFileSync('source/unicorn.png', 'destination/unicorn.png', { 32 | overwrite: false, 33 | }), 34 | ); 35 | expectType( 36 | moveFileSync('unicorn.png', '../destination/unicorn.png', { 37 | cwd: 'source', 38 | }), 39 | ); 40 | expectType( 41 | moveFileSync('source/unicorn.png', 'destination/unicorn.png', { 42 | directoryMode: 0o700, 43 | }), 44 | ); 45 | expectError( 46 | moveFileSync('source/unicorn.png', 'destination/unicorn.png', { 47 | directoryMode: '700', 48 | }), 49 | ); 50 | 51 | expectType>( 52 | renameFile('source/unicorn.png', 'source/unicorns.png'), 53 | ); 54 | expectType>( 55 | renameFile('source/unicorn.png', 'source/unicorns.png', { 56 | overwrite: false, 57 | }), 58 | ); 59 | expectType>( 60 | renameFile('unicorn.png', 'unicorns.png', { 61 | cwd: 'source', 62 | }), 63 | ); 64 | expectType>( 65 | renameFile('source/unicorn.png', 'source/unicorns.png', { 66 | directoryMode: 0o700, 67 | }), 68 | ); 69 | expectError( 70 | await renameFile('source/unicorn.png', 'source/unicorns.png', { 71 | directoryMode: '700', 72 | }), 73 | ); 74 | expectType( 75 | renameFileSync('source/unicorn.png', 'source/unicorns.png'), 76 | ); 77 | expectType( 78 | renameFileSync('source/unicorn.png', 'source/unicorns.png', { 79 | overwrite: false, 80 | }), 81 | ); 82 | expectType( 83 | renameFileSync('unicorn.png', 'unicorns.png', { 84 | cwd: 'source', 85 | }), 86 | ); 87 | expectType( 88 | renameFileSync('source/unicorn.png', 'source/unicorns.png', { 89 | directoryMode: 0o700, 90 | }), 91 | ); 92 | expectError( 93 | renameFileSync('source/unicorn.png', 'source/unicorns.png', { 94 | directoryMode: '700', 95 | }), 96 | ); 97 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "move-file", 3 | "version": "3.1.0", 4 | "description": "Move a file - Even works across devices", 5 | "license": "MIT", 6 | "repository": "sindresorhus/move-file", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "move", 27 | "file", 28 | "mv", 29 | "fs", 30 | "stream", 31 | "file-system", 32 | "ncp", 33 | "fast", 34 | "quick", 35 | "data", 36 | "content", 37 | "contents", 38 | "devices", 39 | "partitions" 40 | ], 41 | "dependencies": { 42 | "path-exists": "^5.0.0" 43 | }, 44 | "devDependencies": { 45 | "@types/node": "^16.0.0", 46 | "ava": "^5.2.0", 47 | "sinon": "^15.0.3", 48 | "temp-write": "^5.0.0", 49 | "tempy": "^3.0.0", 50 | "tsd": "^0.28.1", 51 | "xo": "^0.53.1" 52 | }, 53 | "xo": { 54 | "rules": { 55 | "no-bitwise": "off" 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # move-file 2 | 3 | > Move a file 4 | 5 | The built-in [`fs.rename()`](https://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback) is just a JavaScript wrapper for the C `rename(2)` function, which doesn't support moving files across partitions or devices. This module is what you would have expected `fs.rename()` to be. 6 | 7 | ## Highlights 8 | 9 | - Promise API. 10 | - Supports moving a file across partitions and devices. 11 | - Optionally prevent overwriting an existing file. 12 | - Creates non-existent destination directories for you. 13 | 14 | ## Install 15 | 16 | ```sh 17 | npm install move-file 18 | ``` 19 | 20 | ## Usage 21 | 22 | ```js 23 | import {moveFile} from 'move-file'; 24 | 25 | await moveFile('source/unicorn.png', 'destination/unicorn.png'); 26 | console.log('The file has been moved'); 27 | ``` 28 | 29 | ## API 30 | 31 | ### moveFile(sourcePath, destinationPath, options?) 32 | 33 | Returns a `Promise` that resolves when the file has been moved. 34 | 35 | ### moveFileSync(sourcePath, destinationPath, options?) 36 | 37 | #### sourcePath 38 | 39 | Type: `string` 40 | 41 | The file you want to move. 42 | 43 | #### destinationPath 44 | 45 | Type: `string` 46 | 47 | Where you want the file moved. 48 | 49 | #### options 50 | 51 | Type: `object` 52 | 53 | See [Options](#options-2). 54 | 55 | ### renameFile(source, destination, options?) 56 | 57 | Returns a `Promise` that resolves when the file has been renamed. `source` and `destination` must be in the same directory. 58 | 59 | ### renameFileSync(source, destination, options?) 60 | 61 | #### source 62 | 63 | Type: `string` 64 | 65 | The file you want to rename. 66 | 67 | #### destination 68 | 69 | Type: `string` 70 | 71 | What you want to rename the file to. 72 | 73 | #### options 74 | 75 | Type: `object` 76 | 77 | See [Options](#options-2). 78 | 79 | ### Options 80 | 81 | ##### overwrite 82 | 83 | Type: `boolean`\ 84 | Default: `true` 85 | 86 | Overwrite existing destination file. 87 | 88 | ##### cwd 89 | 90 | Type: `string`\ 91 | Default: `process.cwd()` 92 | 93 | The working directory to find source files. 94 | 95 | The source and destination path are relative to this. 96 | 97 | ##### directoryMode 98 | 99 | Type: `number`\ 100 | Default: `0o777` 101 | 102 | [Permissions](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) for created directories. 103 | 104 | It has no effect on Windows. 105 | 106 | ## Related 107 | 108 | - [move-file-cli](https://github.com/sindresorhus/move-file-cli) - CLI for this module 109 | - [copy-file](https://github.com/sindresorhus/copy-file) - Copy a file 110 | - [cpy](https://github.com/sindresorhus/cpy) - Copy files 111 | - [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed 112 | -------------------------------------------------------------------------------- /test/async.js: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | import path from 'node:path'; 3 | import test from 'ava'; 4 | import {temporaryFile, temporaryDirectory, temporaryWrite} from 'tempy'; 5 | import tempWrite from 'temp-write'; 6 | import sinon from 'sinon'; 7 | import {moveFile, renameFile} from '../index.js'; 8 | 9 | const fixture = '🦄'; 10 | 11 | test('missing `source` or `destination` throws', async t => { 12 | await t.throwsAsync( 13 | moveFile(), 14 | {message: '`sourcePath` and `destinationPath` required'}, 15 | ); 16 | }); 17 | 18 | test('move a file', async t => { 19 | const destination = temporaryFile(); 20 | await moveFile(tempWrite.sync(fixture), destination); 21 | t.is(fs.readFileSync(destination, 'utf8'), fixture); 22 | }); 23 | 24 | test.serial('move a file across devices', async t => { 25 | const exdevError = new Error('exdevError'); 26 | exdevError.code = 'EXDEV'; 27 | fs.rename = sinon.stub(fs, 'rename').throws(exdevError); 28 | 29 | const destination = temporaryFile(); 30 | await moveFile(tempWrite.sync(fixture), destination); 31 | t.is(fs.readFileSync(destination, 'utf8'), fixture); 32 | fs.rename.restore(); 33 | }); 34 | 35 | test('overwrite option', async t => { 36 | await t.throwsAsync( 37 | moveFile(tempWrite.sync('x'), tempWrite.sync('y'), {overwrite: false}), 38 | {message: /The destination file exists/}, 39 | ); 40 | }); 41 | 42 | test('cwd option', async t => { 43 | const destination = temporaryFile(); 44 | await moveFile(tempWrite.sync(fixture), 'unicorn-dir/unicorn.txt', {cwd: destination}); 45 | const movedFiled = path.resolve(destination, 'unicorn-dir/unicorn.txt'); 46 | t.is(fs.readFileSync(movedFiled, 'utf8'), fixture); 47 | }); 48 | 49 | test('directoryMode option', async t => { 50 | const root = temporaryDirectory(); 51 | const directory = `${root}/dir`; 52 | const destination = `${directory}/file`; 53 | const directoryMode = 0o700; 54 | await moveFile(tempWrite.sync(fixture), destination, {directoryMode}); 55 | const stat = fs.statSync(directory); 56 | t.is(stat.mode & directoryMode, directoryMode); 57 | }); 58 | 59 | test('rename a file', async t => { 60 | const file = await temporaryWrite(fixture, {name: 'unicorn.txt'}); 61 | const dir = path.dirname(file); 62 | 63 | const renamedFile = path.resolve(dir, 'unicorns.txt'); 64 | 65 | await renameFile(file, 'unicorns.txt', {cwd: dir}); 66 | t.is(fs.readFileSync(renamedFile, 'utf8'), fixture); 67 | }); 68 | 69 | test('renaming must be in same directory', async t => { 70 | const file = await temporaryWrite(fixture, {name: 'unicorn.txt'}); 71 | const dir = path.dirname(file); 72 | 73 | const renamedFile = path.resolve(dir, 'dir2/unicorns.txt'); 74 | 75 | await t.throwsAsync( 76 | renameFile(file, renamedFile), 77 | {message: '`source` and `destination` must be in the same directory'}, 78 | ); 79 | }); 80 | 81 | test('renaming without `source` or `destination` throws', async t => { 82 | await t.throwsAsync( 83 | renameFile(), 84 | {message: '`source` and `destination` required'}, 85 | ); 86 | }); 87 | -------------------------------------------------------------------------------- /test/sync.js: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | import path from 'node:path'; 3 | import test from 'ava'; 4 | import {temporaryFile, temporaryDirectory, temporaryWriteSync} from 'tempy'; 5 | import tempWrite from 'temp-write'; 6 | import sinon from 'sinon'; 7 | import {moveFileSync, renameFileSync} from '../index.js'; 8 | 9 | const fixture = '🦄'; 10 | 11 | test('missing `source` or `destination` throws', t => { 12 | t.throws( 13 | () => moveFileSync(), 14 | {message: '`sourcePath` and `destinationPath` required'}, 15 | ); 16 | }); 17 | 18 | test('move a file', t => { 19 | const destination = temporaryFile(); 20 | moveFileSync(tempWrite.sync(fixture), destination); 21 | t.is(fs.readFileSync(destination, 'utf8'), fixture); 22 | }); 23 | 24 | test('move a file across devices', t => { 25 | const exdevError = new Error('exdevError'); 26 | exdevError.code = 'EXDEV'; 27 | fs.renameSync = sinon.stub(fs, 'renameSync').throws(exdevError); 28 | 29 | const destination = temporaryFile(); 30 | moveFileSync(tempWrite.sync(fixture), destination); 31 | t.is(fs.readFileSync(destination, 'utf8'), fixture); 32 | fs.renameSync.restore(); 33 | }); 34 | 35 | test('overwrite option', t => { 36 | t.throws( 37 | () => moveFileSync(tempWrite.sync('x'), tempWrite.sync('y'), {overwrite: false}), 38 | {message: /The destination file exists/}, 39 | ); 40 | }); 41 | 42 | test('cwd option', t => { 43 | const destination = temporaryFile(); 44 | moveFileSync(tempWrite.sync(fixture), 'unicorn-dir/unicorn.txt', {cwd: destination}); 45 | const movedFiled = path.resolve(destination, 'unicorn-dir/unicorn.txt'); 46 | t.is(fs.readFileSync(movedFiled, 'utf8'), fixture); 47 | }); 48 | 49 | test('directoryMode option', t => { 50 | const root = temporaryDirectory(); 51 | const directory = `${root}/dir`; 52 | const destination = `${directory}/file`; 53 | const directoryMode = 0o700; 54 | moveFileSync(tempWrite.sync(fixture), destination, {directoryMode}); 55 | const stat = fs.statSync(directory); 56 | t.is(stat.mode & directoryMode, directoryMode); 57 | }); 58 | 59 | test('rename a file', t => { 60 | const file = temporaryWriteSync(fixture, {name: 'unicorn.txt'}); 61 | const dir = path.dirname(file); 62 | 63 | const renamedFile = path.resolve(dir, 'unicorns.txt'); 64 | 65 | renameFileSync(file, 'unicorns.txt', {cwd: dir}); 66 | t.is(fs.readFileSync(renamedFile, 'utf8'), fixture); 67 | }); 68 | 69 | test('renaming must be in same directory', t => { 70 | const file = temporaryWriteSync(fixture, {name: 'unicorn.txt'}); 71 | const dir = path.dirname(file); 72 | 73 | const renamedFile = path.resolve(dir, 'dir2/unicorns.txt'); 74 | 75 | t.throws( 76 | () => renameFileSync(file, renamedFile), 77 | {message: '`source` and `destination` must be in the same directory'}, 78 | ); 79 | }); 80 | 81 | test('renaming without `source` or `destination` throws', t => { 82 | t.throws( 83 | () => renameFileSync(), 84 | {message: '`source` and `destination` required'}, 85 | ); 86 | }); 87 | --------------------------------------------------------------------------------