github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/pkg/test/runner/util.go (about) 1 // Copyright 2021 Google LLC 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 // http://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 package runner 16 17 import ( 18 "bytes" 19 "fmt" 20 "os" 21 "os/exec" 22 "path/filepath" 23 ) 24 25 func runCommand(cmd *exec.Cmd) (string, string, error) { 26 var stdout bytes.Buffer 27 var stderr bytes.Buffer 28 cmd.Stdout = &stdout 29 cmd.Stderr = &stderr 30 err := cmd.Run() 31 return stdout.String(), stderr.String(), err 32 } 33 34 func getCommand(pwd, name string, arg []string) *exec.Cmd { 35 cmd := exec.Command(name, arg...) 36 cmd.Dir = pwd 37 return cmd 38 } 39 40 func copyDir(src, dst string) error { 41 _, _, err := runCommand(getCommand("", "cp", []string{"-r", src, dst})) 42 return err 43 } 44 45 func gitInit(d string) error { 46 stdout, stderr, err := runCommand(getCommand(d, "git", []string{"init"})) 47 if err != nil { 48 return fmt.Errorf("git init error: %w, output: %s, stderr: %s", err, stdout, stderr) 49 } 50 return nil 51 } 52 53 func gitAddAll(d string) error { 54 stdout, stderr, err := runCommand(getCommand(d, "git", []string{"add", "--all"})) 55 if err != nil { 56 return fmt.Errorf("git commit error: %w, output: %s, stderr: %s", err, stdout, stderr) 57 } 58 return nil 59 } 60 61 func gitCommit(d, msg string) error { 62 stdout, stderr, err := runCommand(getCommand(d, "git", []string{"config", "user.name", "none"})) 63 if err != nil { 64 return fmt.Errorf("git config error: %w, output: %s, stderr: %s", err, stdout, stderr) 65 } 66 stdout, stderr, err = runCommand(getCommand(d, "git", []string{"config", "user.email", "none"})) 67 if err != nil { 68 return fmt.Errorf("git config error: %w, output: %s, stderr: %s", err, stdout, stderr) 69 } 70 stdout, stderr, err = runCommand(getCommand(d, "git", []string{"commit", "-m", msg, "--allow-empty"})) 71 if err != nil { 72 return fmt.Errorf("git commit error: %w, output: %s, stderr: %s", err, stdout, stderr) 73 } 74 return nil 75 } 76 77 func gitDiff(d, commit1, commit2 string) (string, error) { 78 stdout, stderr, err := runCommand(getCommand(d, "git", []string{"diff", commit1, commit2})) 79 if err != nil { 80 return "", fmt.Errorf("git diff error: %w, output: %s, stderr: %s", err, stdout, stderr) 81 } 82 return stdout, nil 83 } 84 85 func getCommitHash(d string) (string, error) { 86 stdout, stderr, err := runCommand(getCommand(d, "git", []string{"log", "-n", "1", "--pretty=format:%h"})) 87 if err != nil { 88 return "", fmt.Errorf("git log error: %w, output: %s, stderr: %s", err, stdout, stderr) 89 } 90 return stdout, nil 91 } 92 93 func diffStrings(actual, expected string) (string, error) { 94 tmpDir, err := os.MkdirTemp("", "kpt-e2e-diff-*") 95 if err != nil { 96 return "", fmt.Errorf("failed to create temporary dir: %w", err) 97 } 98 defer os.RemoveAll(tmpDir) 99 actualPath := filepath.Join(tmpDir, "actual") 100 expectedPath := filepath.Join(tmpDir, "expected") 101 if err := os.WriteFile(actualPath, []byte(actual), 0644); err != nil { 102 return "", fmt.Errorf("failed to write file %s", actualPath) 103 } 104 if err := os.WriteFile(expectedPath, []byte(expected), 0644); err != nil { 105 return "", fmt.Errorf("failed to write file %s", expectedPath) 106 } 107 // diff is expected to exit with 1 so we ignore the error here 108 output, _, _ := runCommand(getCommand(tmpDir, "diff", []string{"-u", expectedPath, actualPath})) 109 return output, nil 110 }