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("list", () => {
|
8 | beforeEach(helper.setCleanTestCwd);
|
9 | afterEach(helper.switchBackToCorrectCwd);
|
10 |
|
11 | describe("lists file names in given path", () => {
|
12 | const preparations = () => {
|
13 | fse.mkdirsSync("dir/empty");
|
14 | fse.outputFileSync("dir/empty.txt", "");
|
15 | fse.outputFileSync("dir/file.txt", "abc");
|
16 | fse.outputFileSync("dir/subdir/file.txt", "defg");
|
17 | };
|
18 |
|
19 | const expectations = (data: string[]) => {
|
20 | expect(data).to.eql(["empty", "empty.txt", "file.txt", "subdir"]);
|
21 | };
|
22 |
|
23 | it("sync", () => {
|
24 | preparations();
|
25 | expectations(jetpack.list("dir"));
|
26 | });
|
27 |
|
28 | it("async", done => {
|
29 | preparations();
|
30 | jetpack.listAsync("dir").then(listAsync => {
|
31 | expectations(listAsync);
|
32 | done();
|
33 | });
|
34 | });
|
35 | });
|
36 |
|
37 | describe("lists CWD if no path parameter passed", () => {
|
38 | const preparations = () => {
|
39 | fse.mkdirsSync("dir/a");
|
40 | fse.outputFileSync("dir/b", "");
|
41 | };
|
42 |
|
43 | const expectations = (data: string[]) => {
|
44 | expect(data).to.eql(["a", "b"]);
|
45 | };
|
46 |
|
47 | it("sync", () => {
|
48 | const jetContext = jetpack.cwd("dir");
|
49 | preparations();
|
50 | expectations(jetContext.list());
|
51 | });
|
52 |
|
53 | it("async", done => {
|
54 | const jetContext = jetpack.cwd("dir");
|
55 | preparations();
|
56 | jetContext.listAsync().then(list => {
|
57 | expectations(list);
|
58 | done();
|
59 | });
|
60 | });
|
61 | });
|
62 |
|
63 | describe("returns undefined if path doesn't exist", () => {
|
64 | const expectations = (data: any) => {
|
65 | expect(data).to.equal(undefined);
|
66 | };
|
67 |
|
68 | it("sync", () => {
|
69 | expectations(jetpack.list("nonexistent"));
|
70 | });
|
71 |
|
72 | it("async", done => {
|
73 | jetpack.listAsync("nonexistent").then(data => {
|
74 | expectations(data);
|
75 | done();
|
76 | });
|
77 | });
|
78 | });
|
79 |
|
80 | describe("throws if given path is not a directory", () => {
|
81 | const preparations = () => {
|
82 | fse.outputFileSync("file.txt", "abc");
|
83 | };
|
84 |
|
85 | const expectations = (err: any) => {
|
86 | expect(err.code).to.equal("ENOTDIR");
|
87 | };
|
88 |
|
89 | it("sync", () => {
|
90 | preparations();
|
91 | try {
|
92 | jetpack.list("file.txt");
|
93 | throw new Error("Expected error to be thrown");
|
94 | } catch (err) {
|
95 | expectations(err);
|
96 | }
|
97 | });
|
98 |
|
99 | it("async", done => {
|
100 | preparations();
|
101 | jetpack.listAsync("file.txt").catch(err => {
|
102 | expectations(err);
|
103 | done();
|
104 | });
|
105 | });
|
106 | });
|
107 |
|
108 | describe("respects internal CWD of jetpack instance", () => {
|
109 | const preparations = () => {
|
110 | fse.outputFileSync("a/b/c.txt", "abc");
|
111 | };
|
112 |
|
113 | const expectations = (data: string[]) => {
|
114 | expect(data).to.eql(["c.txt"]);
|
115 | };
|
116 |
|
117 | it("sync", () => {
|
118 | const jetContext = jetpack.cwd("a");
|
119 | preparations();
|
120 | expectations(jetContext.list("b"));
|
121 | });
|
122 |
|
123 | it("async", done => {
|
124 | const jetContext = jetpack.cwd("a");
|
125 | preparations();
|
126 | jetContext.listAsync("b").then(data => {
|
127 | expectations(data);
|
128 | done();
|
129 | });
|
130 | });
|
131 | });
|
132 |
|
133 | describe("input validation", () => {
|
134 | const tests = [
|
135 | { type: "sync", method: jetpack.list as any, methodName: "list" },
|
136 | {
|
137 | type: "async",
|
138 | method: jetpack.listAsync as any,
|
139 | methodName: "listAsync"
|
140 | }
|
141 | ];
|
142 |
|
143 | describe('"path" argument', () => {
|
144 | tests.forEach(test => {
|
145 | it(test.type, () => {
|
146 | expect(() => {
|
147 | test.method(true);
|
148 | }).to.throw(
|
149 | `Argument "path" passed to ${
|
150 | test.methodName
|
151 | }(path) must be a string or an undefined. Received boolean`
|
152 | );
|
153 | });
|
154 | });
|
155 | });
|
156 | });
|
157 | });
|