Huáng Jùnliàng 487f10f84d fix: disable caching when babel could not read/write cache (#10557)
* fix: disable caching when babel could not read/write cache

* emit warning when cache folder resides in readonly fs

* fix: always register save handler

* cache: maintain old behaviour

* test: add more test case

* fix: next tick tasks are FIFO

* test: disable test on Windows
2019-10-17 09:30:01 +02:00

112 lines
2.5 KiB
JavaScript

import fs from "fs";
import path from "path";
const testCacheFilename = path.join(__dirname, ".babel");
const oldBabelDisableCacheValue = process.env.BABEL_DISABLE_CACHE;
process.env.BABEL_CACHE_PATH = testCacheFilename;
delete process.env.BABEL_DISABLE_CACHE;
function writeCache(data, mode = 0o666) {
if (typeof data === "object") {
data = JSON.stringify(data);
}
fs.writeFileSync(testCacheFilename, data, { mode });
}
function cleanCache() {
try {
fs.unlinkSync(testCacheFilename);
} catch (e) {
// It is convenient to always try to clear
}
}
function resetCache() {
process.env.BABEL_CACHE_PATH = null;
process.env.BABEL_DISABLE_CACHE = oldBabelDisableCacheValue;
}
describe("@babel/register - caching", () => {
describe("cache", () => {
let load, get, save;
beforeEach(() => {
// Since lib/cache is a singleton we need to fully reload it
jest.resetModules();
const cache = require("../lib/cache");
load = cache.load;
get = cache.get;
save = cache.save;
});
afterEach(cleanCache);
afterAll(resetCache);
it("should load and get cached data", () => {
writeCache({ foo: "bar" });
load();
expect(get()).toEqual({ foo: "bar" });
});
it("should load and get an object with no cached data", () => {
load();
expect(get()).toEqual({});
});
it("should load and get an object with invalid cached data", () => {
writeCache("foobar");
load();
expect(get()).toEqual({});
});
it("should create the cache on save", () => {
save();
expect(fs.existsSync(testCacheFilename)).toBe(true);
expect(get()).toEqual({});
});
it("should create the cache after load", cb => {
load();
process.nextTick(() => {
expect(fs.existsSync(testCacheFilename)).toBe(true);
expect(get()).toEqual({});
cb();
});
});
// Only write mode is effective on Windows
if (process.platform !== "win32") {
it("should be disabled when CACHE_PATH is not allowed to read", () => {
writeCache({ foo: "bar" }, 0o266);
load();
expect(get()).toEqual({});
});
}
it("should be disabled when CACHE_PATH is not allowed to write", cb => {
writeCache({ foo: "bar" }, 0o466);
load();
expect(get()).toEqual({ foo: "bar" });
process.nextTick(() => {
load();
expect(get()).toEqual({});
cb();
});
});
});
});