code-intelligence.com/cifuzz@v0.40.0/internal/testutil/example_project.go (about) 1 package testutil 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "runtime" 7 "testing" 8 9 "github.com/otiai10/copy" 10 "github.com/pkg/errors" 11 "github.com/stretchr/testify/require" 12 13 "code-intelligence.com/cifuzz/internal/config" 14 ) 15 16 // BootstrapExampleProjectForTest copies the given example project to a temporary folder 17 // and changes into that directory. 18 func BootstrapExampleProjectForTest(prefix, exampleName string) (tempDir string, cleanup func()) { //nolint:nonamedreturns 19 tempDir, cleanup = ChdirToTempDir(prefix) 20 21 _, thisFile, _, ok := runtime.Caller(0) 22 if !ok { 23 panic("runtime.Caller failed") 24 } 25 26 basepath := filepath.Dir(thisFile) 27 examplePath := filepath.Join(basepath, "..", "..", "examples", exampleName) 28 29 err := copy.Copy(examplePath, tempDir) 30 if err != nil { 31 panic(fmt.Sprintf("copying %v to %v failed: %+v", examplePath, tempDir, errors.WithStack(err))) 32 } 33 34 return tempDir, cleanup 35 } 36 37 func BootstrapEmptyProject(t *testing.T, prefix string) string { 38 // Create an empty directory 39 projectDir := MkdirTemp(t, "", prefix) 40 41 // Create an empty config file 42 _, err := config.CreateProjectConfig(projectDir, "", "") 43 require.NoError(t, err) 44 45 return projectDir 46 }