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("write", () => {
|
8 | beforeEach(helper.setCleanTestCwd);
|
9 | afterEach(helper.switchBackToCorrectCwd);
|
10 |
|
11 | describe("writes data from string", () => {
|
12 | const expectations = () => {
|
13 | path("file.txt").shouldBeFileWithContent("abc");
|
14 | };
|
15 |
|
16 | it("sync", () => {
|
17 | jetpack.write("file.txt", "abc");
|
18 | expectations();
|
19 | });
|
20 |
|
21 | it("async", done => {
|
22 | jetpack.writeAsync("file.txt", "abc").then(() => {
|
23 | expectations();
|
24 | done();
|
25 | });
|
26 | });
|
27 | });
|
28 |
|
29 | describe("writes data from Buffer", () => {
|
30 | const expectations = () => {
|
31 | path("file.txt").shouldBeFileWithContent(new Buffer([11, 22]));
|
32 | };
|
33 |
|
34 | it("sync", () => {
|
35 | jetpack.write("file.txt", new Buffer([11, 22]));
|
36 | expectations();
|
37 | });
|
38 |
|
39 | it("async", done => {
|
40 | jetpack.writeAsync("file.txt", new Buffer([11, 22])).then(() => {
|
41 | expectations();
|
42 | done();
|
43 | });
|
44 | });
|
45 | });
|
46 |
|
47 | describe("writes data as JSON", () => {
|
48 | const obj = {
|
49 | utf8: "ąćłźż"
|
50 | };
|
51 |
|
52 | const expectations = () => {
|
53 | const content = JSON.parse(fse.readFileSync("file.json", "utf8"));
|
54 | expect(content).to.eql(obj);
|
55 | };
|
56 |
|
57 | it("sync", () => {
|
58 | jetpack.write("file.json", obj);
|
59 | expectations();
|
60 | });
|
61 |
|
62 | it("async", done => {
|
63 | jetpack.writeAsync("file.json", obj).then(() => {
|
64 | expectations();
|
65 | done();
|
66 | });
|
67 | });
|
68 | });
|
69 |
|
70 | describe("written JSON data can be indented", () => {
|
71 | const obj = {
|
72 | utf8: "ąćłźż"
|
73 | };
|
74 |
|
75 | const expectations = () => {
|
76 | const sizeA = fse.statSync("a.json").size;
|
77 | const sizeB = fse.statSync("b.json").size;
|
78 | const sizeC = fse.statSync("c.json").size;
|
79 | expect(sizeB).to.be.above(sizeA);
|
80 | expect(sizeC).to.be.above(sizeB);
|
81 | };
|
82 |
|
83 | it("sync", () => {
|
84 | jetpack.write("a.json", obj, { jsonIndent: 0 });
|
85 | jetpack.write("b.json", obj);
|
86 | jetpack.write("c.json", obj, { jsonIndent: 4 });
|
87 | expectations();
|
88 | });
|
89 |
|
90 | it("async", done => {
|
91 | Promise.all([
|
92 | jetpack.writeAsync("a.json", obj, { jsonIndent: 0 }),
|
93 | jetpack.writeAsync("b.json", obj),
|
94 | jetpack.writeAsync("c.json", obj, { jsonIndent: 4 })
|
95 | ]).then(() => {
|
96 | expectations();
|
97 | done();
|
98 | });
|
99 | });
|
100 | });
|
101 |
|
102 | describe("writes and reads file as JSON with Date parsing", () => {
|
103 | const obj = {
|
104 | date: new Date()
|
105 | };
|
106 |
|
107 | const expectations = () => {
|
108 | const content = JSON.parse(fse.readFileSync("file.json", "utf8"));
|
109 | expect(content.date).to.equal(obj.date.toISOString());
|
110 | };
|
111 |
|
112 | it("sync", () => {
|
113 | jetpack.write("file.json", obj);
|
114 | expectations();
|
115 | });
|
116 |
|
117 | it("async", done => {
|
118 | jetpack.writeAsync("file.json", obj).then(() => {
|
119 | expectations();
|
120 | done();
|
121 | });
|
122 | });
|
123 | });
|
124 |
|
125 | describe("can create nonexistent parent directories", () => {
|
126 | const expectations = () => {
|
127 | path("a/b/c.txt").shouldBeFileWithContent("abc");
|
128 | };
|
129 |
|
130 | it("sync", () => {
|
131 | jetpack.write("a/b/c.txt", "abc");
|
132 | expectations();
|
133 | });
|
134 |
|
135 | it("async", done => {
|
136 | jetpack.writeAsync("a/b/c.txt", "abc").then(() => {
|
137 | expectations();
|
138 | done();
|
139 | });
|
140 | });
|
141 | });
|
142 |
|
143 | describe("respects internal CWD of jetpack instance", () => {
|
144 | const expectations = () => {
|
145 | path("a/b/c.txt").shouldBeFileWithContent("abc");
|
146 | };
|
147 |
|
148 | it("sync", () => {
|
149 | const jetContext = jetpack.cwd("a");
|
150 | jetContext.write("b/c.txt", "abc");
|
151 | expectations();
|
152 | });
|
153 |
|
154 | it("async", done => {
|
155 | const jetContext = jetpack.cwd("a");
|
156 | jetContext.writeAsync("b/c.txt", "abc").then(() => {
|
157 | expectations();
|
158 | done();
|
159 | });
|
160 | });
|
161 | });
|
162 |
|
163 | describe("input validation", () => {
|
164 | const tests = [
|
165 | { type: "sync", method: jetpack.write as any, methodName: "write" },
|
166 | {
|
167 | type: "async",
|
168 | method: jetpack.writeAsync as any,
|
169 | methodName: "writeAsync"
|
170 | }
|
171 | ];
|
172 |
|
173 | describe('"path" argument', () => {
|
174 | tests.forEach(test => {
|
175 | it(test.type, () => {
|
176 | expect(() => {
|
177 | test.method(undefined);
|
178 | }).to.throw(
|
179 | `Argument "path" passed to ${
|
180 | test.methodName
|
181 | }(path, data, [options]) must be a string. Received undefined`
|
182 | );
|
183 | });
|
184 | });
|
185 | });
|
186 |
|
187 | describe('"data" argument', () => {
|
188 | tests.forEach(test => {
|
189 | it(test.type, () => {
|
190 | expect(() => {
|
191 | test.method("abc", true);
|
192 | }).to.throw(
|
193 | `Argument "data" passed to ${
|
194 | test.methodName
|
195 | }(path, data, [options]) must be a string or a buffer or an object or an array. Received boolean`
|
196 | );
|
197 | });
|
198 | });
|
199 | });
|
200 |
|
201 | describe('"options" object', () => {
|
202 | describe('"atomic" argument', () => {
|
203 | tests.forEach(test => {
|
204 | it(test.type, () => {
|
205 | expect(() => {
|
206 | test.method("abc", "xyz", { atomic: 1 });
|
207 | }).to.throw(
|
208 | `Argument "options.atomic" passed to ${
|
209 | test.methodName
|
210 | }(path, data, [options]) must be a boolean. Received number`
|
211 | );
|
212 | });
|
213 | });
|
214 | });
|
215 | describe('"jsonIndent" argument', () => {
|
216 | tests.forEach(test => {
|
217 | it(test.type, () => {
|
218 | expect(() => {
|
219 | test.method("abc", "xyz", { jsonIndent: true });
|
220 | }).to.throw(
|
221 | `Argument "options.jsonIndent" passed to ${
|
222 | test.methodName
|
223 | }(path, data, [options]) must be a number. Received boolean`
|
224 | );
|
225 | });
|
226 | });
|
227 | });
|
228 | });
|
229 | });
|
230 | });
|