open-code-review/cmd/opencodereview/parent_cmd_test.go
kite 533b526b4c
Some checks are pending
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (push) Waiting to run
CI / cross-compile (arm64, windows) (push) Waiting to run
CI / test (push) Waiting to run
CI / cross-compile (amd64, darwin) (push) Waiting to run
CI / cross-compile (amd64, windows) (push) Waiting to run
CodeQL Advanced / Analyze (go) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
Deploy Pages / build (push) Waiting to run
Deploy Pages / deploy (push) Blocked by required conditions
chore: add SPDX license headers and automated verification (#740)
* chore: add SPDX license headers to all source files

Add Apache-2.0 SPDX license identifiers and copyright notices to all
tracked .go, .sh, .js, .mjs, .ts, and .tsx source files.

Introduce scripts/verify-license.sh and scripts/add-license.sh for
automated verification and bulk addition of license headers. Integrate
the check into CI (ci.yml) and the Makefile (license-check target as
a prerequisite of the existing check target).

This satisfies the OpenSSF Best Practices Badge requirements for
copyright_per_file and license_per_file.

* fix: restore execute permissions on scripts

* docs: add license header instructions to CONTRIBUTING guides

* docs: add license header instructions to pages contributing guides

* fix(pages): strip unclosed HTML comment markers to satisfy CodeQL

* fix: apply code review suggestions for license scripts

- Fix portability: detect macOS vs Linux stat for permission copy
- Fix has_header: check both SPDX and copyright (match verify logic)
- Fix is_ignored: match on path boundaries to avoid false positives
- Fix year extraction: use consistent pipeline across both scripts
- Fix Bash 3.2 compat: quote array length expansion for set -u

* fix(pages): use loop-until-clean for HTML comment stripping (CodeQL)

* fix(pages): use split/join instead of replace to avoid CodeQL false positive

CodeQL's js/incomplete-multi-character-sanitization rule flags any
.replace() that removes multi-character sequences like '<!--...-->',
regardless of context. The data here comes from readFileSync on the
project's own index.html (no untrusted input), making this a false
positive. Using split(regex).join('') achieves the same result without
triggering the taint-tracking rule.
2026-08-05 21:26:27 +08:00

111 lines
3.7 KiB
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 alibaba/open-code-review Contributors
package main
import (
"strings"
"testing"
)
// TestParentCommands_UnknownSubcommand verifies that parent commands which
// previously silently printed help and exited 0 now return a non-zero exit
// code with a clear "unknown command" error when given an unrecognized
// subcommand. This is consistent with the root command and leaf commands.
//
// See: https://github.com/alibaba/open-code-review/issues/641
func TestParentCommands_UnknownSubcommand(t *testing.T) {
tests := []struct {
name string
args []string
want string // substring expected in error message
}{
{"session", []string{"session", "bogus"}, "unknown command"},
{"sessions alias", []string{"sessions", "bogus"}, "unknown command"},
{"config", []string{"config", "bogus"}, "unknown command"},
{"delegate", []string{"delegate", "bogus"}, "unknown command"},
{"delegate alias", []string{"d", "bogus"}, "unknown command"},
{"llm", []string{"llm", "bogus"}, "unknown command"},
{"rules", []string{"rules", "bogus"}, "unknown command"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := rootCmd
root.SetArgs(tt.args)
t.Cleanup(func() { root.SetArgs(nil) })
err := root.Execute()
if err == nil {
t.Fatalf("expected error for args %v, got nil", tt.args)
}
got := err.Error()
if !strings.Contains(got, tt.want) {
t.Errorf("expected error containing %q, got %q", tt.want, got)
}
})
}
}
// TestParentCommands_KnownSubcommandStillWorks ensures that adding RunE to
// parent commands does not break legitimate subcommand routing.
func TestParentCommands_KnownSubcommandStillWorks(t *testing.T) {
// We cannot actually invoke llm test or config provider without a real
// config, but we can verify that the subcommand tree resolves correctly by
// using the help flag, which is handled by Cobra before RunE.
tests := []struct {
name string
args []string
}{
{"session list help", []string{"session", "list", "--help"}},
{"session show help", []string{"session", "show", "--help"}},
{"config set help", []string{"config", "set", "--help"}},
{"config provider help", []string{"config", "provider", "--help"}},
{"delegate preview help", []string{"delegate", "preview", "--help"}},
{"delegate rule help", []string{"delegate", "rule", "--help"}},
{"llm test help", []string{"llm", "test", "--help"}},
{"llm providers help", []string{"llm", "providers", "--help"}},
{"rules check help", []string{"rules", "check", "--help"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := rootCmd
root.SetArgs(tt.args)
t.Cleanup(func() { root.SetArgs(nil) })
// Help output is not treated as an error by Cobra when invoked via --help.
err := root.Execute()
if err != nil {
t.Fatalf("unexpected error for help on known subcommand: %v", err)
}
})
}
}
// TestParentCommands_NoArgsPrintsHelp verifies that invoking a parent
// command with no arguments still prints help and exits 0 (RunE returns
// cmd.Help() which is nil).
func TestParentCommands_NoArgsPrintsHelp(t *testing.T) {
tests := []struct {
name string
args []string
}{
{"session no args", []string{"session"}},
{"config no args", []string{"config"}},
{"delegate no args", []string{"delegate"}},
{"llm no args", []string{"llm"}},
{"rules no args", []string{"rules"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := rootCmd
root.SetArgs(tt.args)
t.Cleanup(func() { root.SetArgs(nil) })
err := root.Execute()
if err != nil {
t.Fatalf("expected nil error when parent command has no args (help), got %v", err)
}
})
}
}