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("append", () => {
|
8 | beforeEach(helper.setCleanTestCwd);
|
9 | afterEach(helper.switchBackToCorrectCwd);
|
10 |
|
11 | describe("appends String to file", () => {
|
12 | const preparations = () => {
|
13 | fse.writeFileSync("file.txt", "abc");
|
14 | };
|
15 |
|
16 | const expectations = () => {
|
17 | path("file.txt").shouldBeFileWithContent("abcxyz");
|
18 | };
|
19 |
|
20 | it("sync", () => {
|
21 | preparations();
|
22 | jetpack.append("file.txt", "xyz");
|
23 | expectations();
|
24 | });
|
25 |
|
26 | it("async", done => {
|
27 | preparations();
|
28 | jetpack.appendAsync("file.txt", "xyz").then(() => {
|
29 | expectations();
|
30 | done();
|
31 | });
|
32 | });
|
33 | });
|
34 |
|
35 | describe("appends Buffer to file", () => {
|
36 | const preparations = () => {
|
37 | fse.writeFileSync("file.bin", new Buffer([11]));
|
38 | };
|
39 |
|
40 | const expectations = () => {
|
41 | path("file.bin").shouldBeFileWithContent(new Buffer([11, 22]));
|
42 | };
|
43 |
|
44 | it("sync", () => {
|
45 | preparations();
|
46 | jetpack.append("file.bin", new Buffer([22]));
|
47 | expectations();
|
48 | });
|
49 |
|
50 | it("async", done => {
|
51 | preparations();
|
52 | jetpack.appendAsync("file.bin", new Buffer([22])).then(() => {
|
53 | expectations();
|
54 | done();
|
55 | });
|
56 | });
|
57 | });
|
58 |
|
59 | describe("if file doesn't exist creates it", () => {
|
60 | const expectations = () => {
|
61 | path("file.txt").shouldBeFileWithContent("xyz");
|
62 | };
|
63 |
|
64 | it("sync", () => {
|
65 | jetpack.append("file.txt", "xyz");
|
66 | expectations();
|
67 | });
|
68 |
|
69 | it("async", done => {
|
70 | jetpack.appendAsync("file.txt", "xyz").then(() => {
|
71 | expectations();
|
72 | done();
|
73 | });
|
74 | });
|
75 | });
|
76 |
|
77 | describe("if parent directory doesn't exist creates it", () => {
|
78 | const expectations = () => {
|
79 | path("dir/dir/file.txt").shouldBeFileWithContent("xyz");
|
80 | };
|
81 |
|
82 | it("sync", () => {
|
83 | jetpack.append("dir/dir/file.txt", "xyz");
|
84 | expectations();
|
85 | });
|
86 |
|
87 | it("async", done => {
|
88 | jetpack.appendAsync("dir/dir/file.txt", "xyz").then(() => {
|
89 | expectations();
|
90 | done();
|
91 | });
|
92 | });
|
93 | });
|
94 |
|
95 | describe("respects internal CWD of jetpack instance", () => {
|
96 | const preparations = () => {
|
97 | fse.outputFileSync("a/b.txt", "abc");
|
98 | };
|
99 |
|
100 | const expectations = () => {
|
101 | path("a/b.txt").shouldBeFileWithContent("abcxyz");
|
102 | };
|
103 |
|
104 | it("sync", () => {
|
105 | const jetContext = jetpack.cwd("a");
|
106 | preparations();
|
107 | jetContext.append("b.txt", "xyz");
|
108 | expectations();
|
109 | });
|
110 |
|
111 | it("async", done => {
|
112 | const jetContext = jetpack.cwd("a");
|
113 | preparations();
|
114 | jetContext.appendAsync("b.txt", "xyz").then(() => {
|
115 | expectations();
|
116 | done();
|
117 | });
|
118 | });
|
119 | });
|
120 |
|
121 | describe("input validation", () => {
|
122 | const tests = [
|
123 | { type: "sync", method: jetpack.append as any, methodName: "append" },
|
124 | {
|
125 | type: "async",
|
126 | method: jetpack.appendAsync as any,
|
127 | methodName: "appendAsync"
|
128 | }
|
129 | ];
|
130 |
|
131 | describe('"path" argument', () => {
|
132 | tests.forEach(test => {
|
133 | it(test.type, () => {
|
134 | expect(() => {
|
135 | test.method(undefined, "xyz");
|
136 | }).to.throw(
|
137 | `Argument "path" passed to ${
|
138 | test.methodName
|
139 | }(path, data, [options]) must be a string. Received undefined`
|
140 | );
|
141 | });
|
142 | });
|
143 | });
|
144 |
|
145 | describe('"data" argument', () => {
|
146 | tests.forEach(test => {
|
147 | it(test.type, () => {
|
148 | expect(() => {
|
149 | test.method("abc");
|
150 | }).to.throw(
|
151 | `Argument "data" passed to ${
|
152 | test.methodName
|
153 | }(path, data, [options]) must be a string or a buffer. Received undefined`
|
154 | );
|
155 | });
|
156 | });
|
157 | });
|
158 |
|
159 | describe('"options" object', () => {
|
160 | describe('"mode" argument', () => {
|
161 | tests.forEach(test => {
|
162 | it(test.type, () => {
|
163 | expect(() => {
|
164 | test.method("abc", "xyz", { mode: true });
|
165 | }).to.throw(
|
166 | `Argument "options.mode" passed to ${
|
167 | test.methodName
|
168 | }(path, data, [options]) must be a string or a number. Received boolean`
|
169 | );
|
170 | });
|
171 | });
|
172 | });
|
173 | });
|
174 | });
|
175 |
|
176 | if (process.platform !== "win32") {
|
177 | describe("sets file mode on created file (unix only)", () => {
|
178 | const expectations = () => {
|
179 | path("file.txt").shouldHaveMode("711");
|
180 | };
|
181 |
|
182 | it("sync", () => {
|
183 | jetpack.append("file.txt", "abc", { mode: "711" });
|
184 | expectations();
|
185 | });
|
186 |
|
187 | it("async", done => {
|
188 | jetpack.appendAsync("file.txt", "abc", { mode: "711" }).then(() => {
|
189 | expectations();
|
190 | done();
|
191 | });
|
192 | });
|
193 | });
|
194 | }
|
195 | });
|