mirror of
https://github.com/go-gitea/gitea.git
synced 2025-04-20 00:19:08 +03:00

This PR uniform all temporary directory usage so that it will be easier to manage. Relate to #31792 - [x] Added a new setting to allow users to configure the global temporary directory. - [x] Move all temporary files and directories to be placed under os.Temp()/gitea. - [x] `setting.Repository.Local.LocalCopyPath` now will be `setting.TempPath/local-repo` and the customized path is removed. ```diff -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;[repository.local] -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; -;; Path for local repository copy. Defaults to TEMP_PATH + `local-repo`, this is deprecated and cannot be changed -;LOCAL_COPY_PATH = local-repo ``` - [x] `setting.Repository.Upload.TempPath` now will be `settting.TempPath/uploads` and the customized path is removed. ```diff ;[repository.upload] -;; -;; Path for uploads. Defaults to TEMP_PATH + `uploads` -;TEMP_PATH = uploads ``` - [x] `setting.Packages.ChunkedUploadPath` now will be `settting.TempPath/package-upload` and the customized path is removed. ```diff ;[packages] -;; -;; Path for chunked uploads. Defaults it's `package-upload` under `TEMP_PATH` unless it's an absolute path. -;CHUNKED_UPLOAD_PATH = package-upload ``` - [x] `setting.SSH.KeyTestPath` now will be `settting.TempPath/ssh_key_test` and the customized path is removed. ```diff [server] -;; -;; Directory to create temporary files in when testing public keys using ssh-keygen, -;; default is the system temporary directory. -;SSH_KEY_TEST_PATH = ``` TODO: - [ ] setting.PprofDataPath haven't been changed because it may need to be kept until somebody read it but temp path may be clean up any time. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
// Copyright 2025 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package tempdir
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestTempDir(t *testing.T) {
|
|
base := t.TempDir()
|
|
|
|
t.Run("Create", func(t *testing.T) {
|
|
td := New(base, "sub1/sub2") // make sure the sub dir supports "/" in the path
|
|
assert.Equal(t, filepath.Join(base, "sub1", "sub2"), td.JoinPath())
|
|
assert.Equal(t, filepath.Join(base, "sub1", "sub2/test"), td.JoinPath("test"))
|
|
|
|
t.Run("MkdirTempRandom", func(t *testing.T) {
|
|
s, cleanup, err := td.MkdirTempRandom("foo")
|
|
assert.NoError(t, err)
|
|
assert.True(t, strings.HasPrefix(s, filepath.Join(base, "sub1/sub2", "foo")))
|
|
|
|
_, err = os.Stat(s)
|
|
assert.NoError(t, err)
|
|
cleanup()
|
|
_, err = os.Stat(s)
|
|
assert.ErrorIs(t, err, os.ErrNotExist)
|
|
})
|
|
|
|
t.Run("CreateTempFileRandom", func(t *testing.T) {
|
|
f, cleanup, err := td.CreateTempFileRandom("foo", "bar")
|
|
filename := f.Name()
|
|
assert.NoError(t, err)
|
|
assert.True(t, strings.HasPrefix(filename, filepath.Join(base, "sub1/sub2", "foo", "bar")))
|
|
_, err = os.Stat(filename)
|
|
assert.NoError(t, err)
|
|
cleanup()
|
|
_, err = os.Stat(filename)
|
|
assert.ErrorIs(t, err, os.ErrNotExist)
|
|
})
|
|
|
|
t.Run("RemoveOutDated", func(t *testing.T) {
|
|
fa1, _, err := td.CreateTempFileRandom("dir-a", "f1")
|
|
assert.NoError(t, err)
|
|
fa2, _, err := td.CreateTempFileRandom("dir-a", "f2")
|
|
assert.NoError(t, err)
|
|
_ = os.Chtimes(fa2.Name(), time.Now().Add(-time.Hour), time.Now().Add(-time.Hour))
|
|
fb1, _, err := td.CreateTempFileRandom("dir-b", "f1")
|
|
assert.NoError(t, err)
|
|
_ = os.Chtimes(fb1.Name(), time.Now().Add(-time.Hour), time.Now().Add(-time.Hour))
|
|
_, _, _ = fa1.Close(), fa2.Close(), fb1.Close()
|
|
|
|
td.RemoveOutdated(time.Minute)
|
|
|
|
_, err = os.Stat(fa1.Name())
|
|
assert.NoError(t, err)
|
|
_, err = os.Stat(fa2.Name())
|
|
assert.ErrorIs(t, err, os.ErrNotExist)
|
|
_, err = os.Stat(fb1.Name())
|
|
assert.ErrorIs(t, err, os.ErrNotExist)
|
|
})
|
|
})
|
|
|
|
t.Run("BaseNotExist", func(t *testing.T) {
|
|
td := New(filepath.Join(base, "not-exist"), "sub")
|
|
_, _, err := td.MkdirTempRandom("foo")
|
|
assert.ErrorIs(t, err, os.ErrNotExist)
|
|
})
|
|
}
|