コード例 #1
0
func parseAndTestFile(t *testing.T, filename string) {
	parser.FixFloatingComments(filename)

	d, err := parser.ParseFile(filename, "./")
	if err != nil {
		t.Error(err)
		os.Exit(1)
	} else {

		header := parser.ReadFileHeader(filename)

		formattedFile := d.Fmt(filename)
		formattedFile = strings.TrimSpace(formattedFile)
		if len(header) != 0 {
			formattedFile = header + formattedFile
		}

		// Test if formatted file can be parsed
		fo, err := os.Create("tempOutput.proto")
		if err != nil {
			t.Error(err)
		}
		defer os.Remove("tempOutput.proto")
		if len(formattedFile) > 0 {
			fo.WriteString(formattedFile)
		}
		fo.Close()

		_, err2 := parser.ParseFile("tempOutput.proto", "./", "../../../")
		if err2 != nil {
			t.Error(err2)
		}

		// Test if formatted string is equal to the Gold standard
		goldString, err := ioutil.ReadFile(strings.Split(filename, ".")[0] + "_Gold." + strings.Split(filename, ".")[1])
		if err != nil {
			t.Error(err)
		}

		if parser.Strcmp(formattedFile, strings.TrimSpace(string(goldString))) != 0 {
			t.Error("Failed the gold standard with: " + fmt.Sprintf("%v", parser.Strcmp(formattedFile, strings.TrimSpace(string(goldString)))))
		}

		return
	}
}
コード例 #2
0
func fmtFn() filepath.WalkFunc {

	return func(pathThusFar string, f os.FileInfo, err error) error {

		if strings.HasSuffix(filepath.Dir(pathThusFar), f.Name()) {
			return nil
		}

		if f.IsDir() && !strings.HasSuffix(pathThusFar, string(os.PathSeparator)) {
			pathThusFar += string(os.PathSeparator)
		}
		if f.IsDir() {
			if *recurs && !stringInSlice(pathThusFar, excluded) {
				return nil
			} else {
				return filepath.SkipDir
			}
		} else if f.Mode().IsRegular() && strings.HasSuffix(f.Name(), ".proto") {
			parser.FixFloatingComments(pathThusFar)

			d, err := parser.ParseFile(pathThusFar, filepath.Dir(pathThusFar), *imp_path)
			if err != nil {
				fmt.Println("Parsing error in " + pathThusFar + "!")
				return err
			} else {
				header := parser.ReadFileHeader(pathThusFar)
				formattedFile := d.Fmt(f.Name())
				formattedFile = strings.TrimSpace(formattedFile)
				if len(header) != 0 {
					formattedFile = header + "\n" + formattedFile
				}

				fo, _ := os.Create(pathThusFar)

				fo.WriteString(formattedFile)
				fo.Close()

				fmt.Println("Successfully Formatted " + pathThusFar)
				return nil
			}
		} else {
			return errors.New(f.Name() + " cannot be processed.")
		}
	}

}