Skip to content

Commit a7371fa

Browse files
committed
add tests
1 parent 8483d03 commit a7371fa

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

cmd/web.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ func serveInstalled(ctx *cli.Context) error {
213213
log.Fatal("Can not find APP_DATA_PATH %q", setting.AppDataPath)
214214
}
215215

216-
setting.AppDataTempDir("").RemoveOutdated()
216+
// the AppDataTempDir is fully managed by us with a safe sub-path
217+
// so it's safe to automatically remove the outdated files
218+
setting.AppDataTempDir("").RemoveOutdated(3 * 24 * time.Hour)
217219

218220
// Override the provided port number within the configuration
219221
if ctx.IsSet("port") {

modules/setting/path.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ func InitWorkPathAndCfgProvider(getEnvFn func(name string) string, args ArgWorkP
198198
CustomConf = tmpCustomConf.Value
199199
}
200200

201+
// AppDataTempDir returns a managed temporary directory for the application data.
202+
// Using empty sub will get the managed base temp directory, and it's safe to delete it.
203+
// Gitea only creates subdirectories under it, but not the APP_TEMP_PATH directory itself.
204+
// * When APP_TEMP_PATH="/tmp": the managed temp directory is "/tmp/gitea-tmp"
205+
// * When APP_TEMP_PATH is not set: the managed temp directory is "/{APP_DATA_PATH}/tmp"
201206
func AppDataTempDir(sub string) *tempdir.TempDir {
202207
if appTempPathInternal != "" {
203208
return tempdir.New(appTempPathInternal, "gitea-tmp/"+sub)

modules/tempdir/tempdir.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package tempdir
66
import (
77
"os"
88
"path/filepath"
9+
"time"
910

1011
"code.gitea.io/gitea/modules/log"
1112
"code.gitea.io/gitea/modules/util"
@@ -80,15 +81,32 @@ func (td *TempDir) CreateTempFileRandom(elems ...string) (*os.File, func(), erro
8081
}, err
8182
}
8283

83-
func (td *TempDir) RemoveOutdated() {
84-
// TODO: remove the out-dated temp files
85-
log.Error("TODO: remove the out-dated temp files, not implemented yet")
86-
}
87-
88-
func OsTempDir(sub string) *TempDir {
89-
return &TempDir{base: os.TempDir(), sub: sub}
84+
func (td *TempDir) RemoveOutdated(d time.Duration) {
85+
var remove func(path string)
86+
remove = func(path string) {
87+
entries, _ := os.ReadDir(path)
88+
for _, entry := range entries {
89+
full := filepath.Join(path, entry.Name())
90+
if entry.IsDir() {
91+
remove(full)
92+
_ = os.Remove(full)
93+
continue
94+
}
95+
info, err := entry.Info()
96+
if err == nil && time.Since(info.ModTime()) > d {
97+
_ = os.Remove(full)
98+
}
99+
}
100+
}
101+
remove(td.JoinPath(""))
90102
}
91103

104+
// New create a new TempDir instance, "base" must be an existing directory,
105+
// "sub" could be a multi-level directory and will be created if not exist
92106
func New(base, sub string) *TempDir {
93107
return &TempDir{base: base, sub: sub}
94108
}
109+
110+
func OsTempDir(sub string) *TempDir {
111+
return New(os.TempDir(), sub)
112+
}

modules/tempdir/tempdir_test.go

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,72 @@
44
package tempdir
55

66
import (
7+
"os"
78
"path/filepath"
9+
"strings"
810
"testing"
11+
"time"
912

1013
"github.com/stretchr/testify/assert"
1114
)
1215

1316
func TestTempDir(t *testing.T) {
1417
base := t.TempDir()
15-
td := New(base, "sub")
16-
assert.Equal(t, filepath.Join(base, "sub"), td.JoinPath(""))
1718

18-
// TODO: add some tests
19+
t.Run("Create", func(t *testing.T) {
20+
td := New(base, "sub1/sub2") // make sure the sub dir supports "/" in the path
21+
assert.Equal(t, filepath.Join(base, "sub1", "sub2"), td.JoinPath())
22+
assert.Equal(t, filepath.Join(base, "sub1", "sub2/test"), td.JoinPath("test"))
23+
24+
t.Run("MkdirTempRandom", func(t *testing.T) {
25+
s, cleanup, err := td.MkdirTempRandom("foo")
26+
assert.NoError(t, err)
27+
assert.True(t, strings.HasPrefix(s, filepath.Join(base, "sub1/sub2", "foo")))
28+
29+
_, err = os.Stat(s)
30+
assert.NoError(t, err)
31+
cleanup()
32+
_, err = os.Stat(s)
33+
assert.ErrorIs(t, err, os.ErrNotExist)
34+
})
35+
36+
t.Run("CreateTempFileRandom", func(t *testing.T) {
37+
f, cleanup, err := td.CreateTempFileRandom("foo", "bar")
38+
filename := f.Name()
39+
assert.NoError(t, err)
40+
assert.True(t, strings.HasPrefix(filename, filepath.Join(base, "sub1/sub2", "foo", "bar")))
41+
_, err = os.Stat(filename)
42+
assert.NoError(t, err)
43+
cleanup()
44+
_, err = os.Stat(filename)
45+
assert.ErrorIs(t, err, os.ErrNotExist)
46+
})
47+
48+
t.Run("RemoveOutDated", func(t *testing.T) {
49+
fa1, _, err := td.CreateTempFileRandom("dir-a", "f1")
50+
assert.NoError(t, err)
51+
fa2, _, err := td.CreateTempFileRandom("dir-a", "f2")
52+
assert.NoError(t, err)
53+
_ = os.Chtimes(fa2.Name(), time.Now().Add(-time.Hour), time.Now().Add(-time.Hour))
54+
fb1, _, err := td.CreateTempFileRandom("dir-b", "f1")
55+
assert.NoError(t, err)
56+
_ = os.Chtimes(fb1.Name(), time.Now().Add(-time.Hour), time.Now().Add(-time.Hour))
57+
_, _, _ = fa1.Close(), fa2.Close(), fb1.Close()
58+
59+
td.RemoveOutdated(time.Minute)
60+
61+
_, err = os.Stat(fa1.Name())
62+
assert.NoError(t, err)
63+
_, err = os.Stat(fa2.Name())
64+
assert.ErrorIs(t, err, os.ErrNotExist)
65+
_, err = os.Stat(fb1.Name())
66+
assert.ErrorIs(t, err, os.ErrNotExist)
67+
})
68+
})
69+
70+
t.Run("BaseNotExist", func(t *testing.T) {
71+
td := New(filepath.Join(base, "not-exist"), "sub")
72+
_, _, err := td.MkdirTempRandom("foo")
73+
assert.ErrorIs(t, err, os.ErrNotExist)
74+
})
1975
}

0 commit comments

Comments
 (0)