diff --git a/.codacy.yml b/.codacy.yml new file mode 100644 index 0000000..2fec52f --- /dev/null +++ b/.codacy.yml @@ -0,0 +1,6 @@ +coverage: + enabled: yes + input: + - ./coverage.out # 用于指定覆盖率报告文件的路径 + parsers: + - go diff --git a/.github/workflows/codacy.yml b/.github/workflows/codacy.yml new file mode 100644 index 0000000..4a84452 --- /dev/null +++ b/.github/workflows/codacy.yml @@ -0,0 +1,40 @@ +name: Codacy and Codecov Integration + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build-and-report: + runs-on: ubuntu-latest + name: Build, Test, and Report Coverage + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: "1.20" + + - name: Build + run: go build -v ./... + + - name: Test + run: ./test.sh 1 + + - name: Test Coverage + run: ./test.sh 4 + + - name: Codacy Coverage Reporter + uses: codacy/codacy-coverage-reporter-action@v1 + with: + project-token: ${{ secrets.CODACY_PROJECT_TOKEN }} + coverage-reports: coverage.out # Replace with the actual path to your coverage report file + + - name: Upload coverage to Codecov + run: bash <(curl -s https://codecov.io/bash) -t ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml deleted file mode 100644 index c4f5e17..0000000 --- a/.github/workflows/go.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Go - -on: - push: - branches: [main] - pull_request: - branches: [main] - -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: "1.20" - - - name: Build - run: go build -v ./... - - - name: Test - run: ./test.sh 1 - - - name: Codecov - run: ./test.sh 4 - - - name: Upload coverage to Codecov - run: bash <(curl -s https://codecov.io/bash) -t ${{ secrets.CODECOV_TOKEN }} diff --git a/.gitignore b/.gitignore index 854276f..325f2ea 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ *.dylib ./cmd/vasedb vasedb +_temp # Test binary, built with `go test -c` *.test diff --git a/README.md b/README.md index 1b12f6d..b5c93f7 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,8 @@ VaseDB is a NoSQL that supports multiple data types and transactions. --- [![Go Report Card](https://goreportcard.com/badge/github.com/auula/vasedb)](https://goreportcard.com/report/github.com/auula/vasedb) -[![Release](https://img.shields.io/github/v/release/auula/vasedb.svg?style=flat-square)](https://github.com/auula/vasedb) -[![License](https://img.shields.io/badge/license-Apache%202-blue)](github.com/auula/vasedb/blob/master/LICENSE) [![Go Reference](https://pkg.go.dev/badge/github.com/auula/vasedb.svg)](https://pkg.go.dev/github.com/auula/vasedb) +[![Codacy Badge](https://app.codacy.com/project/badge/Grade/55bc449808ca4d0c80c0122f170d7313)](https://app.codacy.com/gh/auula/vasedb/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade) [![codecov](https://codecov.io/gh/auula/vasedb/graph/badge.svg?token=ekQ3KzyXtm)](https://codecov.io/gh/auula/vasedb) [![DeepSource](https://deepsource.io/gh/auula/vasedb.svg/?label=active+issues&token=rdl-7kKKCfR0F8b0dojJd50U)](https://deepsource.io/gh/auula/vasedb/?ref=repository-badge) diff --git a/clog/logging.go b/clog/logging.go index eceb31b..27b9ec6 100644 --- a/clog/logging.go +++ b/clog/logging.go @@ -29,7 +29,7 @@ var ( caw = os.O_CREATE | os.O_APPEND | os.O_WRONLY // fix: clog import cycle not allowed. - permissions = fs.FileMode(0600) + permissions = fs.FileMode(0755) ) var ( diff --git a/cmd/vasedb.go b/cmd/vasedb.go index 926207f..eb85728 100644 --- a/cmd/vasedb.go +++ b/cmd/vasedb.go @@ -20,11 +20,11 @@ import ( "fmt" "os" "os/exec" - "strings" "github.com/auula/vasedb/clog" "github.com/auula/vasedb/conf" "github.com/auula/vasedb/server" + "github.com/auula/vasedb/utils" "github.com/auula/vasedb/vfs" "github.com/fatih/color" ) @@ -111,48 +111,11 @@ func parseFlags() (fl *flags) { return } -// trimDaemon 从 os.Args 中移除 "-daemon" 参数 -func trimDaemon(args []string) []string { - var newArgs []string - - // 遍历 args 切片 - for i := 1; i < len(args); i++ { - // 巨坑无比,无法知道用户输入的参数是 -- 还是 - 开头 - if args[i] == "-daemon" || args[i] == "--daemon" { - // 当发现 "-daemon" 参数时,跳过当前参数 - continue - } - newArgs = append(newArgs, args[i]) - } - - return newArgs -} - -func splitArgs(args []string) []string { - var newArgs []string - // i = 1 避免处理 ./vasedb 本身 - for i := 1; i < len(args); i++ { - // 巨坑无比,如果没有这段代码 newArgs 元素中会出现 --port=2468 元素 - // 而不是分割好的 [--port , 2468] 这样的元素,否则命令行解析错误 - if strings.Contains(args[i], "=") && strings.Count(args[i], "=") == 1 { - newArgs = append(newArgs, strings.Split(args[i], "=")...) - } else { - // 如果是 --port==8080 ,过滤掉 == 不合法过滤掉 - if strings.Count(args[i], "=") > 1 { - continue - } - newArgs = append(newArgs, strings.Split(args[i], "=")...) - } - } - - return newArgs -} - func main() { // 检查是否启用了守护进程模式 if daemon { // 后台守护进程模式启动,创建一个与当前程序相同的命令 - cmd := exec.Command(os.Args[0], splitArgs(trimDaemon(os.Args))...) + cmd := exec.Command(os.Args[0], utils.SplitArgs(utils.TrimDaemon(os.Args))...) // 如果需要传递环境变量信息 cmd.Env = os.Environ() diff --git a/test.sh b/test.sh index 4cc8145..292661d 100755 --- a/test.sh +++ b/test.sh @@ -18,15 +18,17 @@ if [ -z "$case_num" ]; then exit 1 fi -if [ ""$case_num"" -eq 1 ]; then - cd cmd && go test -c && ./cmd.test +rm -rf ./_temp +mkdir -p ./_temp + +if [ "$case_num" -eq 1 ]; then + cd cmd && go test -c && sudo -S ./cmd.test elif [ "$case_num" -eq 2 ]; then echo "Testing conf package" elif [ "$case_num" -eq 3 ]; then echo "Testing server package" elif [ "$case_num" -eq 4 ]; then - cd cmd && go test -c -race -coverprofile=coverage.txt -covermode=atomic -v - ./cmd.test + go test ./... -race -coverprofile=coverage.out -covermode=atomic -v else echo "Invalid option. Please provide a valid option (1, 2, or 3)." fi diff --git a/utils/string.go b/utils/string.go new file mode 100644 index 0000000..6c77d7f --- /dev/null +++ b/utils/string.go @@ -0,0 +1,40 @@ +package utils + +import "strings" + +// trimDaemon 从 os.Args 中移除 "-daemon" 参数 +func TrimDaemon(args []string) []string { + var newArgs []string + + // 遍历 args 切片 + for i := 1; i < len(args); i++ { + // 巨坑无比,无法知道用户输入的参数是 -- 还是 - 开头 + if args[i] == "-daemon" || args[i] == "--daemon" { + // 当发现 "-daemon" 参数时,跳过当前参数 + continue + } + newArgs = append(newArgs, args[i]) + } + + return newArgs +} + +func SplitArgs(args []string) []string { + var newArgs []string + // i = 1 避免处理 ./vasedb 本身 + for i := 1; i < len(args); i++ { + // 巨坑无比,如果没有这段代码 newArgs 元素中会出现 --port=2468 元素 + // 而不是分割好的 [--port , 2468] 这样的元素,否则命令行解析错误 + if strings.Contains(args[i], "=") && strings.Count(args[i], "=") == 1 { + newArgs = append(newArgs, strings.Split(args[i], "=")...) + } else { + // 如果是 --port==8080 ,过滤掉 == 不合法过滤掉 + if strings.Count(args[i], "=") > 1 { + continue + } + newArgs = append(newArgs, strings.Split(args[i], "=")...) + } + } + + return newArgs +} diff --git a/cmd/vasedb_test.go b/utils/string_test.go similarity index 94% rename from cmd/vasedb_test.go rename to utils/string_test.go index bf5a243..899a1c2 100644 --- a/cmd/vasedb_test.go +++ b/utils/string_test.go @@ -1,4 +1,4 @@ -package main +package utils import ( "reflect" @@ -26,7 +26,7 @@ func TestSplitArgs(t *testing.T) { for _, testCase := range testCases { t.Run("", func(t *testing.T) { - result := splitArgs(testCase.input) + result := SplitArgs(testCase.input) if !reflect.DeepEqual(result, testCase.expected) { t.Errorf("Expected %v, but got %v", testCase.expected, result) } @@ -53,7 +53,7 @@ func TestTrimDaemon(t *testing.T) { for _, test := range tests { t.Run("", func(t *testing.T) { - result := trimDaemon(test.input) + result := TrimDaemon(test.input) if !reflect.DeepEqual(result, test.expected) { t.Errorf("Expected %v, but got %v", test.expected, result) }