mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: add error handling for duplicate or not exists
This commit is contained in:
1 parent
9b78f0d41c
commit
5a1fc6499a
3 files changed
+86
-56
No files matched your search
@@ -0,0 +1,14 @@
|
||||
/* eslint-disable max-classes-per-file */
|
||||
export class DuplicateItemError extends Error {
|
||||
constructor(message) {
|
||||
super(message)
|
||||
this.name = 'DuplicateItemError'
|
||||
}
|
||||
}
|
||||
|
||||
export class ParentNotExistsError extends Error {
|
||||
constructor(message) {
|
||||
super(message)
|
||||
this.name = 'ParentNotExistsError'
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import path from 'path'
|
||||
import File from './file.js'
|
||||
import { DuplicateItemError, ParentNotExistsError } from './errors.js'
|
||||
|
||||
export default class FileNode extends File {
|
||||
constructor(file) {
|
||||
@@ -10,7 +11,11 @@ export default class FileNode extends File {
|
||||
addChild(fileInput) {
|
||||
const file = new File(fileInput)
|
||||
if (file.path === this.path) {
|
||||
throw new Error('File already exists in the tree')
|
||||
throw new DuplicateItemError('File already exists in the tree')
|
||||
}
|
||||
|
||||
if (this.children.some((child) => child.path === file.path)) {
|
||||
throw new DuplicateItemError('File already exists in the tree')
|
||||
}
|
||||
|
||||
const parentPath = path.dirname(file.path)
|
||||
@@ -23,19 +28,17 @@ export default class FileNode extends File {
|
||||
if (childNode) {
|
||||
childNode.addChild(file)
|
||||
} else {
|
||||
console.log(fileInput, this)
|
||||
throw new Error('Parent path not found in the tree')
|
||||
throw new ParentNotExistsError('Parent path not found in the tree')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
toString() {
|
||||
const indent = ' '.repeat(this.path.split('/').length - 1)
|
||||
const suffix = this.isDirectory() ? '/' : ''
|
||||
const childrenString = this.children
|
||||
.map((child) => child.toString())
|
||||
.join('')
|
||||
return `${indent}${this.name}${
|
||||
this.isDirectory() ? '/' : ''
|
||||
}\n${childrenString}`
|
||||
return `${indent}${this.name}${suffix}\n${childrenString}`
|
||||
}
|
||||
}
|
||||
@@ -1,64 +1,77 @@
|
||||
import { DuplicateItemError, ParentNotExistsError } from './errors.js'
|
||||
import FileNode from './fileNode.js'
|
||||
|
||||
test('addChild method adds a child node to the tree', () => {
|
||||
const files = [
|
||||
{
|
||||
describe('FileNode', () => {
|
||||
let rootNode
|
||||
|
||||
beforeEach(() => {
|
||||
rootNode = new FileNode({
|
||||
path: '/home/rudeus',
|
||||
name: 'rudeus',
|
||||
type: 'folder',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/.bash_history',
|
||||
name: '.bash_history',
|
||||
type: 'file',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/.viminfo',
|
||||
name: '.viminfo',
|
||||
type: 'file',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/.zshrc',
|
||||
name: '.zshrc',
|
||||
type: 'file',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/forgotten_scroll.txt',
|
||||
name: 'forgotten_scroll.txt',
|
||||
type: 'file',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
discovered: true,
|
||||
})
|
||||
})
|
||||
|
||||
test('should throw error when adding duplicate item', () => {
|
||||
const child = {
|
||||
path: '/home/rudeus/test',
|
||||
name: 'test',
|
||||
type: 'folder',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/test/.b',
|
||||
name: '.b',
|
||||
type: 'file',
|
||||
discovered: false,
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/test/a',
|
||||
name: 'a',
|
||||
type: 'file',
|
||||
discovered: false,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
const root = new FileNode(files[0])
|
||||
// Adding the child for the first time
|
||||
rootNode.addChild(child)
|
||||
|
||||
files.slice(1).forEach((file) => {
|
||||
root.addChild(file)
|
||||
expect(rootNode.children.length).toBe(1)
|
||||
|
||||
// Trying to add the same child again should throw an error
|
||||
expect(() => {
|
||||
rootNode.addChild(child)
|
||||
}).toThrow(DuplicateItemError)
|
||||
})
|
||||
|
||||
console.log(root.toString())
|
||||
test('should throw error when parent does not exist', () => {
|
||||
const child = {
|
||||
path: '/home/rudeus/alphabet/test',
|
||||
name: 'test',
|
||||
type: 'folder',
|
||||
}
|
||||
|
||||
expect(root.children.length).toBe(5)
|
||||
// Trying to add to a nonexistent parent should throw an error
|
||||
expect(() => {
|
||||
rootNode.addChild(child)
|
||||
}).toThrow(ParentNotExistsError)
|
||||
})
|
||||
|
||||
test('adds nested children to the tree', () => {
|
||||
const files = [
|
||||
{
|
||||
path: '/home/rudeus/forgotten_scroll.txt',
|
||||
name: 'forgotten_scroll.txt',
|
||||
type: 'file',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/test',
|
||||
name: 'test',
|
||||
type: 'folder',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/test/a',
|
||||
name: 'a',
|
||||
type: 'file',
|
||||
discovered: false,
|
||||
},
|
||||
]
|
||||
|
||||
files.forEach((file) => {
|
||||
rootNode.addChild(file)
|
||||
})
|
||||
|
||||
expect(rootNode.children.length).toBe(2)
|
||||
expect(rootNode.children[1].children.length).toBe(1)
|
||||
console.log(rootNode.toString())
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user