mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 07:57:21 +08:00
fix: lint and test
This commit is contained in:
@@ -2,7 +2,6 @@ package tools
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// IsDebian 判断是否是 Debian 系统
|
||||
@@ -16,18 +15,3 @@ func IsRHEL() bool {
|
||||
_, err := os.Stat("/etc/redhat-release")
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// IsArm 判断是否是 ARM 架构
|
||||
func IsArm() bool {
|
||||
return runtime.GOARCH == "arm" || runtime.GOARCH == "arm64"
|
||||
}
|
||||
|
||||
// IsLinux 判断是否是 Linux 系统
|
||||
func IsLinux() bool {
|
||||
return runtime.GOOS == "linux"
|
||||
}
|
||||
|
||||
// IsWindows 判断是否是 Windows 系统
|
||||
func IsWindows() bool {
|
||||
return runtime.GOOS == "windows"
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package tools
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/goravel/framework/support/env"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
@@ -15,19 +16,15 @@ func TestOSHelperTestSuite(t *testing.T) {
|
||||
}
|
||||
|
||||
func (s *OSHelperTestSuite) TestIsDebian() {
|
||||
if IsWindows() {
|
||||
if env.IsWindows() {
|
||||
return
|
||||
}
|
||||
s.True(IsDebian())
|
||||
}
|
||||
|
||||
func (s *OSHelperTestSuite) TestIsRHEL() {
|
||||
if IsWindows() {
|
||||
if env.IsWindows() {
|
||||
return
|
||||
}
|
||||
s.False(IsRHEL())
|
||||
}
|
||||
|
||||
func (s *OSHelperTestSuite) TestIsArm() {
|
||||
s.False(IsArm())
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/goravel/framework/support"
|
||||
"github.com/goravel/framework/support/env"
|
||||
"github.com/mholt/archiver/v3"
|
||||
)
|
||||
|
||||
@@ -42,7 +43,7 @@ func Remove(path string) error {
|
||||
// Exec 执行 shell 命令
|
||||
func Exec(shell string) (string, error) {
|
||||
var cmd *exec.Cmd
|
||||
if IsLinux() {
|
||||
if env.IsLinux() {
|
||||
cmd = exec.Command("bash", "-c", "LC_ALL=C "+shell)
|
||||
} else {
|
||||
cmd = exec.Command("cmd", "/C", "chcp 65001 >nul && "+shell)
|
||||
@@ -63,7 +64,7 @@ func Exec(shell string) (string, error) {
|
||||
// ExecAsync 异步执行 shell 命令
|
||||
func ExecAsync(shell string) error {
|
||||
var cmd *exec.Cmd
|
||||
if IsLinux() {
|
||||
if env.IsLinux() {
|
||||
cmd = exec.Command("bash", "-c", "LC_ALL=C "+shell)
|
||||
} else {
|
||||
cmd = exec.Command("cmd", "/C", "chcp 65001 >nul && "+shell)
|
||||
@@ -99,7 +100,7 @@ func Chmod(path string, permission os.FileMode) error {
|
||||
|
||||
// Chown 修改文件或目录所有者
|
||||
func Chown(path, user, group string) error {
|
||||
if IsWindows() {
|
||||
if env.IsWindows() {
|
||||
return errors.New("chown is not supported on Windows")
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/goravel/framework/support/env"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
@@ -25,6 +26,7 @@ func (s *SystemHelperTestSuite) TestWrite() {
|
||||
|
||||
content, _ := Read(filePath.Name())
|
||||
s.Equal("test data", content)
|
||||
s.Nil(filePath.Close())
|
||||
s.Nil(Remove(filePath.Name()))
|
||||
}
|
||||
|
||||
@@ -37,6 +39,7 @@ func (s *SystemHelperTestSuite) TestRead() {
|
||||
data, err := Read(filePath.Name())
|
||||
s.Nil(err)
|
||||
s.Equal("test data", data)
|
||||
s.Nil(filePath.Close())
|
||||
s.Nil(Remove(filePath.Name()))
|
||||
}
|
||||
|
||||
@@ -58,7 +61,7 @@ func (s *SystemHelperTestSuite) TestExec() {
|
||||
|
||||
func (s *SystemHelperTestSuite) TestExecAsync() {
|
||||
command := "echo test > test.txt"
|
||||
if IsWindows() {
|
||||
if env.IsWindows() {
|
||||
command = "echo test> test.txt"
|
||||
}
|
||||
|
||||
@@ -71,7 +74,7 @@ func (s *SystemHelperTestSuite) TestExecAsync() {
|
||||
s.Nil(err)
|
||||
|
||||
condition := "test\n"
|
||||
if IsWindows() {
|
||||
if env.IsWindows() {
|
||||
condition = "test\r\n"
|
||||
}
|
||||
s.Equal(condition, content)
|
||||
@@ -92,6 +95,7 @@ func (s *SystemHelperTestSuite) TestChmod() {
|
||||
s.Nil(err)
|
||||
|
||||
s.Nil(Chmod(filePath.Name(), 0755))
|
||||
s.Nil(filePath.Close())
|
||||
s.Nil(Remove(filePath.Name()))
|
||||
}
|
||||
|
||||
@@ -107,11 +111,12 @@ func (s *SystemHelperTestSuite) TestChown() {
|
||||
s.Nil(err)
|
||||
|
||||
err = Chown(filePath.Name(), currentUser.Username, groups[0])
|
||||
if IsWindows() {
|
||||
if env.IsWindows() {
|
||||
s.NotNil(err)
|
||||
} else {
|
||||
s.Nil(err)
|
||||
}
|
||||
s.Nil(filePath.Close())
|
||||
s.Nil(Remove(filePath.Name()))
|
||||
}
|
||||
|
||||
@@ -120,18 +125,19 @@ func (s *SystemHelperTestSuite) TestExists() {
|
||||
defer Remove(filePath.Name())
|
||||
|
||||
s.True(Exists(filePath.Name()))
|
||||
s.False(Exists("/tmp/123"))
|
||||
s.False(Exists("123"))
|
||||
}
|
||||
|
||||
func (s *SystemHelperTestSuite) TestEmpty() {
|
||||
filePath, _ := TempFile("testfile")
|
||||
|
||||
s.True(Empty(filePath.Name()))
|
||||
if IsWindows() {
|
||||
if env.IsWindows() {
|
||||
s.True(Empty("C:\\Windows\\System32\\drivers\\etc\\hosts"))
|
||||
} else {
|
||||
s.True(Empty("/etc/hosts"))
|
||||
}
|
||||
s.Nil(filePath.Close())
|
||||
s.Nil(Remove(filePath.Name()))
|
||||
}
|
||||
|
||||
@@ -143,8 +149,8 @@ func (s *SystemHelperTestSuite) TestMv() {
|
||||
|
||||
newFilePath, _ := TempFile("testfile2")
|
||||
|
||||
filePath.Close()
|
||||
newFilePath.Close()
|
||||
s.Nil(newFilePath.Close())
|
||||
s.Nil(filePath.Close())
|
||||
|
||||
s.Nil(Mv(filePath.Name(), newFilePath.Name()))
|
||||
s.False(Exists(filePath.Name()))
|
||||
@@ -172,6 +178,7 @@ func (s *SystemHelperTestSuite) TestSize() {
|
||||
size, err := Size(filePath.Name())
|
||||
s.Nil(err)
|
||||
s.Equal(int64(len("test data")), size)
|
||||
s.Nil(filePath.Close())
|
||||
s.Nil(Remove(filePath.Name()))
|
||||
}
|
||||
|
||||
@@ -184,6 +191,7 @@ func (s *SystemHelperTestSuite) TestFileInfo() {
|
||||
info, err := FileInfo(filePath.Name())
|
||||
s.Nil(err)
|
||||
s.Equal(filepath.Base(filePath.Name()), info.Name())
|
||||
s.Nil(filePath.Close())
|
||||
s.Nil(Remove(filePath.Name()))
|
||||
}
|
||||
|
||||
@@ -201,6 +209,7 @@ func (s *SystemHelperTestSuite) TestUnArchiveSuccessfullyUnarchivesFile() {
|
||||
err = UnArchive(filepath.Join(dstDir, "test.zip"), dstDir)
|
||||
s.Nil(err)
|
||||
s.FileExists(filepath.Join(dstDir, filepath.Base(file.Name())))
|
||||
s.Nil(file.Close())
|
||||
s.Nil(Remove(file.Name()))
|
||||
s.Nil(Remove(dstDir))
|
||||
}
|
||||
@@ -224,6 +233,7 @@ func (s *SystemHelperTestSuite) TestArchiveSuccessfullyArchivesFiles() {
|
||||
err = Archive([]string{srcFile.Name()}, filepath.Join(dstDir, "test.zip"))
|
||||
s.Nil(err)
|
||||
s.FileExists(filepath.Join(dstDir, "test.zip"))
|
||||
s.Nil(srcFile.Close())
|
||||
s.Nil(Remove(srcFile.Name()))
|
||||
s.Nil(Remove(dstDir))
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gookit/color"
|
||||
"github.com/goravel/framework/support/env"
|
||||
"github.com/imroc/req/v3"
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"github.com/shirou/gopsutil/disk"
|
||||
@@ -213,7 +214,7 @@ func GetLatestPanelVersion() (PanelInfo, error) {
|
||||
if checksumsUrl, err = Exec("jq -r '.assets.links[] | select(.name | contains(\"checksums\")) | .direct_asset_url' " + fileName); err != nil {
|
||||
return info, errors.New("获取最新版本失败")
|
||||
}
|
||||
if IsArm() {
|
||||
if env.IsArm() {
|
||||
if downloadName, err = Exec("jq -r '.assets.links[] | select(.name | contains(\"arm64\")) | .name' " + fileName); err != nil {
|
||||
return info, errors.New("获取最新版本失败")
|
||||
}
|
||||
@@ -247,7 +248,7 @@ func GetLatestPanelVersion() (PanelInfo, error) {
|
||||
if checksumsUrl, err = Exec("jq -r '.assets[] | select(.name | contains(\"checksums\")) | .browser_download_url' " + fileName); err != nil {
|
||||
return info, errors.New("获取最新版本失败")
|
||||
}
|
||||
if IsArm() {
|
||||
if env.IsArm() {
|
||||
if downloadName, err = Exec("jq -r '.assets[] | select(.name | contains(\"arm64\")) | .name' " + fileName); err != nil {
|
||||
return info, errors.New("获取最新版本失败")
|
||||
}
|
||||
@@ -332,7 +333,7 @@ func GetPanelVersion(version string) (PanelInfo, error) {
|
||||
if checksumsUrl, err = Exec("jq -r '.assets.links[] | select(.name | contains(\"checksums\")) | .direct_asset_url' " + fileName); err != nil {
|
||||
return info, errors.New("获取面板版本失败")
|
||||
}
|
||||
if IsArm() {
|
||||
if env.IsArm() {
|
||||
if downloadName, err = Exec("jq -r '.assets.links[] | select(.name | contains(\"arm64\")) | .name' " + fileName); err != nil {
|
||||
return info, errors.New("获取面板版本失败")
|
||||
}
|
||||
@@ -366,7 +367,7 @@ func GetPanelVersion(version string) (PanelInfo, error) {
|
||||
if checksumsUrl, err = Exec("jq -r '.assets[] | select(.name | contains(\"checksums\")) | .browser_download_url' " + fileName); err != nil {
|
||||
return info, errors.New("获取面板版本失败")
|
||||
}
|
||||
if IsArm() {
|
||||
if env.IsArm() {
|
||||
if downloadName, err = Exec("jq -r '.assets[] | select(.name | contains(\"arm64\")) | .name' " + fileName); err != nil {
|
||||
return info, errors.New("获取面板版本失败")
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package tools
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/goravel/framework/support/env"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
@@ -84,7 +85,7 @@ func (s *HelperTestSuite) TestGenerateVersions() {
|
||||
}
|
||||
|
||||
func (s *HelperTestSuite) TestGetLatestPanelVersion() {
|
||||
if IsWindows() {
|
||||
if env.IsWindows() {
|
||||
return
|
||||
}
|
||||
version, err := GetLatestPanelVersion()
|
||||
@@ -93,7 +94,7 @@ func (s *HelperTestSuite) TestGetLatestPanelVersion() {
|
||||
}
|
||||
|
||||
func (s *HelperTestSuite) TestGetPanelVersion() {
|
||||
if IsWindows() {
|
||||
if env.IsWindows() {
|
||||
return
|
||||
}
|
||||
version, err := GetPanelVersion("v2.0.58")
|
||||
|
||||
Reference in New Issue
Block a user