From 417c8887b9368f75cec0f35d075b9c506b6a1645 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 17 Aug 2022 22:37:03 +0530 Subject: [PATCH] Start on testing infra for command to JSON serialization --- kitty/rc/__init__.py | 1 - kitty_tests/main.py | 6 +++++- tools/cmd/at/main.go | 2 ++ tools/cmd/at/main_test.go | 27 +++++++++++++++++++++++++++ tools/cmd/at/template.go | 28 +++++++++++++++++++++++----- 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/kitty/rc/__init__.py b/kitty/rc/__init__.py index 8b1378917..e69de29bb 100644 --- a/kitty/rc/__init__.py +++ b/kitty/rc/__init__.py @@ -1 +0,0 @@ - diff --git a/kitty_tests/main.py b/kitty_tests/main.py index 69e76e8a9..570fa0f8d 100644 --- a/kitty_tests/main.py +++ b/kitty_tests/main.py @@ -113,7 +113,11 @@ def create_go_filter(packages: List[str], *names: str) -> str: if not go: return '' all_tests = set() - for line in subprocess.check_output(f'{go} test -list .'.split() + packages).decode().splitlines(): + try: + lines = subprocess.check_output(f'{go} test -list .'.split() + packages).decode().splitlines() + except subprocess.CalledProcessError as e: + raise SystemExit(e.returncode) + for line in lines: if line.startswith('Test'): all_tests.add(line[4:]) tests = set(names) & all_tests diff --git a/tools/cmd/at/main.go b/tools/cmd/at/main.go index eb2811ef3..31e3f299d 100644 --- a/tools/cmd/at/main.go +++ b/tools/cmd/at/main.go @@ -19,6 +19,8 @@ import ( "kitty/tools/utils" ) +var ProtocolVersion [3]int = [3]int{0, 20, 0} + func add_bool_set(cmd *cobra.Command, name string, short string, usage string) *bool { if short == "" { return cmd.Flags().Bool(name, false, usage) diff --git a/tools/cmd/at/main_test.go b/tools/cmd/at/main_test.go index 857a60ceb..587e94f95 100644 --- a/tools/cmd/at/main_test.go +++ b/tools/cmd/at/main_test.go @@ -2,11 +2,38 @@ package at import ( "encoding/json" + "fmt" "kitty/tools/crypto" "kitty/tools/utils" "testing" ) +func TestCommandToJSON(t *testing.T) { + pv := fmt.Sprint(ProtocolVersion[0], ",", ProtocolVersion[1], ",", ProtocolVersion[2]) + rc, err := create_rc_ls([]string{}) + if err != nil { + t.Fatal(err) + } + + marshal := func(rc *utils.RemoteControlCmd) string { + q, err := json.Marshal(rc) + if err != nil { + t.Fatal(err) + } + return string(q) + } + + test := func(rc *utils.RemoteControlCmd, rest string) { + q := marshal(rc) + expected := `{"cmd":"` + rc.Cmd + `","version":[` + pv + `]` + expected += rest + "}" + if q != expected { + t.Fatalf("expected != actual: %#v != %#v", expected, q) + } + } + test(rc, "") +} + func TestRCSerialization(t *testing.T) { serializer, err := create_serializer("", "") if err != nil { diff --git a/tools/cmd/at/template.go b/tools/cmd/at/template.go index c4ca4d21d..d5d3fa658 100644 --- a/tools/cmd/at/template.go +++ b/tools/cmd/at/template.go @@ -24,19 +24,37 @@ type CMD_NAME_json_type struct { var CMD_NAME_json CMD_NAME_json_type +func create_payload_CMD_NAME(args []string) (payload map[string]interface{}, err error) { + return +} + +func create_rc_CMD_NAME(args []string) (*utils.RemoteControlCmd, error) { + var err error + payload, err := create_payload_CMD_NAME(args) + if err != nil { + return nil, err + } + rc := utils.RemoteControlCmd{ + Cmd: "CLI_NAME", + Version: ProtocolVersion, + NoResponse: NO_RESPONSE_BASE, + Payload: payload, + } + return &rc, nil +} + func run_CMD_NAME(cmd *cobra.Command, args []string) (err error) { SET_OPTION_VALUES_CODE - rc := utils.RemoteControlCmd{ - Cmd: "CLI_NAME", - Version: [3]int{0, 20, 0}, - NoResponse: NO_RESPONSE_BASE, + rc, err := create_rc_CMD_NAME(args) + if err != nil { + return err } nrv, err := cmd.Flags().GetBool("no-response") if err == nil { rc.NoResponse = nrv } - err = send_rc_command(&rc, WAIT_TIMEOUT) + err = send_rc_command(rc, WAIT_TIMEOUT) return }