-
Notifications
You must be signed in to change notification settings - Fork 884
/
scan_copy.test.ts
141 lines (120 loc) · 4.48 KB
/
scan_copy.test.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
132
133
134
135
136
137
138
139
140
141
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { chmodSync, statSync } from 'fs';
import { resolve } from 'path';
import del from 'del';
import { getChildPaths } from '../fs';
import { scanCopy } from '../scan_copy';
import { PROCESS_WORKING_DIR } from '@osd/cross-platform';
const IS_WINDOWS = process.platform === 'win32';
const FIXTURES = resolve(__dirname, '../__fixtures__');
const TMP = resolve(__dirname, '../__tmp__');
const WORLD_EXECUTABLE = resolve(FIXTURES, 'bin/world_executable');
const getCommonMode = (path: string) => statSync(path).mode.toString(8).slice(-3);
// ensure WORLD_EXECUTABLE is actually executable by all
beforeAll(async () => {
chmodSync(WORLD_EXECUTABLE, 0o777);
});
// cleanup TMP directory
afterEach(async () => {
await del(TMP, { cwd: PROCESS_WORKING_DIR });
});
it('rejects if source path is not absolute', async () => {
await expect(
scanCopy({
source: 'foo/bar',
destination: __dirname,
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Please use absolute paths to keep things explicit. You probably want to use \`build.resolvePath()\` or \`config.resolveFromRepo()\`."`
);
});
it('rejects if destination path is not absolute', async () => {
await expect(
scanCopy({
source: __dirname,
destination: 'foo/bar',
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Please use absolute paths to keep things explicit. You probably want to use \`build.resolvePath()\` or \`config.resolveFromRepo()\`."`
);
});
it('rejects if neither path is absolute', async () => {
await expect(
scanCopy({
source: 'foo/bar',
destination: 'foo/bar',
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Please use absolute paths to keep things explicit. You probably want to use \`build.resolvePath()\` or \`config.resolveFromRepo()\`."`
);
});
it.skip('copies files and directories from source to dest, including dot files, creating dest if necessary, respecting mode', async () => {
const destination = resolve(TMP, 'a/b/c');
await scanCopy({
source: FIXTURES,
destination,
});
expect((await getChildPaths(resolve(destination, 'foo_dir'))).sort()).toEqual([
resolve(destination, 'foo_dir/.bar'),
resolve(destination, 'foo_dir/bar.txt'),
resolve(destination, 'foo_dir/foo'),
]);
expect(getCommonMode(resolve(destination, 'bin/world_executable'))).toBe(
IS_WINDOWS ? '666' : '777'
);
expect(getCommonMode(resolve(destination, 'foo_dir/bar.txt'))).toBe(IS_WINDOWS ? '666' : '644');
});
it('applies filter function specified', async () => {
const destination = resolve(TMP, 'a/b/c/d');
await scanCopy({
source: FIXTURES,
destination,
filter: (record) => !record.name.includes('bar'),
});
expect((await getChildPaths(resolve(destination, 'foo_dir'))).sort()).toEqual([
resolve(destination, 'foo_dir/foo'),
]);
});
it('supports atime and mtime', async () => {
const destination = resolve(TMP, 'a/b/c/d/e');
const time = new Date(1425298511000);
await scanCopy({
source: FIXTURES,
destination,
time,
});
const barTxt = statSync(resolve(destination, 'foo_dir/bar.txt'));
const fooDir = statSync(resolve(destination, 'foo_dir'));
// precision is platform specific
const oneDay = 86400000;
expect(Math.abs(barTxt.atimeMs - time.getTime())).toBeLessThan(oneDay);
expect(Math.abs(barTxt.mtimeMs - time.getTime())).toBeLessThan(oneDay);
expect(Math.abs(fooDir.atimeMs - time.getTime())).toBeLessThan(oneDay);
});