Skip to content

Commit

Permalink
fix: lint and test
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 20, 2023
1 parent 58a13f0 commit 154e0af
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 38 deletions.
16 changes: 0 additions & 16 deletions pkg/tools/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tools

import (
"os"
"runtime"
)

// IsDebian 判断是否是 Debian 系统
Expand All @@ -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"
}
9 changes: 3 additions & 6 deletions pkg/tools/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tools
import (
"testing"

"github.com/goravel/framework/support/env"
"github.com/stretchr/testify/suite"
)

Expand All @@ -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())
}
7 changes: 4 additions & 3 deletions pkg/tools/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/goravel/framework/support"
"github.com/goravel/framework/support/env"
"github.com/mholt/archiver/v3"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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")
}

Expand Down
24 changes: 17 additions & 7 deletions pkg/tools/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/goravel/framework/support/env"
"github.com/stretchr/testify/suite"
)

Expand All @@ -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()))
}

Expand All @@ -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()))
}

Expand All @@ -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"
}

Expand All @@ -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)
Expand All @@ -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()))
}

Expand All @@ -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()))
}

Expand All @@ -120,18 +125,19 @@ func (s *SystemHelperTestSuite) TestExists() {
defer Remove(filePath.Name())

Check failure on line 125 in pkg/tools/system_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)

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()))
}

Expand All @@ -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()))
Expand Down Expand Up @@ -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()))
}

Expand All @@ -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()))
}

Expand All @@ -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))
}
Expand All @@ -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))
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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("获取最新版本失败")
}
Expand Down Expand Up @@ -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("获取最新版本失败")
}
Expand Down Expand Up @@ -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("获取面板版本失败")
}
Expand Down Expand Up @@ -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("获取面板版本失败")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/tools/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tools
import (
"testing"

"github.com/goravel/framework/support/env"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -84,7 +85,7 @@ func (s *HelperTestSuite) TestGenerateVersions() {
}

func (s *HelperTestSuite) TestGetLatestPanelVersion() {
if IsWindows() {
if env.IsWindows() {
return
}
version, err := GetLatestPanelVersion()
Expand All @@ -93,7 +94,7 @@ func (s *HelperTestSuite) TestGetLatestPanelVersion() {
}

func (s *HelperTestSuite) TestGetPanelVersion() {
if IsWindows() {
if env.IsWindows() {
return
}
version, err := GetPanelVersion("v2.0.58")
Expand Down

0 comments on commit 154e0af

Please sign in to comment.