diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-07-21 16:38:48 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-07-22 15:17:25 +0300 |
commit | 8c4e55d91a7cb11b2e3c39cf19cc623f1f576a9b (patch) | |
tree | f10d3d35f60cdc4587e280b60894f297fb5178ad | |
parent | 252ce303cb3b0521701b6d0e3010f2920abacf9b (diff) |
tempdir: Provide new `NewWithPrefix()` constructor
Provide a new constructor `NewWithPrefix()` which lets the caller
specify the prefix that is to be used for the temporary directory. This
will be used in a subsequent commit which convert quarantine directories
to have a deterministic, repository-specific prefix.
-rw-r--r-- | internal/tempdir/tempdir.go | 9 | ||||
-rw-r--r-- | internal/tempdir/tempdir_test.go | 13 |
2 files changed, 21 insertions, 1 deletions
diff --git a/internal/tempdir/tempdir.go b/internal/tempdir/tempdir.go index 8d016d208..7fde16344 100644 --- a/internal/tempdir/tempdir.go +++ b/internal/tempdir/tempdir.go @@ -26,7 +26,14 @@ func (d Dir) Path() string { // New returns the path of a new temporary directory for the given storage. The directory is removed // asynchronously with os.RemoveAll when the context expires. func New(ctx context.Context, storageName string, locator storage.Locator) (Dir, error) { - dir, err := newDirectory(ctx, storageName, "repo", locator) + return NewWithPrefix(ctx, storageName, "repo", locator) +} + +// NewWithPrefix returns the path of a new temporary directory for the given storage with a specific +// prefix used to create the temporary directory's name. The directory is removed asynchronously +// with os.RemoveAll when the context expires. +func NewWithPrefix(ctx context.Context, storageName, prefix string, locator storage.Locator) (Dir, error) { + dir, err := newDirectory(ctx, storageName, prefix, locator) if err != nil { return Dir{}, err } diff --git a/internal/tempdir/tempdir_test.go b/internal/tempdir/tempdir_test.go index a5ba368fe..fc2423255 100644 --- a/internal/tempdir/tempdir_test.go +++ b/internal/tempdir/tempdir_test.go @@ -37,6 +37,19 @@ func TestNewRepositorySuccess(t *testing.T) { require.NoDirExists(t, tempDir.Path()) } +func TestNewWithPrefix(t *testing.T) { + cfg := testcfg.Build(t) + locator := config.NewLocator(cfg) + + ctx, cancel := testhelper.Context() + defer cancel() + + dir, err := NewWithPrefix(ctx, cfg.Storages[0].Name, "foobar-", locator) + require.NoError(t, err) + + require.Contains(t, dir.Path(), "/foobar-") +} + func TestNewAsRepositoryFailStorageUnknown(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() |