UNPKG

3.89 kBPlain TextView Raw
1import * as fse from "fs-extra";
2import { expect } from "chai";
3import * as pathUtil from "path";
4import * as os from "os";
5import path from "./assert_path";
6import helper from "./helper";
7import * as jetpack from "..";
8import { FSJetpack } from "../types";
9
10describe("tmpDir", () => {
11 beforeEach(helper.setCleanTestCwd);
12 afterEach(helper.switchBackToCorrectCwd);
13
14 describe("creates temporary directory", () => {
15 const expectations = (jetpackContext: FSJetpack) => {
16 path(jetpackContext.path()).shouldBeDirectory();
17 expect(jetpackContext.path().startsWith(os.tmpdir())).to.equal(true);
18 expect(jetpackContext.path()).to.match(/(\/|\\)[0-9a-f]+$/);
19 };
20
21 it("sync", () => {
22 expectations(jetpack.tmpDir());
23 });
24
25 it("async", done => {
26 jetpack
27 .tmpDirAsync()
28 .then(jetpackContext => {
29 expectations(jetpackContext);
30 done();
31 })
32 .catch(done);
33 });
34 });
35
36 describe("directory name can be prefixed", () => {
37 const expectations = (jetpackContext: FSJetpack) => {
38 path(jetpackContext.path()).shouldBeDirectory();
39 expect(jetpackContext.path().startsWith(os.tmpdir())).to.equal(true);
40 expect(jetpackContext.path()).to.match(/(\/|\\)abc_[0-9a-f]+$/);
41 };
42
43 it("sync", () => {
44 expectations(jetpack.tmpDir({ prefix: "abc_" }));
45 });
46
47 it("async", done => {
48 jetpack
49 .tmpDirAsync({ prefix: "abc_" })
50 .then(jetpackContext => {
51 expectations(jetpackContext);
52 done();
53 })
54 .catch(done);
55 });
56 });
57
58 describe("directory can be created in any base directory", () => {
59 const expectations = (jetpackContext: FSJetpack) => {
60 path(jetpackContext.path()).shouldBeDirectory();
61 expect(jetpackContext.path().startsWith(jetpack.cwd())).to.equal(true);
62 };
63
64 it("sync", () => {
65 expectations(jetpack.tmpDir({ basePath: "." }));
66 });
67
68 it("async", done => {
69 jetpack
70 .tmpDirAsync({ basePath: "." })
71 .then(jetpackContext => {
72 expectations(jetpackContext);
73 done();
74 })
75 .catch(done);
76 });
77 });
78
79 describe("if base directory doesn't exist it will be created", () => {
80 const expectations = (jetpackContext: FSJetpack) => {
81 path(jetpackContext.path()).shouldBeDirectory();
82 expect(jetpackContext.path().startsWith(jetpack.path("abc"))).to.equal(
83 true
84 );
85 };
86
87 it("sync", () => {
88 expectations(jetpack.tmpDir({ basePath: "abc" }));
89 });
90
91 it("async", done => {
92 jetpack
93 .tmpDirAsync({ basePath: "abc" })
94 .then(jetpackContext => {
95 expectations(jetpackContext);
96 done();
97 })
98 .catch(done);
99 });
100 });
101
102 describe("input validation", () => {
103 const tests = [
104 { type: "sync", method: jetpack.tmpDir as any, methodName: "tmpDir" },
105 {
106 type: "async",
107 method: jetpack.tmpDirAsync as any,
108 methodName: "tmpDirAsync"
109 }
110 ];
111
112 describe('"options" object', () => {
113 describe('"prefix" argument', () => {
114 tests.forEach(test => {
115 it(test.type, () => {
116 expect(() => {
117 test.method({ prefix: 1 });
118 }).to.throw(
119 `Argument "options.prefix" passed to ${
120 test.methodName
121 }([options]) must be a string. Received number`
122 );
123 });
124 });
125 });
126 describe('"basePath" argument', () => {
127 tests.forEach(test => {
128 it(test.type, () => {
129 expect(() => {
130 test.method({ basePath: 1 });
131 }).to.throw(
132 `Argument "options.basePath" passed to ${
133 test.methodName
134 }([options]) must be a string. Received number`
135 );
136 });
137 });
138 });
139 });
140 });
141});