gopkg.in/docker/docker.v20@v20.10.27/pkg/system/path_windows_test.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  package system // import "github.com/docker/docker/pkg/system"
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/containerd/continuity/pathdriver"
    10  )
    11  
    12  // TestCheckSystemDriveAndRemoveDriveLetter tests CheckSystemDriveAndRemoveDriveLetter
    13  func TestCheckSystemDriveAndRemoveDriveLetter(t *testing.T) {
    14  	// Fails if not C drive.
    15  	_, err := CheckSystemDriveAndRemoveDriveLetter(`d:\`, pathdriver.LocalPathDriver)
    16  	if err == nil || err.Error() != "The specified path is not on the system drive (C:)" {
    17  		t.Fatalf("Expected error for d:")
    18  	}
    19  
    20  	// Single character is unchanged
    21  	var path string
    22  	if path, err = CheckSystemDriveAndRemoveDriveLetter("z", pathdriver.LocalPathDriver); err != nil {
    23  		t.Fatalf("Single character should pass")
    24  	}
    25  	if path != "z" {
    26  		t.Fatalf("Single character should be unchanged")
    27  	}
    28  
    29  	// Two characters without colon is unchanged
    30  	if path, err = CheckSystemDriveAndRemoveDriveLetter("AB", pathdriver.LocalPathDriver); err != nil {
    31  		t.Fatalf("2 characters without colon should pass")
    32  	}
    33  	if path != "AB" {
    34  		t.Fatalf("2 characters without colon should be unchanged")
    35  	}
    36  
    37  	// Abs path without drive letter
    38  	if path, err = CheckSystemDriveAndRemoveDriveLetter(`\l`, pathdriver.LocalPathDriver); err != nil {
    39  		t.Fatalf("abs path no drive letter should pass")
    40  	}
    41  	if path != `\l` {
    42  		t.Fatalf("abs path without drive letter should be unchanged")
    43  	}
    44  
    45  	// Abs path without drive letter, linux style
    46  	if path, err = CheckSystemDriveAndRemoveDriveLetter(`/l`, pathdriver.LocalPathDriver); err != nil {
    47  		t.Fatalf("abs path no drive letter linux style should pass")
    48  	}
    49  	if path != `\l` {
    50  		t.Fatalf("abs path without drive letter linux failed %s", path)
    51  	}
    52  
    53  	// Drive-colon should be stripped
    54  	if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:\`, pathdriver.LocalPathDriver); err != nil {
    55  		t.Fatalf("An absolute path should pass")
    56  	}
    57  	if path != `\` {
    58  		t.Fatalf(`An absolute path should have been shortened to \ %s`, path)
    59  	}
    60  
    61  	// Verify with a linux-style path
    62  	if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:/`, pathdriver.LocalPathDriver); err != nil {
    63  		t.Fatalf("An absolute path should pass")
    64  	}
    65  	if path != `\` {
    66  		t.Fatalf(`A linux style absolute path should have been shortened to \ %s`, path)
    67  	}
    68  
    69  	// Failure on c:
    70  	if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:`, pathdriver.LocalPathDriver); err == nil {
    71  		t.Fatalf("c: should fail")
    72  	}
    73  	if err.Error() != `No relative path specified in "c:"` {
    74  		t.Fatalf(path, err)
    75  	}
    76  
    77  	// Failure on d:
    78  	if path, err = CheckSystemDriveAndRemoveDriveLetter(`d:`, pathdriver.LocalPathDriver); err == nil {
    79  		t.Fatalf("c: should fail")
    80  	}
    81  	if err.Error() != `No relative path specified in "d:"` {
    82  		t.Fatalf(path, err)
    83  	}
    84  }