Beispiel #1
0
func _assert(t *testing.T, cases Cases) {
	for _, c := range cases {
		if !reflect.DeepEqual(c.got, c.want) {
			if s1, ok := c.got.(string); ok {
				if s2, ok := c.want.(string); ok {
					diff := difflib.Diff(strings.Split(s1, "\n"), strings.Split(s2, "\n"))
					diffs := []string{}

					for l, d := range diff {
						switch d.Delta {
						case difflib.LeftOnly:
							diffs = append(diffs, fmt.Sprintf("%04d - %s", l, d.Payload))
						case difflib.RightOnly:
							diffs = append(diffs, fmt.Sprintf("%04d + %s", l, d.Payload))
						}
					}

					t.Errorf("Unexpected result:\n%s", strings.Join(diffs, "\n"))
					return
				}
			}

			t.Errorf("%q\n%q\n", c.got, c.want)
		}
	}
}
Beispiel #2
0
func Diff(t *testing.T, name, s1, s2 string) {
	diff1 := strings.Split(strings.TrimSpace(s1), "\n")
	diff2 := strings.Split(strings.TrimSpace(s2), "\n")

	diff := difflib.Diff(diff1, diff2)
	diffs := []string{}

	// bigger than max
	prev := 1000000

	for l, d := range diff {
		switch d.Delta {
		case difflib.LeftOnly:
			if (l - prev) > 1 {
				diffs = append(diffs, "")
			}
			diffs = append(diffs, fmt.Sprintf("%04d - %s", l, d.Payload))
			prev = l
		case difflib.RightOnly:
			if (l - prev) > 1 {
				diffs = append(diffs, "")
			}
			diffs = append(diffs, fmt.Sprintf("%04d + %s", l, d.Payload))
			prev = l
		}
	}

	if len(diffs) > 0 {
		t.Errorf("Unexpected results for %s:\n%s", name, strings.Join(diffs, "\n"))
	}
}
Beispiel #3
0
func assertFixture(t *testing.T, name string) {
	orig := manifest.ManifestRandomPorts
	manifest.ManifestRandomPorts = false
	defer func() { manifest.ManifestRandomPorts = orig }()

	app := models.App{
		Name: "httpd",
		Tags: map[string]string{
			"Name":   "httpd",
			"Type":   "app",
			"System": "convox",
			"Rack":   "convox-test",
		},
	}

	data, err := ioutil.ReadFile(fmt.Sprintf("fixtures/%s.yml", name))
	require.Nil(t, err)

	manifest, err := manifest.Load(data)
	require.Nil(t, err)

	formation, err := app.Formation(*manifest)
	require.Nil(t, err)

	pretty, err := models.PrettyJSON(formation)
	require.Nil(t, err)

	data, err = ioutil.ReadFile(fmt.Sprintf("fixtures/%s.json", name))
	require.Nil(t, err)

	diff1 := strings.Split(strings.TrimSpace(string(data)), "\n")
	diff2 := strings.Split(strings.TrimSpace(pretty), "\n")

	diff := difflib.Diff(diff1, diff2)
	diffs := []string{}

	// bigger than max
	prev := 1000000

	for l, d := range diff {
		switch d.Delta {
		case difflib.LeftOnly:
			if (l - prev) > 1 {
				diffs = append(diffs, "")
			}
			diffs = append(diffs, fmt.Sprintf("%04d - %s", l, d.Payload))
			prev = l
		case difflib.RightOnly:
			if (l - prev) > 1 {
				diffs = append(diffs, "")
			}
			diffs = append(diffs, fmt.Sprintf("%04d + %s", l, d.Payload))
			prev = l
		}
	}

	if len(diffs) > 0 {
		t.Errorf("Unexpected results for %s:\n%s", name, strings.Join(diffs, "\n"))
	}
}
Beispiel #4
0
func Cmp(feed1, feed2 MDQ) {
	ents1, _ := feed1.getEntityList()
	ents2, _ := feed2.getEntityList()
	//    len1 := len(ents1)
	len2 := len(ents2)
	seen := 0
	for id, _ := range ents1 {
		x1, _ := feed1.MDQ(id)
		x2, _ := feed2.MDQ(id)
		str1 := x1.Pp()
		str2 := x2.Pp()
		hash1 := hex.EncodeToString(gosaml.Hash(crypto.SHA1, str1))
		hash2 := hex.EncodeToString(gosaml.Hash(crypto.SHA1, str2))
		if hash1 == hash2 {
			fmt.Println("OK ", id)
		} else {
			fmt.Println("NOT", id)
			diffs := difflib.Diff(strings.Split(str1, "\n"), strings.Split(str2, "\n"))
			for _, diff := range diffs {
				if diff.Delta != difflib.Common {
					fmt.Println(diff)
				}
			}
			fmt.Println()
		}
		seen++
	}
	fmt.Println("Number of entityies: ", seen, len2)
}
Beispiel #5
0
func compare(oldVersion []string, newVersion []string) string {
	diff := difflib.Diff(oldVersion, newVersion)
	output := ""
	for _, v := range diff {
		if v.Delta == difflib.Common {
			continue
		}
		output += "\n" + v.String()
	}
	return output
}
Beispiel #6
0
func applyPatch(diff patch.TextDiff, prev string) (string, error) {
	curr, err := diff.Apply([]byte(prev))
	if err != nil {
		return "", err
	}

	chunks := difflib.Diff(strings.Split(prev, "\n"), strings.Split(string(curr), "\n"))
	ret := ""

	for _, ch := range chunks {
		ret += ch.String() + "\n"
	}

	return ret, nil
}
Beispiel #7
0
func renderDiff(to io.Writer, a, b string) {
	diffs := difflib.Diff(strings.Split(a, "\n"), strings.Split(b, "\n"))

	for _, diff := range diffs {
		text := diff.Payload

		switch diff.Delta {
		case difflib.RightOnly:
			fmt.Fprintf(to, "%s\n", ansi.Color(text, "green"))
		case difflib.LeftOnly:
			fmt.Fprintf(to, "%s\n", ansi.Color(text, "red"))
		case difflib.Common:
			fmt.Fprintf(to, "%s\n", text)
		}
	}
}
//diff returns a slice of added users and a slice of deleted users; this is lame in that if a user's name, location, or description changes, it doesn't currently identify it as "changed" but just as both added and deleted
func diff() ([]string, []string) {
	prev, _ := ioutil.ReadFile(PREV)
	current, _ := ioutil.ReadFile(CURRENT)

	rawdiff := difflib.Diff(strings.Split(string(current), "\n"), strings.Split(string(prev), "\n"))

	var added []string
	var deleted []string
	for _, record := range rawdiff {
		if record.Delta == difflib.LeftOnly {
			added = append(added, record.Payload)
		}
		if record.Delta == difflib.RightOnly {
			deleted = append(deleted, record.Payload)
		}
	}
	return added, deleted
}
Beispiel #9
0
func statsFromDiff(diff patch.TextDiff) *DiffStats {
	stats := DiffStats{Chunks: diff}

	for _, chunk := range diff {
		atoms := difflib.Diff(
			strings.Split(string(chunk.Old), "\n"),
			strings.Split(string(chunk.New), "\n"),
		)
		for _, a := range atoms {
			if a.Delta == difflib.LeftOnly {
				stats.Deletions++
			} else if a.Delta == difflib.RightOnly {
				stats.Additions++
			}
		}
	}

	return &stats
}
Beispiel #10
0
func TestCimDeclaration(t *testing.T) {
	bs, e := xml.MarshalIndent(declaration, "", "  ")
	if nil != e {
		t.Error(e)
		return
	} else {
		t.Log(string(bs))
	}

	var declaration2 CimDeclaration
	if e := xml.Unmarshal(bs, &declaration2); nil != e {
		t.Error(e)
		return
	}

	if !reflect.DeepEqual(declaration, declaration2) {

		// t.Errorf("excepted is %#v", declaration)
		// t.Errorf("actual is %#v", declaration2)

		bs2, e := xml.MarshalIndent(declaration2, "", "  ")
		if nil != e {

			t.Errorf("excepted is %#v", declaration)
			t.Errorf("actual is %#v", declaration2)
			t.Error(e)
			return
		} else {

			if string(bs) != string(bs2) {

				//t.Log(string(bs))

				results := difflib.Diff(strings.Split(string(bs), "\n"), strings.Split(string(bs2), "\n"))
				if 0 != len(results) {
					for _, rec := range results {
						t.Error(rec.String())
					}
				}
			}
		}
	}
}
Beispiel #11
0
func TestCimClass(t *testing.T) {
	bs, e := xml.MarshalIndent(class, "", "  ")
	if nil != e {
		t.Error(e)
		return
	} else {
		t.Log(string(bs))
	}

	var cls2 CimClass
	if e := xml.Unmarshal(bs, &cls2); nil != e {
		t.Error(e)
		return
	}

	if !reflect.DeepEqual(class, cls2) {

		bs2, e := xml.MarshalIndent(cls2, "", "  ")
		if nil != e {

			t.Errorf("excepted is %#v", class)
			t.Errorf("actual is %#v", cls2)
			t.Error(e)
			return
		} else {

			if string(bs) != string(bs2) {

				t.Errorf("excepted is %#v", class)
				t.Errorf("actual is %#v", cls2)
				//t.Log(string(bs))

				results := difflib.Diff(strings.Split(string(bs), "\n"), strings.Split(string(bs2), "\n"))
				if 0 != len(results) {
					for _, rec := range results {
						t.Error(rec.String())
					}
				}
			}
		}
	}
}
Beispiel #12
0
func TestMain(t *testing.T) {
	build, _ := filepath.Abs("build")
	filepath.Walk("test", func(path string, info os.FileInfo, err error) error {
		if strings.HasSuffix(path, ".sh") {
			cmd := exec.Command("bash", filepath.Base(path))
			cmd.Dir = filepath.Dir(path)
			cmd.Env = []string{"PATH=" + build}
			stderr := new(bytes.Buffer)
			cmd.Stderr = stderr
			output, err := cmd.Output()
			if err != nil {
				t.Errorf("FAIL: execution failed: " + path + ": " + err.Error() + " " + stderr.String())
			} else {
				outfile := strings.TrimSuffix(path, filepath.Ext(path)) + ".txt"
				expected, err := ioutil.ReadFile(outfile)
				if err != nil {
					t.Errorf("FAIL: error on reading output file: " + outfile)
				} else {
					diffs := difflib.Diff(strings.Split(stderr.String()+string(output), "\n"), strings.Split(string(expected), "\n"))
					help := strings.Contains(string(output), "NAME:")
					differs := false
					for _, diff := range diffs {
						differs = differs || (help && diff.Delta == difflib.RightOnly || !help && diff.Delta != difflib.Common)
					}
					if differs {
						buf := bytes.NewBufferString("")
						for _, diff := range diffs {
							if diff.Delta != difflib.Common {
								buf.WriteString(diff.String() + "\n")
							}
						}
						t.Errorf("FAIL: output differs: " + path + "\n" + buf.String())
					} else {
						t.Logf("PASS: "******"\n")
					}
				}
			}
		}
		return nil
	})
}
Beispiel #13
0
func assertGoCode(t *testing.T, s1, s2, msg string, com ...interface{}) {
	s1 = fmt.Sprintf("%s", s1)
	s2 = fmt.Sprintf("%s", s2)
	tmp1 := strings.Split(s1, "\n")
	tmp2 := strings.Split(s2, "\n")
	if s1 != s2 {
		b := bytes.NewBuffer(nil)
		for _, diff := range difflib.Diff(tmp1, tmp2) {
			p := strings.Replace(diff.Payload, "\t", "\\t", -1)
			switch diff.Delta {
			case difflib.Common:
				fmt.Fprintf(b, "%s %s\n", diff.Delta.String(), p)
			case difflib.LeftOnly:
				fmt.Fprintf(b, "\033[31m%s %s\033[39m\n", diff.Delta.String(), p)
			case difflib.RightOnly:
				fmt.Fprintf(b, "\033[32m%s %s\033[39m\n", diff.Delta.String(), p)
			}
		}
		t.Errorf(b.String())
	}
}
Beispiel #14
0
func TestSimpleRsp(t *testing.T) {
	for _, rsp := range []CimSimpleRsp{simple_rsp1, simple_error_rsp} {
		bs, e := xml.MarshalIndent(rsp, "", "  ")
		if nil != e {
			t.Error(e)
			return
		} else {
			t.Log(string(bs))
		}

		var unmarshal_rsp CimSimpleRsp
		if e := xml.Unmarshal(bs, &unmarshal_rsp); nil != e {
			t.Error(e)
			return
		}

		if !reflect.DeepEqual(rsp, unmarshal_rsp) {

			bs2, e := xml.MarshalIndent(unmarshal_rsp, "", "  ")
			if nil != e {
				t.Error(e)
				return
			} else {

				if string(bs) != string(bs2) {
					t.Errorf("excepted is %#v", rsp)
					t.Errorf("actual is %#v", unmarshal_rsp)
					//t.Log(string(bs))

					results := difflib.Diff(strings.Split(string(bs), "\n"), strings.Split(string(bs2), "\n"))
					if 0 != len(results) {
						for _, rec := range results {
							t.Error(rec.String())
						}
					}
				}
			}
		}
	}
}
Beispiel #15
0
func diff(a, b interface{}) []difflib.DiffRecord {
	return difflib.Diff(
		strings.SplitAfter(spew.Sdump(a), "\n"),
		strings.SplitAfter(spew.Sdump(b), "\n"),
	)
}
Beispiel #16
0
// Make sure that generated code isn't altered by running gofmt.
func testFmt(t *testing.T, srcname string, src io.Reader) {
	var err error

	var fmtin io.WriteCloser
	var fmtout io.ReadCloser
	var validbuf *bytes.Buffer
	var fmtbuf *bytes.Buffer

	var validbytes []byte

	// process (i.e., "govalid" output) will be buffered here.
	validbuf = new(bytes.Buffer)
	// gofmt output will be buffered here.
	fmtbuf = new(bytes.Buffer)

	testProcess(t, "-", validbuf, srcname, src)
	validbytes = validbuf.Bytes()

	// XXX Isn't there a way to gofmt without having to launch a
	// subprocess?
	cmd := exec.Command("gofmt")

	fmtin, err = cmd.StdinPipe()
	if err != nil {
		t.Error(err)
	}
	fmtout, err = cmd.StdoutPipe()
	if err != nil {
		t.Error(err)
	}

	if err := cmd.Start(); err != nil {
		t.Error(err)
	}

	sync := make(chan struct{})
	go func() {
		io.Copy(fmtin, bytes.NewBuffer(validbytes))
		fmtin.Close()
		sync <- struct{}{}
	}()
	go func() {
		io.Copy(fmtbuf, fmtout)
		sync <- struct{}{}
	}()
	<-sync
	<-sync

	if err := cmd.Wait(); err != nil {
		t.Error(err)
	}

	if !bytes.Equal(validbytes, fmtbuf.Bytes()) {
		t.Log("gofmt output differs")
		validlines := strings.Split(string(validbytes), "\n")
		fmtlines := strings.Split(string(fmtbuf.Bytes()), "\n")
		diffrecords := difflib.Diff(validlines, fmtlines)
		t.Log("--- valid output")
		t.Log("+++ gofmt output")
		for _, dr := range diffrecords {
			if dr.Delta == difflib.Common {
				continue
			}
			t.Log(dr)
		}
		t.Fail()
	}
}
Beispiel #17
0
func main() {
	domain := flag.String("domain", "", "mailgun domain")
	privateKey := flag.String("private-key", "", "secret mailgun api key")
	publicKey := flag.String("public-key", "", "mailgun public api key")
	dry := flag.Bool("dry", false, "do not update cache & only print to stdout (no e-mail)")
	verbose := flag.Bool("verbose", false, "Print detailed output per monitored url.")
	flag.Parse()

	if *domain == "" || *privateKey == "" || *publicKey == "" {
		log.Fatalln("domain, private-key and public-key flags are required")
	}

	gun := m.NewMailgun(*domain, *privateKey, *publicKey)

	urls := loadUrls()
	for _, url := range urls {
		body, err := requestURL(url)
		if err != nil {
			log.Println("Skipped URL because of error requesting it:", url)
			continue
		}
		filename := getExecFolder() + "/cache/" + getMD5Hash(url) + ".html"

		cached, err := ioutil.ReadFile(filename)
		if err != nil {
			if *dry == false {
				updateCache(filename, body)
			}

			switch err := err.(type) {
			case *os.PathError:
				fmt.Printf("This URL will now be monitored: %s\n\n", url)
			default:
				log.Fatalf("Fatal errors type %T\n", err)
			}
		} else {
			if reflect.DeepEqual(cached, body) {
				if *verbose {
					fmt.Printf("This URL didn't change: %s\n\n", url)
				}
			} else {
				cachedDoc := getGoqueryDoc(cached)
				currentDoc := getGoqueryDoc(body)
				cachedContent, _ := cachedDoc.Find("#content").Html()
				currentContent, _ := currentDoc.Find("#content").Html()

				if cachedContent == currentContent {
					if *verbose {
						fmt.Println("The website changed, but the content stayed the same.")
					}
				} else {
					cachedText, _ := html2text.FromString(cachedContent)
					currentText, _ := html2text.FromString(currentContent)
					diff := difflib.Diff(strings.Split(cachedText, "\n"), strings.Split(currentText, "\n"))

					msg := createMessage(diff, url)
					htmlMsg := createHTMLMessage(diff, url)
					if *dry {
						fmt.Println(msg)
					} else {
						sendEmails(gun, msg, htmlMsg)
					}
				}
				if *dry == false {
					updateCache(filename, body)
				}
			}
		}
	}
}
Beispiel #18
0
func getDifferentFiles(listA, listB []string) (diff []difflib.DiffRecord) {
	return difflib.Diff(listA, listB)
}