github.com/mattn/wecho@v0.0.0-20170216094930-02666edb8444/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestArgline(t *testing.T) {
    13  	tests := []struct {
    14  		exe  string
    15  		line string
    16  		want string
    17  	}{
    18  		{`foo`, `foo/`, `/`},
    19  		{`foo.exe`, `foo.exe/`, `/`},
    20  		{`foo.exe`, `foo.exe /`, `/`},
    21  	}
    22  
    23  	for _, test := range tests {
    24  		got := argline(test.exe, test.line)
    25  		if test.want != got {
    26  			t.Fatalf("want %q, but %q:", test.want, got)
    27  		}
    28  	}
    29  }
    30  
    31  func TestRun(t *testing.T) {
    32  	dir, err := ioutil.TempDir("", "")
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	defer os.RemoveAll(dir)
    37  	_, err = exec.Command("go", "build", "-o", filepath.Join(dir, "wecho.exe"), "main.go").CombinedOutput()
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	wd, _ := os.Getwd()
    42  	defer os.Chdir(wd)
    43  
    44  	os.Chdir(dir)
    45  
    46  	tests := []struct {
    47  		cmd  string
    48  		want string
    49  	}{
    50  		{`wecho/`, `/`},
    51  		{`wecho /`, `/`},
    52  		{`wecho  /`, ` /`},
    53  		{`wecho.exe/`, `/`},
    54  		{`wecho.exe /`, `/`},
    55  		{`wecho.exe  /`, ` /`},
    56  		{`wecho.exe  こんにちわ     世界`, ` こんにちわ     世界`},
    57  	}
    58  
    59  	for _, test := range tests {
    60  		b, err := exec.Command("cmd", "/c", test.cmd).CombinedOutput()
    61  		if err != nil {
    62  			t.Fatal(err)
    63  		}
    64  		got := strings.Split(string(b), "\n")[0]
    65  		if test.want != got {
    66  			t.Fatalf("want %q, but %q:", test.want, got)
    67  		}
    68  	}
    69  
    70  }