-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.ts
131 lines (112 loc) · 3.25 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import * as faker from 'faker'
import * as minimatch from 'minimatch'
import {
Item,
Container,
ContainerType,
ListItemsOptions,
} from '../src/types'
export class MockItem implements Item {
public key: string = faker.system.fileName('js', 'js')
public modtime: Date = faker.date.past()
public size: number = faker.random.number({ min: 64, max: 1024 })
public isSymbolicLink: boolean = false
constructor(item: Partial<Item> = {}) {
Object.assign(this, item)
}
public read(): Promise<Buffer> {
return Promise.resolve(new Buffer(this.size))
}
}
const randomContainerType = (): ContainerType =>
['FILE' as ContainerType, 'S3' as ContainerType][Math.floor(Math.random() * 2)]
export class MockContainer implements Container {
private items: Item[] = []
public type: ContainerType = randomContainerType()
constructor(container: Partial<Container> = {}) {
Object.assign(this, container)
}
public setItems(items: Item[]) {
this.items = items
}
public listItems(options: ListItemsOptions = {}): Promise<Item[]> {
let items = this.items
if (options.include) {
items = items.filter(item => minimatch(item.key, options.include as string))
}
if (options.exclude) {
items = items.filter(item => !minimatch(item.key, options.exclude as string))
}
return Promise.resolve(items)
}
public putItem(item: Item): Promise<Item> {
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].key === item.key) {
this.items[i] = item
return Promise.resolve(item)
}
}
this.items.push(item)
return Promise.resolve(item)
}
public delItem(item: Item): Promise<void> {
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].key === item.key) {
this.items.splice(i, 1)
}
}
return Promise.resolve()
}
}
export class MockSuite {
public commonItems: Item[]
public sourceItems: Item[]
public targetItems: Item[]
public sourceContainer: Container
public targetContainer: Container
constructor() {
const commonItems = [
new MockItem({ key: 'common_1' }),
new MockItem({ key: 'common_2' }),
new MockItem({ key: 'common_3' }),
]
const sourceItems = [
...commonItems,
// Updated
new MockItem({
key: 'update.html',
modtime: new Date('8/5/2017'),
}),
// Created
new MockItem({
key: 'js/create.js',
}),
]
const targetItems = [
...commonItems,
// for Updated index.html
new MockItem({
key: 'update.html',
modtime: new Date('8/1/2017'),
}),
// Deleted
new MockItem({
key: 'i/am/deleted.css',
}),
]
const sourceContainer = new MockContainer({ type: 'FILE' })
const targetContainer = new MockContainer({ type: 'S3' })
sourceContainer.setItems(sourceItems)
targetContainer.setItems(targetItems)
this.commonItems = commonItems
this.sourceItems = sourceItems
this.targetItems = targetItems
this.sourceContainer = sourceContainer
this.targetContainer = targetContainer
}
}
export function findItem(key: string, items: Item[]): Item | undefined {
for (let i = 0; i < items.length; i++) {
if (items[i].key === key) return items[i]
}
}