github.com/yourbase/yb@v0.7.1/cmd/yb/run_test.go (about) 1 // Copyright 2021 YourBase Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // SPDX-License-Identifier: Apache-2.0 16 17 package main 18 19 import ( 20 "context" 21 "io" 22 "io/ioutil" 23 "os" 24 "path/filepath" 25 "strings" 26 "testing" 27 28 "zombiezen.com/go/log/testlog" 29 ) 30 31 func TestRunCommand(t *testing.T) { 32 cfg, err := ioutil.ReadFile(filepath.Join("testdata", "TestRunCmd", "config.yml")) 33 if err != nil { 34 t.Fatal(err) 35 } 36 37 cdTempDir(t) 38 if err := ioutil.WriteFile(".yourbase.yml", cfg, 0666); err != nil { 39 t.Fatal(err) 40 } 41 outFile, err := os.Create("out.txt") 42 if err != nil { 43 t.Fatal(err) 44 } 45 defer outFile.Close() 46 oldStdout := os.Stdout 47 os.Stdout = outFile 48 t.Cleanup(func() { os.Stdout = oldStdout }) 49 50 ctx := testlog.WithTB(context.Background(), t) 51 c := newRunCmd() 52 const want = "foo\n" 53 c.SetArgs([]string{"echo", strings.TrimSuffix(want, "\n")}) 54 if err := c.ExecuteContext(ctx); err != nil { 55 t.Error("yb run:", err) 56 } 57 if _, err := outFile.Seek(0, io.SeekStart); err != nil { 58 t.Fatal(err) 59 } 60 got, err := ioutil.ReadAll(outFile) 61 if err != nil { 62 t.Fatal(err) 63 } 64 if string(got) != want { 65 t.Errorf("stdout = %q; want %q", got, want) 66 } 67 }