Browse Source

chore: add more tests (#3006)

Kevin Wan 2 years ago
parent
commit
3a493cd6a6

+ 44 - 0
core/codec/gzip_test.go

@@ -2,6 +2,8 @@ package codec
 
 import (
 	"bytes"
+	"compress/gzip"
+	"errors"
 	"fmt"
 	"testing"
 
@@ -21,3 +23,45 @@ func TestGzip(t *testing.T) {
 	assert.True(t, len(bs) < buf.Len())
 	assert.Equal(t, buf.Bytes(), actual)
 }
+
+func TestGunzip(t *testing.T) {
+	tests := []struct {
+		name        string
+		input       []byte
+		expected    []byte
+		expectedErr error
+	}{
+		{
+			name: "valid input",
+			input: func() []byte {
+				var buf bytes.Buffer
+				gz := gzip.NewWriter(&buf)
+				gz.Write([]byte("hello"))
+				gz.Close()
+				return buf.Bytes()
+			}(),
+			expected:    []byte("hello"),
+			expectedErr: nil,
+		},
+		{
+			name:        "invalid input",
+			input:       []byte("invalid input"),
+			expected:    nil,
+			expectedErr: gzip.ErrHeader,
+		},
+	}
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			result, err := Gunzip(test.input)
+
+			if !bytes.Equal(result, test.expected) {
+				t.Errorf("unexpected result: %v", result)
+			}
+
+			if !errors.Is(err, test.expectedErr) {
+				t.Errorf("unexpected error: %v", err)
+			}
+		})
+	}
+}

+ 0 - 1
core/fs/files+polyfill.go

@@ -1,5 +1,4 @@
 //go:build windows
-// +build windows
 
 package fs
 

+ 0 - 1
core/fs/files.go

@@ -1,5 +1,4 @@
 //go:build linux || darwin
-// +build linux darwin
 
 package fs
 

+ 0 - 1
core/mr/mapreduce_fuzzcase_test.go

@@ -1,5 +1,4 @@
 //go:build fuzz
-// +build fuzz
 
 package mr
 

+ 0 - 1
core/proc/goroutines+polyfill.go

@@ -1,5 +1,4 @@
 //go:build windows
-// +build windows
 
 package proc
 

+ 0 - 1
core/proc/goroutines.go

@@ -1,5 +1,4 @@
 //go:build linux || darwin
-// +build linux darwin
 
 package proc
 

+ 0 - 1
core/proc/profile+polyfill.go

@@ -1,5 +1,4 @@
 //go:build windows
-// +build windows
 
 package proc
 

+ 0 - 1
core/proc/profile.go

@@ -1,5 +1,4 @@
 //go:build linux || darwin
-// +build linux darwin
 
 package proc
 

+ 0 - 1
core/proc/shutdown+polyfill.go

@@ -1,5 +1,4 @@
 //go:build windows
-// +build windows
 
 package proc
 

+ 0 - 1
core/proc/shutdown.go

@@ -1,5 +1,4 @@
 //go:build linux || darwin
-// +build linux darwin
 
 package proc
 

+ 0 - 1
core/proc/shutdown_test.go

@@ -1,5 +1,4 @@
 //go:build linux || darwin
-// +build linux darwin
 
 package proc
 

+ 0 - 1
core/proc/signals+polyfill.go

@@ -1,5 +1,4 @@
 //go:build windows
-// +build windows
 
 package proc
 

+ 0 - 1
core/proc/signals.go

@@ -1,5 +1,4 @@
 //go:build linux || darwin
-// +build linux darwin
 
 package proc
 

+ 0 - 1
core/search/tree_debug.go

@@ -1,5 +1,4 @@
 //go:build debug
-// +build debug
 
 package search
 

+ 0 - 1
core/stat/alert+polyfill.go

@@ -1,5 +1,4 @@
 //go:build !linux
-// +build !linux
 
 package stat
 

+ 0 - 1
core/stat/alert.go

@@ -1,5 +1,4 @@
 //go:build linux
-// +build linux
 
 package stat
 

+ 0 - 1
core/stat/alert_test.go

@@ -1,5 +1,4 @@
 //go:build linux
-// +build linux
 
 package stat
 

+ 2 - 4
core/stat/internal/cgroup_linux.go

@@ -278,10 +278,8 @@ func runningInUserNS() bool {
 		var a, b, c int64
 		fmt.Sscanf(line, "%d %d %d", &a, &b, &c)
 
-		/*
-		 * We assume we are in the initial user namespace if we have a full
-		 * range - 4294967295 uids starting at uid 0.
-		 */
+		// We assume we are in the initial user namespace if we have a full
+		// range - 4294967295 uids starting at uid 0.
 		if a == 0 && b == 0 && c == 4294967295 {
 			return
 		}

+ 0 - 1
core/stat/internal/cpu_other.go

@@ -1,5 +1,4 @@
 //go:build !linux
-// +build !linux
 
 package internal
 

+ 0 - 3
core/stringx/node_fuzz_test.go

@@ -1,6 +1,3 @@
-//go:build go1.18
-// +build go1.18
-
 package stringx
 
 import (

+ 20 - 0
core/utils/version_test.go

@@ -15,8 +15,10 @@ func TestCompareVersions(t *testing.T) {
 		out      bool
 	}{
 		{"1", "1.0.1", ">", false},
+		{"1.0.1", "1.0", "<", false},
 		{"1", "0.9.9", ">", true},
 		{"1", "1.0-1", "<", true},
+		{"1", "1.0-1", "!", false},
 		{"1.0.1", "1-0.1", "<", false},
 		{"1.0.1", "1.0.1", "==", true},
 		{"1.0.1", "1.0.2", "==", false},
@@ -37,3 +39,21 @@ func TestCompareVersions(t *testing.T) {
 		})
 	}
 }
+
+func TestStrsToInts(t *testing.T) {
+	testCases := []struct {
+		input    []string
+		expected []int64
+	}{
+		{[]string{}, nil},
+		{[]string{"1", "2", "3"}, []int64{1, 2, 3}},
+	}
+
+	for _, tc := range testCases {
+		tc := tc
+		t.Run("", func(t *testing.T) {
+			actual := strsToInts(tc.input)
+			assert.Equal(t, tc.expected, actual)
+		})
+	}
+}

+ 0 - 1
tools/goctl/migrate/cancel+polyfill.go

@@ -1,5 +1,4 @@
 //go:build windows
-// +build windows
 
 package migrate
 

+ 0 - 1
tools/goctl/migrate/cancel.go

@@ -1,5 +1,4 @@
 //go:build linux || darwin
-// +build linux darwin
 
 package migrate
 

+ 0 - 1
tools/goctl/util/pathx/readlink+polyfill.go

@@ -1,5 +1,4 @@
 //go:build windows
-// +build windows
 
 package pathx
 

+ 0 - 1
tools/goctl/util/pathx/readlink.go

@@ -1,5 +1,4 @@
 //go:build linux || darwin
-// +build linux darwin
 
 package pathx