github.com/april1989/origin-go-tools@v0.0.32/cmd/go-contrib-init/contrib_test.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"errors"
     9  	"os"
    10  	"os/exec"
    11  	"runtime"
    12  	"testing"
    13  )
    14  
    15  func TestExpandUser(t *testing.T) {
    16  	env := "HOME"
    17  	if runtime.GOOS == "windows" {
    18  		env = "USERPROFILE"
    19  	} else if runtime.GOOS == "plan9" {
    20  		env = "home"
    21  	}
    22  
    23  	oldenv := os.Getenv(env)
    24  	os.Setenv(env, "/home/gopher")
    25  	defer os.Setenv(env, oldenv)
    26  
    27  	tests := []struct {
    28  		input string
    29  		want  string
    30  	}{
    31  		{input: "~/foo", want: "/home/gopher/foo"},
    32  		{input: "${HOME}/foo", want: "/home/gopher/foo"},
    33  		{input: "/~/foo", want: "/~/foo"},
    34  	}
    35  	for _, tt := range tests {
    36  		got := expandUser(tt.input)
    37  		if got != tt.want {
    38  			t.Fatalf("want %q, but %q", tt.want, got)
    39  		}
    40  	}
    41  }
    42  
    43  func TestCmdErr(t *testing.T) {
    44  	tests := []struct {
    45  		input error
    46  		want  string
    47  	}{
    48  		{input: errors.New("cmd error"), want: "cmd error"},
    49  		{input: &exec.ExitError{ProcessState: nil, Stderr: nil}, want: "<nil>"},
    50  		{input: &exec.ExitError{ProcessState: nil, Stderr: []byte("test")}, want: "<nil>: test"},
    51  	}
    52  
    53  	for i, tt := range tests {
    54  		got := cmdErr(tt.input)
    55  		if got != tt.want {
    56  			t.Fatalf("%d. got %q, want %q", i, got, tt.want)
    57  		}
    58  	}
    59  }