mirror of
https://github.com/alibaba/open-code-review.git
synced 2026-08-31 10:35:56 +00:00
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 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.
171 lines
4 KiB
Go
171 lines
4 KiB
Go
// SPDX-License-Identifier: Apache-2.0
|
|
// Copyright 2026 alibaba/open-code-review Contributors
|
|
|
|
package main
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func initTestGitRepo(t *testing.T) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
cmds := [][]string{
|
|
{"git", "-C", dir, "init"},
|
|
{"git", "-C", dir, "config", "user.email", "test@test.com"},
|
|
{"git", "-C", dir, "config", "user.name", "Test"},
|
|
}
|
|
for _, args := range cmds {
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git init: %v: %s", err, out)
|
|
}
|
|
}
|
|
f := filepath.Join(dir, "README.md")
|
|
if err := os.WriteFile(f, []byte("hello"), 0o644); err != nil {
|
|
t.Fatalf("write README: %v", err)
|
|
}
|
|
cmd := exec.Command("git", "-C", dir, "add", ".")
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git add: %v: %s", err, out)
|
|
}
|
|
cmd = exec.Command("git", "-C", dir, "commit", "-m", "initial commit")
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git commit: %v: %s", err, out)
|
|
}
|
|
return dir
|
|
}
|
|
|
|
func TestRunGitCmd_Success(t *testing.T) {
|
|
dir := initTestGitRepo(t)
|
|
out, err := runGitCmd(dir, "rev-parse", "--git-dir")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if len(out) == 0 {
|
|
t.Error("expected non-empty output")
|
|
}
|
|
}
|
|
|
|
func TestRunGitCmd_Failure(t *testing.T) {
|
|
dir := t.TempDir()
|
|
_, err := runGitCmd(dir, "rev-parse", "--git-dir")
|
|
if err == nil {
|
|
t.Error("expected error for non-git dir")
|
|
}
|
|
}
|
|
|
|
func TestGetCommitMessage(t *testing.T) {
|
|
dir := initTestGitRepo(t)
|
|
msg, err := getCommitMessage(dir, "HEAD")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if msg != "initial commit" {
|
|
t.Errorf("msg = %q, want 'initial commit'", msg)
|
|
}
|
|
}
|
|
|
|
func TestGetCommitMessage_InvalidCommit(t *testing.T) {
|
|
dir := initTestGitRepo(t)
|
|
_, err := getCommitMessage(dir, "nonexistent-ref-xyz")
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid commit")
|
|
}
|
|
}
|
|
|
|
func TestResolveRepoDir_ValidGitRepo(t *testing.T) {
|
|
dir := initTestGitRepo(t)
|
|
resolved, err := resolveRepoDir(dir)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resolved == "" {
|
|
t.Error("expected non-empty resolved path")
|
|
}
|
|
}
|
|
|
|
func TestResolveRepoDir_NotGitRepo(t *testing.T) {
|
|
dir := t.TempDir()
|
|
_, err := resolveRepoDir(dir)
|
|
if err == nil {
|
|
t.Fatal("expected error for non-git dir")
|
|
}
|
|
if !strings.Contains(err.Error(), "not a git repository") {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestResolveRepoDir_EmptyUsesWd(t *testing.T) {
|
|
dir := initTestGitRepo(t)
|
|
origDir, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("getwd: %v", err)
|
|
}
|
|
defer func() {
|
|
if err := os.Chdir(origDir); err != nil {
|
|
t.Errorf("restore chdir: %v", err)
|
|
}
|
|
}()
|
|
if err := os.Chdir(dir); err != nil {
|
|
t.Fatalf("chdir: %v", err)
|
|
}
|
|
|
|
resolved, err := resolveRepoDir("")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resolved == "" {
|
|
t.Error("expected non-empty resolved path")
|
|
}
|
|
}
|
|
|
|
func TestRequireGitRepo_Valid(t *testing.T) {
|
|
dir := initTestGitRepo(t)
|
|
if err := requireGitRepo(dir); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRequireGitRepo_Invalid(t *testing.T) {
|
|
dir := t.TempDir()
|
|
err := requireGitRepo(dir)
|
|
if err == nil {
|
|
t.Fatal("expected error for non-git dir")
|
|
}
|
|
}
|
|
|
|
func TestValidateReviewRefs_ValidCommit(t *testing.T) {
|
|
dir := initTestGitRepo(t)
|
|
err := validateReviewRefs(dir, reviewOptions{commit: "HEAD"})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateReviewRefs_InvalidCommit(t *testing.T) {
|
|
dir := initTestGitRepo(t)
|
|
err := validateReviewRefs(dir, reviewOptions{commit: "nonexistent-ref-xyz"})
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid commit ref")
|
|
}
|
|
}
|
|
|
|
func TestValidateReviewRefs_EmptySkipped(t *testing.T) {
|
|
dir := initTestGitRepo(t)
|
|
err := validateReviewRefs(dir, reviewOptions{})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestBuildToolRegistry(t *testing.T) {
|
|
reg := buildToolRegistry(nil, nil)
|
|
if reg == nil {
|
|
t.Fatal("expected non-nil registry")
|
|
}
|
|
}
|