|
4 | 4 | require "fileutils"
|
5 | 5 |
|
6 | 6 | class TestSignedCommits < Test::Unit::TestCase
|
7 |
| - def git_working_dir |
8 |
| - cwd = FileUtils.pwd |
9 |
| - if File.directory?(File.join(cwd, 'files')) |
10 |
| - test_dir = File.join(cwd, 'files') |
11 |
| - elsif File.directory?(File.join(cwd, '..', 'files')) |
12 |
| - test_dir = File.join(cwd, '..', 'files') |
13 |
| - elsif File.directory?(File.join(cwd, 'tests', 'files')) |
14 |
| - test_dir = File.join(cwd, 'tests', 'files') |
| 7 | + SSH_SIGNATURE_REGEXP = Regexp.new(<<~EOS.chomp, Regexp::MULTILINE) |
| 8 | + -----BEGIN SSH SIGNATURE----- |
| 9 | + .* |
| 10 | + -----END SSH SIGNATURE----- |
| 11 | + EOS |
| 12 | + |
| 13 | + def in_repo_with_signing_config(&block) |
| 14 | + in_temp_dir do |path| |
| 15 | + `git init` |
| 16 | + `ssh-keygen -t dsa -N "" -C "test key" -f .git/test-key` |
| 17 | + `git config --local gpg.format ssh` |
| 18 | + `git config --local user.signingkey .git/test-key` |
| 19 | + |
| 20 | + yield |
15 | 21 | end
|
16 |
| - |
17 |
| - create_temp_repo(File.expand_path(File.join(test_dir, 'signed_commits'))) |
18 |
| - end |
19 |
| - |
20 |
| - def create_temp_repo(clone_path) |
21 |
| - filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0') |
22 |
| - @tmp_path = File.join("/tmp/", filename) |
23 |
| - FileUtils.mkdir_p(@tmp_path) |
24 |
| - FileUtils.cp_r(clone_path, @tmp_path) |
25 |
| - tmp_path = File.join(@tmp_path, File.basename(clone_path)) |
26 |
| - Dir.chdir(tmp_path) do |
27 |
| - FileUtils.mv('dot_git', '.git') |
28 |
| - end |
29 |
| - tmp_path |
30 |
| - end |
31 |
| - |
32 |
| - def setup |
33 |
| - @lib = Git.open(git_working_dir).lib |
34 | 22 | end
|
35 | 23 |
|
36 | 24 | def test_commit_data
|
37 |
| - data = @lib.commit_data('a043c677c93d9f2b') |
| 25 | + in_repo_with_signing_config do |
| 26 | + create_file('README.md', '# My Project') |
| 27 | + `git add README.md` |
| 28 | + `git commit -S -m "Signed, sealed, delivered"` |
38 | 29 |
|
39 |
| - assert_equal('Simon Coffey <simon.coffey@futurelearn.com> 1673868871 +0000', data['author']) |
40 |
| - assert_equal('92fd5b7c1aeb6a4e2602799f01608b3deebbad2d', data['tree']) |
41 |
| - assert_equal(<<~EOS.chomp, data['gpgsig']) |
42 |
| - -----BEGIN PGP SIGNATURE----- |
| 30 | + data = Git.open('.').lib.commit_data('HEAD') |
43 | 31 |
|
44 |
| - iHUEABYKAB0WIQRmiEtd91BkbBpcgV2yCJ+VnJz/iQUCY8U2cgAKCRCyCJ+VnJz/ |
45 |
| - ibjyAP48dGdoFgWL2BjV3CnmebdVjEjTCQtF2QGUybJsyJhhcwEAwbzAAGt3YHfS |
46 |
| - uuLNH9ki9Sqd+/CH+L8Q2dPM5F4l3gg= |
47 |
| - =3ATn |
48 |
| - -----END PGP SIGNATURE----- |
49 |
| - EOS |
50 |
| - assert_equal(<<~EOS, data['message']) |
51 |
| - Signed commit |
52 |
| -
|
53 |
| - This will allow me to test commit data extraction for signed commits. |
54 |
| - I'm making the message multiline to make sure that message extraction is |
55 |
| - preserved. |
56 |
| - EOS |
| 32 | + assert_match(SSH_SIGNATURE_REGEXP, data['gpgsig']) |
| 33 | + assert_equal("Signed, sealed, delivered\n", data['message']) |
| 34 | + end |
57 | 35 | end
|
58 | 36 | end
|
0 commit comments