-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsourcehut_test.go
More file actions
152 lines (118 loc) · 5.21 KB
/
sourcehut_test.go
File metadata and controls
152 lines (118 loc) · 5.21 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package githosts
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
const (
sourcehutEnvVarToken = "SOURCEHUT_PAT" //nolint:gosec
)
func TestPublicsourcehutRepositoryBackupCloneMethod(t *testing.T) {
resetBackups()
if os.Getenv(sourcehutEnvVarToken) == "" {
t.Skip("Skipping sourcehut test as SOURCEHUT_PAT is missing")
}
envBackup := backupEnvironmentVariables()
defer restoreEnvironmentVariables(envBackup)
unsetEnvVars([]string{envVarGitBackupDir, sourcehutEnvVarToken})
backupDIR := os.Getenv(envVarGitBackupDir)
gl, err := NewSourcehutHost(NewSourcehutHostInput{
APIURL: sourcehutAPIURL,
DiffRemoteMethod: cloneMethod,
BackupDir: backupDIR,
PersonalAccessToken: os.Getenv(sourcehutEnvVarToken),
})
require.NoError(t, err)
gl.Backup()
// Test that the public repository (sobaOne) was backed up successfully
expectedSobaOnePath := filepath.Join(backupDIR, "sourcehut", "jonhadfield", "sobaOne")
require.DirExists(t, expectedSobaOnePath)
projectOneEntries, err := dirContents(expectedSobaOnePath)
require.NoError(t, err)
require.Len(t, projectOneEntries, 1)
require.Contains(t, projectOneEntries[0].Name(), "sobaOne.")
// Verify repository contents by extracting the bundle and checking for expected files
bundlePath := projectOneEntries[0]
tempExtractDir := filepath.Join(os.TempDir(), "sobaOne_extract_test")
defer func() {
if cleanupErr := os.RemoveAll(tempExtractDir); cleanupErr != nil {
t.Logf("Warning: failed to cleanup temp directory: %v", cleanupErr)
}
}()
// Extract the bundle to temporary directory
err = extractBundleToTemp(bundlePath.Name(), expectedSobaOnePath, tempExtractDir)
require.NoError(t, err)
// Check that LICENSE and README.md exist in the repository root
licensePath := filepath.Join(tempExtractDir, "LICENSE")
readmePath := filepath.Join(tempExtractDir, "README.md")
require.FileExists(t, licensePath, "LICENSE file should exist in sobaOne repository")
require.FileExists(t, readmePath, "README.md file should exist in sobaOne repository")
// Note: sobaTwo is private and will be automatically skipped
// SourceHut private repositories cannot be cloned via HTTPS with personal access tokens
// Only public repositories are backed up due to authentication limitations
t.Logf("Public repository sobaOne backed up successfully and contains expected files")
}
func TestPublicsourcehutRepositoryBackupRefsMethod(t *testing.T) {
resetBackups()
if os.Getenv(sourcehutEnvVarToken) == "" {
t.Skip("Skipping sourcehut test as SOURCEHUT_PAT is missing")
}
envBackup := backupEnvironmentVariables()
defer restoreEnvironmentVariables(envBackup)
unsetEnvVars([]string{envVarGitBackupDir, sourcehutEnvVarToken})
backupDIR := os.Getenv(envVarGitBackupDir)
gl, err := NewSourcehutHost(NewSourcehutHostInput{
APIURL: sourcehutAPIURL,
DiffRemoteMethod: refsMethod,
BackupDir: backupDIR,
PersonalAccessToken: os.Getenv(sourcehutEnvVarToken),
LogLevel: 1,
})
require.NoError(t, err)
gl.Backup()
// Test that the public repository (sobaOne) was backed up successfully
expectedSobaOnePath := filepath.Join(backupDIR, "sourcehut", "jonhadfield", "sobaOne")
require.DirExists(t, expectedSobaOnePath)
projectOneEntries, err := dirContents(expectedSobaOnePath)
require.NoError(t, err)
require.Len(t, projectOneEntries, 1)
require.Contains(t, projectOneEntries[0].Name(), "sobaOne.")
// Verify repository contents by extracting the bundle and checking for expected files
bundlePath := projectOneEntries[0]
tempExtractDir := filepath.Join(os.TempDir(), "sobaOne_extract_test_refs")
defer func() {
if cleanupErr := os.RemoveAll(tempExtractDir); cleanupErr != nil {
t.Logf("Warning: failed to cleanup temp directory: %v", cleanupErr)
}
}()
// Extract the bundle to temporary directory
err = extractBundleToTemp(bundlePath.Name(), expectedSobaOnePath, tempExtractDir)
require.NoError(t, err)
// Check that LICENSE and README.md exist in the repository root
licensePath := filepath.Join(tempExtractDir, "LICENSE")
readmePath := filepath.Join(tempExtractDir, "README.md")
require.FileExists(t, licensePath, "LICENSE file should exist in sobaOne repository")
require.FileExists(t, readmePath, "README.md file should exist in sobaOne repository")
// Note: sobaTwo is private and will be automatically skipped
// SourceHut private repositories cannot be cloned via HTTPS with personal access tokens
t.Logf("Public repository sobaOne backed up successfully with refs method and contains expected files")
}
// extractBundleToTemp extracts a git bundle to a temporary directory for content verification
func extractBundleToTemp(bundleFileName, bundleDir, tempDir string) error {
// Create temporary directory
if err := os.MkdirAll(tempDir, 0o755); err != nil {
return fmt.Errorf("failed to create temp directory: %w", err)
}
// Full path to the bundle file
bundlePath := filepath.Join(bundleDir, bundleFileName)
// Use git clone to extract bundle contents
cloneCmd := exec.Command("git", "clone", bundlePath, tempDir)
output, err := cloneCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to clone bundle %s: %s - %w", bundlePath, string(output), err)
}
return nil
}