Files
kite 533b526b4c 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

155 lines
3.3 KiB
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 alibaba/open-code-review Contributors
package gitcmd
import (
"context"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
func initRepo(t *testing.T) string {
t.Helper()
dir := t.TempDir()
run := func(args ...string) {
cmd := exec.Command("git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(),
"GIT_AUTHOR_NAME=test",
"GIT_AUTHOR_EMAIL=test@test.com",
"GIT_COMMITTER_NAME=test",
"GIT_COMMITTER_EMAIL=test@test.com",
)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
run("init")
run("config", "user.email", "test@test.com")
run("config", "user.name", "test")
if err := os.WriteFile(filepath.Join(dir, "hello.txt"), []byte("hello\n"), 0644); err != nil {
t.Fatal(err)
}
run("add", "hello.txt")
run("commit", "-m", "init")
return dir
}
func TestRunner_New(t *testing.T) {
r := New(0)
if r == nil {
t.Fatal("New(0) returned nil")
}
if cap(r.sem) != defaultMaxConcurrent {
t.Errorf("default capacity = %d, want %d", cap(r.sem), defaultMaxConcurrent)
}
r2 := New(4)
if cap(r2.sem) != 4 {
t.Errorf("capacity = %d, want 4", cap(r2.sem))
}
}
func TestRunner_Run(t *testing.T) {
dir := initRepo(t)
r := New(2)
out, err := r.Run(context.Background(), dir, "log", "--oneline")
if err != nil {
t.Fatalf("Run error: %v", err)
}
if !strings.Contains(out, "init") {
t.Errorf("expected 'init' in output: %q", out)
}
}
func TestRunner_Run_InvalidCommand(t *testing.T) {
dir := initRepo(t)
r := New(2)
_, err := r.Run(context.Background(), dir, "nonexistent-subcommand")
if err == nil {
t.Error("expected error for invalid git subcommand")
}
}
func TestRunner_Output(t *testing.T) {
dir := initRepo(t)
r := New(2)
out, err := r.Output(context.Background(), dir, "rev-parse", "HEAD")
if err != nil {
t.Fatalf("Output error: %v", err)
}
hash := strings.TrimSpace(string(out))
if len(hash) != 40 {
t.Errorf("expected 40-char hash, got %q", hash)
}
}
func TestRunner_RunSplit(t *testing.T) {
dir := initRepo(t)
r := New(2)
stdout, stderr, err := r.RunSplit(context.Background(), dir, "status", "--short")
if err != nil {
t.Fatalf("RunSplit error: %v", err)
}
_ = stderr
if strings.Contains(stdout, "??") {
t.Errorf("unexpected untracked files in clean repo: %q", stdout)
}
}
func TestRunner_Stream(t *testing.T) {
dir := initRepo(t)
r := New(2)
var content string
err := r.Stream(context.Background(), dir, func(stdout io.Reader) error {
data, err := io.ReadAll(stdout)
if err != nil {
return err
}
content = string(data)
return nil
}, "show", "HEAD:hello.txt")
if err != nil {
t.Fatalf("Stream error: %v", err)
}
if content != "hello\n" {
t.Errorf("Stream content = %q, want %q", content, "hello\n")
}
}
func TestRunner_ContextCancelled(t *testing.T) {
r := New(1)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := r.Run(ctx, ".", "status")
if err == nil {
t.Error("expected error for cancelled context")
}
}
func TestRunner_AcquireTimeout(t *testing.T) {
r := New(1)
r.sem <- struct{}{}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
_, err := r.Run(ctx, ".", "status")
if err == nil {
t.Error("expected timeout error when semaphore full")
}
}