@@ -110,67 +110,51 @@ func TestRunBuildCommand(t *testing.T) {
110110 name string
111111 build bool
112112 libraryID string
113- repo gitrepo.Repository
114- state * config.LibrarianState
115113 container * mockContainerClient
116114 wantBuildCalls int
117115 wantErr bool
118116 }{
119117 {
120- name : "build flag not specified " ,
118+ name : "build_flag_not_specified " ,
121119 build : false ,
122120 container : & mockContainerClient {},
123121 wantBuildCalls : 0 ,
124122 },
125123 {
126- name : "build with library id" ,
127- build : true ,
128- libraryID : "some-library" ,
129- repo : newTestGitRepo (t ),
130- state : & config.LibrarianState {
131- Libraries : []* config.LibraryState {
132- {
133- ID : "some-library" ,
134- },
135- },
136- },
124+ name : "build_with_library_id" ,
125+ build : true ,
126+ libraryID : "some-library" ,
137127 container : & mockContainerClient {},
138128 wantBuildCalls : 1 ,
139129 },
140130 {
141- name : "build with no library id" ,
142- build : true ,
143- container : & mockContainerClient {},
131+ name : "build_with_no_library_id" ,
132+ build : true ,
133+ container : & mockContainerClient {},
134+ wantBuildCalls : 0 ,
144135 },
145136 {
146- name : "build with no response " ,
137+ name : "build_with_no_response " ,
147138 build : true ,
148139 libraryID : "some-library" ,
149- repo : newTestGitRepo (t ),
150- state : & config.LibrarianState {
151- Libraries : []* config.LibraryState {
152- {
153- ID : "some-library" ,
154- },
155- },
156- },
157140 container : & mockContainerClient {
158141 noBuildResponse : true ,
159142 },
160143 wantBuildCalls : 1 ,
161144 },
162145 {
163- name : "build with error response in response " ,
146+ name : "build_with_docker_command_error_files_restored " ,
164147 build : true ,
165148 libraryID : "some-library" ,
166- repo : newTestGitRepo (t ),
167- state : & config.LibrarianState {
168- Libraries : []* config.LibraryState {
169- {
170- ID : "some-library" ,
171- },
172- },
149+ container : & mockContainerClient {
150+ buildErr : errors .New ("simulate build error" ),
173151 },
152+ wantErr : true ,
153+ },
154+ {
155+ name : "build_with_error_response_in_response" ,
156+ build : true ,
157+ libraryID : "some-library" ,
174158 container : & mockContainerClient {
175159 wantErrorMsg : true ,
176160 },
@@ -179,20 +163,87 @@ func TestRunBuildCommand(t *testing.T) {
179163 } {
180164 t .Run (test .name , func (t * testing.T ) {
181165 t .Parallel ()
166+ repo := newTestGitRepo (t )
167+ state := & config.LibrarianState {
168+ Libraries : []* config.LibraryState {
169+ {
170+ ID : "some-library" ,
171+ SourceRoots : []string {
172+ "a/path" ,
173+ "another/path" ,
174+ },
175+ },
176+ },
177+ }
182178 r := & generateRunner {
183179 build : test .build ,
184- repo : test . repo ,
185- state : test . state ,
180+ repo : repo ,
181+ state : state ,
186182 containerClient : test .container ,
187183 }
188184
185+ // Create library files and commit the change.
186+ repoDir := r .repo .GetDir ()
187+ for _ , library := range r .state .Libraries {
188+ for _ , srcPath := range library .SourceRoots {
189+ relPath := filepath .Join (repoDir , srcPath )
190+ if err := os .MkdirAll (relPath , 0755 ); err != nil {
191+ t .Fatal (err )
192+ }
193+ file := filepath .Join (relPath , "example.txt" )
194+ if err := os .WriteFile (file , []byte ("old content" ), 0755 ); err != nil {
195+ t .Fatal (err )
196+ }
197+ }
198+ }
199+ if _ , err := r .repo .AddAll (); err != nil {
200+ t .Fatal (err )
201+ }
202+ if err := r .repo .Commit ("test commit" ); err != nil {
203+ t .Fatal (err )
204+ }
205+ // Modify library files and add untacked files.
206+ for _ , library := range r .state .Libraries {
207+ for _ , srcPath := range library .SourceRoots {
208+ file := filepath .Join (repoDir , srcPath , "example.txt" )
209+ if err := os .WriteFile (file , []byte ("new content" ), 0755 ); err != nil {
210+ t .Fatal (err )
211+ }
212+
213+ newFile := filepath .Join (repoDir , srcPath , "another_example.txt" )
214+ if err := os .WriteFile (newFile , []byte ("new content" ), 0755 ); err != nil {
215+ t .Fatal (err )
216+ }
217+ }
218+ }
219+
189220 err := r .runBuildCommand (context .Background (), test .libraryID )
190221 if test .wantErr {
191222 if err == nil {
192- t .Fatalf ( "%s should return error" , test . name )
223+ t .Fatal ( err )
193224 }
225+ // Verify the library files are restore.
226+ for _ , library := range r .state .Libraries {
227+ for _ , srcPath := range library .SourceRoots {
228+ file := filepath .Join (repoDir , srcPath , "example.txt" )
229+ readFile , err := os .ReadFile (file )
230+ if err != nil {
231+ t .Fatal (err )
232+ }
233+ if diff := cmp .Diff ("old content" , string (readFile )); diff != "" {
234+ t .Errorf ("file content mismatch (-want +got):%s" , diff )
235+ }
236+
237+ newFile := filepath .Join (repoDir , srcPath , "another_example.txt" )
238+ if _ , err := os .Stat (newFile ); ! os .IsNotExist (err ) {
239+ t .Fatal (err )
240+ }
241+ }
242+ }
243+
194244 return
195245 }
246+
196247 if err != nil {
197248 t .Fatal (err )
198249 }
0 commit comments