|
1 | 1 | import { test } from 'tap' |
2 | | -import { readFileSync } from 'node:fs' |
3 | | -import { spawn } from 'node:child_process' |
| 2 | +import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs' |
| 3 | +import { execFileSync, spawn } from 'node:child_process' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import { join } from 'node:path' |
| 6 | +import { fileURLToPath } from 'node:url' |
4 | 7 | import subsystems from '../lib/rules/subsystem.js' |
5 | 8 |
|
| 9 | +const cmdPath = fileURLToPath(new URL('../bin/cmd.js', import.meta.url)) |
| 10 | + |
| 11 | +function git (cwd, args, env = undefined) { |
| 12 | + return execFileSync('git', args, { |
| 13 | + cwd, |
| 14 | + env: env === undefined ? process.env : { ...process.env, ...env }, |
| 15 | + encoding: 'utf8' |
| 16 | + }) |
| 17 | +} |
| 18 | + |
6 | 19 | test('Test cli flags', (t) => { |
7 | 20 | t.test('test list-subsystems', (tt) => { |
8 | 21 | const ls = spawn('./bin/cmd.js', ['--list-subsystems'], { |
@@ -358,5 +371,67 @@ test('Test cli flags', (t) => { |
358 | 371 | }) |
359 | 372 | }) |
360 | 373 |
|
| 374 | + t.test('test sha ignores mailmap when validating sign-off', (tt) => { |
| 375 | + tt.plan(6) |
| 376 | + |
| 377 | + const repoDir = mkdtempSync(join(tmpdir(), 'core-validate-commit-')) |
| 378 | + const env = { |
| 379 | + GIT_AUTHOR_NAME: 'Matteo Collina', |
| 380 | + GIT_AUTHOR_EMAIL: 'hello@matteocollina.com', |
| 381 | + GIT_COMMITTER_NAME: 'Matteo Collina', |
| 382 | + GIT_COMMITTER_EMAIL: 'hello@matteocollina.com' |
| 383 | + } |
| 384 | + |
| 385 | + git(repoDir, ['init', '-b', 'main']) |
| 386 | + git(repoDir, ['config', 'user.name', 'Test User']) |
| 387 | + git(repoDir, ['config', 'user.email', 'test@example.com']) |
| 388 | + |
| 389 | + writeFileSync(join(repoDir, '.mailmap'), |
| 390 | + 'Matteo Collina <matteo.collina@gmail.com> <hello@matteocollina.com>\n') |
| 391 | + writeFileSync(join(repoDir, 'README.md'), 'test\n') |
| 392 | + |
| 393 | + git(repoDir, ['add', '.mailmap', 'README.md']) |
| 394 | + git(repoDir, ['commit', '-m', |
| 395 | + 'doc: add mailmap sign-off fixture\n\n' + |
| 396 | + 'Signed-off-by: Matteo Collina <hello@matteocollina.com>\n' + |
| 397 | + 'PR-URL: https://github.com/nodejs/node/pull/1234\n' + |
| 398 | + 'Reviewed-By: Rich Trott <rtrott@gmail.com>' |
| 399 | + ], env) |
| 400 | + |
| 401 | + const defaultShow = git(repoDir, ['show', '--quiet', '--format=medium', 'HEAD']) |
| 402 | + const rawShow = git(repoDir, ['show', '--no-mailmap', '--quiet', '--format=medium', 'HEAD']) |
| 403 | + |
| 404 | + tt.match(defaultShow, |
| 405 | + /Author:\s+Matteo Collina <matteo\.collina@gmail\.com>/, |
| 406 | + 'git show applies mailmap by default') |
| 407 | + tt.match(rawShow, |
| 408 | + /Author:\s+Matteo Collina <hello@matteocollina\.com>/, |
| 409 | + 'git show --no-mailmap preserves the raw author email') |
| 410 | + |
| 411 | + const ls = spawn(process.execPath, [cmdPath, '--no-validate-metadata', 'HEAD'], { |
| 412 | + cwd: repoDir, |
| 413 | + env: { ...process.env, FORCE_COLOR: 0 } |
| 414 | + }) |
| 415 | + let compiledData = '' |
| 416 | + let errorData = '' |
| 417 | + |
| 418 | + ls.stdout.on('data', (data) => { |
| 419 | + compiledData += data |
| 420 | + }) |
| 421 | + |
| 422 | + ls.stderr.on('data', (data) => { |
| 423 | + errorData += data |
| 424 | + }) |
| 425 | + |
| 426 | + ls.on('close', (code) => { |
| 427 | + tt.equal(code, 0, 'CLI exits with zero code on success') |
| 428 | + tt.equal(errorData, '', 'no error output') |
| 429 | + tt.match(compiledData, /has valid Signed-off-by/, 'sign-off remains valid') |
| 430 | + tt.notMatch(compiledData, |
| 431 | + /email does not match the commit author email/, |
| 432 | + 'mailmap does not trigger a false mismatch warning') |
| 433 | + }) |
| 434 | + }) |
| 435 | + |
361 | 436 | t.end() |
362 | 437 | }) |
0 commit comments