mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 23:16:30 +01:00
chore: simplify and cleanup
Signed-off-by: Keith Zantow <keith.zantow@anchore.com>
This commit is contained in:
@@ -1,84 +1,52 @@
|
||||
package archive
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anchore/syft/internal/tmpdir"
|
||||
)
|
||||
|
||||
func TestWorkDir_isNotCreatedUntilAskedFor(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
w := NewWorkDir(tmpdir.WithValue(context.Background(), tmpdir.FromPath(root)))
|
||||
ctx, root := scanContext(t)
|
||||
w := NewWorkDir(ctx)
|
||||
|
||||
assert.False(t, w.created(), "constructing a work directory must touch nothing")
|
||||
entries, err := os.ReadDir(root)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, entries)
|
||||
assert.False(t, w.wasCreated())
|
||||
assert.Empty(t, workDirsUnder(t, root))
|
||||
|
||||
dir, err := w.Path()
|
||||
require.NoError(t, err)
|
||||
assert.DirExists(t, dir)
|
||||
assert.True(t, w.created())
|
||||
assert.True(t, w.wasCreated())
|
||||
assert.Equal(t, []string{dir}, workDirsUnder(t, root))
|
||||
}
|
||||
|
||||
func TestWorkDir_pathIsStableAcrossCalls(t *testing.T) {
|
||||
w := NewWorkDir(tmpdir.WithValue(context.Background(), tmpdir.FromPath(t.TempDir())))
|
||||
ctx, _ := scanContext(t)
|
||||
w := NewWorkDir(ctx)
|
||||
|
||||
first, err := w.Path()
|
||||
require.NoError(t, err)
|
||||
second, err := w.Path()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, first, second, "the directory is created once, not once per writer")
|
||||
}
|
||||
|
||||
func TestWorkDir_concurrentCallersShareOneDirectory(t *testing.T) {
|
||||
// the store spills under its own lock while the archive's content may be spilling too, so the
|
||||
// creation has to be safe to race
|
||||
root := t.TempDir()
|
||||
w := NewWorkDir(tmpdir.WithValue(context.Background(), tmpdir.FromPath(root)))
|
||||
|
||||
var wg sync.WaitGroup
|
||||
paths := make([]string, 8)
|
||||
for i := range paths {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
dir, err := w.Path()
|
||||
assert.NoError(t, err)
|
||||
paths[i] = dir
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
for _, p := range paths {
|
||||
assert.Equal(t, paths[0], p)
|
||||
}
|
||||
entries, err := os.ReadDir(root)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, entries, 1, "racing callers must not each create a directory")
|
||||
assert.Equal(t, first, second)
|
||||
}
|
||||
|
||||
func TestWorkDir_removeIsANoOpWhenNothingWasCreated(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
w := NewWorkDir(tmpdir.WithValue(context.Background(), tmpdir.FromPath(root)))
|
||||
ctx, root := scanContext(t)
|
||||
w := NewWorkDir(ctx)
|
||||
|
||||
w.Remove()
|
||||
w.Remove()
|
||||
|
||||
entries, err := os.ReadDir(root)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, entries)
|
||||
assert.Empty(t, workDirsUnder(t, root))
|
||||
}
|
||||
|
||||
func TestWorkDir_removeDeletesWhatWasCreated(t *testing.T) {
|
||||
w := NewWorkDir(tmpdir.WithValue(context.Background(), tmpdir.FromPath(t.TempDir())))
|
||||
ctx, _ := scanContext(t)
|
||||
w := NewWorkDir(ctx)
|
||||
|
||||
dir, err := w.Path()
|
||||
require.NoError(t, err)
|
||||
@@ -86,8 +54,6 @@ func TestWorkDir_removeDeletesWhatWasCreated(t *testing.T) {
|
||||
|
||||
w.Remove()
|
||||
assert.NoDirExists(t, dir)
|
||||
|
||||
// safe to call more than once, as the extraction's cleanup is
|
||||
w.Remove()
|
||||
}
|
||||
|
||||
@@ -95,7 +61,7 @@ func TestWorkDir_nilHasNowhereToWrite(t *testing.T) {
|
||||
var w *WorkDir
|
||||
|
||||
_, err := w.Path()
|
||||
assert.Error(t, err, "a caller with nowhere to put content must fail rather than pick a directory")
|
||||
assert.False(t, w.created())
|
||||
assert.Error(t, err)
|
||||
assert.False(t, w.wasCreated())
|
||||
w.Remove()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user