@zenfs/devel 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/dist/bin/zenfs-test.js +3 -2
  2. package/dist/tests/common.js +1 -1
  3. package/dist/tests/fs/append.test.d.ts +1 -0
  4. package/dist/tests/fs/append.test.js +27 -0
  5. package/dist/tests/fs/concurrency.test.d.ts +1 -0
  6. package/dist/tests/fs/concurrency.test.js +76 -0
  7. package/dist/tests/fs/dir.test.d.ts +1 -0
  8. package/dist/tests/fs/dir.test.js +83 -0
  9. package/dist/tests/fs/directory.test.d.ts +1 -0
  10. package/dist/tests/fs/directory.test.js +151 -0
  11. package/dist/tests/fs/errors.test.d.ts +1 -0
  12. package/dist/tests/fs/errors.test.js +141 -0
  13. package/dist/tests/fs/exists.test.d.ts +1 -0
  14. package/dist/tests/fs/exists.test.js +19 -0
  15. package/dist/tests/fs/links.test.d.ts +1 -0
  16. package/dist/tests/fs/links.test.js +248 -0
  17. package/dist/tests/fs/open.test.d.ts +1 -0
  18. package/dist/tests/fs/open.test.js +95 -0
  19. package/dist/tests/fs/permissions.test.d.ts +1 -0
  20. package/dist/tests/fs/permissions.test.js +346 -0
  21. package/dist/tests/fs/read.test.d.ts +1 -0
  22. package/dist/tests/fs/read.test.js +149 -0
  23. package/dist/tests/fs/readFile.test.d.ts +1 -0
  24. package/dist/tests/fs/readFile.test.js +110 -0
  25. package/dist/tests/fs/rename.test.d.ts +1 -0
  26. package/dist/tests/fs/rename.test.js +70 -0
  27. package/dist/tests/fs/scaling.test.d.ts +1 -0
  28. package/dist/tests/fs/scaling.test.js +27 -0
  29. package/dist/tests/fs/stat.test.d.ts +1 -0
  30. package/dist/tests/fs/stat.test.js +93 -0
  31. package/dist/tests/fs/streams.test.d.ts +1 -0
  32. package/dist/tests/fs/streams.test.js +161 -0
  33. package/dist/tests/fs/tempdir.test.d.ts +1 -0
  34. package/dist/tests/fs/tempdir.test.js +118 -0
  35. package/dist/tests/fs/times.test.d.ts +7 -0
  36. package/dist/tests/fs/times.test.js +134 -0
  37. package/dist/tests/fs/truncate.test.d.ts +1 -0
  38. package/dist/tests/fs/truncate.test.js +62 -0
  39. package/dist/tests/fs/watch.test.d.ts +1 -0
  40. package/dist/tests/fs/watch.test.js +320 -0
  41. package/dist/tests/fs/write.test.d.ts +1 -0
  42. package/dist/tests/fs/write.test.js +83 -0
  43. package/dist/tests/fs/xattr.test.d.ts +1 -0
  44. package/dist/tests/fs/xattr.test.js +137 -0
  45. package/dist/tests/setup/memory.d.ts +1 -0
  46. package/dist/tests/setup/memory.js +3 -0
  47. package/package.json +5 -5
  48. package/tests/readme.md +2 -2
  49. package/tests/fs/append.test.ts +0 -34
  50. package/tests/fs/concurrency.test.ts +0 -113
  51. package/tests/fs/dir.test.ts +0 -103
  52. package/tests/fs/directory.test.ts +0 -189
  53. package/tests/fs/errors.test.ts +0 -159
  54. package/tests/fs/exists.test.ts +0 -23
  55. package/tests/fs/links.test.ts +0 -308
  56. package/tests/fs/open.test.ts +0 -23
  57. package/tests/fs/permissions.test.ts +0 -344
  58. package/tests/fs/read.test.ts +0 -94
  59. package/tests/fs/readFile.test.ts +0 -58
  60. package/tests/fs/rename.test.ts +0 -93
  61. package/tests/fs/scaling.test.ts +0 -35
  62. package/tests/fs/stat.test.ts +0 -115
  63. package/tests/fs/streams.test.ts +0 -204
  64. package/tests/fs/tempdir.test.ts +0 -64
  65. package/tests/fs/times.test.ts +0 -85
  66. package/tests/fs/truncate.test.ts +0 -89
  67. package/tests/fs/watch.test.ts +0 -246
  68. package/tests/fs/write.test.ts +0 -118
  69. package/tests/fs/xattr.test.ts +0 -96
  70. package/tests/setup/memory.ts +0 -3
  71. package/tests/tsconfig.json +0 -8
