github.com/mongey/nomad@v0.5.2/command/init_test.go (about) 1 package command 2 3 import ( 4 "io/ioutil" 5 "os" 6 "strings" 7 "testing" 8 9 "github.com/mitchellh/cli" 10 ) 11 12 func TestInitCommand_Implements(t *testing.T) { 13 var _ cli.Command = &InitCommand{} 14 } 15 16 func TestInitCommand_Run(t *testing.T) { 17 ui := new(cli.MockUi) 18 cmd := &InitCommand{Meta: Meta{Ui: ui}} 19 20 // Fails on misuse 21 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 22 t.Fatalf("expect exit code 1, got: %d", code) 23 } 24 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 25 t.Fatalf("expect help output, got: %s", out) 26 } 27 ui.ErrorWriter.Reset() 28 29 // Ensure we change the cwd back 30 origDir, err := os.Getwd() 31 if err != nil { 32 t.Fatalf("err: %s", err) 33 } 34 defer os.Chdir(origDir) 35 36 // Create a temp dir and change into it 37 dir, err := ioutil.TempDir("", "nomad") 38 if err != nil { 39 t.Fatalf("err: %s", err) 40 } 41 defer os.RemoveAll(dir) 42 if err := os.Chdir(dir); err != nil { 43 t.Fatalf("err: %s", err) 44 } 45 46 // Works if the file doesn't exist 47 if code := cmd.Run([]string{}); code != 0 { 48 t.Fatalf("expect exit code 0, got: %d", code) 49 } 50 content, err := ioutil.ReadFile(DefaultInitName) 51 if err != nil { 52 t.Fatalf("err: %s", err) 53 } 54 if string(content) != defaultJob { 55 t.Fatalf("unexpected file content\n\n%s", string(content)) 56 } 57 58 // Fails if the file exists 59 if code := cmd.Run([]string{}); code != 1 { 60 t.Fatalf("expect exit code 1, got: %d", code) 61 } 62 if out := ui.ErrorWriter.String(); !strings.Contains(out, "exists") { 63 t.Fatalf("expect file exists error, got: %s", out) 64 } 65 } 66 67 func TestInitCommand_defaultJob(t *testing.T) { 68 // Ensure the job file is always written with spaces instead of tabs. Since 69 // the default job file is embedded in the go file, it's easy for tabs to 70 // slip in. 71 if strings.Contains("\t", defaultJob) { 72 t.Error("default job contains tab character - please convert to spaces") 73 } 74 }