Files
forge/modules/tempdir/tempdir_test.go
zeekayandHanzo Dev 9a07b4d089 legal: Hanzo AI, Inc. copyright above the upstream notices
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git),
so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form
used by the handful of files that had ours.

Shape is now:

	// Copyright 2026 Hanzo AI, Inc. All rights reserved.
	// Copyright 2015 The Gogs Authors. All rights reserved.
	// Copyright 2019 The Gitea Authors. All rights reserved.
	// SPDX-License-Identifier: MIT

Ours on top, theirs retained verbatim. Retained because MIT requires it, and
separately because they are true — those authors did write that code.

Not renamed to "The Git Authors": Git is a different project under a different
license, and putting its name here would attribute this code to people who have
nothing to do with it. Adding our line is the fix; overwriting theirs would have
been both a licence violation and a false statement.

gofmt still reports import-order drift in some of these files. It predates this
change (verified against a stash) and is left alone rather than burying a
one-line-per-file legal change under 3000 files of unrelated reordering.

Build clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 13:49:52 -07:00

77 lines
2.2 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// 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)
})
}