mirror of
https://github.com/spf13/cobra
synced 2024-11-17 19:27:09 +00:00
5755ecf102
* update Example in README.md (#769) * specify the color as the required arg (#777) * command: fix typo in docstring of InheritedFlags (#779) * add istio to the list of projects built with Cobra (#786) * remove redundant 'else' (#806) * add mattermost-server as a project built with Cobra (#824) * update README.md (#826) Fix the comment: consistent with others * add uber/prototool as a project built with Cobra (#831) * fix(ci): use go vet, update to Go 1.12, update shellcheck to v0.4.6 (#832) * add go.mod and go.sum (#833) * chore(travis): move 'diff' job to separate stage in Travis (#839) * chore(travis): use language configuration list instead of explicit entries in matrix.include (#839) * chore(travis): update shellcheck-docker to v0.6.0 (#839) * update(README.md): separate projects by commas, instead of using a list * chore: update viper to v1.3.2 and go-md2man to v1.0.10 * fix: convert CRLF to LF when comparing files * use kyoh86/richgo to provide colored test outputs
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os/exec"
|
|
)
|
|
|
|
var update = flag.Bool("update", false, "update .golden files")
|
|
|
|
func init() {
|
|
// Mute commands.
|
|
addCmd.SetOutput(new(bytes.Buffer))
|
|
initCmd.SetOutput(new(bytes.Buffer))
|
|
}
|
|
|
|
// ensureLF converts any \r\n to \n
|
|
func ensureLF(content []byte) []byte {
|
|
return bytes.Replace(content, []byte("\r\n"), []byte("\n"), -1)
|
|
}
|
|
|
|
// compareFiles compares the content of files with pathA and pathB.
|
|
// If contents are equal, it returns nil.
|
|
// If not, it returns which files are not equal
|
|
// and diff (if system has diff command) between these files.
|
|
func compareFiles(pathA, pathB string) error {
|
|
contentA, err := ioutil.ReadFile(pathA)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
contentB, err := ioutil.ReadFile(pathB)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !bytes.Equal(ensureLF(contentA), ensureLF(contentB)) {
|
|
output := new(bytes.Buffer)
|
|
output.WriteString(fmt.Sprintf("%q and %q are not equal!\n\n", pathA, pathB))
|
|
|
|
diffPath, err := exec.LookPath("diff")
|
|
if err != nil {
|
|
// Don't execute diff if it can't be found.
|
|
return nil
|
|
}
|
|
diffCmd := exec.Command(diffPath, "-u", pathA, pathB)
|
|
diffCmd.Stdout = output
|
|
diffCmd.Stderr = output
|
|
|
|
output.WriteString("$ diff -u " + pathA + " " + pathB + "\n")
|
|
if err := diffCmd.Run(); err != nil {
|
|
output.WriteString("\n" + err.Error())
|
|
}
|
|
return errors.New(output.String())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// checkLackFiles checks if all elements of expected are in got.
|
|
func checkLackFiles(expected, got []string) error {
|
|
lacks := make([]string, 0, len(expected))
|
|
for _, ev := range expected {
|
|
if !stringInStringSlice(ev, got) {
|
|
lacks = append(lacks, ev)
|
|
}
|
|
}
|
|
if len(lacks) > 0 {
|
|
return fmt.Errorf("Lack %v file(s): %v", len(lacks), lacks)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// stringInStringSlice checks if s is an element of slice.
|
|
func stringInStringSlice(s string, slice []string) bool {
|
|
for _, v := range slice {
|
|
if s == v {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|