From 6df820ba02fb0ca4bb52d05e6158bfd9bdef2ed2 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Thu, 23 Nov 2023 09:07:11 +0100 Subject: [PATCH] add some tests --- .github/workflows/golang.yml | 19 ++++++++++++++++++ main.go | 7 ++++++- main_test.go | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/golang.yml create mode 100644 main_test.go diff --git a/.github/workflows/golang.yml b/.github/workflows/golang.yml new file mode 100644 index 0000000..78b89e0 --- /dev/null +++ b/.github/workflows/golang.yml @@ -0,0 +1,19 @@ +name: Go +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '1.21.x' + - name: Install dependencies + run: go get . + - name: Build + run: go build -v ./... + - name: Test with the Go CLI + run: go test diff --git a/main.go b/main.go index 0309fdf..c5d9496 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ var md = goldmark.New( ), ) -var templates = template.Must(template.ParseFS(os.DirFS("templates/"), "*.html")) +var templates *template.Template //go:embed sitemap.xsl var sitemapXSL []byte @@ -427,6 +427,11 @@ func main() { var err error timeStart := time.Now() + templates, err = template.ParseFS(os.DirFS("templates/"), "*.html") + if err != nil { + log.Fatal("Error reading templates/ directory: %s", err) + } + site := Site{} // read config.xml diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..90458b5 --- /dev/null +++ b/main_test.go @@ -0,0 +1,37 @@ +package main + +import ( + "strings" + "testing" +) + +func TestParseFrontMatter(t *testing.T) { + p := &Page{ + Path: "example/content/index.md", + } + + if err := parseFrontMatter(p); err != nil { + t.Fatal(err) + } + + expectedTitle := "My site" + if p.Title != expectedTitle { + t.Errorf("Invalid title. Expected %v, got %v", expectedTitle, p.Title) + } +} + +func TestParseContent(t *testing.T) { + p := &Page{ + Path: "example/content/index.md", + } + + content, err := p.ParseContent() + if err != nil { + t.Fatal(err) + } + + if !strings.Contains(content, "

Hey, welcome on my site!

") { + t.Errorf("Invalid content. Got %v", content) + } + +}