1515package librarian
1616
1717import (
18+ "errors"
1819 "fmt"
1920 "os"
2021 "testing"
2122
2223 "github.com/google/go-cmp/cmp"
2324 "github.com/googleapis/librarian/internal/config"
25+ "github.com/googleapis/librarian/internal/git"
2426 "github.com/googleapis/librarian/internal/sample"
2527 "github.com/googleapis/librarian/internal/testhelper"
2628)
@@ -29,11 +31,11 @@ func TestPublish(t *testing.T) {
2931 // Each test starts (before setup) with Lib1Name with a version of 1.0.0 and
3032 // Lib2Name with a version of 1.2.0.
3133 for _ , test := range []struct {
32- name string
33- setup func (cfg * config.Config )
34- library string
35- execute bool
36- want string
34+ name string
35+ setup func (cfg * config.Config )
36+ releaseCommit string
37+ execute bool
38+ want string
3739 }{
3840 {
3941 name : "publish Lib1Name and Lib2Name" ,
@@ -63,23 +65,26 @@ func TestPublish(t *testing.T) {
6365 want : fmt .Sprintf ("libraries=%s; execute=false" , sample .Lib1Name ),
6466 },
6567 {
66- name : "publish Lib1Name, specified in flags, with a later release of Lib2Name ignored" ,
68+ name : "publish Lib1Name due to release commit specified in flags, with a later release of Lib2Name ignored" ,
6769 setup : func (cfg * config.Config ) {
6870 cfg .Libraries [0 ].Version = "1.1.0"
6971 writeConfigAndCommit (t , cfg )
7072 cfg .Libraries [1 ].Version = "1.3.0"
7173 writeConfigAndCommit (t , cfg )
7274 },
73- library : sample .Lib1Name ,
74- want : fmt .Sprintf ("libraries=%s; execute=false" , sample .Lib1Name ),
75+ // Fortunately this doesn't have to be an actual commit, just a
76+ // committish, so we don't need to know the hash of the HEAD~
77+ // commit when creating the test data.
78+ releaseCommit : "HEAD~" ,
79+ want : fmt .Sprintf ("libraries=%s; execute=false" , sample .Lib1Name ),
7580 },
7681 } {
7782 t .Run (test .name , func (t * testing.T ) {
7883 cfg := sample .Config ()
7984 cfg .Libraries [1 ].Version = "1.2.0"
8085 testhelper .Setup (t , testhelper.SetupOptions {Config : cfg })
8186 test .setup (cfg )
82- if err := publish (t .Context (), cfg , test .library , test .execute ); err != nil {
87+ if err := publish (t .Context (), cfg , test .releaseCommit , test .execute ); err != nil {
8388 t .Fatal (err )
8489 }
8590 got , err := os .ReadFile (fakePublishedFile )
@@ -95,9 +100,10 @@ func TestPublish(t *testing.T) {
95100
96101func TestPublish_Error (t * testing.T ) {
97102 for _ , test := range []struct {
98- name string
99- setup func (cfg * config.Config )
100- library string
103+ name string
104+ setup func (cfg * config.Config )
105+ releaseCommit string
106+ wantErr error
101107 }{
102108 {
103109 name : "custom tool specified for git and doesn't exist" ,
@@ -111,6 +117,7 @@ func TestPublish_Error(t *testing.T) {
111117 }
112118 writeConfigAndCommit (t , cfg )
113119 },
120+ // Can't easily check this error.
114121 },
115122 {
116123 name : "repo is dirty" ,
@@ -122,6 +129,7 @@ func TestPublish_Error(t *testing.T) {
122129 t .Fatal (err )
123130 }
124131 },
132+ wantErr : git .ErrGitStatusUnclean ,
125133 },
126134 {
127135 name : "language isn't supported" ,
@@ -131,30 +139,42 @@ func TestPublish_Error(t *testing.T) {
131139 cfg .Language = "unsupported-for-publish"
132140 writeConfigAndCommit (t , cfg )
133141 },
142+ // Can't easily check this error.
134143 },
135144 {
136145 name : "no release commit" ,
137146 setup : func (cfg * config.Config ) {
138147 },
139148 },
140149 {
141- name : "no release commit for specified library " ,
150+ name : "specified release commit is invalid " ,
142151 setup : func (cfg * config.Config ) {
143- cfg .Libraries [1 ].Version = "1.3.0"
144- writeConfigAndCommit (t , cfg )
145152 },
146- library : sample .Lib1Name ,
153+ releaseCommit : "not a commit" ,
154+ // Can't easily check this error
155+ },
156+ {
157+ name : "release commit does not release anything" ,
158+ setup : func (cfg * config.Config ) {
159+ writeFileAndCommit (t , "README.txt" , []byte ("Just a readme" ), "Modified config" )
160+ },
161+ releaseCommit : "HEAD" ,
162+ wantErr : errNoLibrariesAtReleaseCommit ,
147163 },
148164 } {
149165 t .Run (test .name , func (t * testing.T ) {
150166 cfg := sample .Config ()
151167 cfg .Libraries [1 ].Version = "1.2.0"
152168 testhelper .Setup (t , testhelper.SetupOptions {Config : cfg })
153169 test .setup (cfg )
154- err := publish (t .Context (), cfg , test .library , false )
170+ err := publish (t .Context (), cfg , test .releaseCommit , false )
155171 if err == nil {
156- t .Errorf ( "publish(): expected error, got none " )
172+ t .Fatal ( " expected error, got nil " )
157173 }
174+ if test .wantErr != nil && ! errors .Is (err , test .wantErr ) {
175+ t .Errorf ("expected %v, got %v" , test .wantErr , err )
176+ }
177+
158178 })
159179 }
160180}
@@ -170,7 +190,7 @@ func TestPublishCommand(t *testing.T) {
170190 cfg .Libraries [1 ].Version = "1.3.0"
171191 writeConfigAndCommit (t , cfg )
172192
173- if err := Run (t .Context (), "librarian" , "publish" , "--library " , sample . Lib1Name , "--execute" ); err != nil {
193+ if err := Run (t .Context (), "librarian" , "publish" , "--release-commit " , "HEAD~" , "--execute" ); err != nil {
174194 t .Fatal (err )
175195 }
176196 want := fmt .Sprintf ("libraries=%s; execute=true" , sample .Lib1Name )
0 commit comments