@@ -24,6 +24,7 @@ import (
2424 "strings"
2525 "testing"
2626
27+ "github.com/go-git/go-git/v5/plumbing"
2728 "github.com/google/go-cmp/cmp"
2829 "github.com/googleapis/librarian/internal/config"
2930 "github.com/googleapis/librarian/internal/gitrepo"
@@ -750,6 +751,7 @@ func TestGenerateScenarios(t *testing.T) {
750751 build : true ,
751752 wantGenerateCalls : 1 ,
752753 wantErr : true ,
754+ wantErrMsg : "failed to make output directory" ,
753755 },
754756 {
755757 name : "generate error" ,
@@ -873,6 +875,17 @@ func TestGenerateScenarios(t *testing.T) {
873875 t .Fatal (err )
874876 }
875877
878+ // Create a symlink in the output directory to trigger an error.
879+ if test .name == "symlink in output" {
880+ outputDir := filepath .Join (r .workRoot , "output" )
881+ if err := os .MkdirAll (outputDir , 0755 ); err != nil {
882+ t .Fatalf ("os.MkdirAll() = %v" , err )
883+ }
884+ if err := os .Symlink ("target" , filepath .Join (outputDir , "symlink" )); err != nil {
885+ t .Fatalf ("os.Symlink() = %v" , err )
886+ }
887+ }
888+
876889 err := r .run (context .Background ())
877890 if test .wantErr {
878891 if err == nil {
@@ -1468,3 +1481,139 @@ func TestCompileRegexps(t *testing.T) {
14681481 })
14691482 }
14701483}
1484+
1485+ func TestUpdateChangesSinceLastGeneration (t * testing.T ) {
1486+ t .Parallel ()
1487+ hash1 := plumbing .NewHash ("1234567" )
1488+ hash2 := plumbing .NewHash ("abcdefg" )
1489+ for _ , test := range []struct {
1490+ name string
1491+ libraryID string
1492+ libraries []* config.LibraryState
1493+ repo gitrepo.Repository
1494+ want * config.LibraryState
1495+ wantErr bool
1496+ wantErrMsg string
1497+ }{
1498+ {
1499+ name : "update changes in a library" ,
1500+ libraryID : "another-id" ,
1501+ libraries : []* config.LibraryState {
1502+ {
1503+ ID : "example-d" ,
1504+ },
1505+ {
1506+ ID : "another-id" ,
1507+ LastGeneratedCommit : "fake-sha" ,
1508+ APIs : []* config.API {
1509+ {
1510+ Path : "api/one/path" ,
1511+ },
1512+ {
1513+ Path : "api/another/path" ,
1514+ },
1515+ },
1516+ },
1517+ },
1518+ repo : & MockRepository {
1519+ GetCommitsForPathsSinceLastGenByPath : map [string ][]* gitrepo.Commit {
1520+ "api/one/path" : {
1521+ {
1522+ Message : "feat: new feature\n \n This is body.\n \n PiperOrigin-RevId: 98765" ,
1523+ Hash : hash1 ,
1524+ },
1525+ },
1526+ "api/another/path" : {
1527+ {
1528+ Message : "fix: a bug fix\n \n This is another body.\n \n PiperOrigin-RevId: 573342" ,
1529+ Hash : hash2 ,
1530+ },
1531+ },
1532+ },
1533+ ChangedFilesInCommitValueByHash : map [string ][]string {
1534+ hash1 .String (): {
1535+ "api/one/path/file.txt" ,
1536+ "api/another/path/example.txt" ,
1537+ },
1538+ hash2 .String (): {
1539+ "api/one/path/another-file.txt" ,
1540+ "api/another/path/another-example.txt" ,
1541+ },
1542+ },
1543+ },
1544+ want : & config.LibraryState {
1545+ ID : "another-id" ,
1546+ LastGeneratedCommit : "fake-sha" ,
1547+ APIs : []* config.API {
1548+ {
1549+ Path : "api/one/path" ,
1550+ },
1551+ {
1552+ Path : "api/another/path" ,
1553+ },
1554+ },
1555+ Changes : []* config.Change {
1556+ {
1557+ Type : "feat" ,
1558+ Subject : "new feature" ,
1559+ Body : "This is body." ,
1560+ ClNum : "98765" ,
1561+ CommitHash : hash1 .String (),
1562+ },
1563+ {
1564+ Type : "fix" ,
1565+ Subject : "a bug fix" ,
1566+ Body : "This is another body." ,
1567+ ClNum : "573342" ,
1568+ CommitHash : hash2 .String (),
1569+ },
1570+ },
1571+ },
1572+ },
1573+ {
1574+ name : "failed to get conventional commits" ,
1575+ libraryID : "another-id" ,
1576+ libraries : []* config.LibraryState {
1577+ {
1578+ ID : "example-d" ,
1579+ },
1580+ {
1581+ ID : "another-id" ,
1582+ },
1583+ },
1584+ repo : & MockRepository {
1585+ GetCommitsForPathsSinceLastGenError : errors .New ("simulated error" ),
1586+ },
1587+ wantErr : true ,
1588+ wantErrMsg : "failed to fetch conventional commits for library" ,
1589+ },
1590+ } {
1591+ t .Run (test .name , func (t * testing.T ) {
1592+ runner := & generateRunner {
1593+ sourceRepo : test .repo ,
1594+ state : & config.LibrarianState {
1595+ Libraries : test .libraries ,
1596+ },
1597+ }
1598+ err := runner .updateChangesSinceLastGeneration (test .libraryID )
1599+ if test .wantErr {
1600+ if err == nil {
1601+ t .Error ("updateChangesSinceLastGeneration() should fail" )
1602+ }
1603+ if ! strings .Contains (err .Error (), test .wantErrMsg ) {
1604+ t .Errorf ("want error message %s, got %s" , test .wantErrMsg , err .Error ())
1605+ }
1606+
1607+ return
1608+ }
1609+
1610+ if err != nil {
1611+ t .Errorf ("updateChangesSinceLastGeneration() failed: %q" , err )
1612+ }
1613+
1614+ if diff := cmp .Diff (test .want , runner .state .Libraries [1 ]); diff != "" {
1615+ t .Errorf ("updateChangesSinceLastGeneration() dirs mismatch (-want +got):\n %s" , diff )
1616+ }
1617+ })
1618+ }
1619+ }
0 commit comments