github.com/echohead/hub@v2.2.1+incompatible/github/editor_test.go (about) 1 package github 2 3 import ( 4 "bufio" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "strings" 9 "testing" 10 11 "github.com/github/hub/Godeps/_workspace/src/github.com/bmizerany/assert" 12 ) 13 14 func TestEditor_openAndEdit_deleteFileWhenOpeningEditorFails(t *testing.T) { 15 tempFile, _ := ioutil.TempFile("", "editor-test") 16 tempFile.Close() 17 18 ioutil.WriteFile(tempFile.Name(), []byte("hello"), 0644) 19 editor := Editor{ 20 Program: "memory", 21 File: tempFile.Name(), 22 Topic: "test", 23 openEditor: func(program string, file string) error { 24 assert.Equal(t, "memory", program) 25 assert.Equal(t, tempFile.Name(), file) 26 return fmt.Errorf("error") 27 }, 28 } 29 30 _, err := os.Stat(tempFile.Name()) 31 assert.Equal(t, nil, err) 32 33 _, err = editor.openAndEdit() 34 assert.Equal(t, "error using text editor for test message", fmt.Sprintf("%s", err)) 35 36 // file is removed if there's error 37 _, err = os.Stat(tempFile.Name()) 38 assert.T(t, os.IsNotExist(err)) 39 } 40 41 func TestEditor_openAndEdit_readFileIfExist(t *testing.T) { 42 tempFile, _ := ioutil.TempFile("", "editor-test") 43 tempFile.Close() 44 45 ioutil.WriteFile(tempFile.Name(), []byte("hello"), 0644) 46 editor := Editor{ 47 Program: "memory", 48 File: tempFile.Name(), 49 openEditor: func(program string, file string) error { 50 assert.Equal(t, "memory", program) 51 assert.Equal(t, tempFile.Name(), file) 52 53 return nil 54 }, 55 } 56 57 content, err := editor.openAndEdit() 58 assert.Equal(t, nil, err) 59 assert.Equal(t, "hello", string(content)) 60 } 61 62 func TestEditor_openAndEdit_writeFileIfNotExist(t *testing.T) { 63 tempFile, _ := ioutil.TempFile("", "PULLREQ") 64 tempFile.Close() 65 66 editor := Editor{ 67 Program: "memory", 68 File: tempFile.Name(), 69 openEditor: func(program string, file string) error { 70 assert.Equal(t, "memory", program) 71 assert.Equal(t, tempFile.Name(), file) 72 73 return ioutil.WriteFile(file, []byte("hello"), 0644) 74 }, 75 } 76 77 content, err := editor.openAndEdit() 78 assert.Equal(t, nil, err) 79 assert.Equal(t, "hello", string(content)) 80 } 81 82 func TestEditor_EditTitleAndBodyEmptyTitle(t *testing.T) { 83 tempFile, _ := ioutil.TempFile("", "PULLREQ") 84 tempFile.Close() 85 86 editor := Editor{ 87 Program: "memory", 88 File: tempFile.Name(), 89 CS: "#", 90 openEditor: func(program string, file string) error { 91 assert.Equal(t, "memory", program) 92 assert.Equal(t, tempFile.Name(), file) 93 return ioutil.WriteFile(file, []byte(""), 0644) 94 }, 95 } 96 97 title, body, err := editor.EditTitleAndBody() 98 assert.Equal(t, nil, err) 99 assert.Equal(t, "", title) 100 assert.Equal(t, "", body) 101 102 _, err = os.Stat(tempFile.Name()) 103 assert.T(t, os.IsNotExist(err)) 104 } 105 106 func TestEditor_EditTitleAndBody(t *testing.T) { 107 tempFile, _ := ioutil.TempFile("", "PULLREQ") 108 tempFile.Close() 109 110 editor := Editor{ 111 Program: "memory", 112 File: tempFile.Name(), 113 CS: "#", 114 openEditor: func(program string, file string) error { 115 assert.Equal(t, "memory", program) 116 assert.Equal(t, tempFile.Name(), file) 117 118 message := `A title 119 A title continues 120 121 A body 122 A body continues 123 # comment 124 ` 125 return ioutil.WriteFile(file, []byte(message), 0644) 126 }, 127 } 128 129 title, body, err := editor.EditTitleAndBody() 130 assert.Equal(t, nil, err) 131 assert.Equal(t, "A title A title continues", title) 132 assert.Equal(t, "A body\nA body continues", body) 133 } 134 135 func TestReadTitleAndBody(t *testing.T) { 136 message := `A title 137 A title continues 138 139 A body 140 A body continues 141 # comment 142 ` 143 r := strings.NewReader(message) 144 reader := bufio.NewReader(r) 145 title, body, err := readTitleAndBody(reader, "#") 146 assert.Equal(t, nil, err) 147 assert.Equal(t, "A title A title continues", title) 148 assert.Equal(t, "A body\nA body continues", body) 149 150 message = `# Dat title 151 152 / This line is commented out. 153 154 Dem body. 155 ` 156 r = strings.NewReader(message) 157 reader = bufio.NewReader(r) 158 title, body, err = readTitleAndBody(reader, "/") 159 assert.Equal(t, nil, err) 160 assert.Equal(t, "# Dat title", title) 161 assert.Equal(t, "Dem body.", body) 162 } 163 164 func TestGetMessageFile(t *testing.T) { 165 gitPullReqMsgFile, _ := getMessageFile("PULLREQ") 166 assert.T(t, strings.Contains(gitPullReqMsgFile, "PULLREQ_EDITMSG")) 167 }