2022-01-27 13:44:35 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
2024-07-07 10:07:09 +02:00
|
|
|
|
2024-10-26 14:56:17 +02:00
|
|
|
"github.com/semaphoreui/semaphore/util"
|
2024-11-23 12:23:58 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-27 13:44:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepository_GetSchema(t *testing.T) {
|
|
|
|
repo := Repository{GitURL: "https://example.com/hello/world"}
|
2022-03-30 17:31:00 +02:00
|
|
|
schema := repo.GetType()
|
2024-11-23 12:23:58 +01:00
|
|
|
assert.Equal(t, RepositoryHTTP, schema)
|
2022-01-27 13:44:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_ClearCache(t *testing.T) {
|
|
|
|
util.Config = &util.ConfigType{
|
|
|
|
TmpPath: path.Join(os.TempDir(), util.RandString(rand.Intn(10-4)+4)),
|
|
|
|
}
|
|
|
|
repoDir := path.Join(util.Config.TmpPath, "repository_123_55")
|
|
|
|
err := os.MkdirAll(repoDir, 0755)
|
2024-11-23 12:23:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-27 13:44:35 +01:00
|
|
|
repo := Repository{ID: 123}
|
|
|
|
err = repo.ClearCache()
|
2024-11-23 12:23:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-27 13:44:35 +01:00
|
|
|
_, err = os.Stat(repoDir)
|
2024-11-23 12:23:58 +01:00
|
|
|
require.Error(t, err, "repo directory not deleted")
|
|
|
|
assert.True(t, os.IsNotExist(err))
|
2022-01-27 13:44:35 +01:00
|
|
|
}
|
2024-07-07 10:07:09 +02:00
|
|
|
|
|
|
|
func TestRepository_GetGitURL(t *testing.T) {
|
|
|
|
for _, v := range []struct {
|
|
|
|
Repository Repository
|
|
|
|
ExpectedGitUrl string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Repository: Repository{GitURL: "https://github.com/user/project.git", SSHKey: AccessKey{
|
|
|
|
Type: AccessKeyLoginPassword,
|
|
|
|
LoginPassword: LoginPassword{
|
|
|
|
Login: "login",
|
|
|
|
Password: "password",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ExpectedGitUrl: "https://login:password@github.com/user/project.git",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Repository: Repository{GitURL: "https://github.com/user/project.git", SSHKey: AccessKey{
|
|
|
|
Type: AccessKeyLoginPassword,
|
|
|
|
LoginPassword: LoginPassword{
|
|
|
|
Password: "password",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ExpectedGitUrl: "https://password@github.com/user/project.git",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
gitUrl := v.Repository.GetGitURL()
|
2024-11-23 12:23:58 +01:00
|
|
|
assert.Equal(t, v.ExpectedGitUrl, gitUrl, "wrong gitUrl")
|
2024-07-07 10:07:09 +02:00
|
|
|
}
|
|
|
|
}
|