Skip to content

Git designed for AI agents.

14.4 KB
Raw Blame
package classify

import (
	"testing"

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

const (
	clsCode        = gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED
	clsComment     = gitalypb.ReviewFile_CHANGE_CLASS_COMMENT_ONLY
	clsDocs        = gitalypb.ReviewFile_CHANGE_CLASS_DOCS
	clsTests       = gitalypb.ReviewFile_CHANGE_CLASS_TESTS
	clsGenerated   = gitalypb.ReviewFile_CHANGE_CLASS_GENERATED
	clsConfig      = gitalypb.ReviewFile_CHANGE_CLASS_CONFIG
	absentSentinel = "\x00absent"
)

type fileCase struct {
	name string
	path string
	// old and new are the file contents. The sentinel absentSentinel means
	// the file does not exist in that version.
	old, new string
	want     gitalypb.ReviewFile_ChangeClass
}

// content returns the bytes for a version, nil when the version is absent.
func content(s string) []byte {
	if s == absentSentinel {
		return nil
	}
	return []byte(s)
}

func runFileCases(t *testing.T, cases []fileCase) {
	t.Helper()
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got, reason := File(tc.path, content(tc.old), content(tc.new))
			if got != tc.want {
				t.Fatalf("File(%q) = %v (%s), want %v", tc.path, got, reason, tc.want)
			}
			if reason == "" {
				t.Fatal("reason is empty")
			}
			again, againReason := File(tc.path, content(tc.old), content(tc.new))
			if again != got || againReason != reason {
				t.Fatalf("File is not deterministic: %v/%q then %v/%q", got, reason, again, againReason)
			}
		})
	}
}

func TestFilePathRules(t *testing.T) {
	runFileCases(t, []fileCase{
		{"pb go is generated", "api/x.pb.go", "a", "b", clsGenerated},
		{"pb2 python is generated", "pkg/gen_pb2.py", "a", "b", clsGenerated},
		{"min js is generated", "web/app.min.js", "a", "b", clsGenerated},
		{"generated go suffix", "internal/foo_generated.go", "a", "b", clsGenerated},
		{"go test file", "internal/foo_test.go", "func A() {}", "func B() {}", clsTests},
		{"tests directory", "tests/a.go", "x := 1", "x := 2", clsTests},
		{"testdata directory", "testdata/a.go", "x := 1", "x := 2", clsTests},
		{"__tests__ directory", "web/__tests__/a.js", "f()", "g()", clsTests},
		{"test_ python file", "pkg/test_util.py", "x = 1", "x = 2", clsTests},
		{"_test python file", "pkg/util_test.py", "x = 1", "x = 2", clsTests},
		{"spec ts file", "web/a.spec.ts", "f()", "g()", clsTests},
		{"test jsx file", "web/a.test.jsx", "f()", "g()", clsTests},
		{"markdown is docs", "README.md", "one", "two", clsDocs},
		{"rst is docs", "guide/index.rst", "one", "two", clsDocs},
		{"doc directory is docs", "doc/x.go", "x := 1", "x := 2", clsDocs},
		{"docs directory is docs", "docs/notes.txt", "one", "two", clsDocs},
		{"toml is config", "config/a.toml", "a = 1", "a = 2", clsConfig},
		{"yaml is config", "ci.yml", "a: 1", "a: 2", clsConfig},
		{"json is config", "x.json", "{}", "[]", clsConfig},
		{"editorconfig is config", ".editorconfig", "a", "b", clsConfig},
		{"gitignore is config", "sub/.gitignore", "a", "b", clsConfig},
		{"file named test is not a test", "test", "x", "y", clsCode},
		{"test in file name only is not a test", "pkg/latest.go", "x := 1", "x := 2", clsCode},
	})
}

func TestFileGeneratedHeader(t *testing.T) {
	header := "// Code generated by protoc-gen-go. DO NOT EDIT.\n"
	runFileCases(t, []fileCase{
		{"header in both versions", "main.go", header + "package a\n", header + "package a\n", clsGenerated},
		{"header only in old version", "main.go", header + "package a\n", "package a\n", clsGenerated},
		{"at-generated marker", "main.go", "package a\n// @generated\n", "package a\n", clsGenerated},
		{"header added to existing file", "main.go",
			"package a\nfunc f() int { return 1 }\n",
			header + "package a\nfunc f() int { return 2 }\n", clsCode},
		{"at-generated added to existing file", "main.go",
			"package a\nfunc f() int { return 1 }\n",
			"// @generated\npackage a\nfunc f() int { return 2 }\n", clsCode},
		{"header added to empty file", "main.go", "", header + "package a\n", clsCode},
		{"added file with header", "main.go", absentSentinel, header + "package a\n", clsGenerated},
		{"deleted file with header", "main.go", header + "package a\n", absentSentinel, clsGenerated},
		{"added file without header", "main.go", absentSentinel, "package a\n", clsCode},
		{"code generated without DO NOT EDIT", "main.go", "// Code generated by x.\npackage a\nvar x = 1\n", "// Code generated by x.\npackage a\nvar x = 2\n", clsCode},
		{"header after line 20 is ignored", "main.go",
			lines(20, "package a") + header + "func f() {}\n",
			lines(20, "package a") + header + "func g() {}\n", clsCode},
	})
}

