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("read", () => {
|
8 | beforeEach(helper.setCleanTestCwd);
|
9 | afterEach(helper.switchBackToCorrectCwd);
|
10 |
|
11 | describe("reads file as a string", () => {
|
12 | const preparations = () => {
|
13 | fse.outputFileSync("file.txt", "abc");
|
14 | };
|
15 |
|
16 | const expectations = (content: any) => {
|
17 | expect(content).to.equal("abc");
|
18 | };
|
19 |
|
20 | it("sync", () => {
|
21 | preparations();
|
22 | expectations(jetpack.read("file.txt"));
|
23 | expectations(jetpack.read("file.txt", "utf8"));
|
24 | });
|
25 |
|
26 | it("async", done => {
|
27 | preparations();
|
28 | jetpack
|
29 | .readAsync("file.txt")
|
30 | .then(content => {
|
31 | expectations(content);
|
32 | return jetpack.readAsync("file.txt", "utf8");
|
33 | })
|
34 | .then(content => {
|
35 | expectations(content);
|
36 | done();
|
37 | });
|
38 | });
|
39 | });
|
40 |
|
41 | describe("reads file as a Buffer", () => {
|
42 | const preparations = () => {
|
43 | fse.outputFileSync("file.txt", new Buffer([11, 22]));
|
44 | };
|
45 |
|
46 | const expectations = (content: Buffer) => {
|
47 | expect(Buffer.isBuffer(content)).to.equal(true);
|
48 | expect(content.length).to.equal(2);
|
49 | expect(content[0]).to.equal(11);
|
50 | expect(content[1]).to.equal(22);
|
51 | };
|
52 |
|
53 | it("sync", () => {
|
54 | preparations();
|
55 | expectations(jetpack.read("file.txt", "buffer"));
|
56 | });
|
57 |
|
58 | it("async", done => {
|
59 | preparations();
|
60 | jetpack.readAsync("file.txt", "buffer").then(content => {
|
61 | expectations(content);
|
62 | done();
|
63 | });
|
64 | });
|
65 | });
|
66 |
|
67 | describe("reads file as JSON", () => {
|
68 | const obj = {
|
69 | utf8: "ąćłźż"
|
70 | };
|
71 |
|
72 | const preparations = () => {
|
73 | fse.outputFileSync("file.json", JSON.stringify(obj));
|
74 | };
|
75 |
|
76 | const expectations = (content: any) => {
|
77 | expect(content).to.eql(obj);
|
78 | };
|
79 |
|
80 | it("sync", () => {
|
81 | preparations();
|
82 | expectations(jetpack.read("file.json", "json"));
|
83 | });
|
84 |
|
85 | it("async", done => {
|
86 | preparations();
|
87 | jetpack.readAsync("file.json", "json").then(content => {
|
88 | expectations(content);
|
89 | done();
|
90 | });
|
91 | });
|
92 | });
|
93 |
|
94 | describe("gives nice error message when JSON parsing failed", () => {
|
95 | const preparations = () => {
|
96 | fse.outputFileSync("file.json", '{ "abc: 123 }');
|
97 | };
|
98 |
|
99 | const expectations = (err: any) => {
|
100 | expect(err.message).to.have.string("JSON parsing failed while reading");
|
101 | };
|
102 |
|
103 | it("sync", () => {
|
104 | preparations();
|
105 | try {
|
106 | jetpack.read("file.json", "json");
|
107 | throw new Error("Expected error to be thrown");
|
108 | } catch (err) {
|
109 | expectations(err);
|
110 | }
|
111 | });
|
112 |
|
113 | it("async", done => {
|
114 | preparations();
|
115 | jetpack.readAsync("file.json", "json").catch(err => {
|
116 | expectations(err);
|
117 | done();
|
118 | });
|
119 | });
|
120 | });
|
121 |
|
122 | describe("reads file as JSON with Date parsing", () => {
|
123 | const obj = {
|
124 | utf8: "ąćłźż",
|
125 | date: new Date()
|
126 | };
|
127 |
|
128 | const preparations = () => {
|
129 | fse.outputFileSync("file.json", JSON.stringify(obj));
|
130 | };
|
131 |
|
132 | const expectations = (content: any) => {
|
133 | expect(content).to.eql(obj);
|
134 | };
|
135 |
|
136 | it("sync", () => {
|
137 | preparations();
|
138 | expectations(jetpack.read("file.json", "jsonWithDates"));
|
139 | });
|
140 |
|
141 | it("async", done => {
|
142 | preparations();
|
143 | jetpack.readAsync("file.json", "jsonWithDates").then(content => {
|
144 | expectations(content);
|
145 | done();
|
146 | });
|
147 | });
|
148 | });
|
149 |
|
150 | describe("returns undefined if file doesn't exist", () => {
|
151 | const expectations = (content: any) => {
|
152 | expect(content).to.equal(undefined);
|
153 | };
|
154 |
|
155 | it("sync", () => {
|
156 | expectations(jetpack.read("nonexistent.txt"));
|
157 | expectations(jetpack.read("nonexistent.txt", "json"));
|
158 | expectations(jetpack.read("nonexistent.txt", "buffer"));
|
159 | });
|
160 |
|
161 | it("async", done => {
|
162 | jetpack
|
163 | .readAsync("nonexistent.txt")
|
164 | .then(content => {
|
165 | expectations(content);
|
166 | return jetpack.readAsync("nonexistent.txt", "json");
|
167 | })
|
168 | .then(content => {
|
169 | expectations(content);
|
170 | return jetpack.readAsync("nonexistent.txt", "buffer");
|
171 | })
|
172 | .then(content => {
|
173 | expectations(content);
|
174 | done();
|
175 | });
|
176 | });
|
177 | });
|
178 |
|
179 | describe("throws if given path is a directory", () => {
|
180 | const preparations = () => {
|
181 | fse.mkdirsSync("dir");
|
182 | };
|
183 |
|
184 | const expectations = (err: any) => {
|
185 | expect(err.code).to.equal("EISDIR");
|
186 | };
|
187 |
|
188 | it("sync", () => {
|
189 | preparations();
|
190 | try {
|
191 | jetpack.read("dir");
|
192 | throw new Error("Expected error to be thrown");
|
193 | } catch (err) {
|
194 | expectations(err);
|
195 | }
|
196 | });
|
197 |
|
198 | it("async", done => {
|
199 | preparations();
|
200 | jetpack.readAsync("dir").catch(err => {
|
201 | expectations(err);
|
202 | done();
|
203 | });
|
204 | });
|
205 | });
|
206 |
|
207 | describe("respects internal CWD of jetpack instance", () => {
|
208 | const preparations = () => {
|
209 | fse.outputFileSync("a/file.txt", "abc");
|
210 | };
|
211 |
|
212 | const expectations = (data: any) => {
|
213 | expect(data).to.equal("abc");
|
214 | };
|
215 |
|
216 | it("sync", () => {
|
217 | const jetContext = jetpack.cwd("a");
|
218 | preparations();
|
219 | expectations(jetContext.read("file.txt"));
|
220 | });
|
221 |
|
222 | it("async", done => {
|
223 | const jetContext = jetpack.cwd("a");
|
224 | preparations();
|
225 | jetContext.readAsync("file.txt").then(data => {
|
226 | expectations(data);
|
227 | done();
|
228 | });
|
229 | });
|
230 | });
|
231 |
|
232 | describe("input validation", () => {
|
233 | const tests = [
|
234 | { type: "sync", method: jetpack.read as any, methodName: "read" },
|
235 | {
|
236 | type: "async",
|
237 | method: jetpack.readAsync as any,
|
238 | methodName: "readAsync"
|
239 | }
|
240 | ];
|
241 |
|
242 | describe('"path" argument', () => {
|
243 | tests.forEach(test => {
|
244 | it(test.type, () => {
|
245 | expect(() => {
|
246 | test.method(undefined, "xyz");
|
247 | }).to.throw(
|
248 | `Argument "path" passed to ${
|
249 | test.methodName
|
250 | }(path, returnAs) must be a string. Received undefined`
|
251 | );
|
252 | });
|
253 | });
|
254 | });
|
255 |
|
256 | describe('"returnAs" argument', () => {
|
257 | tests.forEach(test => {
|
258 | it(test.type, () => {
|
259 | expect(() => {
|
260 | test.method("abc", true);
|
261 | }).to.throw(
|
262 | `Argument "returnAs" passed to ${
|
263 | test.methodName
|
264 | }(path, returnAs) must be a string or an undefined. Received boolean`
|
265 | );
|
266 | expect(() => {
|
267 | test.method("abc", "foo");
|
268 | }).to.throw(
|
269 | `Argument "returnAs" passed to ${
|
270 | test.methodName
|
271 | }(path, returnAs) must have one of values: utf8, buffer, json, jsonWithDates`
|
272 | );
|
273 | });
|
274 | });
|
275 | });
|
276 | });
|
277 | });
|