2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 18:27:13 +08:00
Files
panel/pkg/tools/system_test.go
2023-11-14 02:08:26 +08:00

145 lines
2.8 KiB
Go

package tools
import (
"os"
"os/user"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
type SystemHelperTestSuite struct {
suite.Suite
}
func TestSystemHelperTestSuite(t *testing.T) {
suite.Run(t, &SystemHelperTestSuite{})
}
func (s *SystemHelperTestSuite) TestWrite() {
filePath := "/tmp/testfile"
defer os.Remove(filePath)
s.Nil(Write(filePath, "test data", 0644))
s.FileExists(filePath)
content, _ := os.ReadFile(filePath)
s.Equal("test data", string(content))
}
func (s *SystemHelperTestSuite) TestRead() {
filePath := "/tmp/testfile"
defer os.Remove(filePath)
err := os.WriteFile(filePath, []byte("test data"), 0644)
s.Nil(err)
data, err := Read(filePath)
s.Nil(err)
s.Equal("test data", data)
}
func (s *SystemHelperTestSuite) TestRemove() {
filePath := "/tmp/testfile"
err := os.WriteFile(filePath, []byte("test data"), 0644)
s.Nil(err)
s.True(Remove(filePath))
}
func (s *SystemHelperTestSuite) TestExec() {
output, err := Exec("echo 'test'")
s.Equal("test", output)
s.Nil(err)
}
func (s *SystemHelperTestSuite) TestExecAsync() {
command := "echo 'test' > /tmp/testfile"
defer os.Remove("/tmp/testfile")
err := ExecAsync(command)
s.Nil(err)
time.Sleep(time.Second)
content, _ := os.ReadFile("/tmp/testfile")
s.Equal("test\n", string(content))
}
func (s *SystemHelperTestSuite) TestMkdir() {
dirPath := "/tmp/testdir"
defer os.RemoveAll(dirPath)
s.Nil(Mkdir(dirPath, 0755))
}
func (s *SystemHelperTestSuite) TestChmod() {
filePath := "/tmp/testfile"
defer os.Remove(filePath)
err := os.WriteFile(filePath, []byte("test data"), 0644)
s.Nil(err)
s.Nil(Chmod(filePath, 0755))
}
func (s *SystemHelperTestSuite) TestChown() {
filePath := "/tmp/testfile"
defer os.Remove(filePath)
err := os.WriteFile(filePath, []byte("test data"), 0644)
s.Nil(err)
currentUser, err := user.Current()
s.Nil(err)
groups, err := currentUser.GroupIds()
s.Nil(err)
s.Nil(Chown(filePath, currentUser.Username, groups[0]))
}
func (s *SystemHelperTestSuite) TestExists() {
s.True(Exists("/tmp"))
s.False(Exists("/tmp/123"))
}
func (s *SystemHelperTestSuite) TestEmpty() {
s.True(Empty("/tmp/123"))
s.False(Empty("/tmp"))
}
func (s *SystemHelperTestSuite) TestMv() {
filePath := "/tmp/testfile"
defer os.Remove(filePath)
err := os.WriteFile(filePath, []byte("test data"), 0644)
s.Nil(err)
s.Nil(Mv(filePath, "/tmp/testfile2"))
s.False(Exists(filePath))
}
func (s *SystemHelperTestSuite) TestCp() {
filePath := "/tmp/testfile"
defer os.Remove(filePath)
err := os.WriteFile(filePath, []byte("test data"), 0644)
s.Nil(err)
s.Nil(Cp(filePath, "/tmp/testfile2"))
s.True(Exists(filePath))
}
func (s *SystemHelperTestSuite) TestSize() {
size, err := Size("/tmp/123")
s.Equal(int64(0), size)
s.Error(err)
}
func (s *SystemHelperTestSuite) TestFileInfo() {
_, err := FileInfo("/tmp/123")
s.Error(err)
}