53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const test = require('node:test');
|
|
|
|
const gitrepo = require('../src/lib/gitrepo');
|
|
const gitref = require('../src/lib/gitref');
|
|
|
|
test('assertSafeBranchRef 接受一般分支名稱', () => {
|
|
assert.equal(gitref.assertSafeBranchRef('feature/review-123', 'baseRef'), 'feature/review-123');
|
|
});
|
|
|
|
test('assertSafeBranchRef 拒絕路徑穿越分支名稱', () => {
|
|
assert.throws(
|
|
() => gitref.assertSafeBranchRef('../../hooks/pre-push', 'baseRef'),
|
|
/不是安全的分支名稱/,
|
|
);
|
|
});
|
|
|
|
test('assertSafeBranchRef 拒絕空白分支名稱', () => {
|
|
for (const value of ['', ' ', null, undefined]) {
|
|
assert.throws(
|
|
() => gitref.assertSafeBranchRef(value, 'baseRef'),
|
|
/baseRef 不可為空/,
|
|
);
|
|
}
|
|
});
|
|
|
|
test('assertSafeBranchRef 拒絕不安全的 refspec 分支名稱', () => {
|
|
for (const value of ['/feature', 'feature/', 'feature\\x', 'feature..x']) {
|
|
assert.throws(
|
|
() => gitref.assertSafeBranchRef(value, 'baseRef'),
|
|
/不是安全的分支名稱/,
|
|
);
|
|
}
|
|
});
|
|
|
|
test('assertSafeBranchRef 拒絕 git 不合法的分支名稱', () => {
|
|
for (const value of ['feature.lock', 'feature@{x}', 'feature~x', 'feature^x']) {
|
|
assert.throws(
|
|
() => gitref.assertSafeBranchRef(value, 'baseRef'),
|
|
/不是合法的 git 分支名稱/,
|
|
);
|
|
}
|
|
});
|
|
|
|
test('resolveMergeBase 會在 git fetch 前拒絕不安全 baseRef', () => {
|
|
assert.throws(
|
|
() => gitrepo.resolveMergeBase(process.cwd(), '../../hooks/pre-push'),
|
|
/不是安全的分支名稱/,
|
|
);
|
|
});
|