func TestFileAddedDeletedBinaryUnknown(t *testing.T) {
	runFileCases(t, []fileCase{
		{"added go file", "a.go", absentSentinel, "package a\n", clsCode},
		{"deleted go file", "a.go", "package a\n", absentSentinel, clsCode},
		{"binary content in go file", "a.go", "package a\x00", "package a\x00 // c", clsCode},
		{"unknown extension", "image.bin", "x", "x", clsCode},
		{"no extension", "Makefile", "a:\n\techo\n# c", "a:\n\techo\n# d", clsCode},
		{"jsx markup is not lexed", "web/app.jsx", "<a>x</a>", "<a>y</a>", clsCode},
		{"tsx markup is not lexed", "web/app.tsx", "<a>x</a>", "<a>x</a> // c", clsCode},
		{"js with jsx closing tag is not lexed", "web/app.js", "x = <a>y</a> // c", "x = <a>y</a> // d", clsCode},
		{"deleted python file", "a.py", "x = 1\n", absentSentinel, clsCode},
	})
}

func lines(n int, line string) string {
	s := ""
	for i := 0; i < n; i++ {
		s += line + "\n"
	}
	return s
}

func TestFileGo(t *testing.T) {
	runFileCases(t, []fileCase{
		{"comment changed", "a.go",
			"package a\n\n// old\nfunc F() int { return 1 }\n",
			"package a\n// new comment\nfunc F() int { return 1 }\n", clsComment},
		{"whitespace only", "a.go", "x := 1\n", "x  :=   1\n", clsComment},
		{"trailing newline added", "a.go", "x := 1", "x := 1\n", clsComment},
		{"blank line added", "a.go", "x := 1\ny := 2\n", "x := 1\n\ny := 2\n", clsComment},
		{"url in string, comment changed", "a.go",
			`s := "http://a" // one`, `s := "http://a" // two`, clsComment},
		{"url in string changed", "a.go",
			`s := "http://a" // c`, `s := "http://b" // c`, clsCode},
		{"comment marker in string is code", "a.go",
			`s := "// x"` + "\n" + "f()\n", `s := "// y"` + "\n" + "f()\n", clsCode},
		{"back-tick raw string with comment marker", "a.go",
			"x := `a // b`\n", "x := `a // c`\n", clsCode},
		{"rune containing a quote", "a.go",
			"c := '\"'; s := \"a\" // x\n", "c := '\"'; s := \"a\" // y\n", clsComment},
		{"rune containing a slash", "a.go",
			"c := '/'\n", "c := '/' // c\n", clsComment},
		{"block comment changed", "a.go", "x := 1 /* a */\n", "x := 1 /* b */\n", clsComment},
		{"line break removed before return value", "a.go",
			"func f() int {\n\treturn\n\tg()\n}\n", "func f() int {\n\treturn g()\n}\n", clsCode},
		{"operator spelling changed", "a.go", "x := a &^ b\n", "x := a & ^b\n", clsCode},
		{"channel send changed", "a.go", "ch <- v\n", "ch < -v\n", clsCode},
		{"string literal changed", "a.go", `x := "a"`, `x := "b"`, clsCode},
		{"unterminated string is code", "a.go", `x := "a`, `x := "a" // c`, clsCode},
	})
}

