Skip to content

Commit

Permalink
Modify FileSystem copy and move methods' behavior
Browse files Browse the repository at this point in the history
Remove DirectoryAlreadyExists exception
  • Loading branch information
paulshryock committed Sep 30, 2023
1 parent c7bfda5 commit c878694
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 35 deletions.
8 changes: 0 additions & 8 deletions src/FileSystem/FileSystemException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Exception } from '../Exception/Exception.ts'
* @since 0.1.1
*/
export enum FileSystemExceptionMessage {
DirectoryAlreadyExists = 'Directory already exists.',
DirectoryNotFound = 'Directory not found.',
FileNotFound = 'File not found.',
FileOrDirectoryNotFound = 'File or directory not found.',
Expand Down Expand Up @@ -39,13 +38,6 @@ export class FileSystemException extends Exception {
}
}

/**
* Directory already exists exception.
*
* @since 0.1.1
*/
export class DirectoryAlreadyExists extends FileSystemException {}

/**
* File or directory not found exception.
*
Expand Down
10 changes: 5 additions & 5 deletions src/FileSystem/LocalFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
writeFile,
} from 'node:fs/promises'
import {
DirectoryAlreadyExists,
DirectoryNotFound,
FileNotFound,
FileOrDirectoryNotFound,
Expand Down Expand Up @@ -287,11 +286,12 @@ export class LocalFileSystem implements FileSystem {
): Promise<void> {
if (await this.isFile(dest)) throw new DirectoryNotFound(dest)

if (action === 'move' && (await this.isDirectory(dest)))
throw new DirectoryAlreadyExists(dest)
const destination = (await this.isDirectory(dest))
? `${dest}/${parse(src).base}`
: dest

await mkdir(dirname(dest), { recursive: true })
await this.ACTIONS[action].directory(src, dest)
await mkdir(dirname(destination), { recursive: true })
await this.ACTIONS[action].directory(src, destination)
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export { Exception } from './Exception/Exception.ts'
// File System.
export {
FileSystemException,
DirectoryAlreadyExists,
DirectoryNotFound,
FileNotFound,
FileOrDirectoryNotFound,
Expand Down
2 changes: 0 additions & 2 deletions tests/FileSystem/FileSystemException.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it } from '@jest/globals'
import {
DirectoryAlreadyExists,
DirectoryNotFound,
FileNotFound,
FileOrDirectoryNotFound,
Expand All @@ -9,7 +8,6 @@ import {
} from '../../src/FileSystem/FileSystemException.ts'

describe.each([
new DirectoryAlreadyExists(''),
new DirectoryNotFound(''),
new FileNotFound(''),
new FileOrDirectoryNotFound(''),
Expand Down
56 changes: 37 additions & 19 deletions tests/FileSystem/LocalFileSystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
test,
} from '@jest/globals'
import {
DirectoryAlreadyExists,
DirectoryNotFound,
FileNotFound,
FileOrDirectoryNotFound,
Expand Down Expand Up @@ -456,37 +455,37 @@ describe('LocalFileSystem', () => {
describe('when dest is a directory', () => {
beforeEach(() =>
mockFs({
new: { path: { to: {} } },
path: { to: { file: {} } },
new: { path: {} },
path: { to: { file: '' } },
}),
)

it('should copy the directory to the new location', async () => {
it('should copy the directory into the new location', async () => {
const fs = new LocalFileSystem()

await fs.copy(path, newPath)
await fs.copy('path/to', 'new/path')

expect(await fs.exists(path)).toBe(true)
expect(await fs.isDirectory(path)).toBe(true)
expect(await fs.exists('new/path/to')).toBe(true)
expect(await fs.isDirectory('new/path/to')).toBe(true)

expect(await fs.exists(newPath)).toBe(true)
expect(await fs.isDirectory(newPath)).toBe(true)
expect(await fs.exists('new/path/to/file')).toBe(true)
expect(await fs.isFile('new/path/to/file')).toBe(true)
})
})

describe('when dest does not exist', () => {
beforeEach(() => mockFs({ path: { to: { file: {} } } }))
beforeEach(() => mockFs({ path: { to: { file: '' } } }))

it('should copy the directory to the new location', async () => {
const fs = new LocalFileSystem()

await fs.copy(path, newPath)
await fs.copy('path/to', 'new/path/to')

expect(await fs.exists(path)).toBe(true)
expect(await fs.isDirectory(path)).toBe(true)
expect(await fs.exists('new/path/to')).toBe(true)
expect(await fs.isDirectory('new/path/to')).toBe(true)

expect(await fs.exists(newPath)).toBe(true)
expect(await fs.isDirectory(newPath)).toBe(true)
expect(await fs.exists('new/path/to')).toBe(true)
expect(await fs.isFile('new/path/to/file')).toBe(true)
})
})
})
Expand Down Expand Up @@ -601,12 +600,31 @@ describe('LocalFileSystem', () => {
}),
)

it('should throw an exception', async () => {
it('should not change file in destination directory', async () => {
const fs = new LocalFileSystem()

await expect(
fs.move(pathDirectory, newPathDirectory),
).rejects.toThrow(DirectoryAlreadyExists)
await fs.move('path/to', 'new/path/to')

expect(await fs.readFile('new/path/to/file')).toBe(oldData)
})

it('should move the directory into destination directory', async () => {
const fs = new LocalFileSystem()

await fs.move('path/to', 'new/path/to')

expect(await fs.exists('new/path/to/to')).toBe(true)
expect(await fs.isDirectory('new/path/to/to')).toBe(true)
})

it('should move directory contents', async () => {
const fs = new LocalFileSystem()

await fs.move('path/to', 'new/path/to')

expect(await fs.exists('new/path/to/to/file')).toBe(true)
expect(await fs.isFile('new/path/to/to/file')).toBe(true)
expect(await fs.readFile('new/path/to/to/file')).toBe(newData)
})
})

Expand Down

0 comments on commit c878694

Please sign in to comment.