Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add golangci-lint linter and go tests #50

Merged
merged 8 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI

on:
push:
branches:
- master

pull_request:

jobs:
golangci:
name: Lint
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Go environment
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Run
uses: golangci/golangci-lint-action@v3
with:
version: v1.52
2 changes: 0 additions & 2 deletions .github/workflows/releaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Go environment
uses: actions/setup-go@v4
Expand Down
45 changes: 45 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Tests

on:
push:
branches:
- master

pull_request:

jobs:
build:
name: Build
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Go environment
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Setup VS environment
shell: cmd
run: |
for /f "usebackq delims=" %%i in (`vswhere.exe -latest -property installationPath`) do echo %%i\VC\Auxiliary\Build>>%GITHUB_PATH%

- name: Build
shell: cmd
run: build.bat

test:
name: Go
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Go environment
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Test
run: go test -v ./...
13 changes: 13 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
run:
timeout: 5m

linters:
disable-all: true
enable:
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- whitespace
- unused
46 changes: 46 additions & 0 deletions pkg/config/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config

import (
"reflect"
"testing"
)

func TestUnmarshalAppConfFromIni(t *testing.T) {
input := `
password = abcde

[defaults]
server_port = 7000
log_level = info
log_max_days = 5
protocol = kcp
login_fail_exit = false
user = user
tcp_mux = true
manual_start = true
delete_after_days = 1
`
expected := App{
Password: "abcde",
Defaults: ClientCommon{
ServerPort: "7000",
LogLevel: "info",
LogMaxDays: 5,
Protocol: "kcp",
LoginFailExit: false,
User: "user",
TCPMux: true,
ManualStart: true,
AutoDelete: AutoDelete{
DeleteAfterDays: 1,
},
},
}
var actual App
if err := UnmarshalAppConfFromIni([]byte(input), &actual); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expected: %v, got: %v", expected, actual)
}
}
72 changes: 72 additions & 0 deletions pkg/config/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package config

import (
"reflect"
"testing"
"time"
)

func TestUnmarshalClientConfFromIni(t *testing.T) {
input := `
[common]
server_addr = example.com
server_port = 7001
token = 123456
manual_start = true
delete_method = absolute
delete_after_date = 2023-03-23T00:00:00Z
meta_1 = value

[ssh]
type = tcp
local_ip = 192.168.1.1
local_port = 22
remote_port = 6000
meta_2 = value
`
expected := NewDefaultClientConfig()
expected.ServerAddress = "example.com"
expected.ServerPort = "7001"
expected.Token = "123456"
expected.ManualStart = true
expected.Custom = map[string]string{"meta_1": "value"}
expected.DeleteMethod = "absolute"
expected.DeleteAfterDate = time.Date(2023, 3, 23, 0, 0, 0, 0, time.UTC)
expected.Proxies = append(expected.Proxies, &Proxy{
BaseProxyConf: BaseProxyConf{
Name: "ssh",
Type: "tcp",
LocalIP: "192.168.1.1",
LocalPort: "22",
Custom: map[string]string{"meta_2": "value"},
},
RemotePort: "6000",
})
cc, err := UnmarshalClientConfFromIni([]byte(input))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(cc, expected) {
t.Errorf("Expected: %v, got: %v", expected, cc)
}
}

