mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: add post-build-script to gradually migrate
This commit is contained in:
1 parent
3977db87a3
commit
8eaec839d3
11 files changed
+624
-154
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import File from './file.js'
|
||||
|
||||
describe('File', () => {
|
||||
let file
|
||||
beforeEach(() => {
|
||||
file = new File({ path: '/home/user', type: 'folder', discovered: true })
|
||||
})
|
||||
|
||||
const filesToCompare = [
|
||||
{
|
||||
name: 'direct children',
|
||||
path: '/home/user/file1.txt',
|
||||
answer: true,
|
||||
},
|
||||
{
|
||||
name: 'nested children',
|
||||
path: '/home/user/folder1/file1.txt',
|
||||
answer: true,
|
||||
},
|
||||
{
|
||||
name: 'outer file',
|
||||
path: '/etc/file1.txt',
|
||||
answer: false,
|
||||
},
|
||||
{
|
||||
name: 'fake children',
|
||||
path: '/home/user2/file2.txt',
|
||||
answer: false,
|
||||
},
|
||||
{
|
||||
name: 'same',
|
||||
path: '/home/user',
|
||||
answer: true,
|
||||
},
|
||||
{
|
||||
name: 'same with trailing',
|
||||
path: '/home/user/',
|
||||
answer: true,
|
||||
},
|
||||
]
|
||||
|
||||
filesToCompare.forEach(({ name, path, answer }) => {
|
||||
test(`${name}: ${path}`, () => {
|
||||
const fileToCompare = new File({ path, type: 'file', discovered: true })
|
||||
expect(file.contains(fileToCompare)).toBe(answer)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,174 @@
|
||||
import FileGraph from './fileGraph.js'
|
||||
import FileNode from './fileNode.js'
|
||||
|
||||
describe('FileGraph', () => {
|
||||
let root
|
||||
|
||||
beforeEach(() => {
|
||||
root = new FileNode({
|
||||
path: '/',
|
||||
type: 'folder',
|
||||
discovered: true,
|
||||
})
|
||||
})
|
||||
|
||||
test('add files to graph', () => {
|
||||
const fileGraph = new FileGraph(root)
|
||||
|
||||
const filesToAdd = [
|
||||
{
|
||||
path: '/home/user/file1.txt',
|
||||
type: 'file',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder1',
|
||||
type: 'folder',
|
||||
discovered: true,
|
||||
},
|
||||
]
|
||||
|
||||
fileGraph.add(filesToAdd)
|
||||
|
||||
expect(fileGraph.children.length).toBe(1)
|
||||
expect(fileGraph.children[0].children[0].children.length).toBe(2)
|
||||
})
|
||||
|
||||
test('remove files from graph', () => {
|
||||
const fileGraph = new FileGraph(root)
|
||||
|
||||
const filesToAdd = [
|
||||
{
|
||||
path: '/home/user/folder1',
|
||||
type: 'folder',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder2',
|
||||
type: 'folder',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder1/file1.txt',
|
||||
type: 'file',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder2/file2.txt',
|
||||
type: 'file',
|
||||
discovered: true,
|
||||
},
|
||||
]
|
||||
|
||||
fileGraph.add(filesToAdd)
|
||||
|
||||
const filesToRemove = [
|
||||
{
|
||||
path: '/home/user/folder1/file1.txt',
|
||||
type: 'folder',
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder2',
|
||||
type: 'folder',
|
||||
},
|
||||
]
|
||||
|
||||
fileGraph.remove(filesToRemove)
|
||||
|
||||
expect(fileGraph.children[0].children[0].children.length).toBe(1)
|
||||
expect(fileGraph.children[0].children[0].children[0].name).toBe('folder1')
|
||||
expect(fileGraph.children[0].children[0].children[0].children.length).toBe(
|
||||
0
|
||||
)
|
||||
})
|
||||
|
||||
test('discover', () => {
|
||||
const fileGraph = new FileGraph(root)
|
||||
const discoverFiles = [
|
||||
{
|
||||
path: '/home/user/folder1',
|
||||
type: 'folder',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder1/file1.txt',
|
||||
type: 'file',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder1/file2.txt',
|
||||
type: 'file',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder1/file3.txt',
|
||||
type: 'file',
|
||||
discovered: true,
|
||||
},
|
||||
]
|
||||
|
||||
fileGraph.discover(discoverFiles)
|
||||
expect(fileGraph.children[0].children[0].children[0].children.length).toBe(
|
||||
3
|
||||
)
|
||||
})
|
||||
|
||||
test('discover new files', () => {
|
||||
const fileGraph = new FileGraph(root)
|
||||
|
||||
const filesToAdd = [
|
||||
{
|
||||
path: '/home/user/folder1',
|
||||
type: 'folder',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder2',
|
||||
type: 'folder',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder1/file1.txt',
|
||||
type: 'file',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder2/file2.txt',
|
||||
type: 'file',
|
||||
discovered: true,
|
||||
},
|
||||
]
|
||||
|
||||
fileGraph.add(filesToAdd)
|
||||
|
||||
const newFiles = [
|
||||
{
|
||||
path: '/home/user/folder1',
|
||||
type: 'folder',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder1/file1.txt',
|
||||
type: 'file',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder1/file2.txt',
|
||||
type: 'file',
|
||||
discovered: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/folder1/file3.txt',
|
||||
type: 'file',
|
||||
discovered: true,
|
||||
},
|
||||
]
|
||||
|
||||
fileGraph.discover(newFiles)
|
||||
|
||||
expect(fileGraph.children[0].children[0].children.length).toBe(2)
|
||||
expect(fileGraph.children[0].children[0].children[0].children.length).toBe(
|
||||
3
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,277 @@
|
||||
import { DuplicateItemError, ParentNotExistsError } from './errors'
|
||||
import FileNode from './fileNode.js'
|
||||
|
||||
function createNodeFromFiles(files) {
|
||||
const node = new FileNode(files[0])
|
||||
files.slice(1).forEach((file) => node.addChild(file))
|
||||
return node
|
||||
}
|
||||
|
||||
function mergeTest(files1, files2, expectedFiles) {
|
||||
const node1 = createNodeFromFiles(files1)
|
||||
const node2 = createNodeFromFiles(files2)
|
||||
const expectedNode = createNodeFromFiles(expectedFiles)
|
||||
|
||||
node1.merge(node2)
|
||||
|
||||
expect(node1.toString()).toBe(expectedNode.toString())
|
||||
}
|
||||
|
||||
function removeChildTest(files, fileToRemove, expectedFiles) {
|
||||
const node = createNodeFromFiles(files)
|
||||
const expectedNode = createNodeFromFiles(expectedFiles)
|
||||
|
||||
node.removeChild(fileToRemove)
|
||||
|
||||
expect(node.toString()).toBe(expectedNode.toString())
|
||||
}
|
||||
|
||||
describe('FileNode', () => {
|
||||
let rootNode
|
||||
|
||||
beforeEach(() => {
|
||||
rootNode = new FileNode({
|
||||
path: '/home/rudeus',
|
||||
name: 'rudeus',
|
||||
type: 'folder',
|
||||
discovered: true,
|
||||
})
|
||||
})
|
||||
|
||||
test('should throw error when adding duplicate item', () => {
|
||||
const child = {
|
||||
path: '/home/rudeus/test',
|
||||
name: 'test',
|
||||
type: 'folder',
|
||||
}
|
||||
|
||||
// Adding the child for the first time
|
||||
rootNode.addChild(child)
|
||||
|
||||
expect(rootNode.children.length).toBe(1)
|
||||
|
||||
// Trying to add the same child again should throw an error
|
||||
expect(() => {
|
||||
rootNode.addChild(child)
|
||||
}).toThrow(DuplicateItemError)
|
||||
})
|
||||
|
||||
test('should throw error when parent does not exist', () => {
|
||||
const child = {
|
||||
path: '/home/rudeus/alphabet/test',
|
||||
name: 'test',
|
||||
type: 'folder',
|
||||
}
|
||||
|
||||
// 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)
|
||||
})
|
||||
|
||||
test('adds nested children to the tree with makeParents option', () => {
|
||||
const file = {
|
||||
path: '/home/rudeus/test/a/b/c/d/e',
|
||||
name: 'a',
|
||||
type: 'file',
|
||||
discovered: false,
|
||||
}
|
||||
|
||||
rootNode.addChild(file, { makeParents: true })
|
||||
|
||||
let currentNode = rootNode
|
||||
for (let i = 0; i < 5; i += 1) {
|
||||
expect(currentNode.children.length).toBe(1)
|
||||
// eslint-disable-next-line prefer-destructuring
|
||||
currentNode = currentNode.children[0]
|
||||
}
|
||||
})
|
||||
|
||||
test('merge-subtree', () => {
|
||||
const root = {
|
||||
path: '/',
|
||||
type: 'folder',
|
||||
}
|
||||
const files1 = [
|
||||
root,
|
||||
{ path: '/a', type: 'file' },
|
||||
{ path: '/b', type: 'folder' },
|
||||
{ path: '/b/c', type: 'folder' },
|
||||
{ path: '/b/d', type: 'file' },
|
||||
{ path: '/b/c/f', type: 'file' },
|
||||
]
|
||||
|
||||
const files2 = [
|
||||
{ path: '/b', type: 'folder' },
|
||||
{ path: '/b/c', type: 'folder' },
|
||||
{ path: '/b/e', type: 'file' },
|
||||
{ path: '/b/c/g', type: 'file' },
|
||||
]
|
||||
|
||||
const expected = [
|
||||
root,
|
||||
{ path: '/a', type: 'file' },
|
||||
{ path: '/b', type: 'folder' },
|
||||
{ path: '/b/c', type: 'folder' },
|
||||
{ path: '/b/e', type: 'file' },
|
||||
{ path: '/b/c/g', type: 'file' },
|
||||
]
|
||||
|
||||
mergeTest(files1, files2, expected)
|
||||
})
|
||||
|
||||
test('merge-middle-level', () => {
|
||||
// Test if the node to merge is only the middle part of the tree
|
||||
// (i.e. the node to merge doesn't contains all level of children)
|
||||
// The original tree should keep the children that are not in the node to merge
|
||||
const root = {
|
||||
path: '/',
|
||||
type: 'folder',
|
||||
}
|
||||
const files1 = [
|
||||
root,
|
||||
{ path: '/a', type: 'file' },
|
||||
{ path: '/b', type: 'folder' },
|
||||
{ path: '/b/c', type: 'folder' },
|
||||
{ path: '/b/d', type: 'file' },
|
||||
{ path: '/b/c/f', type: 'file' },
|
||||
]
|
||||
|
||||
const files2 = [
|
||||
{ path: '/b', type: 'folder' },
|
||||
{ path: '/b/c', type: 'folder' },
|
||||
{ path: '/b/e', type: 'file' },
|
||||
]
|
||||
|
||||
const expected = [
|
||||
root,
|
||||
{ path: '/a', type: 'file' },
|
||||
{ path: '/b', type: 'folder' },
|
||||
{ path: '/b/c', type: 'folder' },
|
||||
{ path: '/b/e', type: 'file' },
|
||||
{ path: '/b/c/f', type: 'file' },
|
||||
]
|
||||
|
||||
mergeTest(files1, files2, expected)
|
||||
})
|
||||
|
||||
test('merge-hidden', () => {
|
||||
const root = {
|
||||
path: '/',
|
||||
type: 'folder',
|
||||
}
|
||||
const files1 = [
|
||||
root,
|
||||
{ path: '/a', type: 'file', discovered: true },
|
||||
{ path: '/.b', type: 'file', discovered: false },
|
||||
{ path: '/c', type: 'folder', discovered: true },
|
||||
{ path: '/c/.d', type: 'file', discovered: false },
|
||||
]
|
||||
|
||||
const files2 = [
|
||||
{ path: '/c', type: 'folder', discovered: true },
|
||||
{ path: '/c/.d', type: 'file', discovered: true },
|
||||
]
|
||||
|
||||
const expected = [
|
||||
root,
|
||||
{ path: '/a', type: 'file', discovered: true },
|
||||
{ path: '/.b', type: 'file', discovered: false },
|
||||
{ path: '/c', type: 'folder', discovered: true },
|
||||
{ path: '/c/.d', type: 'file', discovered: true },
|
||||
]
|
||||
|
||||
mergeTest(files1, files2, expected)
|
||||
})
|
||||
|
||||
test('remove-child', () => {
|
||||
const root = {
|
||||
path: '/',
|
||||
type: 'folder',
|
||||
}
|
||||
const files = [
|
||||
root,
|
||||
{ path: '/a', type: 'file' },
|
||||
{ path: '/b', type: 'folder' },
|
||||
{ path: '/b/c', type: 'folder' },
|
||||
{ path: '/b/d', type: 'file' },
|
||||
{ path: '/b/c/f', type: 'file' },
|
||||
]
|
||||
|
||||
const fileToRemove = { path: '/b/c', type: 'folder' }
|
||||
|
||||
const expected = [
|
||||
root,
|
||||
{ path: '/a', type: 'file' },
|
||||
{ path: '/b', type: 'folder' },
|
||||
{ path: '/b/d', type: 'file' },
|
||||
]
|
||||
|
||||
removeChildTest(files, fileToRemove, expected)
|
||||
})
|
||||
})
|
||||
|
||||
test('load nested fileNode', () => {
|
||||
const data = {
|
||||
path: '/',
|
||||
type: 'folder',
|
||||
children: [
|
||||
{
|
||||
path: '/a',
|
||||
type: 'file',
|
||||
},
|
||||
{
|
||||
path: '/b',
|
||||
type: 'folder',
|
||||
children: [
|
||||
{
|
||||
path: '/b/c',
|
||||
type: 'folder',
|
||||
children: [
|
||||
{
|
||||
path: '/b/c/d',
|
||||
type: 'file',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const node = new FileNode(data)
|
||||
|
||||
expect(node.children.length).toBe(2)
|
||||
expect(node.children[1].children.length).toBe(1)
|
||||
expect(node.children[1].children[0].children.length).toBe(1)
|
||||
})
|
||||
Reference in new issue
Block a user