Skip to content

Commit

Permalink
Merge pull request #319 from kolyshkin/more-linters
Browse files Browse the repository at this point in the history
CI: enable more linters
  • Loading branch information
guelfey authored Mar 17, 2022
2 parents 8305584 + 972b6b1 commit fc37d31
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 67 deletions.
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# For documentation, see https://golangci-lint.run/usage/configuration/

linters:
enable:
- gofumpt
- unconvert
- unparam
2 changes: 1 addition & 1 deletion _examples/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
}
defer conn.Close()

var rules = []string{
rules := []string{
"type='signal',member='Notify',path='/org/freedesktop/Notifications',interface='org.freedesktop.Notifications'",
"type='method_call',member='Notify',path='/org/freedesktop/Notifications',interface='org.freedesktop.Notifications'",
"type='method_return',member='Notify',path='/org/freedesktop/Notifications',interface='org.freedesktop.Notifications'",
Expand Down
14 changes: 8 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func SessionBus() (conn *Conn, err error) {
func getSessionBusAddress(autolaunch bool) (string, error) {
if address := os.Getenv("DBUS_SESSION_BUS_ADDRESS"); address != "" && address != "autolaunch:" {
return address, nil

} else if address := tryDiscoverDbusSessionBusAddress(); address != "" {
os.Setenv("DBUS_SESSION_BUS_ADDRESS", address)
return address, nil
Expand Down Expand Up @@ -630,7 +629,7 @@ func (conn *Conn) AddMatchSignal(options ...MatchOption) error {

// AddMatchSignalContext acts like AddMatchSignal but takes a context.
func (conn *Conn) AddMatchSignalContext(ctx context.Context, options ...MatchOption) error {
options = append([]MatchOption{withMatchType("signal")}, options...)
options = append([]MatchOption{withMatchTypeSignal()}, options...)
return conn.busObj.CallWithContext(
ctx,
"org.freedesktop.DBus.AddMatch", 0,
Expand All @@ -645,7 +644,7 @@ func (conn *Conn) RemoveMatchSignal(options ...MatchOption) error {

// RemoveMatchSignalContext acts like RemoveMatchSignal but takes a context.
func (conn *Conn) RemoveMatchSignalContext(ctx context.Context, options ...MatchOption) error {
options = append([]MatchOption{withMatchType("signal")}, options...)
options = append([]MatchOption{withMatchTypeSignal()}, options...)
return conn.busObj.CallWithContext(
ctx,
"org.freedesktop.DBus.RemoveMatch", 0,
Expand Down Expand Up @@ -740,9 +739,7 @@ type transport interface {
SendMessage(*Message) error
}

var (
transports = make(map[string]func(string) (transport, error))
)
var transports = make(map[string]func(string) (transport, error))

func getTransport(address string) (transport, error) {
var err error
Expand Down Expand Up @@ -853,16 +850,19 @@ type nameTracker struct {
func newNameTracker() *nameTracker {
return &nameTracker{names: map[string]struct{}{}}
}

func (tracker *nameTracker) acquireUniqueConnectionName(name string) {
tracker.lck.Lock()
defer tracker.lck.Unlock()
tracker.unique = name
}

func (tracker *nameTracker) acquireName(name string) {
tracker.lck.Lock()
defer tracker.lck.Unlock()
tracker.names[name] = struct{}{}
}

func (tracker *nameTracker) loseName(name string) {
tracker.lck.Lock()
defer tracker.lck.Unlock()
Expand All @@ -874,12 +874,14 @@ func (tracker *nameTracker) uniqueNameIsKnown() bool {
defer tracker.lck.RUnlock()
return tracker.unique != ""
}

func (tracker *nameTracker) isKnownName(name string) bool {
tracker.lck.RLock()
defer tracker.lck.RUnlock()
_, ok := tracker.names[name]
return ok || name == tracker.unique
}

func (tracker *nameTracker) listKnownNames() []string {
tracker.lck.RLock()
defer tracker.lck.RUnlock()
Expand Down
1 change: 0 additions & 1 deletion conn_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const defaultSystemBusAddress = "unix:path=/opt/local/var/run/dbus/system_bus_so
func getSessionBusPlatformAddress() (string, error) {
cmd := exec.Command("launchctl", "getenv", "DBUS_LAUNCHD_SESSION_BUS_SOCKET")
b, err := cmd.CombinedOutput()

if err != nil {
return "", err
}
Expand Down
1 change: 0 additions & 1 deletion conn_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var execCommand = exec.Command
func getSessionBusPlatformAddress() (string, error) {
cmd := execCommand("dbus-launch")
b, err := cmd.CombinedOutput()

if err != nil {
return "", err
}
Expand Down
12 changes: 6 additions & 6 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func TestAddAndRemoveMatchSignalContext(t *testing.T) {
if err = conn.Emit("/", "org.test.CtxTest"); err != nil {
t.Fatal(err)
}
if sig := waitSignal(sigc, "org.test.CtxTest", time.Second); sig == nil {
if sig := waitSignal(sigc, "org.test.CtxTest"); sig == nil {
t.Fatal("signal receive timed out")
}

Expand All @@ -294,7 +294,7 @@ func TestAddAndRemoveMatchSignalContext(t *testing.T) {
if err = conn.Emit("/", "org.test.CtxTest"); err != nil {
t.Fatal(err)
}
if sig := waitSignal(sigc, "org.test.CtxTest", time.Second); sig != nil {
if sig := waitSignal(sigc, "org.test.CtxTest"); sig != nil {
t.Fatalf("unsubscribed from %q signal, but received %#v", "org.test.CtxTest", sig)
}
}
Expand All @@ -319,7 +319,7 @@ func TestAddAndRemoveMatchSignal(t *testing.T) {
if err = conn.Emit("/", "org.test.Test"); err != nil {
t.Fatal(err)
}
if sig := waitSignal(sigc, "org.test.Test", time.Second); sig == nil {
if sig := waitSignal(sigc, "org.test.Test"); sig == nil {
t.Fatal("signal receive timed out")
}

Expand All @@ -333,19 +333,19 @@ func TestAddAndRemoveMatchSignal(t *testing.T) {
if err = conn.Emit("/", "org.test.Test"); err != nil {
t.Fatal(err)
}
if sig := waitSignal(sigc, "org.test.Test", time.Second); sig != nil {
if sig := waitSignal(sigc, "org.test.Test"); sig != nil {
t.Fatalf("unsubscribed from %q signal, but received %#v", "org.test.Test", sig)
}
}

func waitSignal(sigc <-chan *Signal, name string, timeout time.Duration) *Signal {
func waitSignal(sigc <-chan *Signal, name string) *Signal {
for {
select {
case sig := <-sigc:
if sig.Name == name {
return sig
}
case <-time.After(timeout):
case <-time.After(time.Second):
return nil
}
}
Expand Down
12 changes: 6 additions & 6 deletions dbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func storeBase(dest, src reflect.Value) error {

func setDest(dest, src reflect.Value) error {
if !isVariant(src.Type()) && isVariant(dest.Type()) {
//special conversion for dbus.Variant
// special conversion for dbus.Variant
dest.Set(reflect.ValueOf(MakeVariant(src.Interface())))
return nil
}
Expand Down Expand Up @@ -163,8 +163,8 @@ func storeMapIntoVariant(dest, src reflect.Value) error {
func storeMapIntoInterface(dest, src reflect.Value) error {
var dv reflect.Value
if isVariant(src.Type().Elem()) {
//Convert variants to interface{} recursively when converting
//to interface{}
// Convert variants to interface{} recursively when converting
// to interface{}
dv = reflect.MakeMap(
reflect.MapOf(src.Type().Key(), interfaceType))
} else {
Expand Down Expand Up @@ -197,7 +197,7 @@ func storeMapIntoMap(dest, src reflect.Value) error {
func storeSlice(dest, src reflect.Value) error {
switch {
case src.Type() == interfacesType && dest.Kind() == reflect.Struct:
//The decoder always decodes structs as slices of interface{}
// The decoder always decodes structs as slices of interface{}
return storeStruct(dest, src)
case !kindsAreCompatible(dest.Type(), src.Type()):
return fmt.Errorf(
Expand Down Expand Up @@ -257,8 +257,8 @@ func storeSliceIntoVariant(dest, src reflect.Value) error {
func storeSliceIntoInterface(dest, src reflect.Value) error {
var dv reflect.Value
if isVariant(src.Type().Elem()) {
//Convert variants to interface{} recursively when converting
//to interface{}
// Convert variants to interface{} recursively when converting
// to interface{}
dv = reflect.MakeSlice(reflect.SliceOf(interfaceType),
src.Len(), src.Cap())
} else {
Expand Down
6 changes: 3 additions & 3 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ func (dec *decoder) decode(s string, depth int) interface{} {
// Even for empty arrays, the correct padding must be included
align := alignment(typeFor(s[1:]))
if len(s) > 1 && s[1] == '(' {
//Special case for arrays of structs
//structs decode as a slice of interface{} values
//but the dbus alignment does not match this
// Special case for arrays of structs
// structs decode as a slice of interface{} values
// but the dbus alignment does not match this
align = 8
}
dec.align(align)
Expand Down
2 changes: 1 addition & 1 deletion default_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (m exportedMethod) Call(args ...interface{}) ([]interface{}, error) {
out[i] = val.Interface()
}
if nilErr || err == nil {
//concrete type to interface nil is a special case
// concrete type to interface nil is a special case
return out, nil
}
return out, err
Expand Down
12 changes: 8 additions & 4 deletions exec_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ import (
// How to mock exec.Command for unit tests
// https://stackoverflow.com/q/45789101/10513533

var mockedExitStatus = 0
var mockedStdout string
var (
mockedExitStatus = 0
mockedStdout string
)

func fakeExecCommand(command string, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestExecCommandHelper", "--", command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
es := strconv.Itoa(mockedExitStatus)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1",
cmd.Env = []string{
"GO_WANT_HELPER_PROCESS=1",
"STDOUT=" + mockedStdout,
"EXIT_STATUS=" + es}
"EXIT_STATUS=" + es,
}
return cmd
}

Expand Down
8 changes: 4 additions & 4 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func WithMatchOption(key, value string) MatchOption {
return MatchOption{key, value}
}

// doesn't make sense to export this option because clients can only
// subscribe to messages with signal type.
func withMatchType(typ string) MatchOption {
return WithMatchOption("type", typ)
// It does not make sense to have a public WithMatchType function
// because clients can only subscribe to messages with signal type.
func withMatchTypeSignal() MatchOption {
return WithMatchOption("type", "signal")
}

// WithMatchSender sets sender match option.
Expand Down
2 changes: 1 addition & 1 deletion match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "testing"

func TestFormatMatchOptions(t *testing.T) {
opts := []MatchOption{
withMatchType("signal"),
withMatchTypeSignal(),
WithMatchSender("org.bluez"),
WithMatchInterface("org.freedesktop.DBus.Properties"),
WithMatchMember("PropertiesChanged"),
Expand Down
3 changes: 1 addition & 2 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dbus
import "testing"

func TestMessage_validateHeader(t *testing.T) {
var tcs = []struct {
tcs := []struct {
msg Message
err error
}{
Expand Down Expand Up @@ -85,7 +85,6 @@ func TestMessage_validateHeader(t *testing.T) {
err: InvalidMessageError("invalid error name"),
},
{

msg: Message{
Type: TypeError,
Headers: map[HeaderField]Variant{
Expand Down
4 changes: 2 additions & 2 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (o *Object) CallWithContext(ctx context.Context, method string, flags Flags
// Deprecated: use (*Conn) AddMatchSignal instead.
func (o *Object) AddMatchSignal(iface, member string, options ...MatchOption) *Call {
base := []MatchOption{
withMatchType("signal"),
withMatchTypeSignal(),
WithMatchInterface(iface),
WithMatchMember(member),
}
Expand All @@ -65,7 +65,7 @@ func (o *Object) AddMatchSignal(iface, member string, options ...MatchOption) *C
// Deprecated: use (*Conn) RemoveMatchSignal instead.
func (o *Object) RemoveMatchSignal(iface, member string, options ...MatchOption) *Call {
base := []MatchOption{
withMatchType("signal"),
withMatchTypeSignal(),
WithMatchInterface(iface),
WithMatchMember(member),
}
Expand Down
2 changes: 1 addition & 1 deletion prop/prop.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ type Prop struct {
// Introspection returns the introspection data for p.
// The "name" argument is used as the property's name in the resulting data.
func (p *Prop) Introspection(name string) introspect.Property {
var result = introspect.Property{Name: name, Type: dbus.SignatureOf(p.Value).String()}
result := introspect.Property{Name: name, Type: dbus.SignatureOf(p.Value).String()}
if p.Writable {
result.Access = "readwrite"
} else {
Expand Down
33 changes: 11 additions & 22 deletions sequential_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package dbus

import (
"context"
"errors"
"fmt"
"testing"
"time"
)
Expand All @@ -20,9 +18,7 @@ func TestSequentialHandlerNoDrop(t *testing.T) {

writeSignals(handler, 1000)

if err := readSignals(t, channel, 1000); err != nil {
t.Error(err)
}
readSignals(t, channel)
}

// Verifies that signals are written to the destination channel in the
Expand All @@ -39,9 +35,7 @@ func TestSequentialHandlerSequential(t *testing.T) {

// Concurrently read and write signals
go func() {
if err := readSignals(t, channel, 1000); err != nil {
t.Error(err)
}
readSignals(t, channel)
close(done)
}()
writeSignals(handler, 1000)
Expand All @@ -63,9 +57,7 @@ func TestSequentialHandlerMultipleChannel(t *testing.T) {

writeSignals(handler, 1000)

if err := readSignals(t, channelTwo, 1000); err != nil {
t.Error(err)
}
readSignals(t, channelTwo)
}

// Test that removing one channel results in no more messages being
Expand Down Expand Up @@ -119,14 +111,10 @@ func TestSequentialHandler_RemoveOneChannelOfMany(t *testing.T) {
}

// Check that closing channel two does not close channel one.
if err := readSignals(t, channelOne, 1000); err != nil {
t.Error(err)
}
readSignals(t, channelOne)

// Check that closing channel two does not close channel three.
if err := readSignals(t, channelThree, 1000); err != nil {
t.Error(err)
}
readSignals(t, channelThree)
}

// Test that Terminate() closes all channels that were attached at the time.
Expand Down Expand Up @@ -221,21 +209,22 @@ func writeSignals(handler SignalHandler, count int) {
}
}

func readSignals(t *testing.T, channel <-chan *Signal, count int) error {
func readSignals(t *testing.T, channel <-chan *Signal) {
// Overly generous timeout
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
for i := 1; i <= count; i++ {
for i := 1; i <= 1000; i++ {
select {
case signal := <-channel:
if signal.Sequence != Sequence(i) {
return fmt.Errorf("Received signal out of order. Expected %v, got %v", i, signal.Sequence)
t.Errorf("Received signal out of order. Expected %v, got %v", i, signal.Sequence)
return
}
case <-ctx.Done():
return errors.New("Timeout occurred before all messages received")
t.Error("Timeout occurred before all messages received")
return
}
}
return nil
}

func countSignals(channel <-chan *Signal) (count int, closed bool) {
Expand Down
Loading

0 comments on commit fc37d31

Please sign in to comment.