临时文件兜底
This commit is contained in:
1 parent
08456f26cf
commit
be058c6c80
3 files changed
+52
-3
No files matched your search
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { cleanupStaleFiles } = require('../js/library-watcher.js');
|
||||
|
||||
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'spine-thumb-cleanup-'));
|
||||
const now = Date.now();
|
||||
const old = path.join(directory, 'spine-thumb-old.webp');
|
||||
const fresh = path.join(directory, 'spine-thumb-fresh.webp');
|
||||
const unrelated = path.join(directory, 'other-old.webp');
|
||||
|
||||
try {
|
||||
for (const file of [old, fresh, unrelated]) fs.writeFileSync(file, 'x');
|
||||
const oldTime = new Date(now - 25 * 60 * 60 * 1000);
|
||||
fs.utimesSync(old, oldTime, oldTime);
|
||||
fs.utimesSync(unrelated, oldTime, oldTime);
|
||||
|
||||
assert.equal(cleanupStaleFiles(directory, 'spine-thumb-', 24 * 60 * 60 * 1000, now), 1);
|
||||
assert.equal(fs.existsSync(old), false);
|
||||
assert.equal(fs.existsSync(fresh), true);
|
||||
assert.equal(fs.existsSync(unrelated), true);
|
||||
console.log('PASS: 只清理超过 24 小时的本插件临时文件');
|
||||
} finally {
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
Reference in new issue
Block a user