Skip to content

Commit

Permalink
tests: add tests for cmd
Browse files Browse the repository at this point in the history
- add Fprint* functions for PrintError, PrintSuccess, etc.
- move actual config loading from NewConfig to LoadConfig to
  be able to easily load a test config for testing
- rename NewHctl argument from "loadCfg" to "testing" and
  only load config from defined config paths if we're not
  in testing mode
- add newTestingHctl to create Hctl instance for testing
- add testCmd function to handle the work for all the cmd
  command testing
  • Loading branch information
xx4h committed Oct 19, 2024
1 parent 227c9cb commit 37b1dd1
Show file tree
Hide file tree
Showing 33 changed files with 764 additions and 70 deletions.
7 changes: 4 additions & 3 deletions cmd/brightness.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"fmt"
"io"
"slices"

"github.com/rs/zerolog/log"
Expand All @@ -30,7 +31,7 @@ var (
brightnessRange = append([]string{"+", "-", "min", "mid", "max"}, util.MakeRangeString(1, 99)...)
)

func newBrightnessCmd(h *pkg.Hctl) *cobra.Command {
func newBrightnessCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "brightness [+|-|min|max|1-99]",
Short: "Change brightness",
Expand All @@ -57,9 +58,9 @@ func newBrightnessCmd(h *pkg.Hctl) *cobra.Command {
c := h.GetRest()
obj, state, sub, err := c.TurnLightOnBrightness(args[0], args[1])
if err != nil {
o.PrintError(err)
o.FprintError(out, err)
} else {
o.PrintSuccessAction(obj, state)
o.FprintSuccess(out, fmt.Sprintf("%s brightness set to %s%%", obj, args[1]))
}
log.Debug().Caller().Msgf("Result: %s(%s) to %s", obj, sub, state)
},
Expand Down
39 changes: 39 additions & 0 deletions cmd/brightness_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 Fabian `xx4h` Sylvester
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"testing"

"github.com/xx4h/hctl/pkg/hctltest"
)

func Test_newCmdBrightness(t *testing.T) {
ms := hctltest.MockServer(t)
h := newTestingHctl(t)
if err := h.SetConfigValue("hub.url", ms.URL); err != nil {
t.Error(err)
}

var tests = map[string]cmdTest{
"set brightness": {
"brightness light.livingroom_other 20",
"(?m)^.*livingroom_other brightness set to 20%",
"",
},
}

testCmd(t, h, tests)
}
19 changes: 8 additions & 11 deletions cmd/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ package cmd
import (
"testing"

"github.com/xx4h/hctl/pkg"
"github.com/xx4h/hctl/pkg/hctltest"
)

func Test_compListStates(t *testing.T) {
ms := hctltest.MockServer(t)
h, err := pkg.NewHctl(false)
if err != nil {
t.Errorf("Error createing new Hctl instance: %+v", err)
}
h := newTestingHctl(t)

if err := h.SetConfigValue("hub.url", ms.URL); err != nil {
t.Errorf("Could not set hub.url to %s: %+v", ms.URL, err)
}
Expand All @@ -47,42 +44,42 @@ func Test_compListStates(t *testing.T) {
nil,
[]string{"brightness"},
"",
5,
6,
},
"serviceCap turn_on": {
nil,
[]string{"turn_on"},
nil,
"",
10,
11,
},
"serviceCap turn_on + state off": {
nil,
[]string{"turn_on"},
nil,
"off",
4,
5,
},
"serviceCap turn_off + state on": {
nil,
[]string{"turn_off"},
nil,
"on",
6,
7,
},
"serviceCap play_media + attrib device_class": {
nil,
[]string{"play_media"},
[]string{"device_class"},
"",
1,
2,
},
"serviceCap play_media + attrib device_class or video_out": {
nil,
[]string{"play_media"},
[]string{"device_class", "video_out"},
"",
2,
3,
},
}

Expand Down
16 changes: 12 additions & 4 deletions cmd/config_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
// editorconfig-checker-enable
)

func newConfigGetCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
func newConfigGetCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "get [PATH]",
Short: "Get configuration parameters",
Expand All @@ -55,14 +55,22 @@ func newConfigGetCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
a, _ := compListConfig("", []string{}, h)
slices.Sort(a)
for _, b := range a {
l := append([]any{}, b, h.GetConfigValue(b))
v, err := h.GetConfigValue(b)
l := []any{}
if err == nil {
l = append(l, b, v)
}
clist = append(clist, l)
}
} else {
l := append([]any{}, args[0], h.GetConfigValue(args[0]))
v, err := h.GetConfigValue(args[0])
if err != nil {
o.FprintError(out, err)
}
l := append([]any{}, args[0], v)
clist = append(clist, l)
}
o.PrintSuccessListWithHeader(header, clist)
o.FprintSuccessListWithHeader(out, header, clist)
},
}