// TestFileUnsupportedLanguagesAreCode checks PLAN5 "WP44 RULE" 1: outside Go,
// Java, Rust and Python a change not covered by a path rule is always CODE,
// even when only a comment or a blank changed.
func TestFileUnsupportedLanguagesAreCode(t *testing.T) {
	runFileCases(t, []fileCase{
		{"C block comment", "src/a.c", "/* a */ int x;\n", "/* b */ int x;\n", clsCode},
		{"C header line comment", "src/a.h", "// a\nint x;\n", "// b\nint x;\n", clsCode},
		{"C++ line comment", "src/a.cpp", "// a\nint x;\n", "// b\nint x;\n", clsCode},
		{"C++ cc", "src/a.cc", "// a\nint x;\n", "// b\nint x;\n", clsCode},
		{"C++ header hpp", "src/a.hpp", "class A { // x\n};\n", "class A { // y\n};\n", clsCode},
		{"C whitespace only", "src/a.c", "int x=1;\n", "int x = 1;\n", clsCode},
		{"identical C file", "src/a.c", "int x;\n", "int x;\n", clsCode},
		{"JavaScript line comment", "web/a.js", "// a\nconst x = 1;\n", "// b\nconst x = 1;\n", clsCode},
		{"JavaScript mjs", "web/a.mjs", "// a\nconst x = 1;\n", "// b\nconst x = 1;\n", clsCode},
		{"JavaScript NBSP", "web/a.js", "x" + nbsp + "=" + nbsp + "1;\n", "x = 1;\n", clsCode},
		{"TypeScript line comment", "web/a.ts", "// a\nconst x = 1;\n", "// b\nconst x = 1;\n", clsCode},
		{"JSX", "web/a.jsx", "// a\nconst x = 1;\n", "// b\nconst x = 1;\n", clsCode},
		{"TSX", "web/a.tsx", "// a\nconst x = 1;\n", "// b\nconst x = 1;\n", clsCode},
		{"shell comment", "run.sh", "# a\necho hi\n", "# b\necho hi\n", clsCode},
		{"bash comment", "run.bash", "# a\necho hi\n", "# b\necho hi\n", clsCode},
		{"Ruby comment", "src/a.rb", "# a\nx = 1\n", "# b\nx = 1\n", clsCode},
		{"Ruby blank change", "src/a.rb", "puts x\n", "puts  x\n", clsCode},
		{"Ruby embedded document", "src/a.rb", "=begin\nold\n=end\nx = 1\n", "=begin\nnew\n=end\nx = 1\n", clsCode},
		{"Kotlin comment", "src/A.kt", "// a\nval x = 1\n", "// b\nval x = 1\n", clsCode},
	})
}

func TestFileJava(t *testing.T) {
	runFileCases(t, []fileCase{
		{"line comment changed", "A.java", "class A { // x\n}\n", "class A { // y\n}\n", clsComment},
		{"block comment changed", "A.java", "/* a */\nclass A {}\n", "/* b */\nclass A {}\n", clsComment},
		{"empty block comment", "A.java", "/**/\nclass A {}\n", "class A {}\n", clsComment},
		{"string with comment marker", "A.java",
			`String s = "a // b"; // c`, `String s = "a // d"; // c`, clsCode},
		{"javadoc changed", "src/Main.java", "/** a */\nclass A {}\n", "/** b */\nclass A {}\n", clsCode},
		{"javadoc removed", "src/Main.java", "/** a */\nclass A {}\n", "class A {}\n", clsCode},
		{"javadoc added", "src/Main.java", "class A {}\n", "/** a */\nclass A {}\n", clsCode},
		{"markdown doc comment changed", "A.java", "/// a\nclass A {}\n", "/// b\nclass A {}\n", clsCode},
		{"text block changed", "A.java", "String s = \"\"\"\nx\n\"\"\";\n", "String s = \"\"\"\ny\n\"\"\";\n", clsCode},
		{"text block with comment marker changed", "A.java",
			"String s = \"\"\"\n// x\n\"\"\";\n", "String s = \"\"\"\n// y\n\"\"\";\n", clsCode},
		{"text block with escaped quotes changed", "A.java",
			"String s = \"\"\"\n\\\"\"\" // x\n\"\"\";\n", "String s = \"\"\"\n\\\"\"\" // y\n\"\"\";\n", clsCode},
		{"text block unchanged, comment changed", "A.java",
			"String s = \"\"\"\n/* x */\n\"\"\"; // a\n", "String s = \"\"\"\n/* x */\n\"\"\"; // b\n", clsComment},
		{"unterminated text block", "A.java", "String s = \"\"\"\nx\n", "String s = \"\"\"\nx\n// c\n", clsCode},
		{"char literal with quote", "A.java", "char c = '\"'; int x; // a\n", "char c = '\"'; int x; // b\n", clsComment},
		{"unsigned shift assignment split", "A.java", "x >>>= 1;\n", "x >>> = 1;\n", clsCode},
	})
}

