1 | import * as fse from "fs-extra";
|
2 | import { expect } from "chai";
|
3 | import path from "./assert_path";
|
4 | import helper from "./helper";
|
5 | import * as jetpack from "..";
|
6 |
|
7 | describe("remove", () => {
|
8 | beforeEach(helper.setCleanTestCwd);
|
9 | afterEach(helper.switchBackToCorrectCwd);
|
10 |
|
11 | describe("doesn't throw if path already doesn't exist", () => {
|
12 | it("sync", () => {
|
13 | jetpack.remove("dir");
|
14 | });
|
15 |
|
16 | it("async", done => {
|
17 | jetpack.removeAsync("dir").then(() => {
|
18 | done();
|
19 | });
|
20 | });
|
21 | });
|
22 |
|
23 | describe("should delete file", () => {
|
24 | const preparations = () => {
|
25 | fse.outputFileSync("file.txt", "abc");
|
26 | };
|
27 |
|
28 | const expectations = () => {
|
29 | path("file.txt").shouldNotExist();
|
30 | };
|
31 |
|
32 | it("sync", () => {
|
33 | preparations();
|
34 | jetpack.remove("file.txt");
|
35 | expectations();
|
36 | });
|
37 |
|
38 | it("async", done => {
|
39 | preparations();
|
40 | jetpack.removeAsync("file.txt").then(() => {
|
41 | expectations();
|
42 | done();
|
43 | });
|
44 | });
|
45 | });
|
46 |
|
47 | describe("removes directory with stuff inside", () => {
|
48 | const preparations = () => {
|
49 | fse.mkdirsSync("a/b/c");
|
50 | fse.outputFileSync("a/f.txt", "abc");
|
51 | fse.outputFileSync("a/b/f.txt", "123");
|
52 | };
|
53 |
|
54 | const expectations = () => {
|
55 | path("a").shouldNotExist();
|
56 | };
|
57 |
|
58 | it("sync", () => {
|
59 | preparations();
|
60 | jetpack.remove("a");
|
61 | expectations();
|
62 | });
|
63 |
|
64 | it("async", done => {
|
65 | preparations();
|
66 | jetpack.removeAsync("a").then(() => {
|
67 | expectations();
|
68 | done();
|
69 | });
|
70 | });
|
71 | });
|
72 |
|
73 | describe("will retry attempt if file is locked", () => {
|
74 | const preparations = () => {
|
75 | fse.mkdirsSync("a/b/c");
|
76 | fse.outputFileSync("a/f.txt", "abc");
|
77 | fse.outputFileSync("a/b/f.txt", "123");
|
78 | };
|
79 |
|
80 | const expectations = () => {
|
81 | path("a").shouldNotExist();
|
82 | };
|
83 |
|
84 | it("async", done => {
|
85 | preparations();
|
86 |
|
87 | fse.open("a/f.txt", "w", (err, fd) => {
|
88 | if (err) {
|
89 | done(err);
|
90 | } else {
|
91 |
|
92 | setTimeout(() => {
|
93 | fse.close(fd);
|
94 | }, 150);
|
95 |
|
96 | jetpack
|
97 | .removeAsync("a")
|
98 | .then(() => {
|
99 | expectations();
|
100 | done();
|
101 | })
|
102 | .catch(done);
|
103 | }
|
104 | });
|
105 | });
|
106 | });
|
107 |
|
108 | describe("respects internal CWD of jetpack instance", () => {
|
109 | const preparations = () => {
|
110 | fse.outputFileSync("a/b/c.txt", "123");
|
111 | };
|
112 |
|
113 | const expectations = () => {
|
114 | path("a").shouldBeDirectory();
|
115 | path("a/b").shouldNotExist();
|
116 | };
|
117 |
|
118 | it("sync", () => {
|
119 | const jetContext = jetpack.cwd("a");
|
120 | preparations();
|
121 | jetContext.remove("b");
|
122 | expectations();
|
123 | });
|
124 |
|
125 | it("async", done => {
|
126 | const jetContext = jetpack.cwd("a");
|
127 | preparations();
|
128 | jetContext.removeAsync("b").then(() => {
|
129 | expectations();
|
130 | done();
|
131 | });
|
132 | });
|
133 | });
|
134 |
|
135 | describe("can be called with no parameters, what will remove CWD directory", () => {
|
136 | const preparations = () => {
|
137 | fse.outputFileSync("a/b/c.txt", "abc");
|
138 | };
|
139 |
|
140 | const expectations = () => {
|
141 | path("a").shouldNotExist();
|
142 | };
|
143 |
|
144 | it("sync", () => {
|
145 | const jetContext = jetpack.cwd("a");
|
146 | preparations();
|
147 | jetContext.remove();
|
148 | expectations();
|
149 | });
|
150 |
|
151 | it("async", done => {
|
152 | const jetContext = jetpack.cwd("a");
|
153 | preparations();
|
154 | jetContext.removeAsync().then(() => {
|
155 | expectations();
|
156 | done();
|
157 | });
|
158 | });
|
159 | });
|
160 |
|
161 | describe("removes only symlinks, never real content where symlinks point", () => {
|
162 | const preparations = () => {
|
163 | fse.outputFileSync("have_to_stay_file", "abc");
|
164 | fse.mkdirsSync("to_remove");
|
165 | fse.symlinkSync("../have_to_stay_file", "to_remove/symlink");
|
166 |
|
167 | expect(fse.readFileSync("to_remove/symlink", "utf8")).to.equal("abc");
|
168 | };
|
169 |
|
170 | const expectations = () => {
|
171 | path("have_to_stay_file").shouldBeFileWithContent("abc");
|
172 | path("to_remove").shouldNotExist();
|
173 | };
|
174 |
|
175 | it("sync", () => {
|
176 | preparations();
|
177 | jetpack.remove("to_remove");
|
178 | expectations();
|
179 | });
|
180 |
|
181 | it("async", done => {
|
182 | preparations();
|
183 | jetpack.removeAsync("to_remove").then(() => {
|
184 | expectations();
|
185 | done();
|
186 | });
|
187 | });
|
188 | });
|
189 |
|
190 | describe("input validation", () => {
|
191 | const tests = [
|
192 | { type: "sync", method: jetpack.remove as any, methodName: "remove" },
|
193 | {
|
194 | type: "async",
|
195 | method: jetpack.removeAsync as any,
|
196 | methodName: "removeAsync"
|
197 | }
|
198 | ];
|
199 |
|
200 | describe('"path" argument', () => {
|
201 | tests.forEach(test => {
|
202 | it(test.type, () => {
|
203 | expect(() => {
|
204 | test.method(true);
|
205 | }).to.throw(
|
206 | `Argument "path" passed to ${
|
207 | test.methodName
|
208 | }([path]) must be a string or an undefined. Received boolean`
|
209 | );
|
210 | });
|
211 | });
|
212 | });
|
213 | });
|
214 | });
|