Skip to content

Commit 5d9e741

Browse files
authored
chore: add unit test to show libraries can have global files in src roots (#2595)
Fixes #2483
1 parent bcb914a commit 5d9e741

File tree

2 files changed

+305
-1
lines changed

2 files changed

+305
-1
lines changed

internal/librarian/generate_command_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,54 @@ func TestGenerateScenarios(t *testing.T) {
835835
wantBuildCalls: 0,
836836
wantConfigureCalls: 0,
837837
},
838+
{
839+
name: "source_roots_have_same_global_files",
840+
state: &config.LibrarianState{
841+
Image: "gcr.io/test/image:v1.2.3",
842+
Libraries: []*config.LibraryState{
843+
{
844+
ID: "some-library",
845+
SourceRoots: []string{
846+
"src/some/path",
847+
"one/global/example.txt",
848+
},
849+
APIs: []*config.API{
850+
{
851+
Path: "google/cloud/some",
852+
},
853+
},
854+
},
855+
{
856+
ID: "another-library",
857+
SourceRoots: []string{
858+
"src/another/path",
859+
"one/global/example.txt",
860+
},
861+
APIs: []*config.API{
862+
{
863+
Path: "google/cloud/another",
864+
},
865+
},
866+
},
867+
},
868+
},
869+
librarianConfig: &config.LibrarianConfig{
870+
GlobalFilesAllowlist: []*config.GlobalFile{
871+
{
872+
Path: "one/global/example.txt",
873+
Permissions: "read-write",
874+
},
875+
},
876+
},
877+
container: &mockContainerClient{
878+
wantLibraryGen: true,
879+
},
880+
ghClient: &mockGitHubClient{},
881+
build: true,
882+
wantGenerateCalls: 2,
883+
wantBuildCalls: 2,
884+
wantConfigureCalls: 0,
885+
},
838886
} {
839887
t.Run(test.name, func(t *testing.T) {
840888
repo := newTestGitRepoWithState(t, test.state, true)

internal/librarian/release_init_test.go

Lines changed: 257 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,114 @@ func TestInitRun(t *testing.T) {
288288
},
289289
},
290290
},
291+
{
292+
name: "run release init command for libraries have the same global files in src roots",
293+
containerClient: &mockContainerClient{},
294+
dockerInitCalls: 1,
295+
setupRunner: func(containerClient *mockContainerClient) *initRunner {
296+
return &initRunner{
297+
workRoot: t.TempDir(),
298+
containerClient: containerClient,
299+
state: &config.LibrarianState{
300+
Libraries: []*config.LibraryState{
301+
{
302+
ID: "another-example-id",
303+
Version: "1.0.0",
304+
SourceRoots: []string{
305+
"dir3",
306+
"one/global/example.txt",
307+
},
308+
RemoveRegex: []string{
309+
"dir3",
310+
},
311+
},
312+
{
313+
ID: "example-id",
314+
Version: "2.0.0",
315+
SourceRoots: []string{
316+
"dir1",
317+
"one/global/example.txt",
318+
},
319+
RemoveRegex: []string{
320+
"dir1",
321+
},
322+
},
323+
},
324+
},
325+
repo: &MockRepository{
326+
Dir: t.TempDir(),
327+
RemotesValue: []*gitrepo.Remote{
328+
{
329+
Name: "origin",
330+
URLs: []string{"https://github.com/googleapis/librarian.git"},
331+
},
332+
},
333+
GetCommitsForPathsSinceTagValueByTag: map[string][]*gitrepo.Commit{
334+
"another-example-id-1.0.0": {
335+
{
336+
Hash: plumbing.NewHash("123456"),
337+
Message: "feat: bump version",
338+
},
339+
},
340+
"example-id-2.0.0": {
341+
{
342+
Hash: plumbing.NewHash("123456"),
343+
Message: "feat: bump version",
344+
},
345+
},
346+
},
347+
ChangedFilesInCommitValueByHash: map[string][]string{
348+
plumbing.NewHash("123456").String(): {
349+
"one/global/example.txt",
350+
},
351+
},
352+
},
353+
librarianConfig: &config.LibrarianConfig{
354+
GlobalFilesAllowlist: []*config.GlobalFile{
355+
{
356+
Path: "one/global/example.txt",
357+
Permissions: "read-write",
358+
},
359+
},
360+
},
361+
}
362+
},
363+
files: map[string]string{
364+
"one/global/example.txt": "",
365+
"dir1/file1.txt": "",
366+
"dir3/file3.txt": "",
367+
},
368+
want: &config.LibrarianState{
369+
Libraries: []*config.LibraryState{
370+
{
371+
ID: "another-example-id",
372+
Version: "1.1.0", // version is bumped.
373+
APIs: []*config.API{},
374+
SourceRoots: []string{
375+
"dir3",
376+
"one/global/example.txt",
377+
},
378+
PreserveRegex: []string{},
379+
RemoveRegex: []string{
380+
"dir3",
381+
},
382+
},
383+
{
384+
ID: "example-id",
385+
Version: "2.1.0", // version is bumped.
386+
APIs: []*config.API{},
387+
SourceRoots: []string{
388+
"dir1",
389+
"one/global/example.txt",
390+
},
391+
PreserveRegex: []string{},
392+
RemoveRegex: []string{
393+
"dir1",
394+
},
395+
},
396+
},
397+
},
398+
},
291399
{
292400
name: "run release init command, skips blocked libraries!",
293401
containerClient: &mockContainerClient{},
@@ -1032,7 +1140,7 @@ func TestInitRun(t *testing.T) {
10321140
}
10331141

10341142
// If there is no release triggered for any library, then the librarian state
1035-
// is not be written back. The `want` value for the librarian state is nil
1143+
// is not written back. The `want` value for the librarian state is nil
10361144
var got *config.LibrarianState
10371145
if err := yaml.Unmarshal(bytes, &got); err != nil {
10381146
t.Fatal(err)
@@ -1045,6 +1153,154 @@ func TestInitRun(t *testing.T) {
10451153
}
10461154
}
10471155

1156+
func TestRunInitCommand(t *testing.T) {
1157+
t.Parallel()
1158+
for _, test := range []struct {
1159+
name string
1160+
state *config.LibrarianState
1161+
config *config.LibrarianConfig
1162+
repo gitrepo.Repository
1163+
client ContainerClient
1164+
want *config.LibrarianState
1165+
}{
1166+
{
1167+
name: "global_file_commits_appear_in_multiple_libraries",
1168+
state: &config.LibrarianState{
1169+
Libraries: []*config.LibraryState{
1170+
{
1171+
ID: "another-example-id",
1172+
Version: "1.0.0",
1173+
SourceRoots: []string{
1174+
"dir3",
1175+
"one/global/example.txt",
1176+
},
1177+
RemoveRegex: []string{
1178+
"dir3",
1179+
},
1180+
},
1181+
{
1182+
ID: "example-id",
1183+
Version: "2.0.0",
1184+
SourceRoots: []string{
1185+
"dir1",
1186+
"one/global/example.txt",
1187+
},
1188+
RemoveRegex: []string{
1189+
"dir1",
1190+
},
1191+
},
1192+
},
1193+
},
1194+
config: &config.LibrarianConfig{
1195+
GlobalFilesAllowlist: []*config.GlobalFile{
1196+
{
1197+
Path: "one/global/example.txt",
1198+
Permissions: "read-write",
1199+
},
1200+
},
1201+
},
1202+
repo: &MockRepository{
1203+
Dir: t.TempDir(),
1204+
RemotesValue: []*gitrepo.Remote{
1205+
{
1206+
Name: "origin",
1207+
URLs: []string{"https://github.com/googleapis/librarian.git"},
1208+
},
1209+
},
1210+
GetCommitsForPathsSinceTagValueByTag: map[string][]*gitrepo.Commit{
1211+
"another-example-id-1.0.0": {
1212+
{
1213+
Hash: plumbing.NewHash("123456"),
1214+
Message: "feat: bump version",
1215+
},
1216+
},
1217+
"example-id-2.0.0": {
1218+
{
1219+
Hash: plumbing.NewHash("123456"),
1220+
Message: "feat: bump version",
1221+
},
1222+
},
1223+
},
1224+
ChangedFilesInCommitValueByHash: map[string][]string{
1225+
plumbing.NewHash("123456").String(): {
1226+
"one/global/example.txt",
1227+
},
1228+
},
1229+
},
1230+
client: &mockContainerClient{},
1231+
want: &config.LibrarianState{
1232+
Libraries: []*config.LibraryState{
1233+
{
1234+
ID: "another-example-id",
1235+
Version: "1.1.0",
1236+
PreviousVersion: "1.0.0",
1237+
SourceRoots: []string{
1238+
"dir3",
1239+
"one/global/example.txt",
1240+
},
1241+
RemoveRegex: []string{
1242+
"dir3",
1243+
},
1244+
Changes: []*config.Commit{
1245+
{
1246+
Type: "feat",
1247+
Subject: "bump version",
1248+
CommitHash: "1234560000000000000000000000000000000000",
1249+
},
1250+
},
1251+
ReleaseTriggered: true,
1252+
},
1253+
{
1254+
ID: "example-id",
1255+
Version: "2.1.0",
1256+
PreviousVersion: "2.0.0",
1257+
SourceRoots: []string{
1258+
"dir1",
1259+
"one/global/example.txt",
1260+
},
1261+
RemoveRegex: []string{
1262+
"dir1",
1263+
},
1264+
Changes: []*config.Commit{
1265+
{
1266+
Type: "feat",
1267+
Subject: "bump version",
1268+
CommitHash: "1234560000000000000000000000000000000000",
1269+
},
1270+
},
1271+
ReleaseTriggered: true,
1272+
},
1273+
},
1274+
},
1275+
},
1276+
} {
1277+
output := t.TempDir()
1278+
for _, globalFile := range test.config.GlobalFilesAllowlist {
1279+
file := filepath.Join(output, globalFile.Path)
1280+
if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
1281+
t.Fatal(err)
1282+
}
1283+
if err := os.WriteFile(file, []byte("new content"), 0755); err != nil {
1284+
t.Fatal(err)
1285+
}
1286+
}
1287+
r := &initRunner{
1288+
repo: test.repo,
1289+
state: test.state,
1290+
librarianConfig: test.config,
1291+
containerClient: test.client,
1292+
}
1293+
err := r.runInitCommand(t.Context(), output)
1294+
if err != nil {
1295+
t.Errorf("failed to run runInitCommand(): %q", err.Error())
1296+
return
1297+
}
1298+
if diff := cmp.Diff(test.want, r.state); diff != "" {
1299+
t.Errorf("commit filter mismatch (-want +got):\n%s", diff)
1300+
}
1301+
}
1302+
}
1303+
10481304
func TestProcessLibrary(t *testing.T) {
10491305
t.Parallel()
10501306
for _, test := range []struct {

0 commit comments

Comments
 (0)