Swap-Aggregator-Subgraph/node_modules/fs-jetpack/spec/write_atomic.spec.ts
Richa-iitr d211083153 Revert "Revert "added handler""
This reverts commit c36ee8c5ca.
2022-07-03 07:30:05 +05:30

85 lines
2.1 KiB
TypeScript

import * as fse from "fs-extra";
import { expect } from "chai";
import path from "./assert_path";
import helper from "./helper";
import * as jetpack from "..";
describe("atomic write", () => {
const filePath = "file.txt";
const tempPath = `${filePath}.__new__`;
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
describe("fresh write (file doesn't exist yet)", () => {
const expectations = () => {
path(filePath).shouldBeFileWithContent("abc");
path(tempPath).shouldNotExist();
};
it("sync", () => {
jetpack.write(filePath, "abc", { atomic: true });
expectations();
});
it("async", done => {
jetpack.writeAsync(filePath, "abc", { atomic: true }).then(() => {
expectations();
done();
});
});
});
describe("overwrite existing file", () => {
const preparations = () => {
fse.outputFileSync(filePath, "xyz");
};
const expectations = () => {
path(filePath).shouldBeFileWithContent("abc");
path(tempPath).shouldNotExist();
};
it("sync", () => {
preparations();
jetpack.write(filePath, "abc", { atomic: true });
expectations();
});
it("async", done => {
preparations();
jetpack.writeAsync(filePath, "abc", { atomic: true }).then(() => {
expectations();
done();
});
});
});
describe("if previous operation failed", () => {
const preparations = () => {
fse.outputFileSync(filePath, "xyz");
// Simulating remained file from interrupted previous write attempt.
fse.outputFileSync(tempPath, "123");
};
const expectations = () => {
path(filePath).shouldBeFileWithContent("abc");
path(tempPath).shouldNotExist();
};
it("sync", () => {
preparations();
jetpack.write(filePath, "abc", { atomic: true });
expectations();
});
it("async", done => {
preparations();
jetpack.writeAsync(filePath, "abc", { atomic: true }).then(() => {
expectations();
done();
});
});
});
});