deno.land / std@0.224.0 / fs / ensure_dir_test.ts

ensure_dir_test.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertRejects, assertThrows } from "../assert/mod.ts";import * as path from "../path/mod.ts";import { ensureDir, ensureDirSync } from "./ensure_dir.ts";import { ensureFile, ensureFileSync } from "./ensure_file.ts";
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));const testdataDir = path.resolve(moduleDir, "testdata", "ensure_dir");
Deno.test("ensureDir() creates dir if it does not exist", async function () { const baseDir = path.join(testdataDir, "not_exist"); const testDir = path.join(baseDir, "test");
try { await ensureDir(testDir);
// test dir should exists. await Deno.stat(testDir); } finally { await Deno.remove(baseDir, { recursive: true }); }});
Deno.test("ensureDirSync() creates dir if it does not exist", function () { const baseDir = path.join(testdataDir, "sync_not_exist"); const testDir = path.join(baseDir, "test");
try { ensureDirSync(testDir);
// test dir should exists. Deno.statSync(testDir); } finally { Deno.removeSync(baseDir, { recursive: true }); }});
Deno.test("ensureDir() ensures existing dir exists", async function () { const baseDir = path.join(testdataDir, "exist"); const testDir = path.join(baseDir, "test");
try { // create test directory await Deno.mkdir(testDir, { recursive: true });
await ensureDir(testDir);
// test dir should still exists. await Deno.stat(testDir); } finally { await Deno.remove(baseDir, { recursive: true }); }});
Deno.test("ensureDirSync() ensures existing dir exists", function () { const baseDir = path.join(testdataDir, "sync_exist"); const testDir = path.join(baseDir, "test");
try { // create test directory Deno.mkdirSync(testDir, { recursive: true });
ensureDirSync(testDir);
// test dir should still exists. Deno.statSync(testDir); } finally { Deno.removeSync(baseDir, { recursive: true }); }});
Deno.test("ensureDir() accepts links to dirs", async function () { const ldir = path.join(testdataDir, "ldir");
await ensureDir(ldir);
// test dir should still exists. await Deno.stat(ldir); // ldir should be still be a symlink const { isSymlink } = await Deno.lstat(ldir); assertEquals(isSymlink, true);});
Deno.test("ensureDirSync() accepts links to dirs", function () { const ldir = path.join(testdataDir, "ldir");
ensureDirSync(ldir);
// test dir should still exists. Deno.statSync(ldir); // ldir should be still be a symlink const { isSymlink } = Deno.lstatSync(ldir); assertEquals(isSymlink, true);});
Deno.test("ensureDir() rejects if input is a file", async function () { const baseDir = path.join(testdataDir, "exist_file"); const testFile = path.join(baseDir, "test");
try { await ensureFile(testFile);
await assertRejects( async () => { await ensureDir(testFile); }, Error, `Ensure path exists, expected 'dir', got 'file'`, ); } finally { await Deno.remove(baseDir, { recursive: true }); }});
Deno.test("ensureDirSync() throws if input is a file", function () { const baseDir = path.join(testdataDir, "exist_file_async"); const testFile = path.join(baseDir, "test");
try { ensureFileSync(testFile);
assertThrows( () => { ensureDirSync(testFile); }, Error, `Ensure path exists, expected 'dir', got 'file'`, ); } finally { Deno.removeSync(baseDir, { recursive: true }); }});
Deno.test("ensureDir() rejects links to files", async function () { const lf = path.join(testdataDir, "lf");
await assertRejects( async () => { await ensureDir(lf); }, Error, `Ensure path exists, expected 'dir', got 'file'`, );});
Deno.test("ensureDirSync() rejects links to files", function () { const lf = path.join(testdataDir, "lf");
assertThrows( () => { ensureDirSync(lf); }, Error, `Ensure path exists, expected 'dir', got 'file'`, );});
Deno.test({ name: "ensureDir() rejects permission fs write error", permissions: { read: true }, async fn() { const baseDir = path.join(testdataDir, "without_permission");
// ensureDir fails because this test doesn't have write permissions, // but don't swallow that error. await assertRejects( async () => await ensureDir(baseDir), Deno.errors.PermissionDenied, ); },});
Deno.test({ name: "ensureDirSync() throws permission fs write error", permissions: { read: true }, fn() { const baseDir = path.join( testdataDir, "sync_without_permission", );
// ensureDirSync fails because this test doesn't have write permissions, // but don't swallow that error. assertThrows( () => ensureDirSync(baseDir), Deno.errors.PermissionDenied, ); },});
std

Version Info

Tagged at
6 months ago