github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/builder/dockerfile/dispatchers_unix_test.go (about)

     1  // +build !windows
     2  
     3  package dockerfile
     4  
     5  import (
     6  	"testing"
     7  )
     8  
     9  func TestNormaliseWorkdir(t *testing.T) {
    10  	testCases := []struct{ current, requested, expected, expectedError string }{
    11  		{``, ``, ``, `cannot normalise nothing`},
    12  		{``, `foo`, `/foo`, ``},
    13  		{``, `/foo`, `/foo`, ``},
    14  		{`/foo`, `bar`, `/foo/bar`, ``},
    15  		{`/foo`, `/bar`, `/bar`, ``},
    16  	}
    17  
    18  	for _, test := range testCases {
    19  		normalised, err := normaliseWorkdir(test.current, test.requested)
    20  
    21  		if test.expectedError != "" && err == nil {
    22  			t.Fatalf("NormaliseWorkdir should return an error %s, got nil", test.expectedError)
    23  		}
    24  
    25  		if test.expectedError != "" && err.Error() != test.expectedError {
    26  			t.Fatalf("NormaliseWorkdir returned wrong error. Expected %s, got %s", test.expectedError, err.Error())
    27  		}
    28  
    29  		if normalised != test.expected {
    30  			t.Fatalf("NormaliseWorkdir error. Expected %s for current %s and requested %s, got %s", test.expected, test.current, test.requested, normalised)
    31  		}
    32  	}
    33  }