github.com/abolfazlbeh/zhycan@v0.0.0-20230819144214-24cf38237387/cmd/cmd_main/commands/create_command_test.go (about) 1 package commands 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "io" 8 "os" 9 "path/filepath" 10 "testing" 11 ) 12 13 func Test_executeCreateCommand(t *testing.T) { 14 createCmd := NewCreateCommandCmd() 15 16 b := bytes.NewBufferString("") 17 createCmd.SetOut(b) 18 19 projectName := "test_project" 20 dirPath := "/tmp" 21 commandName := "ttt" 22 createCmd.SetArgs([]string{ 23 commandName, 24 }) 25 26 // changing working directory 27 changeErr := os.Chdir(filepath.Join(dirPath, projectName)) 28 if changeErr != nil { 29 t.Errorf("Expected to change directory without err, bot got: %v", changeErr) 30 return 31 } 32 33 createCmd.Execute() 34 out, err := io.ReadAll(b) 35 if err != nil { 36 t.Errorf("Expected to read without err, bot got: %v", err) 37 return 38 } 39 40 expectedStr := InitializeCreateCommandMessage 41 expectedStr += "\n" + fmt.Sprintf(CommandFileCreatedMessage, filepath.Join(dirPath, projectName, "commands", fmt.Sprintf("%s_command.go", commandName))) 42 43 if string(out) != expectedStr { 44 t.Errorf("Expected %v, but got: %v", expectedStr, string(out)) 45 return 46 } 47 48 expectedFilePath := filepath.Join( 49 dirPath, 50 projectName, 51 "commands", 52 fmt.Sprintf("%s_command.go", commandName), 53 ) 54 if _, err := os.Stat(expectedFilePath); errors.Is(err, os.ErrNotExist) { 55 t.Errorf("Filename with the path of `%v` must be existed, but got err: %v", expectedFilePath, err) 56 return 57 } 58 59 // Read the content of the file 60 61 }