func TestProxyGetAlias(t *testing.T) {
input := `
[range:test_tcp]
type = tcp
local_ip = 127.0.0.1
local_port = 6000-6006,6007
remote_port = 6000-6006,6007
`
expected := []string{"test_tcp_0", "test_tcp_1", "test_tcp_2", "test_tcp_3",
"test_tcp_4", "test_tcp_5", "test_tcp_6", "test_tcp_7"}
proxy, err := UnmarshalProxyFromIni([]byte(input))
if err != nil {
t.Fatal(err)
}
output := proxy.GetAlias()
if !reflect.DeepEqual(output, expected) {
t.Errorf("Expected: %v, got: %v", expected, output)
}
}
2 changes: 1 addition & 1 deletion pkg/config/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Expiry(configPath string, del AutoDelete) (time.Duration, error) {
}
switch del.DeleteMethod {
case consts.DeleteAbsolute:
return del.DeleteAfterDate.Sub(time.Now()), nil
return time.Until(del.DeleteAfterDate), nil
case consts.DeleteRelative:
if del.DeleteAfterDays > 0 {
elapsed := time.Since(fInfo.ModTime())
Expand Down
32 changes: 32 additions & 0 deletions pkg/config/conf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package config

import (
"os"
"testing"
"time"
)

func TestExpiry(t *testing.T) {
if err := os.MkdirAll("testdata", 0750); err != nil {
t.Fatal(err)
}
os.Chdir("testdata")
os.WriteFile("example.ini", []byte("test"), 0666)
tests := []struct {
input AutoDelete
expected time.Duration
}{
{input: AutoDelete{DeleteMethod: "relative", DeleteAfterDays: 5}, expected: 5 * time.Hour * 24},
{input: AutoDelete{DeleteMethod: "absolute", DeleteAfterDate: time.Now().AddDate(0, 0, 3)}, expected: 3 * time.Hour * 24},
}
for i, test := range tests {
output, err := Expiry("example.ini", test.input)
if err != nil {
t.Error(err)
continue
}
if (test.expected - output).Abs() > 3*time.Second {
t.Errorf("Test %d: expected: %v, got: %v", i, test.expected, output)
}
}
}
30 changes: 30 additions & 0 deletions pkg/layout/greedy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package layout

import (
"github.com/lxn/walk"
"testing"
)

func TestNewGreedyLayoutItem(t *testing.T) {
tests := []struct {
input walk.Orientation
expected, unexpected []walk.LayoutFlags
}{
{input: walk.Horizontal, expected: []walk.LayoutFlags{walk.GreedyHorz}, unexpected: []walk.LayoutFlags{walk.GreedyVert}},
{input: walk.Vertical, expected: []walk.LayoutFlags{walk.GreedyVert}, unexpected: []walk.LayoutFlags{walk.GreedyHorz}},
{input: walk.NoOrientation, expected: []walk.LayoutFlags{walk.GreedyHorz, walk.GreedyVert}, unexpected: nil},
}
for i, test := range tests {
flags := NewGreedyLayoutItem(test.input).LayoutFlags()
for _, f := range test.expected {
if f&flags == 0 {
t.Errorf("Test %d: expected: %v, got: %v", i, f, flags)
}
}
for _, f := range test.unexpected {
if f&flags > 0 {
t.Errorf("Test %d: unexpected: %v, got: %v", i, f, flags)
}
}
}
}
11 changes: 11 additions & 0 deletions pkg/sec/passwd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sec

import "testing"

func TestEncryptPassword(t *testing.T) {
output := EncryptPassword("123456")
expected := "fEqNCco3Yq9h5ZUglD3CZJT4lBs="
if output != expected {
t.Errorf("Expected: %v, got: %v", expected, output)
}
}
3 changes: 3 additions & 0 deletions pkg/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func FindLogFiles(path string) ([]string, []string, error) {
fileDir, fileName := filepath.Split(path)
baseName, ext := SplitExt(fileName)
pattern := regexp.MustCompile(`^\.\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$`)
if fileDir == "" {
fileDir = "."
}
files, err := os.ReadDir(fileDir)
if err != nil {
return nil, nil, err
Expand Down
83 changes: 83 additions & 0 deletions pkg/util/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package util

import (
"os"
"reflect"
"testing"
)

func TestSplitExt(t *testing.T) {
tests := []struct {
input string
expectedName string
expectedExt string
}{
{input: "C:\\test\\a.ini", expectedName: "a", expectedExt: ".ini"},
{input: "b.exe", expectedName: "b", expectedExt: ".exe"},
{input: "c", expectedName: "c", expectedExt: ""},
{input: "", expectedName: "", expectedExt: ""},
}
for i, test := range tests {
name, ext := SplitExt(test.input)
if name != test.expectedName {
t.Errorf("Test %d: expected: %v, got: %v", i, test.expectedName, name)
}
if ext != test.expectedExt {
t.Errorf("Test %d: expected: %v, got: %v", i, test.expectedExt, ext)
}
}
}

func TestFindLogFiles(t *testing.T) {
tests := []struct {
create []string
expectedFiles []string
expectedDates []string
}{
{
create: []string{"example.log", "example.2023-03-20.log", "example.2023-03-21.log", "example.2023-03-21T01.log"},
expectedFiles: []string{"example.log", "example.2023-03-20.log", "example.2023-03-21.log"},
expectedDates: []string{"", "2023-03-20", "2023-03-21"},
},
}
if err := os.MkdirAll("testdata", 0750); err != nil {
t.Fatal(err)
}
os.Chdir("testdata")
for i, test := range tests {
for _, f := range test.create {
os.WriteFile(f, []byte("test"), 0666)
}
logs, dates, err := FindLogFiles(test.create[0])
if err != nil {
t.Error(err)
continue
}
if !reflect.DeepEqual(logs, test.expectedFiles) {
t.Errorf("Test %d: expected: %v, got: %v", i, test.expectedFiles, logs)
}
if !reflect.DeepEqual(dates, test.expectedDates) {
t.Errorf("Test %d: expected: %v, got: %v", i, test.expectedDates, dates)
}
}
}

func TestAddFileSuffix(t *testing.T) {
tests := []struct {
input string
suffix string
expected string
}{
{input: "C:\\test\\a.ini", suffix: "_1", expected: "a_1.ini"},
{input: "b.exe", suffix: "_2", expected: "b_2.exe"},
{input: "c", suffix: "_3", expected: "c_3"},
{input: "", suffix: "_4", expected: "_4"},
{input: "", suffix: "", expected: ""},
}
for i, test := range tests {
output := AddFileSuffix(test.input, test.suffix)
if output != test.expected {
t.Errorf("Test %d: expected: %v, got: %v", i, test.expected, output)
}
}
}
Loading