go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/autogardener/changed_files_test.go (about)

     1  // Copyright 2022 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"testing"
     9  
    10  	gerritpb "go.chromium.org/luci/common/proto/gerrit"
    11  )
    12  
    13  func TestScoreChangedFileProximity(t *testing.T) {
    14  	tests := []struct {
    15  		name         string
    16  		gnLabel      string
    17  		changedFiles []string
    18  		project      string
    19  		min          int
    20  		max          int
    21  	}{
    22  		{
    23  			name:    "no nearby files",
    24  			gnLabel: "//src/foo/bar:test(toolchain)",
    25  			changedFiles: []string{
    26  				"tools/quux/main.cc",
    27  				"tools/othertool/main.cc",
    28  			},
    29  			min: 0,
    30  			max: 0,
    31  		},
    32  		{
    33  			name:    "file in parent directory",
    34  			gnLabel: "//src/foo/bar:test(toolchain)",
    35  			changedFiles: []string{
    36  				"src/foo/main.cc",
    37  			},
    38  			min: 50,
    39  			max: 70,
    40  		},
    41  		{
    42  			name:    "one nearby file",
    43  			gnLabel: "//src/foo:test(toolchain)",
    44  			changedFiles: []string{
    45  				"src/foo/main.cc",
    46  				"tools/quux/main.cc",
    47  			},
    48  			min: 70,
    49  			max: 90,
    50  		},
    51  		{
    52  			name:    "many nearby files",
    53  			gnLabel: "//src/foo/bar:test(asfd)",
    54  			changedFiles: []string{
    55  				"src/foo/bar/main.h",
    56  				"src/foo/bar/main.cc",
    57  				"src/foo/bar/lib.cc",
    58  				"src/foo/bar/tests/test_something.cc",
    59  				"src/blah/lib.cc",
    60  			},
    61  			min: 90,
    62  			max: 100,
    63  		},
    64  		{
    65  			name:    "one nearby file among many unrelated",
    66  			gnLabel: "//src/foo/subdir/tests:foo_test(asdf)",
    67  			changedFiles: []string{
    68  				"src/foo/subdir/tests/file01.cc",
    69  				"tools/blah/file02.cc",
    70  				"tools/blah/file03.cc",
    71  				"tools/blah/file04.cc",
    72  				"tools/blah/file05.cc",
    73  			},
    74  			min: 40,
    75  			max: 60,
    76  		},
    77  		{
    78  			// Based on https://fxrev.dev/680582, which broke a test under
    79  			// `src/media/stream_processors/test`.
    80  			name:    "moderately close files",
    81  			gnLabel: "//src/foo/subdir/tests:foo_test(asdf)",
    82  			changedFiles: []string{
    83  				"src/foo/quux/file01.cc",
    84  				"src/foo/quux/file02.cc",
    85  				"src/foo/quux/file03.cc",
    86  				"src/foo/quux/file04.cc",
    87  				"src/foo/quux/file05.cc",
    88  			},
    89  			min: 40,
    90  			max: 60,
    91  		},
    92  		{
    93  			// Based on https://fxrev.dev/678982, which broke a test under
    94  			// `src/proc/tests/chromiumos`.
    95  			name:    "most files have fully shared prefix",
    96  			gnLabel: "//src/foo/tests/bar:bar_tests(toolchain)",
    97  			changedFiles: []string{
    98  				"src/foo/bin/main.cc",
    99  				"src/foo/tests/bar/BUILD.gn",
   100  				"src/foo/tests/bar/file1.cc",
   101  				"src/foo/tests/bar/file2.cc",
   102  				"src/foo/tests/bar/file3.cc",
   103  				"src/foo/tests/bar/file4.cc",
   104  				"src/foo/tests/bar/file5.cc",
   105  				"src/foo/tests/bar/file6.cc",
   106  			},
   107  			min: 90,
   108  			max: 100,
   109  		},
   110  	}
   111  
   112  	for _, test := range tests {
   113  		t.Run(test.name, func(t *testing.T) {
   114  			if test.project == "" {
   115  				test.project = "fuchsia"
   116  			}
   117  			files := make(map[string]*gerritpb.FileInfo)
   118  			for _, f := range test.changedFiles {
   119  				files[f] = nil
   120  			}
   121  
   122  			got := scoreChangedFileProximity(test.changedFiles, test.gnLabel)
   123  			if got < test.min || got > test.max {
   124  				t.Errorf("scoreGNLabelDistance = %d is outside range [%d, %d]",
   125  					got, test.min, test.max,
   126  				)
   127  			}
   128  		})
   129  	}
   130  }
   131  
   132  func TestSharedPrefixLength(t *testing.T) {
   133  	tests := []struct {
   134  		name string
   135  		p1   []string
   136  		p2   []string
   137  		want int
   138  	}{
   139  		{
   140  			name: "empty lists",
   141  			p1:   nil,
   142  			p2:   nil,
   143  			want: 0,
   144  		},
   145  		{
   146  			name: "one empty list",
   147  			p1:   nil,
   148  			p2:   []string{"foo"},
   149  			want: 0,
   150  		},
   151  		{
   152  			name: "lists equal",
   153  			p1:   []string{"foo"},
   154  			p2:   []string{"foo"},
   155  			want: 1,
   156  		},
   157  		{
   158  			name: "no shared prefix",
   159  			p1:   []string{"bar", "foo"},
   160  			p2:   []string{"foo"},
   161  			want: 0,
   162  		},
   163  		{
   164  			name: "shared prefix",
   165  			p1:   []string{"foo", "bar"},
   166  			p2:   []string{"foo", "bar", "baz"},
   167  			want: 2,
   168  		},
   169  	}
   170  
   171  	for _, test := range tests {
   172  		t.Run(test.name, func(t *testing.T) {
   173  			got := sharedPrefixLength(test.p1, test.p2)
   174  			if got != test.want {
   175  				t.Errorf("Wrong prefix length: got %d, wanted %d", got, test.want)
   176  			}
   177  		})
   178  	}
   179  }