github.com/yourbase/yb@v0.7.1/cmd/yb/build_test.go (about) 1 // Copyright 2020 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/ioutil" 22 "os" 23 "path/filepath" 24 "testing" 25 26 "zombiezen.com/go/log/testlog" 27 ) 28 29 func TestBuildCmd(t *testing.T) { 30 t.Run("Success", func(t *testing.T) { 31 cfg, err := ioutil.ReadFile(filepath.Join("testdata", "TestBuildCmd", "success.yml")) 32 if err != nil { 33 t.Fatal(err) 34 } 35 36 cdTempDir(t) 37 if err := ioutil.WriteFile(".yourbase.yml", cfg, 0666); err != nil { 38 t.Fatal(err) 39 } 40 41 ctx := testlog.WithTB(context.Background(), t) 42 c := newBuildCmd() 43 c.SetArgs([]string{"--no-container"}) 44 if err := c.ExecuteContext(ctx); err != nil { 45 t.Error("yb build:", err) 46 } 47 }) 48 49 t.Run("Failure", func(t *testing.T) { 50 cfg, err := ioutil.ReadFile(filepath.Join("testdata", "TestBuildCmd", "failure.yml")) 51 if err != nil { 52 t.Fatal(err) 53 } 54 55 cdTempDir(t) 56 if err := ioutil.WriteFile(".yourbase.yml", cfg, 0666); err != nil { 57 t.Fatal(err) 58 } 59 60 ctx := testlog.WithTB(context.Background(), t) 61 c := newBuildCmd() 62 c.SetArgs([]string{"--no-container"}) 63 err = c.ExecuteContext(ctx) 64 if err == nil { 65 t.Fatal("yb build succeeded") 66 } 67 t.Log("yb build:", err) 68 }) 69 } 70 71 func cdTempDir(t *testing.T) { 72 t.Helper() 73 oldWD, err := os.Getwd() 74 if err != nil { 75 t.Fatal(err) 76 } 77 if err := os.Chdir(t.TempDir()); err != nil { 78 t.Fatal(err) 79 } 80 t.Cleanup(func() { 81 os.Chdir(oldWD) 82 }) 83 } 84 85 func TestMain(m *testing.M) { 86 testlog.Main(nil) 87 os.Exit(m.Run()) 88 }