Skip to content
quarry Artifact [ca12473ece]
Login

Artifact [ca12473ece]

Artifact ca12473ecefee3ac09d7413e36e6aa02ce16586e3ceba320b91614d3efc5c6cb:


package api

import (
	"strings"
	"testing"
)

// Markup is chosen by extension. The regression this pins is real:
// libspatialite ships an autotools plain-text README whose shell
// commands carry the root prompt `#`, and rendering it as markdown read
// every one of them as a heading — "# sudo make install" became an <h2>,
// and the build instructions came apart.
func TestMimetypeOfChoosesByExtension(t *testing.T) {
	for name, want := range map[string]string{
		"README.md":       "text/x-markdown",
		"readme.markdown": "text/x-markdown",
		"README.wiki":     "text/x-fossil-wiki",
		"diagram.pikchr":  "text/x-pikchr",
		"README.txt":      "text/plain",
		// The one that mattered: no extension is NOT markdown.
		"README":          "text/plain",
		"readme":          "text/plain",
		"README.coverage": "text/plain",
		"INSTALL":         "text/plain",
	} {
		if got := mimetypeOf(name); got != want {
			t.Errorf("mimetypeOf(%q) = %q, want %q", name, got, want)
		}
	}
}

// renderBody must send plain text through escaped and unmodified, the
// way fossil's wiki_render_by_mimetype falls through to
// <pre class='textPlain'>. Hard line breaks are the whole content of a
// command listing; markdown discards them.
func TestPlainReadmeKeepsItsLineStructure(t *testing.T) {
	// Real libspatialite README content.
	src := []byte("3.1: Building on Linux\n----------------------\n\n" +
		"# cd libspatialite-3.1.0\n# ./configure\n# make\n")

	got, err := renderBody(nil, "libspatialite", mimetypeOf("README"), src)
	if err != nil {
		t.Fatal(err)
	}
	if !strings.HasPrefix(got, "<pre>") {
		t.Fatalf("plain README not rendered as preformatted text:\n%s", got)
	}
	if strings.Contains(got, "<h1") || strings.Contains(got, "<h2") {
		t.Errorf("shell prompts became headings again:\n%s", got)
	}
	// Every command must survive on its own line.
	for _, cmd := range []string{"# cd libspatialite-3.1.0", "# ./configure", "# make"} {
		if !strings.Contains(got, cmd) {
			t.Errorf("lost %q from the listing:\n%s", cmd, got)
		}
	}
}

// A real markdown README must still render as markdown — the fix must
// not have traded one wrong default for another. Every other repository
// in the corpus ships README.md.
func TestMarkdownReadmeStillRenders(t *testing.T) {
	got, err := renderBody(nil, "pikchr", mimetypeOf("README.md"),
		[]byte("# Pikchr\n\nA **diagram** language.\n"))
	if err != nil {
		t.Fatal(err)
	}
	// h2, not h1: markup.Render demotes headings one level so the page
	// keeps exactly one h1 (its own title).
	if !strings.Contains(got, "<h2") {
		t.Errorf("markdown heading not rendered:\n%s", got)
	}
	if !strings.Contains(got, "<strong>") {
		t.Errorf("markdown emphasis not rendered:\n%s", got)
	}
}

func TestIsReadmeMatchesFossilsSet(t *testing.T) {
	for _, yes := range []string{
		"README", "readme", "ReadMe.md", "README.markdown", "readme.wiki", "README.txt",
	} {
		if !isReadme(yes) {
			t.Errorf("isReadme(%q) = false", yes)
		}
	}
	for _, no := range []string{"README.coverage", "readme.old", "notreadme.md", "src"} {
		if isReadme(no) {
			t.Errorf("isReadme(%q) = true", no)
		}
	}
}