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, "", true) //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) } } }
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 { data.ErrorLog("%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 { data.ErrorLog("%s\n", err) return err } // verboseLog("\t walked to %s\n", file) data.AddFile(file, path) return nil }) if err != nil { data.ErrorLog("%s\n", err) os.Exit(-1) } count := 0 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) count++ if count%100 == 0 { fmt.Printf("\topened %d files so far\n", count) } reader, err := linereader.OpenReader(details.FullPath) if err != nil { data.ErrorLog("%s\n", err) data.AllFiles[file].FormatErrorCount++ } err = checkers.CheckHugoFrontmatter(reader, file) if err != nil { data.ErrorLog("(%s) frontmatter: %s\n", file, err) } if draft, ok := data.AllFiles[file].Meta["draft"]; ok || draft == "true" { data.VerboseLog("Draft=%s: SKIPPING %s link check.\n", draft, file) } else { //fmt.Printf("Draft=%s: %s link check.\n", draft, file) err = checkers.CheckMarkdownLinks(reader, file) if err != nil { // this only errors if there is a fatal issue data.ErrorLog("(%s) links: %s\n", file, err) data.AllFiles[file].FormatErrorCount++ } } reader.Close() } fmt.Printf("Starting to test links (Filter = %s)\n", filter) checkers.TestLinks(filter) // 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) }