summaryrefslogtreecommitdiff
path: root/tests/testdata/compile/npm_fs/main.ts
blob: 6361610a9e49e364f30b43f7ee5942977badb48d (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { url } from "npm:@denotest/esm-basic";
import { fileURLToPath } from "node:url";
import path from "node:path";
import assert from "node:assert/strict";

// will be at node_modules\.deno\@denotest+esm-basic@1.0.0\node_modules\@denotest\esm-basic
const dirPath = path.dirname(fileURLToPath(url));
const nodeModulesPath = path.join(dirPath, "../../../../../");
const packageJsonText = `{
  "name": "@denotest/esm-basic",
  "version": "1.0.0",
  "type": "module",
  "main": "main.mjs",
  "types": "main.d.mts"
}
`;
const vfsPackageJsonPath = path.join(dirPath, "package.json");

// reading a file in vfs
{
  const text = Deno.readTextFileSync(vfsPackageJsonPath);
  assert.equal(text, packageJsonText);
}

// reading a file async in vfs
{
  const text = await Deno.readTextFile(vfsPackageJsonPath);
  assert.equal(text, packageJsonText);
}

// copy file from vfs to real fs
{
  Deno.copyFileSync(vfsPackageJsonPath, "package.json");
  assert.equal(Deno.readTextFileSync("package.json"), packageJsonText);
}

// copy to vfs
assert.throws(
  () => Deno.copyFileSync("package.json", vfsPackageJsonPath),
  Deno.errors.NotSupported,
);
Deno.removeSync("package.json");

// copy file async from vfs to real fs
{
  await Deno.copyFile(vfsPackageJsonPath, "package.json");
  assert.equal(Deno.readTextFileSync("package.json"), packageJsonText);
}

// copy to vfs async
await assert.rejects(
  () => Deno.copyFile("package.json", vfsPackageJsonPath),
  Deno.errors.NotSupported,
);
Deno.removeSync("package.json");

// open
{
  const file = Deno.openSync(vfsPackageJsonPath);
  const bytes = new Uint8Array(10);
  file.seekSync(2, Deno.SeekMode.Start);
  assert.equal(file.readSync(bytes), 10);
  const text = new TextDecoder().decode(bytes);
  assert.equal(text, packageJsonText.slice(2, 12));
}
{
  const file = await Deno.open(vfsPackageJsonPath);
  const bytes = new Uint8Array(10);
  await file.seek(2, Deno.SeekMode.Start);
  assert.equal(await file.read(bytes), 10);
  const text = new TextDecoder().decode(bytes);
  assert.equal(text, packageJsonText.slice(2, 12));
}

// chdir
assert.throws(() => Deno.chdir(dirPath), Deno.errors.NotSupported);

// mkdir
assert.throws(
  () => Deno.mkdirSync(path.join(dirPath, "subDir")),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.mkdir(path.join(dirPath, "subDir")),
  Deno.errors.NotSupported,
);

// chmod
assert.throws(
  () => Deno.chmodSync(vfsPackageJsonPath, 0o777),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.chmod(vfsPackageJsonPath, 0o777),
  Deno.errors.NotSupported,
);

// chown
assert.throws(
  () => Deno.chownSync(vfsPackageJsonPath, 1000, 1000),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.chown(vfsPackageJsonPath, 1000, 1000),
  Deno.errors.NotSupported,
);

// remove
assert.throws(
  () => Deno.removeSync(vfsPackageJsonPath),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.remove(vfsPackageJsonPath),
  Deno.errors.NotSupported,
);

// stat
{
  const result = Deno.statSync(vfsPackageJsonPath);
  assert(result.isFile);
}
{
  const result = await Deno.stat(vfsPackageJsonPath);
  assert(result.isFile);
}

// lstat
{
  const result = Deno.lstatSync(
    path.join(nodeModulesPath, "@denotest", "esm-basic"),
  );
  assert(result.isSymlink);
}
{
  const result = await Deno.lstat(
    path.join(nodeModulesPath, "@denotest", "esm-basic"),
  );
  assert(result.isSymlink);
}

// realpath
{
  const result = Deno.realPathSync(
    path.join(nodeModulesPath, "@denotest", "esm-basic", "package.json"),
  );
  assert.equal(result, vfsPackageJsonPath);
}
{
  const result = await Deno.realPath(
    path.join(nodeModulesPath, "@denotest", "esm-basic", "package.json"),
  );
  assert.equal(result, vfsPackageJsonPath);
}

// read dir
const readDirNames = ["main.d.mts", "main.mjs", "other.mjs", "package.json"];
{
  const names = Array.from(Deno.readDirSync(dirPath))
    .map((e) => e.name);
  assert.deepEqual(readDirNames, names);
}
{
  const names = [];
  for await (const entry of Deno.readDir(dirPath)) {
    names.push(entry.name);
  }
  assert.deepEqual(readDirNames, names);
}

// rename
assert.throws(
  () => Deno.renameSync("package.json", vfsPackageJsonPath),
  Deno.errors.NotSupported,
);
assert.throws(
  () => Deno.renameSync(vfsPackageJsonPath, "package.json"),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.rename("package.json", vfsPackageJsonPath),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.rename(vfsPackageJsonPath, "package.json"),
  Deno.errors.NotSupported,
);

// link
assert.throws(
  () => Deno.linkSync("package.json", vfsPackageJsonPath),
  Deno.errors.NotSupported,
);
assert.throws(
  () => Deno.linkSync(vfsPackageJsonPath, "package.json"),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.link("package.json", vfsPackageJsonPath),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.link(vfsPackageJsonPath, "package.json"),
  Deno.errors.NotSupported,
);

// symlink
assert.throws(
  () => Deno.symlinkSync("package.json", vfsPackageJsonPath),
  Deno.errors.NotSupported,
);
assert.throws(
  () => Deno.symlinkSync(vfsPackageJsonPath, "package.json"),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.symlink("package.json", vfsPackageJsonPath),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.symlink(vfsPackageJsonPath, "package.json"),
  Deno.errors.NotSupported,
);

// read link
{
  const result = Deno.readLinkSync(
    path.join(nodeModulesPath, "@denotest", "esm-basic"),
  );
  assert.equal(result, dirPath);
}
{
  const result = await Deno.readLink(
    path.join(nodeModulesPath, "@denotest", "esm-basic"),
  );
  assert.equal(result, dirPath);
}

// truncate
assert.throws(
  () => Deno.truncateSync(vfsPackageJsonPath, 0),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.truncate(vfsPackageJsonPath, 0),
  Deno.errors.NotSupported,
);

// utime
assert.throws(
  () => Deno.utimeSync(vfsPackageJsonPath, 0, 0),
  Deno.errors.NotSupported,
);
await assert.rejects(
  () => Deno.utime(vfsPackageJsonPath, 0, 0),
  Deno.errors.NotSupported,
);

console.log("success");