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