github.com/moby/docker@v26.1.3+incompatible/pkg/archive/path_windows_test.go (about)

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