@@ -263,20 +263,33 @@ func TestCreateWorkRoot(t *testing.T) {
263263 tempDir = os .TempDir
264264 }()
265265 for _ , test := range []struct {
266- name string
267- workRoot string
268- setup func (t * testing.T ) (string , func ())
269- errMsg string
266+ name string
267+ config * Config
268+ setup func (t * testing.T ) (string , func ())
269+ errMsg string
270270 }{
271271 {
272- name : "configured root" ,
273- workRoot : "/some/path" ,
272+ name : "configured root" ,
273+ config : & Config {
274+ WorkRoot : "/some/path" ,
275+ },
274276 setup : func (t * testing.T ) (string , func ()) {
275277 return "/some/path" , func () {}
276278 },
277279 },
278280 {
279- name : "without override, new dir" ,
281+ name : "version command" ,
282+ config : & Config {
283+ commandName : "version" ,
284+ WorkRoot : "/some/path" ,
285+ },
286+ setup : func (t * testing.T ) (string , func ()) {
287+ return "/some/path" , func () {}
288+ },
289+ },
290+ {
291+ name : "without override, new dir" ,
292+ config : & Config {},
280293 setup : func (t * testing.T ) (string , func ()) {
281294 expectedPath := filepath .Join (localTempDir , fmt .Sprintf ("librarian-%s" , formatTimestamp (timestamp )))
282295 return expectedPath , func () {
@@ -287,7 +300,8 @@ func TestCreateWorkRoot(t *testing.T) {
287300 },
288301 },
289302 {
290- name : "without override, dir exists" ,
303+ name : "without override, dir exists" ,
304+ config : & Config {},
291305 setup : func (t * testing.T ) (string , func ()) {
292306 expectedPath := filepath .Join (localTempDir , fmt .Sprintf ("librarian-%s" , formatTimestamp (timestamp )))
293307 if err := os .Mkdir (expectedPath , 0755 ); err != nil {
@@ -306,10 +320,7 @@ func TestCreateWorkRoot(t *testing.T) {
306320 want , cleanup := test .setup (t )
307321 defer cleanup ()
308322
309- c := & Config {
310- WorkRoot : test .workRoot ,
311- }
312- err := c .createWorkRoot ()
323+ err := test .config .createWorkRoot ()
313324 if test .errMsg != "" {
314325 if ! strings .Contains (err .Error (), test .errMsg ) {
315326 t .Errorf ("createWorkRoot() = %q, want contains %q" , err , test .errMsg )
@@ -320,8 +331,8 @@ func TestCreateWorkRoot(t *testing.T) {
320331 return
321332 }
322333
323- if c .WorkRoot != want {
324- t .Errorf ("createWorkRoot() = %v, want %v" , c .WorkRoot , want )
334+ if test . config .WorkRoot != want {
335+ t .Errorf ("createWorkRoot() = %v, want %v" , test . config .WorkRoot , want )
325336 }
326337 })
327338 }
@@ -330,18 +341,21 @@ func TestCreateWorkRoot(t *testing.T) {
330341func TestDeriveRepo (t * testing.T ) {
331342 for _ , test := range []struct {
332343 name string
333- repoPath string
344+ config * Config
334345 setup func (t * testing.T , dir string )
335346 wantErr bool
336347 wantRepoPath string
337348 }{
338349 {
339- name : "configured repo path" ,
340- repoPath : "/some/path" ,
350+ name : "configured repo path" ,
351+ config : & Config {
352+ Repo : "/some/path" ,
353+ },
341354 wantRepoPath : "/some/path" ,
342355 },
343356 {
344- name : "empty repo path, state file exists" ,
357+ name : "empty repo path, state file exists" ,
358+ config : & Config {},
345359 setup : func (t * testing.T , dir string ) {
346360 stateDir := filepath .Join (dir , LibrarianDir )
347361 if err := os .MkdirAll (stateDir , 0755 ); err != nil {
@@ -355,8 +369,17 @@ func TestDeriveRepo(t *testing.T) {
355369 },
356370 {
357371 name : "empty repo path, no state file" ,
372+ config : & Config {},
358373 wantErr : true ,
359374 },
375+ {
376+ name : "version command" ,
377+ config : & Config {
378+ Repo : "/some/path" ,
379+ commandName : "version" ,
380+ },
381+ wantRepoPath : "/some/path" ,
382+ },
360383 } {
361384 t .Run (test .name , func (t * testing.T ) {
362385 tmpDir := t .TempDir ()
@@ -365,10 +388,7 @@ func TestDeriveRepo(t *testing.T) {
365388 }
366389 t .Chdir (tmpDir )
367390
368- c := & Config {
369- Repo : test .repoPath ,
370- }
371- err := c .deriveRepo ()
391+ err := test .config .deriveRepo ()
372392 if (err != nil ) != test .wantErr {
373393 t .Errorf ("deriveRepoPath() error = %v, wantErr %v" , err , test .wantErr )
374394 return
@@ -379,7 +399,7 @@ func TestDeriveRepo(t *testing.T) {
379399 wantPath = tmpDir
380400 }
381401
382- if diff := cmp .Diff (wantPath , c .Repo ); diff != "" {
402+ if diff := cmp .Diff (wantPath , test . config .Repo ); diff != "" {
383403 t .Errorf ("deriveRepoPath() mismatch (-want +got):\n %s" , diff )
384404 }
385405 })
@@ -491,3 +511,50 @@ func TestSetDefaults(t *testing.T) {
491511 })
492512 }
493513}
514+
515+ func TestValidateHostMount (t * testing.T ) {
516+ for _ , test := range []struct {
517+ name string
518+ hostMount string
519+ defaultMount string
520+ wantErr bool
521+ wantErrMsg string
522+ }{
523+ {
524+ name : "default host mount" ,
525+ hostMount : "example/path:/path" ,
526+ defaultMount : "example/path:/path" ,
527+ },
528+ {
529+ name : "valid host mount" ,
530+ hostMount : "example/path:/mounted/path" ,
531+ defaultMount : "another/path:/path" ,
532+ },
533+ {
534+ name : "invalid host mount" ,
535+ hostMount : "example/path" ,
536+ defaultMount : "example/path:/path" ,
537+ wantErr : true ,
538+ wantErrMsg : "unable to parse host mount" ,
539+ },
540+ } {
541+ t .Run (test .name , func (t * testing.T ) {
542+ ok , err := validateHostMount (test .hostMount , test .defaultMount )
543+ if test .wantErr {
544+ if err == nil {
545+ t .Error ("validateHostMount() should return error" )
546+ }
547+
548+ if ! strings .Contains (err .Error (), test .wantErrMsg ) {
549+ t .Errorf ("want error message: %q, got %q" , test .wantErrMsg , err .Error ())
550+ }
551+
552+ return
553+ }
554+
555+ if ! ok || err != nil {
556+ t .Error ("validateHostMount() should not return error" )
557+ }
558+ })
559+ }
560+ }
0 commit comments