Repository navigation
Expand file tree
/
Copy pathfs.test.js
More file actions
174 lines (153 loc) · 5.75 KB
/
Copy pathfs.test.js
File metadata and controls
174 lines (153 loc) · 5.75 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// @flow
import * as fs from '../fs';
import * as path from 'path';
import { realpathSync } from 'fs';
import modeToPermissions from 'mode-to-permissions';
import fixtures from 'fixturez';
const f = fixtures(__dirname);
const REAL_PLATFORM = process.platform;
describe('fs', () => {
describe('readFile()', () => {
it('should read a file', async () => {
let filePath = path.join(f.find('simple-package'), 'package.json');
let fileContents = await fs.readFile(filePath);
expect(fileContents.toString()).toContain('fixture-basic');
});
});
describe('writeFile()', () => {
it('should write a file', async () => {
let dirName = f.temp();
let filePath = path.join(dirName, 'file.txt');
await fs.writeFile(filePath, 'test');
let fileContents = await fs.readFile(filePath);
expect(fileContents.toString()).toBe('test');
});
});
describe('rimraf()', () => {
it('should delete a directory', async () => {
let dirName = f.temp();
let filePath = path.join(dirName, 'file.txt');
await fs.writeFile(filePath, 'test');
await fs.rimraf(dirName);
await expect(fs.readFile(dirName)).rejects.toHaveProperty(
'code',
'ENOENT'
);
await expect(fs.readFile(filePath)).rejects.toHaveProperty(
'code',
'ENOENT'
);
});
});
describe('stat()', () => {
it('should get the stats of a file', async () => {
let fixturePath = path.join(f.find('simple-package'), 'package.json');
let stat = await fs.stat(fixturePath);
expect(stat.isFile()).toBe(true);
expect(stat.isDirectory()).toBe(false);
});
it('should get the stats of a directory', async () => {
let fixturePath = f.find('simple-package');
let stat = await fs.stat(fixturePath);
expect(stat.isFile()).toBe(false);
expect(stat.isDirectory()).toBe(true);
});
});
describe('lstat()', () => {
it('should get the stats of a file', async () => {
let fixturePath = path.join(f.find('simple-package'), 'package.json');
let stat = await fs.lstat(fixturePath);
expect(stat.isFile()).toBe(true);
expect(stat.isDirectory()).toBe(false);
});
it('should get the stats of a directory', async () => {
let fixturePath = f.find('simple-package');
let stat = await fs.lstat(fixturePath);
expect(stat.isFile()).toBe(false);
expect(stat.isDirectory()).toBe(true);
});
});
describe('symlink()', () => {
afterEach(() => {
process.platform = REAL_PLATFORM;
});
describe('posix', () => {
beforeEach(() => {
process.platform = 'linux';
});
it('should create a relative symlink to a directory', async () => {
let tempDir = f.temp();
let src = path.join(f.find('symlinks'), 'file.txt');
let dest = path.resolve(tempDir, 'file.txt');
await fs.symlink(src, dest, 'junction');
let realPath = (await fs.readlink(dest)) || '';
let resolved = path.join(path.dirname(dest), realPath);
expect(resolved).toEqual(src);
});
it('should create a relative symlink to an executable file', async () => {
let tempDir = f.temp();
let src = path.join(f.find('symlinks'), 'executable.sh');
let dest = path.resolve(tempDir, 'executable.sh');
await fs.symlink(src, dest, 'exec');
let realPath = (await fs.readlink(dest)) || '';
let resolved = path.join(path.dirname(dest), realPath);
expect(resolved).toEqual(src);
});
it('should overwrite an existing symlink', async () => {
let tempDir = f.temp();
let src1 = path.join(f.find('symlinks'), 'file.txt');
let src2 = path.join(f.find('symlinks'), 'file2.txt');
let dest = path.resolve(tempDir, 'file.txt');
await fs.symlink(src1, dest, 'junction');
await fs.symlink(src2, dest, 'junction');
let realPath = (await fs.readlink(dest)) || '';
let resolved = path.join(path.dirname(dest), realPath);
expect(resolved).toEqual(src2);
});
});
describe('windows', () => {
beforeEach(() => {
process.platform = 'win32';
});
it('should create a command shim to an executable file', async () => {
let tempDir = f.temp();
let src = path.join(f.find('symlinks'), 'shim.cmd');
let dest = path.resolve(tempDir, 'unshimmed.cmd');
await fs.symlink(src, dest, 'exec');
let files = await fs.readdir(tempDir);
expect(files).toEqual(['unshimmed', 'unshimmed.cmd']);
});
it('should always use absolute paths when creating symlinks', async () => {
let tempDir = f.temp();
let src = path.join(f.find('symlinks'), 'file.txt');
let dest = path.resolve(tempDir, 'file.txt');
await fs.symlink(src, dest, 'junction');
let files = await fs.readdir(tempDir);
let realPath = await fs.readlink(dest);
expect(realPath).toBe(src);
});
});
});
describe('readdir()', () => {
it('should list a directory of files', async () => {
let fixturePath = f.find('simple-project');
let files = await fs.readdir(fixturePath);
expect(files).toEqual(['package.json', 'packages']);
});
});
describe('readdirSafe()', () => {
it('should list a directory of files', async () => {
let fixturePath = f.find('simple-project');
let files = await fs.readdirSafe(fixturePath);
expect(files).toEqual(['package.json', 'packages']);
});
it('should return an array if no directory exists', async () => {
let fixturePath = path.join(__dirname, 'foo', 'bar', 'baz');
let files = await fs.readdirSafe(fixturePath);
expect(files).toEqual([]);
});
});
describe('readlink()', () => {
// ...
});
});