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("atomic write", () => {
|
8 | const filePath = "file.txt";
|
9 | const tempPath = `${filePath}.__new__`;
|
10 |
|
11 | beforeEach(helper.setCleanTestCwd);
|
12 | afterEach(helper.switchBackToCorrectCwd);
|
13 |
|
14 | describe("fresh write (file doesn't exist yet)", () => {
|
15 | const expectations = () => {
|
16 | path(filePath).shouldBeFileWithContent("abc");
|
17 | path(tempPath).shouldNotExist();
|
18 | };
|
19 |
|
20 | it("sync", () => {
|
21 | jetpack.write(filePath, "abc", { atomic: true });
|
22 | expectations();
|
23 | });
|
24 |
|
25 | it("async", done => {
|
26 | jetpack.writeAsync(filePath, "abc", { atomic: true }).then(() => {
|
27 | expectations();
|
28 | done();
|
29 | });
|
30 | });
|
31 | });
|
32 |
|
33 | describe("overwrite existing file", () => {
|
34 | const preparations = () => {
|
35 | fse.outputFileSync(filePath, "xyz");
|
36 | };
|
37 |
|
38 | const expectations = () => {
|
39 | path(filePath).shouldBeFileWithContent("abc");
|
40 | path(tempPath).shouldNotExist();
|
41 | };
|
42 |
|
43 | it("sync", () => {
|
44 | preparations();
|
45 | jetpack.write(filePath, "abc", { atomic: true });
|
46 | expectations();
|
47 | });
|
48 |
|
49 | it("async", done => {
|
50 | preparations();
|
51 | jetpack.writeAsync(filePath, "abc", { atomic: true }).then(() => {
|
52 | expectations();
|
53 | done();
|
54 | });
|
55 | });
|
56 | });
|
57 |
|
58 | describe("if previous operation failed", () => {
|
59 | const preparations = () => {
|
60 | fse.outputFileSync(filePath, "xyz");
|
61 |
|
62 | fse.outputFileSync(tempPath, "123");
|
63 | };
|
64 |
|
65 | const expectations = () => {
|
66 | path(filePath).shouldBeFileWithContent("abc");
|
67 | path(tempPath).shouldNotExist();
|
68 | };
|
69 |
|
70 | it("sync", () => {
|
71 | preparations();
|
72 | jetpack.write(filePath, "abc", { atomic: true });
|
73 | expectations();
|
74 | });
|
75 |
|
76 | it("async", done => {
|
77 | preparations();
|
78 | jetpack.writeAsync(filePath, "abc", { atomic: true }).then(() => {
|
79 | expectations();
|
80 | done();
|
81 | });
|
82 | });
|
83 | });
|
84 | });
|