mirror of
https://github.com/alibaba/open-code-review.git
synced 2026-08-20 22:24:35 +00:00
Some checks are pending
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
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (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
74 lines
2.3 KiB
Go
74 lines
2.3 KiB
Go
// SPDX-License-Identifier: Apache-2.0
|
|
// Copyright 2026 alibaba/open-code-review Contributors
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateDelegateOptions(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
opts delegateOptions
|
|
wantErr bool
|
|
}{
|
|
{"workspace", delegateOptions{format: "text"}, false},
|
|
{"commit", delegateOptions{commit: "abc", format: "text"}, false},
|
|
{"range", delegateOptions{from: "main", to: "dev", format: "text"}, false},
|
|
{"json format", delegateOptions{format: "json"}, false},
|
|
{"empty format", delegateOptions{}, true},
|
|
{"invalid format", delegateOptions{format: "yaml"}, true},
|
|
{"sarif format not supported by delegate", delegateOptions{format: "sarif"}, true},
|
|
{"from without to", delegateOptions{from: "main"}, true},
|
|
{"to without from", delegateOptions{to: "dev"}, true},
|
|
{"commit and range mixed", delegateOptions{commit: "abc", from: "main", to: "dev"}, true},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
err := validateDelegateOptions(&c.opts)
|
|
if (err != nil) != c.wantErr {
|
|
t.Errorf("validateDelegateOptions() err = %v, wantErr %v", err, c.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDelegateContextReviewMode(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
opts delegateOptions
|
|
want string
|
|
}{
|
|
{"commit", delegateOptions{commit: "abc"}, "commit"},
|
|
{"range", delegateOptions{from: "main", to: "dev"}, "range"},
|
|
{"workspace", delegateOptions{}, "workspace"},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
dc := &delegateContext{cc: &commonContext{}, opts: c.opts}
|
|
if got := dc.reviewMode(); got != c.want {
|
|
t.Errorf("reviewMode() = %q, want %q", got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDelegateContextMergeBaseEmptyForNonRange(t *testing.T) {
|
|
dc := &delegateContext{cc: &commonContext{}, opts: delegateOptions{commit: "abc"}}
|
|
if got := dc.mergeBase(context.Background()); got != "" {
|
|
t.Errorf("mergeBase(commit mode) = %q, want empty", got)
|
|
}
|
|
dc = &delegateContext{cc: &commonContext{}, opts: delegateOptions{}}
|
|
if got := dc.mergeBase(context.Background()); got != "" {
|
|
t.Errorf("mergeBase(workspace mode) = %q, want empty", got)
|
|
}
|
|
}
|
|
|
|
func TestDelegateContextResolver(t *testing.T) {
|
|
dc := &delegateContext{cc: &commonContext{}, opts: delegateOptions{}}
|
|
if got := dc.resolver(); got != nil {
|
|
t.Errorf("resolver() = %v, want nil", got)
|
|
}
|
|
}
|