Skip to content

Commit

Permalink
Add HTMLConverter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed Apr 28, 2020
1 parent 045d35a commit 238a93e
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 10 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Test

on:
- push
- pull_request

defaults:
run:
working-directory: ./HTMLConverter/

jobs:
test:
name: Test
runs-on: macos-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.14

- name: Run tests
run: go test
2 changes: 2 additions & 0 deletions HTMLConverter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/alecthomas/chroma v0.7.2
github.com/microcosm-cc/bluemonday v1.0.2
github.com/samuelmeuli/nbtohtml v0.4.0
github.com/stretchr/testify v1.5.1
github.com/tdewolff/minify/v2 v2.7.4
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
gopkg.in/russross/blackfriday.v2 v2.0.1
)
Expand Down
12 changes: 12 additions & 0 deletions HTMLConverter/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897 h1:p9Sln00KOTlrYkx
github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
github.com/buildkite/terminal-to-html v3.2.0+incompatible h1:WdXzl7ZmYzCAz4pElZosPaUlRTW+qwVx/SkQSCa1jXs=
github.com/buildkite/terminal-to-html v3.2.0+incompatible/go.mod h1:BFFdFecOxCgjdcarqI+8izs6v85CU/1RA/4Bqh4GR7E=
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -24,6 +25,9 @@ github.com/dlclark/regexp2 v1.1.6 h1:CqB4MjHw0MFCDj+PHHjiESmHX+N7t0tJzKvC6M97BRg
github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk=
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
Expand All @@ -46,18 +50,26 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm
github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/tdewolff/minify/v2 v2.7.4 h1:r0OZQ3QzWeDS5cXq53Bk4IFIBDZ7fiXIkw1a4bHONsw=
github.com/tdewolff/minify/v2 v2.7.4/go.mod h1:BkDSm8aMMT0ALGmpt7j3Ra7nLUgZL0qhyrAHXwxcy5w=
github.com/tdewolff/parse/v2 v2.4.2 h1:Bu2Qv6wepkc+Ou7iB/qHjAhEImlAP5vedzlQRUdj3BI=
github.com/tdewolff/parse/v2 v2.4.2/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho=
github.com/tdewolff/test v1.0.6 h1:76mzYJQ83Op284kMT+63iCNCI7NEERsIN8dLM+RiKr4=
github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20200319234117-63522dbf7eec h1:w0SItUiQ4sBiXBAwWNkyu8Fu2Qpn/dtDIcoPkPDqjRw=
golang.org/x/net v0.0.0-20200319234117-63522dbf7eec/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20180715085529-ac767d655b30/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181031143558-9b800f95dbbc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
Binary file modified HTMLConverter/htmlconverter.a
Binary file not shown.
31 changes: 21 additions & 10 deletions HTMLConverter/htmlconverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ import (
"gopkg.in/russross/blackfriday.v2"
)

// Functions for conversion between C and Go strings. Required here because cgo cannot be used in
// tests.

func convertToCString(goString string) *C.char {
return C.CString(goString)
}

func convertToGoString(cString *C.char) string {
return C.GoString(cString)
}

// Convention: Because all functions return C strings, errors are implemented as return values which
// start with "error: ".

//export convertCodeToHTML
func convertCodeToHTML(source *C.char, lexer *C.char) *C.char {
sourceString := C.GoString(source)
lexerString := C.GoString(lexer)
sourceString := convertToGoString(source)
lexerString := convertToGoString(lexer)
htmlBuffer := new(bytes.Buffer)

// Set up lexer for programming language
Expand All @@ -42,23 +53,23 @@ func convertCodeToHTML(source *C.char, lexer *C.char) *C.char {
iterator, err := l.Tokenise(nil, sourceString)
if err != nil {
errMessage := fmt.Sprintf("error: Could not render source code (tokenization error): %d", err)
return C.CString(errMessage)
return convertToCString(errMessage)
}

err = formatter.Format(htmlBuffer, styles.GitHub, iterator)
if err != nil {
errMessage := fmt.Sprintf("error: Could not render source code (formatting error): %d", err)
return C.CString(errMessage)
return convertToCString(errMessage)
}

// Chroma escapes tags, so HTML should be safe from code injection
htmlString := htmlBuffer.String()
return C.CString(htmlString)
return convertToCString(htmlString)
}

//export convertMarkdownToHTML
func convertMarkdownToHTML(source *C.char) *C.char {
sourceString := C.GoString(source)
sourceString := convertToGoString(source)

// Sanitize output but keep classes (required for syntax highlighting)
policy := bluemonday.UGCPolicy()
Expand All @@ -73,21 +84,21 @@ func convertMarkdownToHTML(source *C.char) *C.char {
html := blackfriday.Run([]byte(sourceString), blackfriday.WithRenderer(renderer))
htmlString := string(html)
htmlStringSanitized := policy.Sanitize(htmlString)
return C.CString(htmlStringSanitized)
return convertToCString(htmlStringSanitized)
}

//export convertNotebookToHTML
func convertNotebookToHTML(source *C.char) *C.char {
sourceString := C.GoString(source)
sourceString := convertToGoString(source)

html := new(bytes.Buffer)
err := nbtohtml.ConvertString(html, sourceString)
if err != nil {
errMessage := fmt.Sprintf("error: Could not convert Notebook to HTML: %d", err)
return C.CString(errMessage)
return convertToCString(errMessage)
}
htmlString := html.String()
return C.CString(htmlString)
return convertToCString(htmlString)
}

// Main function is required for `c-archive` builds.
Expand Down
58 changes: 58 additions & 0 deletions HTMLConverter/htmlconverter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/html"
"strings"
"testing"
)

var minifier *minify.M

func minifyHTML(htmlString string) string {
// Initialize minifier if necessary
if minifier == nil {
minifier = minify.New()
minifier.Add("text/html", &html.Minifier{KeepEndTags: true, KeepQuotes: true})
}

minified, err := minifier.String("text/html", htmlString)
if err != nil {
panic(fmt.Sprintf("Could not minify HTML: %d", err))
}
return minified
}

func TestConvertCodeToHTML(t *testing.T) {
source := `const print = (text) => console.log(text);
print("Hello world");`
actual := convertToGoString(convertCodeToHTML(convertToCString(source), convertToCString("js")))
actualTrimmed := strings.TrimSpace(actual)
assert.True(t, strings.HasPrefix(actualTrimmed, `<pre class="chroma">`))
assert.True(t, strings.HasSuffix(actualTrimmed, `</pre>`))
}

func TestConvertMarkdownToHTML(t *testing.T) {
source := `# Heading
Text`
expected := "<h1>Heading</h1><p>Text</p>"
actual := convertToGoString(convertMarkdownToHTML(convertToCString(source)))
assert.Equal(t, expected, minifyHTML(actual))
}

func TestConvertNotebookToHTML(t *testing.T) {
source := `{"cells":[{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Hello world\n"]}],"source":["print(\"Hello world\")"]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.8.2"}},"nbformat":4,"nbformat_minor":4}`
actual := convertToGoString(convertNotebookToHTML(convertToCString(source)))
actualTrimmed := strings.TrimSpace(actual)
assert.True(t, strings.HasPrefix(actualTrimmed, `<div class="notebook">`))
assert.True(t, strings.HasSuffix(actualTrimmed, `</div>`))
}

func TestConvertNotebookToHTMLInvalid(t *testing.T) {
source := "This is not a valid JSON file."
actual := convertToGoString(convertNotebookToHTML(convertToCString(source)))
assert.True(t, strings.HasPrefix(actual, "error: "))
}

0 comments on commit 238a93e

Please sign in to comment.