Merge pull request #1363 from log4js-node/fix/tilde-expansion

fix: tilde expansion for windows
This commit is contained in:
Lam Wei Li 2023-02-20 02:21:44 +08:00 committed by GitHub
commit b4c75d51e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 6 deletions

View File

@ -38,10 +38,10 @@ function fileAppender(
throw new Error(`Invalid filename: ${file}`);
} else if (file.endsWith(path.sep)) {
throw new Error(`Filename is a directory: ${file}`);
} else {
} else if (file.indexOf(`~${path.sep}`) === 0) {
// handle ~ expansion: https://github.com/nodejs/node/issues/684
// exclude ~ and ~filename as these can be valid files
file = file.replace(new RegExp(`^~(?=${path.sep}.+)`), os.homedir());
file = file.replace('~', os.homedir());
}
file = path.normalize(file);
numBackups = !numBackups && numBackups !== 0 ? 5 : numBackups;

View File

@ -183,10 +183,10 @@ function fileAppender(
throw new Error(`Invalid filename: ${file}`);
} else if (file.endsWith(path.sep)) {
throw new Error(`Filename is a directory: ${file}`);
} else {
} else if (file.indexOf(`~${path.sep}`) === 0) {
// handle ~ expansion: https://github.com/nodejs/node/issues/684
// exclude ~ and ~filename as these can be valid files
file = file.replace(new RegExp(`^~(?=${path.sep}.+)`), os.homedir());
file = file.replace('~', os.homedir());
}
file = path.normalize(file);
numBackups = !numBackups && numBackups !== 0 ? 5 : numBackups;

View File

@ -7,7 +7,7 @@ const clustering = require('./clustering');
const categories = require('./categories');
const configuration = require('./configuration');
const stackReg = /at (?:(.+)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;
const stackReg = /^(?:\s*) at (?:(.+) \()?(?:([^(]+?):(\d+):(\d+))\)?$/;
/**
* The top entry is the Error
*/
@ -36,7 +36,7 @@ function defaultParseCallStack(
}
const lineMatch = stackReg.exec(stacklines[0]);
/* istanbul ignore else: failsafe */
if (lineMatch && lineMatch.length === 6) {
if (lineMatch && lineMatch.length === 5) {
// extract class, function and alias names
let className = '';
let functionName = '';

View File

@ -52,6 +52,40 @@ test('log4js fileAppender', (batch) => {
t.end();
});
batch.test('with tilde expansion in filename', async (t) => {
const fileName = 'tmpTilde.log';
const expandedPath = path.join(__dirname, fileName);
await removeFile(expandedPath);
const sandboxedLog4js = sandbox.require('../../lib/log4js', {
requires: {
os: {
homedir() {
return __dirname;
},
},
},
});
t.teardown(async () => {
await new Promise((resolve) => {
sandboxedLog4js.shutdown(resolve);
});
await removeFile(expandedPath);
});
sandboxedLog4js.configure({
appenders: { file: { type: 'file', filename: path.join('~', fileName) } },
categories: { default: { appenders: ['file'], level: 'debug' } },
});
t.ok(
fs.existsSync(expandedPath),
'should expand tilde to create in home directory'
);
t.end();
});
batch.test('should give error if invalid filename', async (t) => {
const file = '';
t.throws(

View File

@ -40,6 +40,41 @@ test('log4js fileSyncAppender', (batch) => {
});
});
batch.test('with tilde expansion in filename', (t) => {
const fileName = 'tmpTilde.log';
const expandedPath = path.join(__dirname, fileName);
remove(expandedPath);
const sandboxedLog4js = sandbox.require('../../lib/log4js', {
requires: {
os: {
homedir() {
return __dirname;
},
},
},
});
t.teardown(() => {
log4js.shutdown(() => {
remove(expandedPath);
});
});
sandboxedLog4js.configure({
appenders: {
sync: { type: 'fileSync', filename: path.join('~', fileName) },
},
categories: { default: { appenders: ['sync'], level: 'debug' } },
});
t.ok(
fs.existsSync(expandedPath),
'should expand tilde to create in home directory'
);
t.end();
});
batch.test('with existing file', (t) => {
const testFile = path.join(__dirname, '/fa-existing-file-sync-test.log');
const logger = log4js.getLogger('default-settings');