Skip to content

Commit 08d04ef

Browse files
author
Simon Coffey
authored
Use dynamically-created repo for signed commits test (#614)
This replaces the test repo introduced in #610 with one created on the fly in the test. I've switched from GPG to SSH signing to minimise the likelihood that we'll need extra dependencies on CI or other contributors' machines. I've also removed some of the commit metadata assertions that didn't feel particularly necessary, and this allowed me to reduce the setup steps for clarity. Signed-off-by: Simon Coffey <simon.coffey@futurelearn.com>
1 parent 354412e commit 08d04ef

File tree

11 files changed

+22
-56
lines changed

11 files changed

+22
-56
lines changed

tests/files/signed_commits/dot_git/HEAD

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/files/signed_commits/dot_git/config

Lines changed: 0 additions & 7 deletions
This file was deleted.
-137 Bytes
Binary file not shown.

tests/files/signed_commits/dot_git/logs/HEAD

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/files/signed_commits/dot_git/logs/refs/heads/main

Lines changed: 0 additions & 1 deletion
This file was deleted.
Binary file not shown.
Binary file not shown.
Binary file not shown.

tests/files/signed_commits/dot_git/refs/heads/main

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/files/signed_commits/notes.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/units/test_signed_commits.rb

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,33 @@
44
require "fileutils"
55

66
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
1521
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
3422
end
3523

3624
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"`
3829

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')
4331

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
5735
end
5836
end

0 commit comments

Comments
 (0)