2024-06-23 13:43:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-23 16:33:34 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2024-06-23 13:43:33 +00:00
|
|
|
"io"
|
2024-06-23 16:33:34 +00:00
|
|
|
"io/fs"
|
2024-06-23 13:43:33 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func mkDirForTest(t *testing.T, fp string) {
|
|
|
|
err := os.MkdirAll(fp, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cpFileForTest(t *testing.T, src string, dst string) {
|
|
|
|
var srcfd *os.File
|
|
|
|
var dstfd *os.File
|
|
|
|
var err error
|
|
|
|
var srcinfo os.FileInfo
|
|
|
|
srcfd, err = os.Open(src)
|
|
|
|
if err != nil {
|
2024-06-23 16:33:34 +00:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-23 13:43:33 +00:00
|
|
|
}
|
2024-06-23 16:33:34 +00:00
|
|
|
// if err != nil {
|
|
|
|
// t.Fatal(err)
|
|
|
|
// }
|
2024-06-23 13:43:33 +00:00
|
|
|
defer srcfd.Close()
|
|
|
|
dstfd, err = os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer dstfd.Close()
|
|
|
|
_, err = io.Copy(dstfd, srcfd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
srcinfo, err = os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = os.Chmod(dst, srcinfo.Mode())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeForTest(t *testing.T, fp string, data []byte) {
|
|
|
|
err := os.WriteFile(fp, data, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsDirEmpty(t *testing.T, name string) bool {
|
|
|
|
f, err := os.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// read in ONLY one file
|
|
|
|
_, err = f.Readdir(1)
|
|
|
|
|
|
|
|
// and if the file is EOF... well, the dir is empty.
|
|
|
|
return err == io.EOF
|
|
|
|
}
|
2024-06-23 16:33:34 +00:00
|
|
|
|
|
|
|
func doesFileExist(name string) bool {
|
|
|
|
_, err := os.Stat(name)
|
|
|
|
return !errors.Is(err, fs.ErrNotExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFileForTest(t *testing.T, name string) {
|
|
|
|
err := os.Remove(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeJsonForTest(t *testing.T, data map[string]string, fp string) {
|
|
|
|
jsonString, _ := json.Marshal(data)
|
|
|
|
err := os.WriteFile(fp, jsonString, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|