blob: b3afaafedd889398d1fbddf816c6228bca8cb993 [file] [log] [blame]
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package main
import (
"testing"
gerritpb "go.chromium.org/luci/common/proto/gerrit"
)
func TestScoreChangedFileProximity(t *testing.T) {
tests := []struct {
name string
gnLabel string
changedFiles []string
project string
min int
max int
}{
{
name: "no nearby files",
gnLabel: "//src/foo/bar:test(toolchain)",
changedFiles: []string{
"tools/quux/main.cc",
"tools/othertool/main.cc",
},
min: 0,
max: 0,
},
{
name: "file in parent directory",
gnLabel: "//src/foo/bar:test(toolchain)",
changedFiles: []string{
"src/foo/main.cc",
},
min: 50,
max: 70,
},
{
name: "one nearby file",
gnLabel: "//src/foo:test(toolchain)",
changedFiles: []string{
"src/foo/main.cc",
"tools/quux/main.cc",
},
min: 70,
max: 90,
},
{
name: "many nearby files",
gnLabel: "//src/foo/bar:test(asfd)",
changedFiles: []string{
"src/foo/bar/main.h",
"src/foo/bar/main.cc",
"src/foo/bar/lib.cc",
"src/foo/bar/tests/test_something.cc",
"src/blah/lib.cc",
},
min: 90,
max: 100,
},
{
name: "one nearby file among many unrelated",
gnLabel: "//src/foo/subdir/tests:foo_test(asdf)",
changedFiles: []string{
"src/foo/subdir/tests/file01.cc",
"tools/blah/file02.cc",
"tools/blah/file03.cc",
"tools/blah/file04.cc",
"tools/blah/file05.cc",
},
min: 40,
max: 60,
},
{
// Based on https://fxrev.dev/680582, which broke a test under
// `src/media/stream_processors/test`.
name: "moderately close files",
gnLabel: "//src/foo/subdir/tests:foo_test(asdf)",
changedFiles: []string{
"src/foo/quux/file01.cc",
"src/foo/quux/file02.cc",
"src/foo/quux/file03.cc",
"src/foo/quux/file04.cc",
"src/foo/quux/file05.cc",
},
min: 40,
max: 60,
},
{
// Based on https://fxrev.dev/678982, which broke a test under
// `src/proc/tests/chromiumos`.
name: "most files have fully shared prefix",
gnLabel: "//src/foo/tests/bar:bar_tests(toolchain)",
changedFiles: []string{
"src/foo/bin/main.cc",
"src/foo/tests/bar/BUILD.gn",
"src/foo/tests/bar/file1.cc",
"src/foo/tests/bar/file2.cc",
"src/foo/tests/bar/file3.cc",
"src/foo/tests/bar/file4.cc",
"src/foo/tests/bar/file5.cc",
"src/foo/tests/bar/file6.cc",
},
min: 90,
max: 100,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.project == "" {
test.project = "fuchsia"
}
files := make(map[string]*gerritpb.FileInfo)
for _, f := range test.changedFiles {
files[f] = nil
}
got := scoreChangedFileProximity(test.changedFiles, test.gnLabel)
if got < test.min || got > test.max {
t.Errorf("scoreGNLabelDistance = %d is outside range [%d, %d]",
got, test.min, test.max,
)
}
})
}
}
func TestSharedPrefixLength(t *testing.T) {
tests := []struct {
name string
p1 []string
p2 []string
want int
}{
{
name: "empty lists",
p1: nil,
p2: nil,
want: 0,
},
{
name: "one empty list",
p1: nil,
p2: []string{"foo"},
want: 0,
},
{
name: "lists equal",
p1: []string{"foo"},
p2: []string{"foo"},
want: 1,
},
{
name: "no shared prefix",
p1: []string{"bar", "foo"},
p2: []string{"foo"},
want: 0,
},
{
name: "shared prefix",
p1: []string{"foo", "bar"},
p2: []string{"foo", "bar", "baz"},
want: 2,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := sharedPrefixLength(test.p1, test.p2)
if got != test.want {
t.Errorf("Wrong prefix length: got %d, wanted %d", got, test.want)
}
})
}
}