Skip to content

Git designed for AI agents.

4.6 KB
Raw Blame
package classify

import (
	goscanner "go/scanner"
	"go/token"
	"testing"

	"github.com/feder-cr/git-ai/proto/go/gitalypb"
)

// FuzzGoMatchesGoScanner checks the Go lexer against the standard library: when
// File says a Go change is COMMENT_ONLY, go/scanner must see the same token
// stream (comments dropped, automatic semicolons kept) in both versions.
func FuzzGoMatchesGoScanner(f *testing.F) {
	f.Add([]byte("package a\n// a\nvar x = 1\n"), []byte("package a\n// b\nvar x = 1\n"))
	f.Add([]byte("x := 1 /* a\n */ + 2\n"), []byte("x := 1 + 2\n"))
	f.Add([]byte("c := '\"'; s := \"a\" // x\n"), []byte("c := '\"'; s := \"a\" // y\n"))
	f.Add([]byte("x := `a // b`\nreturn\ng()\n"), []byte("x := `a // b`\nreturn g()\n"))
	f.Add([]byte("x := a &^ b\nch <- v\n"), []byte("x := a & ^b\nch < -v\n"))
	f.Add([]byte("x := 0x1p-2 + 1e+5 + .5\n"), []byte("x := 0x1p-2+1e+5+.5\n"))

	f.Fuzz(func(t *testing.T, oldData, newData []byte) {
		if class, _ := File("a.go", oldData, newData); class != gitalypb.ReviewFile_CHANGE_CLASS_COMMENT_ONLY {
			return
		}
		oldToks, newToks := goScannerTokens(oldData), goScannerTokens(newData)
		if !equalTokens(oldToks, newToks) {
			t.Fatalf("COMMENT_ONLY but go/scanner tokens differ:\n%q\n%q", oldToks, newToks)
		}
	})
}

// goScannerTokens returns the tokens of src as go/scanner sees them, with
// comments dropped. Each token is its kind and its literal text.
func goScannerTokens(src []byte) []string {
	fset := token.NewFileSet()
	file := fset.AddFile("a.go", -1, len(src))
	var s goscanner.Scanner
	var errs int
	s.Init(file, src, func(token.Position, string) { errs++ }, 0)
	var toks []string
	for {
		_, tok, lit := s.Scan()
		if tok == token.EOF {
			break
		}
		toks = append(toks, tok.String()+" "+lit)
	}
	if errs > 0 {
		toks = append(toks, "scan errors")
	}
	return toks
}

// FuzzFile checks that the classifier never panics, that it is deterministic,
// and that it always gives a reason, for arbitrary paths and contents.
func FuzzFile(f *testing.F) {
	f.Add("a.go", []byte("package a\n// c\nfunc f() {}\n"), []byte("package a\nfunc f() {}\n"))
	f.Add("a.py", []byte("def f():\n    '''doc'''\n"), []byte("def f():\n  # c\n    '''doc'''\n"))
	f.Add("a.js", []byte("const a = `${b}`; /re/"), []byte("const a = `${b}`;"))
	f.Add("a.sh", []byte("echo \"$#\" # c"), []byte("echo $#"))
	f.Add("a.rb", []byte("=begin\nx\n=end\n# c"), []byte(""))
	f.Add("a.rs", []byte("/* /* */ */ fn f<'a>() {}"), []byte(`let s = r#"x"#;`))
	f.Add("a.c", []byte("#include <a//b.h>\n"), []byte("'\\"))
	f.Add("a.go", []byte("\x00"), []byte("\x01\x02"))
	f.Add("a.py", []byte("x = f\"{d[\"#\"]}\"\n"), []byte("\n\t\t"))
	f.Add("A.java", []byte("/** d */ class A { String s = \"\"\"\n x \\\"\"\" \n\"\"\"; }"), []byte("/// m\nclass A {}"))
	f.Add("a.rs", []byte("#![a]\nm!(b'x' r#t br##\"y\"## c\"z\");"), []byte("#!/x /*\n"))
	f.Add("a.py", []byte("x = 1 + \\\n  rb'2'\n"), []byte("x = \\\r\n2\xff"))
	f.Add("a.go", []byte("// Code generated by x. DO NOT EDIT.\npackage a\n"), []byte("// #cgo\n"))

	f.Fuzz(func(t *testing.T, filePath string, oldData, newData []byte) {
		class1, reason1 := File(filePath, oldData, newData)
		class2, reason2 := File(filePath, oldData, newData)
		if class1 != class2 || reason1 != reason2 {
			t.Fatalf("File is not deterministic: %v/%q then %v/%q", class1, reason1, class2, reason2)
		}
		if reason1 == "" {
			t.Fatal("reason is empty")
		}
		// Added and deleted variants must not panic either.
		_, _ = File(filePath, nil, newData)
		_, _ = File(filePath, oldData, nil)
	})
}

// FuzzTokenize checks that every lexer terminates without panicking, and that
// a certain result is deterministic and never contains an empty token.
func FuzzTokenize(f *testing.F) {
	for lang := 0; lang < int(numLanguages); lang++ {
		f.Add(uint8(lang), []byte("a /* b */ 'c' \"d\" `e` # f\n// g\n#{h} $(i) 'j\n"))
	}
	f.Add(uint8(langGo), []byte("x := `raw\n` + '\\''\n"))
	f.Add(uint8(langPython), []byte("'''doc''' f\"{x}\"\n    y\n"))
	f.Add(uint8(langJava), []byte("\"\"\"\n\\\"\"\"\"\"\" /** */ /**/ /// x\n"))
	f.Add(uint8(langRust), []byte("r#\"x\"# r#y b'\\'' c\"z\" 'a 'b' /*! /* */ */"))

	f.Fuzz(func(t *testing.T, sel uint8, src []byte) {
		lang := language(int(sel) % int(numLanguages))
		toks1, reason1, ok1 := tokenize(lang, src)
		toks2, reason2, ok2 := tokenize(lang, src)
		if ok1 != ok2 || reason1 != reason2 || !equalTokens(toks1, toks2) {
			t.Fatalf("tokenize is not deterministic for language %d", lang)
		}
		if !ok1 {
			if reason1 == "" {
				t.Fatal("uncertain result without reason")
			}
			return
		}
		for _, tok := range toks1 {
			if tok == "" {
				t.Fatal("empty token")
			}
		}
	})
}