func TestFileRust(t *testing.T) {
	runFileCases(t, []fileCase{
		{"nested block comment changed", "a.rs", "/* a /* b */ c */ fn f() {}\n", "/* a /* d */ c */ fn f() {}\n", clsComment},
		{"raw string with comment marker", "a.rs", `let s = r#"//"#;` + "\n", `let s = r#"/*"#;` + "\n", clsCode},
		{"raw string changed", "a.rs", `let s = r"x";` + "\n", `let s = r"y";` + "\n", clsCode},
		{"lifetime unchanged, comment added", "a.rs", "fn f<'a>(x: &'a str) {}\n", "fn f<'a>(x: &'a str) {} // c\n", clsComment},
		{"char literal changed", "a.rs", "let c = 'a';\n", "let c = 'b';\n", clsCode},
		{"raw identifier", "a.rs", "let r#type = 1; // a\n", "let r#type = 1; // b\n", clsComment},
		{"raw identifier split", "a.rs", "m!(r#type);\n", "m!(r #type);\n", clsCode},
		{"byte string prefix split", "a.rs", "m!(b\"x\");\n", "m!(b \"x\");\n", clsCode},
		{"byte char prefix split", "a.rs", "m!(b'x');\n", "m!(b 'x');\n", clsCode},
		{"string suffix is not lexed", "a.rs", "m!(\"x\"s); // a\n", "m!(\"x\"s); // b\n", clsCode},
		{"stringify is not lexed", "a.rs", "const S: &str = stringify!(a+b);\n", "const S: &str = stringify!(a + b);\n", clsCode},
		{"range operator split", "a.rs", "m!(a..b);\n", "m!(a. .b);\n", clsCode},
		{"inner attribute, comment changed", "src/lib.rs", "#![allow(dead_code)]\n// a\nfn f() {}\n", "#![allow(dead_code)]\n// b\nfn f() {}\n", clsComment},
		{"shebang hides block comment opener", "a.rs",
			"#!/usr/bin/env run /*\nfn good() {} // */\n", "#!/usr/bin/env run /*\nfn evil() {} // */\n", clsCode},
		{"line doc comment changed", "a.rs", "//! a\nfn f() {}\n", "//! b\nfn f() {}\n", clsCode},
		{"inner block doc comment changed", "a.rs", "/*! a */\nfn f() {}\n", "/*! b */\nfn f() {}\n", clsCode},
		{"whitespace only", "a.rs", "fn f(){let x=1;}\n", "fn f() {\n    let x = 1;\n}\n", clsComment},
	})
}

func TestFilePython(t *testing.T) {
	runFileCases(t, []fileCase{
		{"docstring changed", "a.py",
			"def f():\n    \"\"\"Doc.\"\"\"\n    return 1\n",
			"def f():\n    \"\"\"Doc!\"\"\"\n    return 1\n", clsCode},
		{"comment changed", "a.py", "x = 1  # a\n", "x = 1  # b\n", clsComment},
		{"hash in string changed", "a.py", `s = "#a"`, `s = "#b"`, clsCode},
		{"indentation changed", "a.py", "if x:\n    y()\n", "if x:\n  y()\n", clsCode},
		{"comment line added", "a.py", "if x:\n    y()\n", "if x:\n    # note\n    y()\n", clsComment},
		{"trailing newline added", "a.py", "x = 1", "x = 1\n", clsComment},
		{"triple quoted with hash", "a.py", "s = '''#a'''\n", "s = '''#b'''\n", clsCode},
		{"f-string with braces is not lexed", "a.py", `s = f"{x}"  # a`, `s = f"{x}"  # b`, clsCode},
		{"raw f-string without braces", "a.py", `s = rf"x"  # a`, `s = rf"x"  # b`, clsComment},
		{"single quoted string changed", "a.py", "s = 'a'\n", "s = 'b'\n", clsCode},
		{"blank line added", "a.py", "x = 1\ny = 2\n", "x = 1\n\ny = 2\n", clsComment},
		{"line continuation kept", "a.py", "x = 1 + \\\n    2  # a\n", "x = 1 + \\\n  2  # b\n", clsComment},
		{"line continuation removed", "a.py", "x = 1 + \\\n    2\n", "x = 1 + 2\n", clsCode},
		{"comment line after continuation", "a.py", "x = 1 + \\\n2\n", "x = 1 + \\\n# c\n2\n", clsCode},
		{"blank line after continuation", "a.py", "x = 1 + \\\n2\n", "x = 1 + \\\n\n2\n", clsCode},
		{"backslash before blank is not lexed", "a.py", "x = 1 + \\ \n2  # a\n", "x = 1 + \\ \n2  # b\n", clsCode},
		{"string prefix split", "a.py", "x = rb'a'\n", "x = rb 'a'\n", clsCode},
		{"name before quote is not lexed", "a.py", "x = ab'a'  # a\n", "x = ab'a'  # b\n", clsCode},
		{"line structure changed", "a.py", "if x: y()\n", "if x:\n    y()\n", clsCode},
		{"shebang changed", "a.py", "#!/usr/bin/python2\nx = 1\n", "#!/usr/bin/python3\nx = 1\n", clsCode},
		{"form feed added", "a.py", "x = 1\n", "x = 1\f\n", clsCode},
	})
}

func TestFileIdenticalContentIsCommentOnly(t *testing.T) {
	runFileCases(t, []fileCase{
		{"identical go file", "a.go", "package a\n", "package a\n", clsComment},
	})
}