Expand Down
33 changes: 33 additions & 0 deletions cmd/config_get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Fabian `xx4h` Sylvester
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"testing"
)

func Test_newCmdConfigGet(t *testing.T) {
h := newTestingHctl(t)

var tests = map[string]cmdTest{
"get existing option": {
"config get completion.short_names",
"(?m)^OPTION\\s+VALUE$\n^completion.short_names\\s+true",
"",
},
}

testCmd(t, h, tests)
}
6 changes: 3 additions & 3 deletions cmd/config_rem.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
// editorconfig-checker-enable
)

func newConfigRemCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
func newConfigRemCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "remove PATH",
Short: "Set config variables",
Expand All @@ -45,9 +45,9 @@ func newConfigRemCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
},
Run: func(_ *cobra.Command, args []string) {
if err := h.RemoveConfigOptionWrite(args[0]); err != nil {
o.PrintError(err)
o.FprintError(out, err)
}
o.PrintSuccess(fmt.Sprintf("Option `%s` successfully removed.", args[0]))
o.FprintSuccess(out, fmt.Sprintf("Option `%s` successfully removed.", args[0]))
},
}

Expand Down
33 changes: 33 additions & 0 deletions cmd/config_rem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Fabian `xx4h` Sylvester
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"testing"
)

func Test_newCmdConfigRem(t *testing.T) {
h := newTestingHctl(t)

var tests = map[string]cmdTest{
"rem completion.short_names": {
"config remove device_map.g",
"(?m)^.*Option `device_map.g` successfully removed",
"",
},
}

testCmd(t, h, tests)
}
6 changes: 3 additions & 3 deletions cmd/config_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
// editorconfig-checker-enable
)

func newConfigSetCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
func newConfigSetCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "set PATH VALUE",
Short: "Set config variables",
Expand All @@ -48,9 +48,9 @@ func newConfigSetCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
},
Run: func(_ *cobra.Command, args []string) {
if err := h.SetConfigValueWrite(args[0], args[1]); err != nil {
o.PrintError(err)
o.FprintError(out, err)
}
o.PrintSuccess(fmt.Sprintf("Option `%s` successfully set to `%s`.", args[0], args[1]))
o.FprintSuccess(out, fmt.Sprintf("Option `%s` successfully set to `%s`.", args[0], args[1]))
},
}

Expand Down
33 changes: 33 additions & 0 deletions cmd/config_set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Fabian `xx4h` Sylvester
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"testing"
)

func Test_newCmdConfigSet(t *testing.T) {
h := newTestingHctl(t)

var tests = map[string]cmdTest{
"set completion.short_names option": {
"config set completion.short_names false",
"(?m)^.*Option `completion.short_names` successfully set to `false`",
"",
},
}

testCmd(t, h, tests)
}
8 changes: 5 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package cmd

import (
"io"

"github.com/spf13/cobra"

"github.com/xx4h/hctl/pkg"
)

// listCmd represents the list command
func newListCmd(h *pkg.Hctl) *cobra.Command {
func newListCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {

var domains []string
var services []string
Expand All @@ -33,9 +35,9 @@ func newListCmd(h *pkg.Hctl) *cobra.Command {
ValidArgs: []string{"entities", "services"},
RunE: func(_ *cobra.Command, args []string) error {
if len(args) == 0 || args[0] == "entities" {
h.DumpStates(domains)
h.DumpStates(out, domains)
} else if args[0] == "services" {
h.DumpServices(domains, services)
h.DumpServices(out, domains, services)
}
return nil
},
Expand Down
54 changes: 54 additions & 0 deletions cmd/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2024 Fabian `xx4h` Sylvester
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"testing"

"github.com/xx4h/hctl/pkg/hctltest"
)

func Test_newCmdList(t *testing.T) {
ms := hctltest.MockServer(t)
h := newTestingHctl(t)
if err := h.SetConfigValue("hub.url", ms.URL); err != nil {
t.Error(err)
}

var tests = map[string]cmdTest{
"list services": {
"list services",
`^.*Services`,
"",
},
"list services with play_media": {
"list services -s play_media",
`(?m)^.*Services.*\n.*media_player.*\n.*play_media`,
"",
},
"list entities": {
"list entities",
`^.*States`,
"",
},
"list entities of domain media_player": {
"list entities -d media_player",
`^.*States.*\n.*media_player.*\n.*player1.*\n.*player2`,
"",
},
}

testCmd(t, h, tests)
}
Loading

0 comments on commit 37b1dd1

Please sign in to comment.