mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-09 03:11:51 +01:00
8b0aaa5f86
A testing cleanup. This pull request replaces `os.MkdirTemp` with `t.TempDir`. We can use the `T.TempDir` function from the `testing` package to create temporary directory. The directory created by `T.TempDir` is automatically removed when the test and all its subtests complete. This saves us at least 2 lines (error check, and cleanup) on every instance, or in some cases adds cleanup that we forgot. Reference: https://pkg.go.dev/testing#T.TempDir ```go func TestFoo(t *testing.T) { // before tmpDir, err := os.MkdirTemp("", "") require.NoError(t, err) defer os.RemoveAll(tmpDir) // now tmpDir := t.TempDir() } ``` Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"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"
|
|
"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"
|
|
buf, err := packages_module.CreateHashedBufferFromReader(strings.NewReader(content), 1024)
|
|
assert.NoError(t, err)
|
|
defer buf.Close()
|
|
|
|
v, f, err := packages_service.CreatePackageAndAddFile(&packages_service.PackageCreationInfo{
|
|
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",
|
|
},
|
|
Data: buf,
|
|
IsLead: true,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, v)
|
|
assert.NotNil(t, f)
|
|
|
|
ctx := context.Background()
|
|
|
|
p := t.TempDir()
|
|
|
|
dstStorage, err := storage.NewLocalStorage(
|
|
ctx,
|
|
storage.LocalStorageConfig{
|
|
Path: p,
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
err = migratePackages(ctx, dstStorage)
|
|
assert.NoError(t, err)
|
|
|
|
entries, err := os.ReadDir(p)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 2, len(entries))
|
|
assert.EqualValues(t, "01", entries[0].Name())
|
|
assert.EqualValues(t, "tmp", entries[1].Name())
|
|
}
|