Exemple #1
0
func TestMarkdownLinks(t *testing.T) {
	file := "test/index.md"
	data.AllFiles = make(map[string]*data.FileDetails)
	data.AddFile(file, file)

	htmlFlags := 0
	renderParameters := mmark.HtmlRendererParameters{}
	renderer := &TestRenderer{
		LinkFrom: file,
		//Html:     mmark.HtmlRenderer(htmlFlags, "", "").(*mmark.Html),
		Renderer: mmark.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters),
	}
	out := bytes.NewBuffer(make([]byte, 1024))

	tests := map[string]string{
		"../first.md":            "first.md",
		"second.md":              "test/second.md",
		"./second.md":            "test/second.md",
		"banana/second.md":       "test/banana/second.md",
		"/test/banana/second.md": "test/banana/second.md",
		"twice.md":               "test/twice.md",
		"banana/twice.md":        "test/banana/twice.md",
	}

	for _, path := range tests {
		data.AddFile(path, path)
	}
	for link, _ := range tests {
		renderer.Link(out, []byte(link), []byte("title"), []byte("content"))
	}

	for link, details := range data.AllLinks {
		data.AllLinks[link].Response = testUrl(link)
		//fmt.Printf("\t\t(%d) %d links to %s\n", data.AllLinks[link].Response, details.Count, link)
		fmt.Printf("%s links to %s\n", details.ActualLink[0], link)
		if _, ok := data.AllFiles[link]; !ok {
			t.Errorf("ERROR(%d): not found %s links to %s\n", details.Response, details.ActualLink[0], link)
		}
		if tests[details.ActualLink[0]] != link {
			t.Errorf("ERROR(%d): %s links to %s, should link to %s\n", details.Response, details.ActualLink[0], link, tests[details.ActualLink[0]])
		}
	}
}
func TestFrontmatterFound(t *testing.T) {
	file := "ok"
	data.AllFiles = make(map[string]*data.FileDetails)
	data.AddFile(file, file)

	err := CheckHugoFrontmatter(linereader.ByteReader(OK_TOPIC), file)

	if err != nil {
		t.Errorf("ERROR parsing: %v", err)
	}
}
func TestFrontmatterError(t *testing.T) {
	file := "missing"
	data.AllFiles = make(map[string]*data.FileDetails)
	data.AddFile(file, file)

	err := CheckHugoFrontmatter(linereader.ByteReader(MISSING_COMMENT_END_TOPIC), file)

	if err == nil {
		t.Errorf("Expected error")
	} else {
		if err.Error() != "Did not find expected close metadata comment" {
			t.Errorf("unexpected ERROR parsing: %v", err)
		}
	}
}
Exemple #4
0
func main() {
	flag.Parse()
	args := flag.Args()
	if len(args) < 1 {
		printUsage()
		os.Exit(-1)
	}
	dir := args[0]
	filter := ""
	if len(args) >= 2 {
		filter = args[1]
	}

	data.AllFiles = make(map[string]*data.FileDetails)

	fmt.Println("Finding files")
	err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			fmt.Printf("ERROR: %s\n", err)
			return err
		}
		data.VerboseLog("FOUND: %s\n", path)
		if info.IsDir() {
			return nil
		}
		file, err := filepath.Rel(dir, path)
		if err != nil {
			fmt.Printf("ERROR: %s\n", err)
			return err
		}
		// verboseLog("\t walked to %s\n", file)
		data.AddFile(file, path)
		return nil
	})
	if err != nil {
		fmt.Printf("ERROR: %s\n", err)
		os.Exit(-1)
	}

	for file, details := range data.AllFiles {
		if !strings.HasPrefix(file, filter) {
			data.VerboseLog("FILTERED: %s\n", file)
			continue
		}
		if !strings.HasSuffix(file, ".md") {
			data.VerboseLog("SKIPPING: %s\n", file)
			continue
		}
		fmt.Printf("opening: %s\n", file)

		reader, err := linereader.OpenReader(details.FullPath)
		if err != nil {
			fmt.Printf("ERROR opening: %s\n", err)
			data.AllFiles[file].FormatErrorCount++
		}

		err = checkers.CheckHugoFrontmatter(reader, file)
		if err != nil {
			fmt.Printf("ERROR (%s) frontmatter: %s\n", file, err)
		}

		err = checkers.CheckMarkdownLinks(reader, file)
		if err != nil {
			// this only errors if there is a fatal issue
			fmt.Printf("ERROR (%s) links: %s\n", file, err)
			data.AllFiles[file].FormatErrorCount++
		}
		reader.Close()
	}
	checkers.TestLinks()

	// TODO (JIRA: DOCS-181): Title, unique across products if not, file should include an {identifier}

	summaryFileName := "markdownlint.summary.txt"
	f, err := os.Create(summaryFileName)
	if err == nil {
		fmt.Printf("Also writing summary to %s :\n\n", summaryFileName)
		defer f.Close()
	}

	if filter != "" {
		Printf(f, "# Filtered (%s) Summary:\n\n", filter)
	} else {
		Printf(f, "# Summary:\n\n")
	}
	errorCount, errorString := checkers.FrontSummary(filter)
	Printf(f, errorString)
	count, errorString := checkers.LinkSummary(filter)
	errorCount += count
	Printf(f, errorString)
	Printf(f, "\n\tFound: %d files\n", len(data.AllFiles))
	Printf(f, "\tFound: %d errors\n", errorCount)
	// return the number of 404's to show that there are things to be fixed
	os.Exit(errorCount)
}