2022-08-16 00:05:15 -04:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-08-16 00:05:15 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-09-25 13:24:35 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-08-16 00:05:15 -04:00
|
|
|
"code.gitea.io/gitea/models/packages"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
packages_module "code.gitea.io/gitea/modules/packages"
|
2023-06-13 23:42:38 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-08-16 00:05:15 -04:00
|
|
|
"code.gitea.io/gitea/modules/storage"
|
|
|
|
packages_service "code.gitea.io/gitea/services/packages"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMigratePackages(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
creator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
|
|
|
|
content := "package main\n\nfunc main() {\nfmt.Println(\"hi\")\n}\n"
|
2023-05-02 12:31:35 -04:00
|
|
|
buf, err := packages_module.CreateHashedBufferFromReaderWithSize(strings.NewReader(content), 1024)
|
2022-08-16 00:05:15 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer buf.Close()
|
|
|
|
|
2023-09-25 13:24:35 -04:00
|
|
|
v, f, err := packages_service.CreatePackageAndAddFile(db.DefaultContext, &packages_service.PackageCreationInfo{
|
2022-08-16 00:05:15 -04:00
|
|
|
PackageInfo: packages_service.PackageInfo{
|
|
|
|
Owner: creator,
|
|
|
|
PackageType: packages.TypeGeneric,
|
|
|
|
Name: "test",
|
|
|
|
Version: "1.0.0",
|
|
|
|
},
|
|
|
|
Creator: creator,
|
|
|
|
SemverCompatible: true,
|
|
|
|
VersionProperties: map[string]string{},
|
|
|
|
}, &packages_service.PackageFileCreationInfo{
|
|
|
|
PackageFileInfo: packages_service.PackageFileInfo{
|
|
|
|
Filename: "a.go",
|
|
|
|
},
|
2022-11-09 01:34:27 -05:00
|
|
|
Creator: creator,
|
|
|
|
Data: buf,
|
|
|
|
IsLead: true,
|
2022-08-16 00:05:15 -04:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, v)
|
|
|
|
assert.NotNil(t, f)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-09-04 11:14:53 -04:00
|
|
|
p := t.TempDir()
|
2022-08-16 00:05:15 -04:00
|
|
|
|
|
|
|
dstStorage, err := storage.NewLocalStorage(
|
|
|
|
ctx,
|
2023-06-13 23:42:38 -04:00
|
|
|
&setting.Storage{
|
2022-08-16 00:05:15 -04:00
|
|
|
Path: p,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = migratePackages(ctx, dstStorage)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
entries, err := os.ReadDir(p)
|
|
|
|
assert.NoError(t, err)
|
2023-04-22 17:56:27 -04:00
|
|
|
assert.Len(t, entries, 2)
|
2022-08-16 00:05:15 -04:00
|
|
|
assert.EqualValues(t, "01", entries[0].Name())
|
|
|
|
assert.EqualValues(t, "tmp", entries[1].Name())
|
|
|
|
}
|