github.com/neilgarb/delve@v1.9.2-nobreaks/pkg/terminal/terminal_test.go (about)

     1  package terminal
     2  
     3  import (
     4  	"errors"
     5  	"net/rpc"
     6  	"runtime"
     7  	"testing"
     8  
     9  	"github.com/go-delve/delve/pkg/config"
    10  )
    11  
    12  type tRule struct {
    13  	from string
    14  	to   string
    15  }
    16  
    17  type tCase struct {
    18  	rules []tRule
    19  	path  string
    20  	res   string
    21  }
    22  
    23  func platformCases() []tCase {
    24  	casesUnix := []tCase{
    25  		// Should not depend on separator at the end of rule path
    26  		{[]tRule{{"/tmp/path", "/new/path2"}}, "/tmp/path/file.go", "/new/path2/file.go"},
    27  		{[]tRule{{"/tmp/path/", "/new/path2/"}}, "/tmp/path/file.go", "/new/path2/file.go"},
    28  		{[]tRule{{"/tmp/path/", "/new/path2"}}, "/tmp/path/file.go", "/new/path2/file.go"},
    29  		{[]tRule{{"/tmp/path", "/new/path2/"}}, "/tmp/path/file.go", "/new/path2/file.go"},
    30  		// Should apply only for directory names
    31  		{[]tRule{{"/tmp/path", "/new/path2"}}, "/tmp/path-2/file.go", "/tmp/path-2/file.go"},
    32  		// First matched rule should be used
    33  		{[]tRule{
    34  			{"/tmp/path1", "/new/path1"},
    35  			{"/tmp/path2", "/new/path2"},
    36  			{"/tmp/path2", "/new/path3"}}, "/tmp/path2/file.go", "/new/path2/file.go"},
    37  	}
    38  	casesLinux := []tCase{
    39  		// Should be case-sensitive
    40  		{[]tRule{{"/tmp/path", "/new/path2"}}, "/TmP/path/file.go", "/TmP/path/file.go"},
    41  	}
    42  	casesFreebsd := []tCase{
    43  		// Should be case-sensitive
    44  		{[]tRule{{"/tmp/path", "/new/path2"}}, "/TmP/path/file.go", "/TmP/path/file.go"},
    45  	}
    46  	casesDarwin := []tCase{
    47  		// Can be either case-sensitive or case-insensitive depending on
    48  		// filesystem settings, we always treat it as case-sensitive.
    49  		{[]tRule{{"/tmp/path", "/new/path2"}}, "/TmP/PaTh/file.go", "/TmP/PaTh/file.go"},
    50  	}
    51  	casesWindows := []tCase{
    52  		// Should not depend on separator at the end of rule path
    53  		{[]tRule{{`c:\tmp\path`, `d:\new\path2`}}, `c:\tmp\path\file.go`, `d:\new\path2\file.go`},
    54  		{[]tRule{{`c:\tmp\path\`, `d:\new\path2\`}}, `c:\tmp\path\file.go`, `d:\new\path2\file.go`},
    55  		{[]tRule{{`c:\tmp\path`, `d:\new\path2\`}}, `c:\tmp\path\file.go`, `d:\new\path2\file.go`},
    56  		{[]tRule{{`c:\tmp\path\`, `d:\new\path2`}}, `c:\tmp\path\file.go`, `d:\new\path2\file.go`},
    57  		// Should apply only for directory names
    58  		{[]tRule{{`c:\tmp\path`, `d:\new\path2`}}, `c:\tmp\path-2\file.go`, `c:\tmp\path-2\file.go`},
    59  		// Should be case-insensitive
    60  		{[]tRule{{`c:\tmp\path`, `d:\new\path2`}}, `C:\TmP\PaTh\file.go`, `d:\new\path2\file.go`},
    61  	}
    62  
    63  	if runtime.GOOS == "windows" {
    64  		return casesWindows
    65  	}
    66  	if runtime.GOOS == "darwin" {
    67  		return append(casesUnix, casesDarwin...)
    68  	}
    69  	if runtime.GOOS == "linux" {
    70  		return append(casesUnix, casesLinux...)
    71  	}
    72  	if runtime.GOOS == "freebsd" {
    73  		return append(casesUnix, casesFreebsd...)
    74  	}
    75  	return casesUnix
    76  }
    77  
    78  func TestSubstitutePath(t *testing.T) {
    79  	for _, c := range platformCases() {
    80  		var subRules config.SubstitutePathRules
    81  		for _, r := range c.rules {
    82  			subRules = append(subRules, config.SubstitutePathRule{From: r.from, To: r.to})
    83  		}
    84  		res := New(nil, &config.Config{SubstitutePath: subRules}).substitutePath(c.path)
    85  		if c.res != res {
    86  			t.Errorf("terminal.SubstitutePath(%q) => %q, want %q", c.path, res, c.res)
    87  		}
    88  	}
    89  }
    90  
    91  func TestIsErrProcessExited(t *testing.T) {
    92  	tests := []struct {
    93  		name   string
    94  		err    error
    95  		result bool
    96  	}{
    97  		{"empty error", errors.New(""), false},
    98  		{"non-ServerError", errors.New("Process 33122 has exited with status 0"), false},
    99  		{"ServerError with zero status", rpc.ServerError("Process 33122 has exited with status 0"), true},
   100  		{"ServerError with non-zero status", rpc.ServerError("Process 2 has exited with status 25"), true},
   101  	}
   102  	for _, test := range tests {
   103  		if isErrProcessExited(test.err) != test.result {
   104  			t.Error(test.name)
   105  		}
   106  	}
   107  }