github.com/Theta-Dev/Talon@v0.0.0-20211018130634-ff179e19fa9a/src/fixtures/testutil.go (about)

     1  package fixtures
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"code.thetadev.de/ThetaDev/gotry/try"
     8  )
     9  
    10  func doesFileExist(filepath string) bool {
    11  	_, err := os.Stat(filepath)
    12  	return !os.IsNotExist(err)
    13  }
    14  
    15  func getProjectRoot() string {
    16  	p := try.String(os.Getwd())
    17  
    18  	for i := 0; i < 10; i++ {
    19  		if doesFileExist(filepath.Join(p, "go.mod")) {
    20  			return p
    21  		}
    22  		p = filepath.Join(p, "..")
    23  	}
    24  
    25  	panic("Could not find project root")
    26  }
    27  
    28  func CdProjectRoot() {
    29  	root := getProjectRoot()
    30  	try.Check(os.Chdir(root))
    31  }
    32  
    33  func GetTestfilesDir() string {
    34  	CdProjectRoot()
    35  	return filepath.Join("src", "fixtures", "testfiles")
    36  }
    37  
    38  func WriteTestfile(tfile string) {
    39  	f := try.File(os.Create(tfile))
    40  	defer f.Close()
    41  	try.Int(f.WriteString("HelloTST"))
    42  }