1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/utils/utils_test.go
Sam Roberts 4bb725db9e golangci-lint configuration file
golangci-lint can run all the currently enabled linters, and as far as I
can tell, does it in under 5 seconds as opposed to over 180 seconds
(compare `time make cilint` and `time make lint`).

Some of the linters that are listed in the "enabled" section but
commented out looked like a good idea to me, and fairly low hanging
fruit to fix, but they are not passing at the moment.

All the linters covered in the current Makefile are run.

TODO:
- replace lint target in Makefile with golangci-lint
- remove .github/workflow/errcheck.yml
2020-05-16 12:34:57 -07:00

134 lines
2.4 KiB
Go

package utils
import (
"os/exec"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_DoesNotInclude(t *testing.T) {
tests := []struct {
name string
strs []string
val string
expected bool
}{
{
name: "when included",
strs: []string{"a", "b", "c"},
val: "b",
expected: false,
},
{
name: "when not included",
strs: []string{"a", "b", "c"},
val: "f",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := DoesNotInclude(tt.strs, tt.val)
if tt.expected != actual {
t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual)
}
})
}
}
func Test_ExecuteCommand(t *testing.T) {
tests := []struct {
name string
cmd *exec.Cmd
expected string
}{
{
name: "with nil command",
cmd: nil,
expected: "",
},
{
name: "with defined command",
cmd: exec.Command("echo", "cats"),
expected: "cats\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := ExecuteCommand(tt.cmd)
if tt.expected != actual {
t.Errorf("\nexpected: %s\n got: %s", tt.expected, actual)
}
})
}
}
func Test_FindMatch(t *testing.T) {
expected := [][]string{{"SSID: 7E5B5C", "7E5B5C"}}
result := FindMatch(`s*SSID: (.+)s*`, "SSID: 7E5B5C")
assert.Equal(t, expected, result)
}
func Test_Includes(t *testing.T) {
tests := []struct {
name string
strs []string
val string
expected bool
}{
{
name: "when included",
strs: []string{"a", "b", "c"},
val: "b",
expected: true,
},
{
name: "when not included",
strs: []string{"a", "b", "c"},
val: "f",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := Includes(tt.strs, tt.val)
if tt.expected != actual {
t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual)
}
})
}
}
func Test_ReadFileBytes(t *testing.T) {
tests := []struct {
name string
file string
expected []byte
}{
{
name: "with non-existent file",
file: "/tmp/junk-daa6bf613f4c.md",
expected: []byte{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, _ := ReadFileBytes(tt.file)
if reflect.DeepEqual(tt.expected, actual) == false {
t.Errorf("\nexpected: %q\n got: %q", tt.expected, actual)
}
})
}
}