1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package diff implements a Diff function that compare two inputs
6 // using the 'diff' tool.
16 // Returns diff of two arrays of bytes in diff tool format.
17 func Diff(prefix string, b1, b2 []byte) ([]byte, error) {
18 f1, err := writeTempFile(prefix, b1)
24 f2, err := writeTempFile(prefix, b2)
31 if runtime.GOOS == "plan9" {
35 data, err := exec.Command(cmd, "-u", f1, f2).CombinedOutput()
37 // diff exits with a non-zero status when the files don't match.
38 // Ignore that failure as long as we get output.
44 func writeTempFile(prefix string, data []byte) (string, error) {
45 file, err := ioutil.TempFile("", prefix)
49 _, err = file.Write(data)
50 if err1 := file.Close(); err == nil {
54 os.Remove(file.Name())
57 return file.Name(), nil