go_sysutils/fsutils/funcs_test.go
2024-01-26 09:54:46 -05:00

72 lines
1.3 KiB
Go

package fsutils
import (
`errors`
`fmt`
`os`
`os/user`
`testing`
`r00t2.io/sysutils/paths`
)
var (
testFilename string = "testfile"
testErrBadUser error = errors.New("test must be run as root, on Linux")
)
func testChkUser() (err error) {
var u *user.User
if u, err = user.Current(); err != nil {
return
}
if u.Uid != "0" {
err = testErrBadUser
return
}
return
}
func TestSetAttrs(t *testing.T) {
var err error
var attrs *FsAttrs
if attrs, err = GetAttrs(testFilename); err != nil {
t.Fatalf("Failed to get attrs for %v: %v", testFilename, err)
}
t.Logf("Attrs for %v:\n%#v", testFilename, attrs)
attrs.CompressFile = true
if err = attrs.Apply(testFilename); err != nil {
t.Fatalf("Failed to apply attrs to %v: %v", testFilename, err)
}
t.Logf("Applied new attrs to %v:\n%#v", testFilename, attrs)
}
func TestMain(t *testing.M) {
var err error
var rslt int
if err = testChkUser(); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err = paths.RealPath(&testFilename); err != nil {
fmt.Println(err)
os.Exit(2)
}
if err = os.WriteFile(testFilename, []byte("This is a test file."), 0o0644); err != nil {
fmt.Println(err)
os.Exit(3)
}
rslt = t.Run()
if err = os.Remove(testFilename); err != nil {
fmt.Printf("Failed to remove test file %v: %v", testFilename, err)
}
os.Exit(rslt)
}