UNPKG

7 kBPlain TextView Raw
1import * as fse from "fs-extra";
2import { expect } from "chai";
3import path from "./assert_path";
4import helper from "./helper";
5import * as jetpack from "..";
6
7describe("move", () => {
8 beforeEach(helper.setCleanTestCwd);
9 afterEach(helper.switchBackToCorrectCwd);
10
11 describe("moves file", () => {
12 const preparations = () => {
13 fse.outputFileSync("a/b.txt", "abc");
14 };
15
16 const expectations = () => {
17 path("a/b.txt").shouldNotExist();
18 path("c.txt").shouldBeFileWithContent("abc");
19 };
20
21 it("sync", () => {
22 preparations();
23 jetpack.move("a/b.txt", "c.txt");
24 expectations();
25 });
26
27 it("async", done => {
28 preparations();
29 jetpack.moveAsync("a/b.txt", "c.txt").then(() => {
30 expectations();
31 done();
32 });
33 });
34 });
35
36 describe("moves directory", () => {
37 const preparations = () => {
38 fse.outputFileSync("a/b/c.txt", "abc");
39 fse.mkdirsSync("x");
40 };
41
42 const expectations = () => {
43 path("a").shouldNotExist();
44 path("x/y/b/c.txt").shouldBeFileWithContent("abc");
45 };
46
47 it("sync", () => {
48 preparations();
49 jetpack.move("a", "x/y");
50 expectations();
51 });
52
53 it("async", done => {
54 preparations();
55 jetpack.moveAsync("a", "x/y").then(() => {
56 expectations();
57 done();
58 });
59 });
60 });
61
62 describe("creates nonexistent directories for destination path", () => {
63 const preparations = () => {
64 fse.outputFileSync("a.txt", "abc");
65 };
66
67 const expectations = () => {
68 path("a.txt").shouldNotExist();
69 path("a/b/z.txt").shouldBeFileWithContent("abc");
70 };
71
72 it("sync", () => {
73 preparations();
74 jetpack.move("a.txt", "a/b/z.txt");
75 expectations();
76 });
77
78 it("async", done => {
79 preparations();
80 jetpack.moveAsync("a.txt", "a/b/z.txt").then(() => {
81 expectations();
82 done();
83 });
84 });
85 });
86
87 describe("generates nice error when source path doesn't exist", () => {
88 const expectations = (err: any) => {
89 expect(err.code).to.equal("ENOENT");
90 expect(err.message).to.have.string("Path to move doesn't exist");
91 };
92
93 it("sync", () => {
94 try {
95 jetpack.move("a", "b");
96 throw new Error("Expected error to be thrown");
97 } catch (err) {
98 expectations(err);
99 }
100 });
101
102 it("async", done => {
103 jetpack.moveAsync("a", "b").catch(err => {
104 expectations(err);
105 done();
106 });
107 });
108 });
109
110 describe("overwriting behaviour", () => {
111 describe("does not overwrite by default", () => {
112 const preparations = () => {
113 fse.outputFileSync("file1.txt", "abc");
114 fse.outputFileSync("file2.txt", "xyz");
115 };
116
117 const expectations = (err: any) => {
118 expect(err.code).to.equal("EEXIST");
119 expect(err.message).to.have.string("Destination path already exists");
120 path("file2.txt").shouldBeFileWithContent("xyz");
121 };
122
123 it("sync", () => {
124 preparations();
125 try {
126 jetpack.move("file1.txt", "file2.txt");
127 throw new Error("Expected error to be thrown");
128 } catch (err) {
129 expectations(err);
130 }
131 });
132
133 it("async", done => {
134 preparations();
135 jetpack.moveAsync("file1.txt", "file2.txt").catch(err => {
136 expectations(err);
137 done();
138 });
139 });
140 });
141
142 describe("overwrites if it was specified", () => {
143 const preparations = () => {
144 fse.outputFileSync("file1.txt", "abc");
145 fse.outputFileSync("file2.txt", "xyz");
146 };
147
148 const expectations = () => {
149 path("file1.txt").shouldNotExist();
150 path("file2.txt").shouldBeFileWithContent("abc");
151 };
152
153 it("sync", () => {
154 preparations();
155 jetpack.move("file1.txt", "file2.txt", { overwrite: true });
156 expectations();
157 });
158
159 it("async", done => {
160 preparations();
161 jetpack
162 .moveAsync("file1.txt", "file2.txt", { overwrite: true })
163 .then(() => {
164 expectations();
165 done();
166 });
167 });
168 });
169
170 describe("can overwrite a directory", () => {
171 const preparations = () => {
172 fse.outputFileSync("file1.txt", "abc");
173 fse.mkdirsSync("dir");
174 };
175
176 const expectations = () => {
177 path("file1.txt").shouldNotExist();
178 path("dir").shouldBeFileWithContent("abc");
179 };
180
181 it("sync", () => {
182 preparations();
183 jetpack.move("file1.txt", "dir", { overwrite: true });
184 expectations();
185 });
186
187 it("async", done => {
188 preparations();
189 jetpack.moveAsync("file1.txt", "dir", { overwrite: true }).then(() => {
190 expectations();
191 done();
192 });
193 });
194 });
195 });
196
197 describe("respects internal CWD of jetpack instance", () => {
198 const preparations = () => {
199 fse.outputFileSync("a/b.txt", "abc");
200 };
201
202 const expectations = () => {
203 path("a/b.txt").shouldNotExist();
204 path("a/x.txt").shouldBeFileWithContent("abc");
205 };
206
207 it("sync", () => {
208 const jetContext = jetpack.cwd("a");
209 preparations();
210 jetContext.move("b.txt", "x.txt");
211 expectations();
212 });
213
214 it("async", done => {
215 const jetContext = jetpack.cwd("a");
216 preparations();
217 jetContext.moveAsync("b.txt", "x.txt").then(() => {
218 expectations();
219 done();
220 });
221 });
222 });
223
224 describe("input validation", () => {
225 const tests = [
226 { type: "sync", method: jetpack.move as any, methodName: "move" },
227 {
228 type: "async",
229 method: jetpack.moveAsync as any,
230 methodName: "moveAsync"
231 }
232 ];
233
234 describe('"from" argument', () => {
235 tests.forEach(test => {
236 it(test.type, () => {
237 expect(() => {
238 test.method(undefined, "xyz");
239 }).to.throw(
240 `Argument "from" passed to ${
241 test.methodName
242 }(from, to, [options]) must be a string. Received undefined`
243 );
244 });
245 });
246 });
247
248 describe('"to" argument', () => {
249 tests.forEach(test => {
250 it(test.type, () => {
251 expect(() => {
252 test.method("abc", undefined);
253 }).to.throw(
254 `Argument "to" passed to ${
255 test.methodName
256 }(from, to, [options]) must be a string. Received undefined`
257 );
258 });
259 });
260 });
261
262 describe('"options" object', () => {
263 describe('"overwrite" argument', () => {
264 tests.forEach(test => {
265 it(test.type, () => {
266 expect(() => {
267 test.method("abc", "xyz", { overwrite: 1 });
268 }).to.throw(
269 `Argument "options.overwrite" passed to ${
270 test.methodName
271 }(from, to, [options]) must be a boolean. Received number`
272 );
273 });
274 });
275 });
276 });
277 });
278});