//go:build linux package fsutils import ( `errors` `fmt` `os` `os/user` `testing` `github.com/davecgh/go-spew/spew` `r00t2.io/sysutils/paths` ) var ( testFilename string = "testfile" testErrBadUser error = errors.New("test must be run as root") ) 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 (before):\n%s", testFilename, spew.Sdump(attrs)) attrs.CompressFile = true attrs.SyncUpdate = true attrs.SecureDelete = 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) if attrs, err = GetAttrs(testFilename); err != nil { t.Fatalf("Failed to get attrs for %v: %v", testFilename, err) } t.Logf("Attrs for %v (after):\n%s", testFilename, spew.Sdump(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) }