github.com/driusan/bug@v0.3.2-0.20190306121946-d7f4e7f33fea/bugapp/Create_test.go (about) 1 package bugapp 2 3 import ( 4 "fmt" 5 // "io" 6 "io/ioutil" 7 "os" 8 "testing" 9 ) 10 11 func captureOutput(f func(), t *testing.T) (string, string) { 12 // Capture STDOUT with a pipe 13 stdout := os.Stdout 14 stderr := os.Stderr 15 so, op, _ := os.Pipe() //outpipe 16 oe, ep, _ := os.Pipe() //errpipe 17 defer func(stdout, stderr *os.File) { 18 os.Stdout = stdout 19 os.Stderr = stderr 20 }(stdout, stderr) 21 22 os.Stdout = op 23 os.Stderr = ep 24 25 f() 26 27 os.Stdout = stdout 28 os.Stderr = stderr 29 30 op.Close() 31 ep.Close() 32 33 errOutput, err := ioutil.ReadAll(oe) 34 if err != nil { 35 t.Error("Could not get output from stderr") 36 } 37 stdOutput, err := ioutil.ReadAll(so) 38 if err != nil { 39 t.Error("Could not get output from stdout") 40 } 41 return string(stdOutput), string(errOutput) 42 } 43 44 // Captures stdout and stderr to ensure that 45 // a usage line gets printed to Stderr when 46 // no parameters are specified 47 func TestCreateHelpOutput(t *testing.T) { 48 49 stdout, stderr := captureOutput(func() { 50 Create(ArgumentList{}) 51 }, t) 52 53 if stdout != "" { 54 t.Error("Unexpected output on stdout.") 55 } 56 if stderr[:7] != "Usage: " { 57 t.Error("Expected usage information with no arguments") 58 } 59 60 } 61 62 // Test a very basic invocation of "Create" with the -n 63 // argument. We can't try it without -n, since it means 64 // an editor will be spawned.. 65 func TestCreateNoEditor(t *testing.T) { 66 dir, err := ioutil.TempDir("", "createtest") 67 if err != nil { 68 t.Error("Could not create temporary dir for test") 69 return 70 } 71 os.Chdir(dir) 72 os.MkdirAll("issues", 0700) 73 defer os.RemoveAll(dir) 74 // On MacOS, /tmp is a symlink, which causes GetDirectory() to return 75 // a different path than expected in these tests, so make the issues 76 // directory explicit with an environment variable 77 err = os.Setenv("PMIT", dir) 78 if err != nil { 79 t.Error("Could not set environment variable: " + err.Error()) 80 return 81 } 82 83 stdout, stderr := captureOutput(func() { 84 Create(ArgumentList{"-n", "Test", "bug"}) 85 }, t) 86 if stderr != "" { 87 t.Error("Unexpected output on STDERR for Test-bug") 88 } 89 if stdout != "Created issue: Test bug\n" { 90 t.Error("Unexpected output on STDOUT for Test-bug") 91 } 92 issuesDir, err := ioutil.ReadDir(fmt.Sprintf("%s/issues/", dir)) 93 if err != nil { 94 t.Error("Could not read issues directory") 95 return 96 } 97 if len(issuesDir) != 1 { 98 t.Error("Unexpected number of issues in issues dir\n") 99 } 100 101 bugDir, err := ioutil.ReadDir(fmt.Sprintf("%s/issues/Test-bug", dir)) 102 if len(bugDir) != 1 { 103 t.Error("Unexpected number of files found in Test-bug dir\n") 104 } 105 if err != nil { 106 t.Error("Could not read Test-bug directory") 107 return 108 } 109 110 file, err := ioutil.ReadFile(fmt.Sprintf("%s/issues/Test-bug/Description", dir)) 111 if err != nil { 112 t.Error("Could not load description file for Test bug" + err.Error()) 113 } 114 if len(file) != 0 { 115 t.Error("Expected empty file for Test bug") 116 } 117 }