Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dynamically-created repo for signed commits test #614

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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