@@ -1,115 +0,0 @@
1
- // SPDX-License-Identifier: LGPL-3.0-or-later
2
- import { defaultContext, hasAccess, type Stats } from '@zenfs/core';
3
- import assert from 'node:assert/strict';
4
- import { suite, test } from 'node:test';
5
- import { config, fs } from '@zenfs/devel/tests/common';
6
-
7
- suite('Stats', () => {
8
- const existing_file = 'x.txt';
9
-
10
- test('stat empty path', config('async'), async () => {
11
- await assert.rejects(fs.promises.stat(''));
12
- });
13
-
14
- test('stat directory', config('async'), async () => {
15
- const stats = await fs.promises.stat('/');
16
- assert(stats instanceof fs.Stats);
17
- });
18
-
19
- test('lstat directory', config('async'), async () => {
20
- const stats = await fs.promises.lstat('/');
21
- assert(stats instanceof fs.Stats);
22
- });
23
-
24
- test('FileHandle.stat', config('async'), async () => {
25
- const handle = await fs.promises.open(existing_file, 'r');
26
- const stats = await handle.stat();
27
- assert(stats instanceof fs.Stats);
28
- await handle.close();
29
- });
30
-
31
- test('fstatSync file', config('sync'), () => {
32
- const fd = fs.openSync(existing_file, 'r');
33
- const stats = fs.fstatSync(fd);
34
- assert(stats instanceof fs.Stats);
35
- fs.close(fd);
36
- });
37
-
38
- test('hasAccess for non-root access', config('sync', 'permissions', 'root'), () => {
39
- const newFile = 'new.txt';
40
-
41
- fs.writeFileSync(newFile, 'hello', { mode: 0o640 });
42
-
43
- const prevCredentials = { ...defaultContext.credentials };
44
- const uid = 33;
45
- const nonRootCredentials = {
46
- uid,
47
- gid: uid,
48
- euid: uid,
49
- egid: uid,
50
- suid: uid,
51
- sgid: uid,
52
- };
53
-
54
- fs.chownSync(newFile, 0, nonRootCredentials.gid); // creating with root-user so that non-root user can access
55
-
56
- Object.assign(defaultContext.credentials, nonRootCredentials);
57
- const stat = fs.statSync(newFile);
58
-
59
- assert.equal(stat.gid, nonRootCredentials.gid);
60
- assert.equal(stat.uid, 0);
61
- assert.equal(hasAccess(defaultContext, stat, fs.constants.R_OK), true);
62
- assert.equal(hasAccess(defaultContext, stat, fs.constants.W_OK), false);
63
- assert.equal(hasAccess(defaultContext, stat, fs.constants.X_OK), false);
64
- // changing group
65
-
66
- Object.assign(defaultContext.credentials, { ...nonRootCredentials, gid: 44, egid: 44 });
67
-
68
- assert.equal(hasAccess(defaultContext, stat, fs.constants.R_OK), false);
69
- assert.equal(hasAccess(defaultContext, stat, fs.constants.W_OK), false);
70
- assert.equal(hasAccess(defaultContext, stat, fs.constants.X_OK), false);
71
-
72
- Object.assign(defaultContext.credentials, prevCredentials);
73
- });
74
-
75
- const missing = '/does-not-exist';
76
-
77
- test('statSync throws for a missing entry', config('sync'), () => {
78
- assert.throws(() => fs.statSync(missing), { code: 'ENOENT' });
79
- assert.throws(() => fs.statSync(missing, { throwIfNoEntry: true }), { code: 'ENOENT' });
80
- });
81
-
82
- test('statSync with throwIfNoEntry: false', config('sync'), () => {
83
- assert.equal(fs.statSync(missing, { throwIfNoEntry: false }), undefined);
84
- assert.equal(fs.statSync(existing_file + '/nope', { throwIfNoEntry: false }), undefined);
85
- });
86
-
87
- test('lstatSync with throwIfNoEntry: false', config('sync'), () => {
88
- assert.throws(() => fs.lstatSync(missing), { code: 'ENOENT' });
89
- assert.equal(fs.lstatSync(missing, { throwIfNoEntry: false }), undefined);
90
- });
91
-
92
- test('stat with throwIfNoEntry: false', config('async'), async () => {
93
- await assert.rejects(fs.promises.stat(missing), { code: 'ENOENT' });
94
- assert.equal(await fs.promises.stat(missing, { throwIfNoEntry: false }), undefined);
95
- assert.equal(await fs.promises.stat(existing_file + '/nope', { throwIfNoEntry: false }), undefined);
96
- });
97
-
98
- test('stat callback with throwIfNoEntry: false', config('async'), async () => {
99
- const { promise, resolve, reject } = Promise.withResolvers<Stats | undefined>();
100
- fs.stat(missing, { throwIfNoEntry: false }, (error, stats) => (error ? reject(error) : resolve(stats)));
101
- assert.equal(await promise, undefined);
102
- });
103
-
104
- test('stat file', config('async'), async () => {
105
- const stats = await fs.promises.stat(existing_file);
106
- assert(!stats.isDirectory());
107
- assert(stats.isFile());
108
- assert(!stats.isSocket());
109
- assert(!stats.isBlockDevice());
110
- assert(!stats.isCharacterDevice());
111
- assert(!stats.isFIFO());
112
- assert(!stats.isSymbolicLink());
113
- assert(stats instanceof fs.Stats);
114
- });
115
- });
@@ -1,204 +0,0 @@
1
- // SPDX-License-Identifier: LGPL-3.0-or-later
2
- import assert from 'node:assert/strict';
3
- import { suite, test } from 'node:test';
4
- import { config, fs } from '@zenfs/devel/tests/common';
5
-
6
- // Top-level initialization
7
- const testFilePath = 'test-file.txt';
8
- const testData = 'Hello, World!';
9
- await fs.promises.writeFile(testFilePath, testData);
10
-
11
- const testFilePathWrite = 'test-file-write.txt';
12
- await fs.promises.writeFile(testFilePathWrite, ''); // Ensure the file exists
13
-
14
- suite('Streams', config('streams'), () => {
15
- test('ReadStream reads data correctly', (_, done) => {
16
- const readStream = fs.createReadStream(testFilePath);
17
-
18
- let data = '';
19
- readStream.on('data', chunk => {
20
- data += chunk;
21
- });
22
- readStream.on('end', () => {
23
- assert.equal(data, testData);
24
- done();
25
- });
26
- readStream.on('error', err => {
27
- done(err);
28
- });
29
- });
30
-
31
- test('ReadStream close method works', (_, done) => {
32
- const readStream = fs.createReadStream(testFilePath);
33
-
34
- let closed = false;
35
- readStream.on('close', () => {
36
- closed = true;
37
- });
38
-
39
- // Closing before the stream has finished reading is a premature close
40
- readStream.close(err => {
41
- assert.equal((err as NodeJS.ErrnoException | null)?.code, 'ERR_STREAM_PREMATURE_CLOSE');
42
- assert(closed);
43
- done();
44
- });
45
- });
46
-
47
- test('WriteStream writes data correctly', async () => {
48
- const writeStream = fs.createWriteStream(testFilePathWrite);
49
-
50
- const { promise, resolve, reject } = Promise.withResolvers();
51
- writeStream.on('finish', resolve);
52
- writeStream.on('error', reject);
53
- writeStream.end(testData, 'utf8');
54
- await promise;
55
-
56
- assert.equal(fs.readFileSync(testFilePathWrite, 'utf8'), testData);
57
- });
58
-
59
- test('WriteStream close method works', (_, done) => {
60
- const writeStream = fs.createWriteStream(testFilePathWrite);
61
- let closed = false;
62
- writeStream.on('close', () => {
63
- closed = true;
64
- });
65
- writeStream.close(err => {
66
- assert.ifError(err);
67
- assert(closed);
68
- done();
69
- });
70
- });
71
-
72
- test('createReadStream with start', async () => {
73
- await fs.promises.writeFile('hello.txt', 'Hello world');
74
-
75
- const stream = fs.createReadStream('hello.txt', { start: 6, encoding: 'utf-8' });
76
-
77
- const data = (await stream.toArray()).join('');
78
-
79
- assert.equal(data, 'world');
80
- });
81
-
82
- test('createReadStream with end', async () => {
83
- await fs.promises.writeFile('hello.txt', 'Hello world');
84
-
85
- const stream = fs.createReadStream('hello.txt', { end: 4, encoding: 'utf-8' });
86
-
87
- const data = (await stream.toArray()).join('');
88
-
89
- assert.equal(data, 'Hello');
90
- });
91
-
92
- test('FileHandle.createReadStream reads data correctly', async () => {
93
- const fileHandle = await fs.promises.open(testFilePath, 'r');
94
- const readStream = fileHandle.createReadStream({ encoding: 'utf-8' });
95
- const [data] = await readStream.toArray();
96
- assert.equal(data, testData);
97
- await fileHandle.close();
98
- });
99
-
100
- test('FileHandle.createWriteStream writes data correctly', async () => {
101
- const fileHandle = await fs.promises.open(testFilePathWrite, 'w');
102
- const writeStream = fileHandle.createWriteStream();
103
-
104
- const { promise, resolve, reject } = Promise.withResolvers();
105
- writeStream.on('finish', resolve);
106
- writeStream.on('error', reject);
107
- writeStream.end(testData, 'utf8');
108
- await promise;
109
-
110
- const data = await fs.promises.readFile(testFilePathWrite, 'utf8');
111
- assert.equal(data, testData);
112
- await fileHandle.close();
113
- });
114
-
115
- test('readable web stream', async () => {
116
- const fileHandle = await fs.promises.open(testFilePath, 'r');
117
- const webStream = fileHandle.readableWebStream();
118
-
119
- let data = '';
120
-
121
- const decoder = new TextDecoder();
122
-
123
- for await (const chunk of webStream) {
124
- data += decoder.decode(chunk);
125
- }
126
-
127
- assert.equal(data, testData);
128
- await fileHandle.close();
129
- });
130
-
131
- test('FileHandle.createReadStream after close should give an error', async () => {
132
- const fileHandle = await fs.promises.open(testFilePath, 'r');
133
- await fileHandle.close();
134
- // Closing invalidates the descriptor, so it fails range validation
135
- assert.throws(() => fileHandle.createReadStream(), { code: 'ERR_OUT_OF_RANGE' });
136
- });
137
-
138
- test('FileHandle.createWriteStream after close should give an error', async () => {
139
- const fileHandle = await fs.promises.open(testFilePathWrite, 'w');
140
- await fileHandle.close();
141
- // Closing invalidates the descriptor, so it fails range validation
142
- assert.throws(() => fileHandle.createWriteStream(), { code: 'ERR_OUT_OF_RANGE' });
143
- });
144
-
145
- // A file large enough to span several chunks, so the read path has to stitch
146
- // them back together and the vnode cache has to grow to hold them.
147
- const large = Buffer.alloc(200 * 1024);
148
- for (let i = 0; i < large.length; i++) large[i] = (i * 31 + 7) & 0xff;
149
-
150
- test('createReadStream reads a multi-chunk file byte for byte', async () => {
151
- await fs.promises.writeFile('large.bin', large);
152
-
153
- const chunks: Uint8Array[] = [];
154
- for await (const chunk of fs.createReadStream('large.bin')) chunks.push(chunk as Uint8Array);
155
-
156
- assert.deepEqual(Buffer.concat(chunks), large);
157
- });
158
-
159
- test('createReadStream honors highWaterMark as the chunk size', async () => {
160
- await fs.promises.writeFile('large.bin', large);
161
-
162
- const highWaterMark = 64 * 1024;
163
- const sizes: number[] = [];
164
- for await (const chunk of fs.createReadStream('large.bin', { highWaterMark })) sizes.push((chunk as Uint8Array).byteLength);
165
-
166
- // Every chunk but the last is a full highWaterMark, and they sum to the file
167
- assert.deepEqual(sizes.slice(0, -1), Array(sizes.length - 1).fill(highWaterMark));
168
- assert.equal(
169
- sizes.reduce((a, b) => a + b, 0),
170
- large.length,
171
- );
172
- });
173
-
174
- test('createReadStream with start and end spanning chunks', async () => {
175
- await fs.promises.writeFile('large.bin', large);
176
-
177
- // `end` is inclusive, and this range deliberately starts and stops mid-chunk
178
- const start = 1000;
179
- const end = 150_000;
180
- const chunks: Uint8Array[] = [];
181
- for await (const chunk of fs.createReadStream('large.bin', { start, end, highWaterMark: 8192 })) chunks.push(chunk as Uint8Array);
182
-
183
- assert.deepEqual(Buffer.concat(chunks), large.subarray(start, end + 1));
184
- });
185
-
186
- test('a read stream does not read past the end of the file', async () => {
187
- await fs.promises.writeFile('small.bin', large.subarray(0, 10));
188
-
189
- const chunks: Uint8Array[] = [];
190
- for await (const chunk of fs.createReadStream('small.bin', { highWaterMark: 64 * 1024 })) chunks.push(chunk as Uint8Array);
191
-
192
- assert.equal(chunks.length, 1);
193
- assert.deepEqual(chunks[0], large.subarray(0, 10));
194
- });
195
-
196
- test('a read stream over an empty file ends without a chunk', async () => {
197
- await fs.promises.writeFile('empty.bin', '');
198
-
199
- const chunks: unknown[] = [];
200
- for await (const chunk of fs.createReadStream('empty.bin')) chunks.push(chunk);
201
-
202
- assert.deepEqual(chunks, []);
203
- });
204
- });
@@ -1,64 +0,0 @@
1
- // SPDX-License-Identifier: LGPL-3.0-or-later
2
- import { test, suite } from 'node:test';
3
- import { config, fs } from '@zenfs/devel/tests/common';
4
- import assert from 'node:assert/strict';
5
- import { basename } from 'node:path/posix';
6
-
7
- await fs.promises.mkdir('/tmp');
8
-
9
- suite('Temporary Directories', config('tempdir'), () => {
10
- test('mkdtempSync', config('sync'), () => {
11
- const path = fs.mkdtempSync('/tmp/test-', { encoding: 'utf8' });
12
-
13
- assert.deepEqual(fs.readdirSync('/tmp'), [basename(path)]);
14
-
15
- fs.rmdirSync(path);
16
- });
17
-
18
- test('mkdtemp', config('async'), async () => {
19
- const path = await fs.promises.mkdtemp('/tmp/test-', { encoding: 'utf8' });
20
-
21
- assert.deepEqual(await fs.promises.readdir('/tmp'), [basename(path)]);
22
-
23
- await fs.promises.rmdir(path);
24
- });
25
-
26
- test('mkdtempDisposableSync', config('sync'), () => {
27
- using result = fs.mkdtempDisposableSync('/tmp/test-', { encoding: 'utf8' });
28
-
29
- assert.deepEqual(fs.readdirSync('/tmp'), [basename(result.path)]);
30
-
31
- fs.rmdirSync(result.path);
32
- });
33
-
34
- test('mkdtempDisposable', config('async'), async () => {
35
- await using result = await fs.promises.mkdtempDisposable('/tmp/test-', { encoding: 'utf8' });
36
-
37
- assert.deepEqual(await fs.promises.readdir('/tmp'), [basename(result.path)]);
38
-
39
- await fs.promises.rmdir(result.path);
40
- });
41
-
42
- /* A relative prefix is resolved against the working directory, rather than always being placed in `/tmp`.
43
- Note the returned path keeps the prefix it was given, so it is only compared by basename here. */
44
-
45
- test('mkdtempSync with a relative prefix', config('sync'), () => {
46
- const path = fs.mkdtempSync('tmp/relative-', { encoding: 'utf8' });
47
-
48
- assert(basename(path).startsWith('relative-'));
49
- assert(fs.statSync(path).isDirectory());
50
- assert(fs.readdirSync('/tmp').includes(basename(path)));
51
-
52
- fs.rmdirSync(path);
53
- });
54
-
55
- test('mkdtemp with a relative prefix', config('async'), async () => {
56
- const path = await fs.promises.mkdtemp('tmp/relative-', { encoding: 'utf8' });
57
-
58
- assert(basename(path).startsWith('relative-'));
59
- assert((await fs.promises.stat(path)).isDirectory());
60
- assert((await fs.promises.readdir('/tmp')).includes(basename(path)));
61
-
62
- await fs.promises.rmdir(path);
63
- });
64
- });
@@ -1,85 +0,0 @@
1
- // SPDX-License-Identifier: LGPL-3.0-or-later
2
- import type { StatsLike } from '@zenfs/core';
3
- import type { Exception } from 'kerium';
4
- import assert from 'node:assert/strict';
5
- import { suite, test } from 'node:test';
6
- import { wait } from 'utilium';
7
- import { config, fs } from '@zenfs/devel/tests/common';
8
-
9
- const path = 'x.txt';
10
-
11
- /**
12
- * Gets unix timestamps from stats
13
- *
14
- * @internal
15
- */
16
- export function unixTimestamps(stats: StatsLike<number>): Record<'atime' | 'mtime', number> {
17
- return {
18
- atime: Math.floor(stats.atimeMs),
19
- mtime: Math.floor(stats.mtimeMs),
20
- };
21
- }
22
-
23
- suite('Times', config('times'), () => {
24
- async function runTest(atime: Date | number, mtime: Date | number): Promise<void> {
25
- // Numbers are seconds since the epoch, while `Date`s and stats are in milliseconds
26
- const times = {
27
- atime: typeof atime == 'number' ? Math.floor(atime * 1000) : atime.getTime(),
28
- mtime: typeof mtime == 'number' ? Math.floor(mtime * 1000) : mtime.getTime(),
29
- };
30
-
31
- await fs.promises.utimes(path, atime, mtime);
32
-
33
- assert.deepEqual(unixTimestamps(await fs.promises.stat(path)), times);
34
-
35
- await fs.promises.utimes('foobarbaz', atime, mtime).catch((error: Exception) => {
36
- assert(Error.isError(error));
37
- assert.equal(error.code, 'ENOENT');
38
- });
39
-
40
- await using handle = await fs.promises.open(path, 'r');
41
-
42
- await handle.utimes(atime, mtime);
43
- assert.deepEqual(unixTimestamps(await handle.stat()), times);
44
-
45
- fs.utimesSync(path, atime, mtime);
46
- assert.deepEqual(unixTimestamps(fs.statSync(path)), times);
47
-
48
- try {
49
- fs.utimesSync('foobarbaz', atime, mtime);
50
- } catch (error: any) {
51
- assert.equal(error.code, 'ENOENT');
52
- }
53
-
54
- try {
55
- fs.futimesSync(-1, atime, mtime);
56
- } catch (error: any) {
57
- // A negative descriptor fails range validation before it is looked up
58
- assert.equal(error.code, 'ERR_OUT_OF_RANGE');
59
- }
60
- }
61
-
62
- test('utimes works', async () => {
63
- await test('new Date(...)', () => runTest(new Date('1982/09/10 13:37:00'), new Date('1982/09/10 13:37:00')));
64
- await test('new Date()', () => runTest(new Date(), new Date()));
65
- await test('number', () => runTest(123456.789, 123456.789));
66
- const stats = fs.statSync(path);
67
- await test('from stats', () => runTest(stats.atime, stats.mtime));
68
- });
69
-
70
- test('read changes atime', config('sync'), async () => {
71
- const before = fs.statSync(path).atimeMs;
72
- fs.readFileSync(path);
73
- await wait(25);
74
- const after = fs.statSync(path).atimeMs;
75
- assert(before < after);
76
- });
77
-
78
- test('write changes mtime', config('sync', 'write'), async () => {
79
- const before = fs.statSync(path).mtimeMs;
80
- fs.writeFileSync(path, 'cool');
81
- await wait(25);
82
- const after = fs.statSync(path).mtimeMs;
83
- assert(before < after);
84
- });
85
- });
@@ -1,89 +0,0 @@
1
- // SPDX-License-Identifier: LGPL-3.0-or-later
2
- import assert from 'node:assert/strict';
3
- import { suite, test } from 'node:test';
4
- import { config, fs } from '@zenfs/devel/tests/common';
5
-
6
- const path: string = 'truncate-file.txt',
7
- size = 1024 * 16,
8
- data = new Uint8Array(size).fill('x'.charCodeAt(0));
9
-
10
- suite('Truncating', config('truncate'), () => {
11
- test('Sync path functions', config('sync'), () => {
12
- fs.writeFileSync(path, data);
13
- assert.equal(fs.statSync(path).size, size);
14
-
15
- fs.truncateSync(path, 1024);
16
- assert.equal(fs.statSync(path).size, 1024);
17
-
18
- fs.truncateSync(path);
19
- assert.equal(fs.statSync(path).size, 0);
20
-
21
- fs.writeFileSync(path, data);
22
- assert.equal(fs.statSync(path).size, size);
23
- });
24
-
25
- test('FD functions', config('sync'), () => {
26
- const fd = fs.openSync(path, 'r+');
27
-
28
- fs.ftruncateSync(fd, 1024);
29
- assert.equal(fs.fstatSync(fd).size, 1024);
30
-
31
- fs.ftruncateSync(fd);
32
- assert.equal(fs.fstatSync(fd).size, 0);
33
-
34
- fs.closeSync(fd);
35
- });
36
-
37
- test('Contents follow the size #313', config('sync'), () => {
38
- const path = 'truncate-contents.txt';
39
- fs.writeFileSync(path, 'SECRETDATA');
40
-
41
- fs.truncateSync(path, 4);
42
- assert.equal(fs.readFileSync(path, 'utf8'), 'SECR');
43
-
44
- fs.truncateSync(path, 10);
45
- assert.equal(fs.readFileSync(path, 'utf8'), 'SECR\0\0\0\0\0\0');
46
- });
47
-
48
- const statSize = async (path: string) => (await fs.promises.stat(path)).size;
49
-
50
- test('Async path functions', config('async'), async () => {
51
- await fs.promises.writeFile(path, data);
52
-
53
- assert.equal(await statSize(path), 1024 * 16);
54
-
55
- await fs.promises.truncate(path, 1024);
56
- assert.equal(await statSize(path), 1024);
57
-
58
- await fs.promises.truncate(path);
59
- assert.equal(await statSize(path), 0);
60
-
61
- await fs.promises.writeFile(path, data);
62
- assert.equal(await statSize(path), size);
63
- });
64
-
65
- test('FileHandle', config('async'), async () => {
66
- const handle = await fs.promises.open(path, 'w');
67
-
68
- await handle.truncate(1024);
69
- await handle.sync();
70
- assert.equal(await statSize(path), 1024);
71
-
72
- await handle.truncate();
73
- await handle.sync();
74
- assert.equal(await statSize(path), 0);
75
-
76
- await handle.close();
77
- });
78
-
79
- test('Contents follow the size (async) #313', config('async'), async () => {
80
- const path = 'truncate-contents-async.txt';
81
- await fs.promises.writeFile(path, 'SECRETDATA');
82
-
83
- await fs.promises.truncate(path, 4);
84
- assert.equal(await fs.promises.readFile(path, 'utf8'), 'SECR');
85
-
86
- await fs.promises.truncate(path, 10);
87
- assert.equal(await fs.promises.readFile(path, 'utf8'), 'SECR\0\0\0\0\0\0');
88
- });
89
- });