github.com/grantbow/fit@v0.7.1-0.20220916164603-1f7c88ac81e6/fitapp/Create_test.go (about) 1 package fitapp 2 3 import ( 4 "fmt" 5 bugs "github.com/grantbow/fit/issues" 6 // "io" 7 "io/ioutil" 8 "os" 9 "testing" 10 ) 11 12 //var dops = bugs.Directory(os.PathSeparator) 13 //var sops = string(os.PathSeparator) 14 15 func runCreateOutput(args argumentList, expected string, t *testing.T) { 16 config := bugs.Config{} 17 stdout, stderr := captureOutput(func() { 18 Create(args, config) 19 }, t) 20 if stdout != expected { 21 t.Error("Unexpected output on STDOUT for fitapp/Create_test") 22 fmt.Printf("Expected: %s\nGot: %s\n", expected, stdout) 23 } 24 if stderr[:7] != "Usage: " { 25 t.Error("Expected usage information with no arguments") 26 } 27 } 28 29 // Captures stdout and stderr to ensure that 30 // a usage line gets printed to Stderr when 31 // no parameters are specified 32 func TestCreateHelpOutput(t *testing.T) { 33 runCreateOutput(argumentList{}, "", t) 34 } 35 36 // Test "Create" without a fit directory 37 func TestCreateWithoutIssues(t *testing.T) { 38 t.Skip("see fitapp/Create_test.go+41 and fitapp/utils.go+96") 39 config := bugs.Config{} 40 config.DescriptionFileName = "Description" 41 config.FitDirName = "fit" 42 dir, err := ioutil.TempDir("", "createtest") 43 if err != nil { 44 t.Error("Could not create temporary dir for test") 45 return 46 } 47 pwd, _ := os.Getwd() 48 os.Chdir(dir) 49 // this test should comment MkdirAll. 50 // Oddly that causes a test halt with "exit status 1". 51 // I tracked this down to fitapp/utils.go +96, os.Stdout = op 52 // Capturing the output of the RUNNING process for testing 53 // is a bit sneaky. I don't see another way to make it work. 54 // Even though I can't run this test as a function it passes. 55 // I added t.Skip above. 56 os.MkdirAll(config.FitDirName, 0700) // the real test 57 defer os.RemoveAll(dir) 58 err = os.Setenv("FIT", dir) 59 if err != nil { 60 t.Error("Could not set environment variable: " + err.Error()) 61 return 62 } 63 64 //fmt.Print("1") 65 //fmt.Print(err) 66 stdout, stderr := captureOutput(func() { 67 Create(argumentList{"-n", "Test", "bug"}, config) 68 }, t) 69 if stderr != "" { 70 t.Error("Unexpected output on STDERR for Test-bug: " + stderr) 71 } 72 if stdout != "Created issue: Test bug\n" { 73 t.Error("Unexpected output on STDOUT for Test-bug: " + stdout) 74 } 75 //fmt.Print("2") 76 issuesDir, err := ioutil.ReadDir(fmt.Sprintf("%s%s%s%s", dir, sops, config.FitDirName, sops)) 77 //fmt.Print("3") 78 if err != nil { 79 t.Error("Could not read " + config.FitDirName + " directory") 80 return 81 } 82 if len(issuesDir) != 1 { 83 t.Error(fmt.Sprintf("Unexpected number of issues in %s dir.\n Expected %d, got %d\n", config.FitDirName, 1, len(issuesDir))) 84 } 85 //fmt.Print("4") 86 os.Chdir(pwd) 87 } 88 89 // Test a very basic invocation of "Create" with the -n 90 // argument. We can't yet try it without -n, since it means 91 // an editor will be spawned. 92 func TestCreateNoEditor(t *testing.T) { 93 t.Skip("windows failure - see fitapp/Create_test.go+92") 94 // TODO: finish making tests on Windows pass then redo this test 95 // first issue was ok. 96 // second issue had trouble with setting and using DefaultDescriptionFile 97 /* 98 === RUN TestCreateNoEditor 99 --- FAIL: TestCreateNoEditor (0.01s) 100 Create_test.go:167: Unexpected output on STDOUT for Test2-bug: open \ddf: The system cannot find th 101 e file specified. 102 Created issue: Test2 bug 103 104 Create_test.go:180: Unexpected number of files found in Test2-bug dir. 105 Expected 2, got 1 106 107 Create_test.go:189: Could not load description file for Test2 bugopen C:\cygwin64\tmp\createtest740 108 771043\fit\Test2-bug\ddf: The system cannot find the file specified. 109 Create_test.go:192: Unexpected empty file for Test2 bug 110 111 */ 112 config := bugs.Config{} 113 config.DescriptionFileName = "Description" 114 config.FitDirName = "fit" 115 dir, err := ioutil.TempDir("", "createtest") 116 if err != nil { 117 t.Error("Could not create temporary dir for test") 118 return 119 } 120 pwd, _ := os.Getwd() 121 os.Chdir(dir) 122 os.MkdirAll(config.FitDirName, 0700) 123 defer os.RemoveAll(dir) 124 // On MacOS, /tmp is a symlink, which causes GetDirectory() to return 125 // a different path than expected in these tests, so make the issues 126 // directory explicit with an environment variable 127 err = os.Setenv("FIT", dir) 128 if err != nil { 129 t.Error("Could not set environment variable: " + err.Error()) 130 return 131 } 132 133 ///// without an issue 134 runCreateOutput(argumentList{"-n"}, "", t) 135 136 ///// first issue 137 stdout, stderr := captureOutput(func() { 138 Create(argumentList{"-n", "Test", "bug"}, config) 139 }, t) 140 if stderr != "" { 141 t.Error("Unexpected output on STDERR for Test-bug") 142 } 143 if stdout != "Created issue: Test bug\n" { 144 t.Error("Unexpected output on STDOUT for Test-bug") 145 } 146 issuesDir, err := ioutil.ReadDir(fmt.Sprintf("%s%s%s%s", dir, sops, config.FitDirName, sops)) 147 if err != nil { 148 t.Error("Could not read " + config.FitDirName + " directory") 149 return 150 } 151 if len(issuesDir) != 1 { 152 t.Error(fmt.Sprintf("Unexpected number of issues in %s dir.\n Expected %d, got %d\n", config.FitDirName, 1, len(issuesDir))) 153 } 154 155 bugDir, err := ioutil.ReadDir(fmt.Sprintf("%s%s%s%sTest-bug", dir, sops, config.FitDirName, sops)) 156 if len(bugDir) != 1 { 157 t.Error(fmt.Sprintf("Unexpected number of files found in %s dir.\n Expected %d, got %d\n", "Test-bug", 1, len(bugDir))) 158 } 159 if err != nil { 160 t.Error("Could not read Test-bug directory") 161 return 162 } 163 164 file, err := ioutil.ReadFile(fmt.Sprintf("%s%s%s%sTest-bug%sDescription", dir, sops, config.FitDirName, sops, sops)) 165 if err != nil { 166 t.Error("Could not load description file for Test bug" + err.Error()) 167 } 168 if len(file) != 0 { 169 t.Error("Expected empty file for Test bug") 170 } 171 172 ///// second issue 173 ////// uses a configured file name 174 config.DefaultDescriptionFile = "ddf" 175 // put this ABOVE issues so len(issuesDir) check later is unaltered 176 ioutil.WriteFile(config.DefaultDescriptionFile, 177 []byte("text used in default description file (ddf) issue template"), 0755) 178 179 stdout, stderr = captureOutput(func() { 180 Create(argumentList{"-n", "--generate-id", "Test2", "bug"}, config) 181 }, t) 182 if stderr != "" { 183 t.Error("Unexpected output on STDERR for Test2-bug") 184 } 185 if stdout != "Created issue: Test2 bug\n" { 186 t.Error("Unexpected output on STDOUT for Test2-bug: " + stdout) 187 } 188 issuesDir, err = ioutil.ReadDir(fmt.Sprintf("%s%s%s%s", dir, sops, config.FitDirName, sops)) 189 if err != nil { 190 t.Error("Could not read " + config.FitDirName + " directory") 191 return 192 } 193 if len(issuesDir) != 2 { 194 t.Error(fmt.Sprintf("Unexpected number of issues in %s dir.\n Expected %d, got %d\n", config.FitDirName, 2, len(issuesDir))) 195 } 196 197 bugDir, err = ioutil.ReadDir(fmt.Sprintf("%s%s%s%sTest2-bug", dir, sops, config.FitDirName, sops)) 198 if len(bugDir) != 2 { 199 t.Error(fmt.Sprintf("Unexpected number of files found in %s dir.\n Expected %d, got %d\n", "Test2-bug", 2, len(bugDir))) 200 } 201 if err != nil { 202 t.Error("Could not read Test2-bug directory") 203 return 204 } 205 206 file, err = ioutil.ReadFile(fmt.Sprintf("%s%s%s%sTest2-bug%s%s", dir, sops, config.FitDirName, sops, sops, config.DefaultDescriptionFile)) 207 if err != nil { 208 t.Error("Could not load description file for Test2 bug" + err.Error()) 209 } 210 if len(file) == 0 { 211 t.Error("Unexpected empty file for Test2 bug") 212 } 213 os.Chdir(pwd) 214 } 215 216 /* currently hangs spawning editor 217 218 // this test will not spawn editor 219 func TestCreateNoIssuesDir(t *testing.T) { 220 dir, err := ioutil.TempDir("", "createtest") 221 if err != nil { 222 t.Error("Could not create temporary dir for test") 223 return 224 } 225 os.Chdir(dir) 226 // This is what we are testing for 227 //os.MkdirAll("issues", 0700) 228 //defer os.RemoveAll(dir) 229 err = os.Setenv("FIT", string(dir)) 230 if err != nil { 231 t.Error("Could not set environment variable: " + err.Error()) 232 return 233 } 234 235 stdout, stderr := captureOutput(func() { 236 Create(argumentList{"Test", "bug"}) // fire editor without -n 237 }, t) 238 _ = stderr 239 _ = stdout 240 // stderr is expected from log.Fatal(err) 241 //if stdout != "" { 242 // t.Error("Unexpected output on STDOUT for Test-bug") 243 //} 244 } 245 */