Repository navigation
Expand file tree
/
Copy pathclone_reuse_test.v
More file actions
86 lines (77 loc) · 3.74 KB
/
Copy pathclone_reuse_test.v
File metadata and controls
86 lines (77 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module main
import git
import os
fn must_git_clone_reuse(args []string) string {
res := git.Git.exec(args)
if res.exit_code != 0 {
panic('git ${args} failed: ${res.output}')
}
return res.output.trim_space()
}
fn test_same_clone_source_url_matches_github_forms() {
assert same_clone_source_url('https://github.com/vlang/v.git', 'github.com/VLang/v')
assert same_clone_source_url('https://www.github.com/vlang/v/', 'git@github.com:vlang/v.git')
assert same_clone_source_url('https://example.com/team/repo.git',
'https://example.com/team/repo/')
assert !same_clone_source_url('https://example.com/Team/repo', 'https://example.com/team/repo')
}
fn test_clone_from_existing_fetches_latest_origin_branch() {
tmp_dir := os.join_path(os.temp_dir(), 'gitly_clone_reuse_test_${os.getpid()}')
os.rmdir_all(tmp_dir) or {}
os.mkdir_all(tmp_dir)!
defer {
os.rmdir_all(tmp_dir) or {}
}
work_dir := os.join_path(tmp_dir, 'work')
origin_dir := os.join_path(tmp_dir, 'origin.git')
source_dir := os.join_path(tmp_dir, 'source.git')
target_dir := os.join_path(tmp_dir, 'target.git')
must_git_clone_reuse(['init', '--bare', origin_dir])
must_git_clone_reuse(['init', work_dir])
must_git_clone_reuse(['-C', work_dir, 'checkout', '-b', 'main'])
must_git_clone_reuse(['-C', work_dir, 'config', 'user.name', 'Test User'])
must_git_clone_reuse(['-C', work_dir, 'config', 'user.email', 'test@example.com'])
os.write_file(os.join_path(work_dir, 'file.txt'), 'first\n')!
must_git_clone_reuse(['-C', work_dir, 'add', 'file.txt'])
must_git_clone_reuse(['-C', work_dir, 'commit', '-m', 'first'])
must_git_clone_reuse(['-C', work_dir, 'remote', 'add', 'origin', origin_dir])
must_git_clone_reuse(['-C', work_dir, 'push', 'origin', 'main'])
must_git_clone_reuse(['-C', origin_dir, 'symbolic-ref', 'HEAD', 'refs/heads/main'])
must_git_clone_reuse(['clone', '--bare', origin_dir, source_dir])
// State created by Gitly in the reusable repository must not be copied into
// an unrelated project that happens to use the same upstream URL.
must_git_clone_reuse(['-C', work_dir, 'checkout', '-b', 'private-preview'])
os.write_file(os.join_path(work_dir, 'private.txt'), 'not advertised by origin\n')!
must_git_clone_reuse(['-C', work_dir, 'add', 'private.txt'])
must_git_clone_reuse(['-C', work_dir, 'commit', '-m', 'private preview'])
private_sha := must_git_clone_reuse(['-C', work_dir, 'rev-parse', 'HEAD'])
must_git_clone_reuse(['-C', work_dir, 'push', source_dir, 'HEAD:refs/gitly-comparisons/1/2'])
must_git_clone_reuse(['-C', source_dir, 'remote', 'add', 'copied-only', origin_dir])
must_git_clone_reuse(['-C', work_dir, 'checkout', 'main'])
os.write_file(os.join_path(work_dir, 'file.txt'), 'second\n')!
must_git_clone_reuse(['-C', work_dir, 'commit', '-am', 'second'])
must_git_clone_reuse(['-C', work_dir, 'push', 'origin', 'main'])
latest_sha := must_git_clone_reuse(['-C', work_dir, 'rev-parse', 'main'])
source_repo := Repo{
git_dir: source_dir
name: 'source'
user_name: 'alice'
}
mut target_repo := Repo{
git_dir: target_dir
name: 'target'
user_name: 'bob'
clone_url: origin_dir
primary_branch: 'master'
}
assert target_repo.clone_from_existing(source_repo, false) == .reused
assert target_repo.status == .done
assert target_repo.primary_branch == 'main'
assert repo_origin_url(target_dir) or { '' } == origin_dir
assert must_git_clone_reuse(['-C', target_dir, 'rev-parse', 'main']) == latest_sha
assert must_git_clone_reuse(['-C', target_dir, 'remote']) == 'origin'
assert git.Git.exec_in_dir(target_dir, ['show-ref', '--verify', '--quiet',
'refs/gitly-comparisons/1/2']).exit_code != 0
assert git.Git.exec_in_dir(target_dir, ['cat-file', '-e', private_sha]).exit_code != 0
assert !os.exists(target_repo.clone_progress_path())
}