package copydesk import ( "encoding/json" "testing" ) func TestCopyContractRejectsCommandEscape(t *testing.T) { c := Contract{Version: 1, Paths: []string{"app.go"}, Extract: []string{"python3", "scripts/copy.py", "extract"}, Apply: []string{"python3", "scripts/copy.py", "apply"}, Accept: []string{"python3", "scripts/copy.py", "accept"}, Classify: []string{"python3", "scripts/copy.py", "classify"}, Regenerate: []string{"python3", "scripts/copy.py", "regenerate"}} raw, _ := json.Marshal(c) if _, err := ParseContract(raw); err != nil { t.Fatal(err) } for _, cmd := range [][]string{{"sh", "-c", "evil"}, {"python3", "../evil.py"}, {"python3", "/etc/evil.py"}} { c.Extract = cmd raw, _ = json.Marshal(c) if _, err := ParseContract(raw); err == nil { t.Fatalf("escaped command %v", cmd) } } } func TestGenericDetectionAndByteScope(t *testing.T) { old := []byte("package a\nvar title=\"Old words\"\n") now := []byte("package a\nvar title=\"New words\"\n") ex, err := Generic("a.go", old, now) if err != nil || len(ex) != 1 { t.Fatalf("%+v %v", ex, err) } if ex[0].Text != "New words" { t.Fatal(ex) } ex, err = Generic("README.md", old, now) if err != nil || len(ex) != 0 { t.Fatal("docs trigger") } if got, err := Generic("view.swift", old, now); err != nil || len(got) != 0 { t.Fatal("unsupported interface was not skipped") } } func TestGenericUnquotedDisplayIsUnknown(t *testing.T) { if _, err := Generic("view.html", []byte("

Old words

"), []byte("

New words

")); err == nil { t.Fatal("unquoted display text skipped") } } func TestGenericTemplateExpressionsNeedContract(t *testing.T) { if _, err := Generic("view.js", []byte("const label=`Old words`"), []byte("const label=`Hi ${(()=>{run()})()}`")); err == nil { t.Fatal("template expression exposed as editable wording") } }