Skip to content

Commit

Permalink
Use dynamically-created repo for signed commits test (#614)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Simon Coffey committed Feb 5, 2023
1 parent 354412e commit 08d04ef
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 56 deletions.
1 change: 0 additions & 1 deletion tests/files/signed_commits/dot_git/HEAD

This file was deleted.

7 changes: 0 additions & 7 deletions tests/files/signed_commits/dot_git/config

This file was deleted.

Binary file removed tests/files/signed_commits/dot_git/index
Binary file not shown.
1 change: 0 additions & 1 deletion tests/files/signed_commits/dot_git/logs/HEAD

This file was deleted.

1 change: 0 additions & 1 deletion tests/files/signed_commits/dot_git/logs/refs/heads/main

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion tests/files/signed_commits/dot_git/refs/heads/main

This file was deleted.

1 change: 0 additions & 1 deletion tests/files/signed_commits/notes.txt

This file was deleted.

66 changes: 22 additions & 44 deletions tests/units/test_signed_commits.rb
Expand Up @@ -4,55 +4,33 @@
require "fileutils"

class TestSignedCommits < Test::Unit::TestCase
def git_working_dir
cwd = FileUtils.pwd
if File.directory?(File.join(cwd, 'files'))
test_dir = File.join(cwd, 'files')
elsif File.directory?(File.join(cwd, '..', 'files'))
test_dir = File.join(cwd, '..', 'files')
elsif File.directory?(File.join(cwd, 'tests', 'files'))
test_dir = File.join(cwd, 'tests', 'files')
SSH_SIGNATURE_REGEXP = Regexp.new(<<~EOS.chomp, Regexp::MULTILINE)
-----BEGIN SSH SIGNATURE-----
.*
-----END SSH SIGNATURE-----
EOS

def in_repo_with_signing_config(&block)
in_temp_dir do |path|
`git init`
`ssh-keygen -t dsa -N "" -C "test key" -f .git/test-key`
`git config --local gpg.format ssh`
`git config --local user.signingkey .git/test-key`

yield
end

create_temp_repo(File.expand_path(File.join(test_dir, 'signed_commits')))
end

def create_temp_repo(clone_path)
filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
@tmp_path = File.join("/tmp/", filename)
FileUtils.mkdir_p(@tmp_path)
FileUtils.cp_r(clone_path, @tmp_path)
tmp_path = File.join(@tmp_path, File.basename(clone_path))
Dir.chdir(tmp_path) do
FileUtils.mv('dot_git', '.git')
end
tmp_path
end

def setup
@lib = Git.open(git_working_dir).lib
end

def test_commit_data
data = @lib.commit_data('a043c677c93d9f2b')
in_repo_with_signing_config do
create_file('README.md', '# My Project')
`git add README.md`
`git commit -S -m "Signed, sealed, delivered"`

assert_equal('Simon Coffey <simon.coffey@futurelearn.com> 1673868871 +0000', data['author'])
assert_equal('92fd5b7c1aeb6a4e2602799f01608b3deebbad2d', data['tree'])
assert_equal(<<~EOS.chomp, data['gpgsig'])
-----BEGIN PGP SIGNATURE-----
data = Git.open('.').lib.commit_data('HEAD')

iHUEABYKAB0WIQRmiEtd91BkbBpcgV2yCJ+VnJz/iQUCY8U2cgAKCRCyCJ+VnJz/
ibjyAP48dGdoFgWL2BjV3CnmebdVjEjTCQtF2QGUybJsyJhhcwEAwbzAAGt3YHfS
uuLNH9ki9Sqd+/CH+L8Q2dPM5F4l3gg=
=3ATn
-----END PGP SIGNATURE-----
EOS
assert_equal(<<~EOS, data['message'])
Signed commit
This will allow me to test commit data extraction for signed commits.
I'm making the message multiline to make sure that message extraction is
preserved.
EOS
assert_match(SSH_SIGNATURE_REGEXP, data['gpgsig'])
assert_equal("Signed, sealed, delivered\n", data['message'])
end
end
end

0 comments on commit 08d04ef

Please sign in to comment.