Skip to content

Commit

Permalink
Add basic testing APIs (#481)
Browse files Browse the repository at this point in the history
Signed-off-by: Yaxiong Zhao <[email protected]>
Co-authored-by: Yaxiong Zhao <[email protected]>
  • Loading branch information
yzhao-2023 and yxzhao6 authored Jan 13, 2024
1 parent b7be69f commit 066fabd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
71 changes: 71 additions & 0 deletions pkg/testing/fs.go
Original file line number Diff line number Diff line change
@@ -1 +1,72 @@
package testing

import (
"os"
"path"
"log"
"math/rand"
"time"
)

// Initialize random seed for randStr()
func init() {
rand.New(rand.NewSource(time.Now().UnixNano()))
}

// createTmpDir returns a path to a newly created temporary directory.
func createTmpDir() string {
prefix := "sxwl-test-"
dir := path.Join(os.TempDir(), prefix+randStr(10))
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
log.Fatalf("Failed to create tmp dir '%s', error: %v", dir, err)
}
return dir
}

func randStr(n int) string {
var letters = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}

// getTmpFilePath returns a random file path under temp directory.
// The file is not created.
func getTmpFilePath() string {
return path.Join(createTmpDir(), randStr(10))
}

// CreateTmpFile returns a path to a file under the temporary directory.
func CreateTmpFile() string {
f := getTmpFilePath()
openedFile, err := os.Create(f)
if err != nil {
log.Fatalf("While creating temp file at '%s', failed to create the file, error: %v", f, err)
}
err = openedFile.Close()
if err != nil {
log.Fatalf("While creating temp file at '%s', failed to close the file after creation, error: %v", f, err)
}
return f
}

func CreateTmpFileWithContent(content string) string {
f := getTmpFilePath()
const defaultPerf = 0o644
err := os.WriteFile(f, []byte(content), defaultPerf)
if err != nil {
log.Fatalf("Failed to write to file '%s', error: %v", f, err)
}
return f
}

func MustReadFile(f string) string {
content, err := os.ReadFile(f)
if err != nil {
log.Fatalf("Failed to read file '%s', error: %v", f, err)
}
return string(content)
}
18 changes: 18 additions & 0 deletions pkg/testing/fs_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
package testing

import (
"testing"

"sxwl/3k/pkg/utils/fs"
)

func TestCreateTmpFile(t *testing.T) {
p := CreateTmpFile()
if !fs.Exists(p) {
t.Errorf("%s should be created, but does not exist", p)
}

p = CreateTmpFileWithContent("content")
if MustReadFile(p) != "content" {
t.Errorf("p is not empty, %s", p)
}
}
8 changes: 8 additions & 0 deletions pkg/utils/fs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fs

import "os"

func Exists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}

0 comments on commit 066fabd

Please sign in to comment.