github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/internal/testutil/chdir.go (about)

     1  package testutil
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func Chdir(t *testing.T, dir string) {
    11  	t.Helper()
    12  
    13  	wd, err := os.Getwd()
    14  	if err != nil {
    15  		t.Fatalf("unable to get working directory: %v", err)
    16  	}
    17  
    18  	err = os.Chdir(dir)
    19  	if err != nil {
    20  		t.Fatalf("unable to chdir to '%s': %v", dir, err)
    21  	}
    22  
    23  	t.Cleanup(func() {
    24  		require.NoError(t, os.Chdir(wd))
    25  	})
    26  }