2024-02-13 17:43:20 +01:00
|
|
|
package site
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-02-21 15:05:09 +01:00
|
|
|
"strings"
|
2024-02-13 17:43:20 +01:00
|
|
|
"testing"
|
2024-02-21 15:05:09 +01:00
|
|
|
|
|
|
|
"github.com/facundoolano/jorge/config"
|
2024-02-13 17:43:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadAndRenderTemplates(t *testing.T) {
|
2024-02-16 16:39:19 +01:00
|
|
|
config := newProject()
|
|
|
|
defer os.RemoveAll(config.RootDir)
|
2024-02-13 17:43:20 +01:00
|
|
|
|
|
|
|
// add two layouts
|
|
|
|
content := `---
|
|
|
|
---
|
|
|
|
<html>
|
|
|
|
<head><title>{{page.title}}</title></head>
|
|
|
|
<body>
|
|
|
|
{{content}}
|
|
|
|
</body>
|
|
|
|
</html>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file := newFile(config.LayoutsDir, "base.html", content)
|
2024-02-13 17:43:20 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
content = `---
|
|
|
|
layout: base
|
|
|
|
---
|
|
|
|
<h1>{{page.title}}</h1>
|
|
|
|
<h2>{{page.subtitle}}</h2>
|
|
|
|
{{content}}`
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.LayoutsDir, "post.html", content)
|
2024-02-13 17:43:20 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
// add two posts
|
|
|
|
content = `---
|
|
|
|
layout: post
|
|
|
|
title: hello world!
|
|
|
|
subtitle: my first post
|
|
|
|
date: 2024-01-01
|
|
|
|
---
|
|
|
|
<p>Hello world!</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "hello.html", content)
|
2024-02-14 01:32:04 +01:00
|
|
|
helloPath := file.Name()
|
|
|
|
defer os.Remove(helloPath)
|
2024-02-13 17:43:20 +01:00
|
|
|
|
|
|
|
content = `---
|
|
|
|
layout: post
|
|
|
|
title: goodbye!
|
|
|
|
subtitle: my last post
|
|
|
|
date: 2024-02-01
|
|
|
|
---
|
|
|
|
<p>goodbye world!</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "goodbye.html", content)
|
2024-02-14 01:32:04 +01:00
|
|
|
goodbyePath := file.Name()
|
|
|
|
defer os.Remove(goodbyePath)
|
2024-02-13 17:43:20 +01:00
|
|
|
|
|
|
|
// add a page (no date)
|
|
|
|
content = `---
|
|
|
|
layout: base
|
|
|
|
title: about
|
|
|
|
---
|
|
|
|
<p>about this site</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "about.html", content)
|
2024-02-14 01:32:04 +01:00
|
|
|
aboutPath := file.Name()
|
|
|
|
defer os.Remove(aboutPath)
|
2024-02-13 17:43:20 +01:00
|
|
|
|
|
|
|
// add a static file (no front matter)
|
|
|
|
content = `go away!`
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "robots.txt", content)
|
2024-02-13 17:43:20 +01:00
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
site, err := Load(*config)
|
2024-02-13 17:43:20 +01:00
|
|
|
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
|
|
|
|
assertEqual(t, len(site.posts), 2)
|
|
|
|
assertEqual(t, len(site.pages), 1)
|
|
|
|
assertEqual(t, len(site.layouts), 2)
|
|
|
|
|
|
|
|
_, ok := site.layouts["base"]
|
|
|
|
assert(t, ok)
|
|
|
|
_, ok = site.layouts["post"]
|
|
|
|
assert(t, ok)
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
output, err := site.render(site.templates[helloPath])
|
2024-02-13 17:43:20 +01:00
|
|
|
assertEqual(t, err, nil)
|
2024-02-15 03:54:46 +01:00
|
|
|
assertEqual(t, string(output), `<html>
|
2024-02-13 17:43:20 +01:00
|
|
|
<head><title>hello world!</title></head>
|
|
|
|
<body>
|
|
|
|
<h1>hello world!</h1>
|
|
|
|
<h2>my first post</h2>
|
|
|
|
<p>Hello world!</p>
|
|
|
|
</body>
|
|
|
|
</html>`)
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
output, err = site.render(site.templates[goodbyePath])
|
2024-02-13 17:43:20 +01:00
|
|
|
assertEqual(t, err, nil)
|
2024-02-15 03:54:46 +01:00
|
|
|
assertEqual(t, string(output), `<html>
|
2024-02-13 17:43:20 +01:00
|
|
|
<head><title>goodbye!</title></head>
|
|
|
|
<body>
|
|
|
|
<h1>goodbye!</h1>
|
|
|
|
<h2>my last post</h2>
|
|
|
|
<p>goodbye world!</p>
|
|
|
|
</body>
|
|
|
|
</html>`)
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
output, err = site.render(site.templates[aboutPath])
|
2024-02-13 17:43:20 +01:00
|
|
|
assertEqual(t, err, nil)
|
2024-02-15 03:54:46 +01:00
|
|
|
assertEqual(t, string(output), `<html>
|
2024-02-13 17:43:20 +01:00
|
|
|
<head><title>about</title></head>
|
|
|
|
<body>
|
|
|
|
<p>about this site</p>
|
|
|
|
</body>
|
|
|
|
</html>`)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderArchive(t *testing.T) {
|
2024-02-16 16:39:19 +01:00
|
|
|
config := newProject()
|
|
|
|
defer os.RemoveAll(config.RootDir)
|
2024-02-14 01:32:04 +01:00
|
|
|
|
|
|
|
content := `---
|
|
|
|
title: hello world!
|
|
|
|
date: 2024-01-01
|
|
|
|
---
|
|
|
|
<p>Hello world!</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file := newFile(config.SrcDir, "hello.html", content)
|
2024-02-14 01:32:04 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
content = `---
|
|
|
|
title: goodbye!
|
|
|
|
date: 2024-02-01
|
|
|
|
---
|
|
|
|
<p>goodbye world!</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "goodbye.html", content)
|
2024-02-14 01:32:04 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
content = `---
|
|
|
|
title: an oldie!
|
|
|
|
date: 2023-01-01
|
|
|
|
---
|
|
|
|
<p>oldie</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "an-oldie.html", content)
|
2024-02-14 01:32:04 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
// add a page (no date)
|
|
|
|
content = `---
|
|
|
|
---
|
|
|
|
<ul>{% for post in site.posts %}
|
|
|
|
<li>{{ post.date | date: "%Y-%m-%d" }} <a href="{{ post.url }}">{{post.title}}</a></li>{%endfor%}
|
|
|
|
</ul>`
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "about.html", content)
|
2024-02-14 01:32:04 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
site, err := Load(*config)
|
2024-02-15 03:54:46 +01:00
|
|
|
output, err := site.render(site.templates[file.Name()])
|
2024-02-14 01:32:04 +01:00
|
|
|
assertEqual(t, err, nil)
|
2024-02-15 03:54:46 +01:00
|
|
|
assertEqual(t, string(output), `<ul>
|
2024-02-14 01:32:04 +01:00
|
|
|
<li>2024-02-01 <a href="/goodbye">goodbye!</a></li>
|
|
|
|
<li>2024-01-01 <a href="/hello">hello world!</a></li>
|
|
|
|
<li>2023-01-01 <a href="/an-oldie">an oldie!</a></li>
|
|
|
|
</ul>`)
|
2024-02-13 17:43:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderTags(t *testing.T) {
|
2024-02-16 16:39:19 +01:00
|
|
|
config := newProject()
|
|
|
|
defer os.RemoveAll(config.RootDir)
|
2024-02-14 01:32:04 +01:00
|
|
|
|
|
|
|
content := `---
|
|
|
|
title: hello world!
|
|
|
|
date: 2024-01-01
|
|
|
|
tags: [web, software]
|
|
|
|
---
|
|
|
|
<p>Hello world!</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file := newFile(config.SrcDir, "hello.html", content)
|
2024-02-14 01:32:04 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
content = `---
|
|
|
|
title: goodbye!
|
|
|
|
date: 2024-02-01
|
|
|
|
tags: [web]
|
|
|
|
---
|
|
|
|
<p>goodbye world!</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "goodbye.html", content)
|
2024-02-14 01:32:04 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
content = `---
|
|
|
|
title: an oldie!
|
|
|
|
date: 2023-01-01
|
|
|
|
tags: [software]
|
|
|
|
---
|
|
|
|
<p>oldie</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "an-oldie.html", content)
|
2024-02-14 01:32:04 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
// add a page (no date)
|
|
|
|
content = `---
|
|
|
|
---
|
2024-02-18 16:14:44 +01:00
|
|
|
{% for tag in site.tags | keys | sort %}<h1>{{tag}}</h1>{% for post in site.tags[tag] %}
|
2024-02-14 01:32:04 +01:00
|
|
|
{{post.title}}
|
|
|
|
{% endfor %}
|
|
|
|
{% endfor %}
|
|
|
|
`
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "about.html", content)
|
2024-02-14 01:32:04 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
site, err := Load(*config)
|
2024-02-15 03:54:46 +01:00
|
|
|
output, err := site.render(site.templates[file.Name()])
|
2024-02-14 01:32:04 +01:00
|
|
|
assertEqual(t, err, nil)
|
2024-02-15 03:54:46 +01:00
|
|
|
assertEqual(t, string(output), `<h1>software</h1>
|
2024-02-14 01:32:04 +01:00
|
|
|
hello world!
|
|
|
|
|
|
|
|
an oldie!
|
|
|
|
|
|
|
|
<h1>web</h1>
|
|
|
|
goodbye!
|
|
|
|
|
|
|
|
hello world!
|
|
|
|
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderPagesInDir(t *testing.T) {
|
2024-02-16 16:39:19 +01:00
|
|
|
config := newProject()
|
|
|
|
defer os.RemoveAll(config.RootDir)
|
2024-02-14 04:05:55 +01:00
|
|
|
|
|
|
|
content := `---
|
|
|
|
title: "1. hello world!"
|
|
|
|
---
|
|
|
|
<p>Hello world!</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file := newFile(config.SrcDir, "01-hello.html", content)
|
2024-02-14 04:05:55 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
content = `---
|
|
|
|
title: "3. goodbye!"
|
|
|
|
---
|
|
|
|
<p>goodbye world!</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "03-goodbye.html", content)
|
2024-02-14 04:05:55 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
content = `---
|
|
|
|
title: "2. an oldie!"
|
|
|
|
---
|
|
|
|
<p>oldie</p>`
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "02-an-oldie.html", content)
|
2024-02-14 04:05:55 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
// add a page (no date)
|
|
|
|
content = `---
|
|
|
|
---
|
|
|
|
<ul>{% for page in site.pages %}
|
|
|
|
<li><a href="{{ page.url }}">{{page.title}}</a></li>{%endfor%}
|
|
|
|
</ul>`
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "index.html", content)
|
2024-02-14 04:05:55 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
site, err := Load(*config)
|
2024-02-15 03:54:46 +01:00
|
|
|
output, err := site.render(site.templates[file.Name()])
|
2024-02-14 04:05:55 +01:00
|
|
|
assertEqual(t, err, nil)
|
2024-02-15 03:54:46 +01:00
|
|
|
assertEqual(t, string(output), `<ul>
|
2024-02-14 04:05:55 +01:00
|
|
|
<li><a href="/01-hello">1. hello world!</a></li>
|
|
|
|
<li><a href="/02-an-oldie">2. an oldie!</a></li>
|
|
|
|
<li><a href="/03-goodbye">3. goodbye!</a></li>
|
|
|
|
</ul>`)
|
2024-02-14 01:32:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderArchiveWithExcerpts(t *testing.T) {
|
2024-02-21 15:05:09 +01:00
|
|
|
config := newProject()
|
|
|
|
defer os.RemoveAll(config.RootDir)
|
|
|
|
|
|
|
|
content := `---
|
|
|
|
title: hello world!
|
|
|
|
date: 2024-01-01
|
|
|
|
tags: [web, software]
|
|
|
|
---
|
|
|
|
<p>the intro paragraph</p>
|
|
|
|
<p> and another paragraph>`
|
|
|
|
file := newFile(config.SrcDir, "hello.html", content)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
content = `---
|
|
|
|
title: goodbye!
|
|
|
|
date: 2024-02-01
|
|
|
|
tags: [web]
|
|
|
|
excerpt: an overridden excerpt
|
|
|
|
---
|
|
|
|
<p>goodbye world!</p>
|
|
|
|
<p> and another paragraph</p>`
|
|
|
|
file = newFile(config.SrcDir, "goodbye.html", content)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
content = `---
|
|
|
|
title: an oldie!
|
|
|
|
date: 2023-01-01
|
|
|
|
tags: [software]
|
|
|
|
---
|
|
|
|
`
|
|
|
|
file = newFile(config.SrcDir, "an-oldie.html", content)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
// add a page (no date)
|
|
|
|
content = `---
|
|
|
|
---
|
|
|
|
{% for post in site.posts %}
|
|
|
|
{{post.title}} - {{post.excerpt}}
|
|
|
|
{% endfor %}
|
|
|
|
`
|
|
|
|
|
|
|
|
file = newFile(config.SrcDir, "about.html", content)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
site, err := Load(*config)
|
|
|
|
output, err := site.render(site.templates[file.Name()])
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
assertEqual(t, strings.TrimSpace(string(output)), `goodbye! - an overridden excerpt
|
|
|
|
|
|
|
|
hello world! - the intro paragraph
|
|
|
|
|
|
|
|
an oldie! -`)
|
2024-02-13 17:43:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderDataFile(t *testing.T) {
|
2024-02-16 16:39:19 +01:00
|
|
|
config := newProject()
|
|
|
|
defer os.RemoveAll(config.RootDir)
|
2024-02-15 20:01:35 +01:00
|
|
|
|
|
|
|
content := `
|
|
|
|
- name: feedi
|
|
|
|
url: https://github.com/facundoolano/feedi
|
2024-02-16 19:29:43 +01:00
|
|
|
- name: jorge
|
|
|
|
url: https://github.com/facundoolano/jorge
|
2024-02-15 20:01:35 +01:00
|
|
|
`
|
2024-02-16 16:39:19 +01:00
|
|
|
file := newFile(config.DataDir, "projects.yml", content)
|
2024-02-15 20:01:35 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
// add a page (no date)
|
|
|
|
content = `---
|
|
|
|
---
|
|
|
|
<ul>{% for project in site.data.projects %}
|
|
|
|
<li><a href="{{ project.url }}">{{project.name}}</a></li>{%endfor%}
|
|
|
|
</ul>`
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
file = newFile(config.SrcDir, "projects.html", content)
|
2024-02-15 20:01:35 +01:00
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
site, err := Load(*config)
|
2024-02-15 20:01:35 +01:00
|
|
|
output, err := site.render(site.templates[file.Name()])
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
assertEqual(t, string(output), `<ul>
|
|
|
|
<li><a href="https://github.com/facundoolano/feedi">feedi</a></li>
|
2024-02-16 19:29:43 +01:00
|
|
|
<li><a href="https://github.com/facundoolano/jorge">jorge</a></li>
|
2024-02-15 20:01:35 +01:00
|
|
|
</ul>`)
|
2024-02-13 17:43:20 +01:00
|
|
|
}
|
|
|
|
|
2024-02-27 16:24:45 +01:00
|
|
|
func TestBuildTarget(t *testing.T) {
|
|
|
|
config := newProject()
|
|
|
|
defer os.RemoveAll(config.RootDir)
|
|
|
|
|
|
|
|
// add base layout
|
|
|
|
content := `---
|
|
|
|
---
|
|
|
|
<html>
|
|
|
|
<head><title>{{page.title}}</title></head>
|
|
|
|
<body>
|
|
|
|
{{content}}
|
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
newFile(config.LayoutsDir, "base.html", content)
|
|
|
|
|
|
|
|
// add org post
|
|
|
|
content = `---
|
|
|
|
layout: base
|
|
|
|
title: p1 - hello world!
|
|
|
|
date: 2024-01-01
|
|
|
|
---
|
|
|
|
* Hello world!`
|
|
|
|
newFile(config.SrcDir, "p1.org", content)
|
|
|
|
|
|
|
|
// add markdown post
|
|
|
|
content = `---
|
|
|
|
layout: base
|
|
|
|
title: p2 - goodbye world!
|
|
|
|
date: 2024-01-02
|
|
|
|
---
|
|
|
|
# Goodbye world!`
|
|
|
|
newFile(config.SrcDir, "p2.md", content)
|
|
|
|
|
|
|
|
// add index page
|
|
|
|
content = `---
|
|
|
|
layout: base
|
|
|
|
---
|
|
|
|
<ul>{% for post in site.posts %}
|
|
|
|
<li>{{post.title}}</li>{%endfor%}
|
|
|
|
</ul>`
|
|
|
|
newFile(config.SrcDir, "index.html", content)
|
|
|
|
|
|
|
|
// build site
|
|
|
|
site, err := Load(*config)
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
err = site.Build()
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
|
|
|
|
// test target files generated
|
|
|
|
_, err = os.Stat(filepath.Join(config.TargetDir, "p1.html"))
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
_, err = os.Stat(filepath.Join(config.TargetDir, "p2.html"))
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
|
|
|
|
// test index includes p1 and p2
|
|
|
|
output, err := os.ReadFile(filepath.Join(config.TargetDir, "index.html"))
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
assertEqual(t, string(output), `<html><head><title></title></head>
|
|
|
|
<body>
|
|
|
|
<ul>
|
|
|
|
<li>p2 - goodbye world!</li>
|
|
|
|
<li>p1 - hello world!</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
</body></html>`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildWithDrafts(t *testing.T) {
|
|
|
|
config := newProject()
|
|
|
|
defer os.RemoveAll(config.RootDir)
|
|
|
|
|
|
|
|
// add base layout
|
|
|
|
content := `---
|
|
|
|
---
|
|
|
|
<html>
|
|
|
|
<head><title>{{page.title}}</title></head>
|
|
|
|
<body>
|
|
|
|
{{content}}
|
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
newFile(config.LayoutsDir, "base.html", content)
|
|
|
|
|
|
|
|
// add org post
|
|
|
|
content = `---
|
|
|
|
layout: base
|
|
|
|
title: p1 - hello world!
|
|
|
|
date: 2024-01-01
|
|
|
|
---
|
|
|
|
* Hello world!`
|
|
|
|
newFile(config.SrcDir, "p1.org", content)
|
|
|
|
|
|
|
|
// add markdown post, make it draft
|
|
|
|
content = `---
|
|
|
|
layout: base
|
|
|
|
title: p2 - goodbye world!
|
|
|
|
date: 2024-01-02
|
|
|
|
draft: true
|
|
|
|
---
|
|
|
|
# Goodbye world!`
|
|
|
|
newFile(config.SrcDir, "p2.md", content)
|
|
|
|
|
|
|
|
// add index page
|
|
|
|
content = `---
|
|
|
|
layout: base
|
|
|
|
---
|
|
|
|
<ul>{% for post in site.posts %}
|
|
|
|
<li>{{post.title}}</li>{%endfor%}
|
|
|
|
</ul>`
|
|
|
|
newFile(config.SrcDir, "index.html", content)
|
|
|
|
|
|
|
|
// build site with drafts
|
|
|
|
config.IncludeDrafts = true
|
|
|
|
site, err := Load(*config)
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
err = site.Build()
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
|
|
|
|
// test target files generated
|
|
|
|
_, err = os.Stat(filepath.Join(config.TargetDir, "p1.html"))
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
_, err = os.Stat(filepath.Join(config.TargetDir, "p2.html"))
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
|
|
|
|
// test index includes p1 and p2
|
|
|
|
output, err := os.ReadFile(filepath.Join(config.TargetDir, "index.html"))
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
assertEqual(t, string(output), `<html><head><title></title></head>
|
|
|
|
<body>
|
|
|
|
<ul>
|
|
|
|
<li>p2 - goodbye world!</li>
|
|
|
|
<li>p1 - hello world!</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
</body></html>`)
|
|
|
|
|
|
|
|
// build site WITHOUT drafts
|
|
|
|
config.IncludeDrafts = false
|
|
|
|
site, err = Load(*config)
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
err = site.Build()
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
|
|
|
|
// test only non drafts generated
|
|
|
|
_, err = os.Stat(filepath.Join(config.TargetDir, "p1.html"))
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
_, err = os.Stat(filepath.Join(config.TargetDir, "p2.html"))
|
|
|
|
assert(t, os.IsNotExist(err))
|
|
|
|
|
|
|
|
// test index includes p1 but NOT p2
|
|
|
|
output, err = os.ReadFile(filepath.Join(config.TargetDir, "index.html"))
|
|
|
|
assertEqual(t, err, nil)
|
|
|
|
assertEqual(t, string(output), `<html><head><title></title></head>
|
|
|
|
<body>
|
|
|
|
<ul>
|
|
|
|
<li>p1 - hello world!</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
</body></html>`)
|
|
|
|
}
|
|
|
|
|
2024-02-13 17:43:20 +01:00
|
|
|
// ------ HELPERS --------
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
func newProject() *config.Config {
|
2024-02-13 17:43:20 +01:00
|
|
|
projectDir, _ := os.MkdirTemp("", "root")
|
|
|
|
layoutsDir := filepath.Join(projectDir, "layouts")
|
|
|
|
srcDir := filepath.Join(projectDir, "src")
|
2024-02-15 20:01:35 +01:00
|
|
|
dataDir := filepath.Join(projectDir, "data")
|
2024-02-27 16:30:00 +01:00
|
|
|
os.Mkdir(layoutsDir, DIR_RWE_MODE)
|
|
|
|
os.Mkdir(srcDir, DIR_RWE_MODE)
|
|
|
|
os.Mkdir(dataDir, DIR_RWE_MODE)
|
2024-02-13 17:43:20 +01:00
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
config, _ := config.Load(projectDir)
|
2024-02-16 19:14:30 +01:00
|
|
|
config.Minify = false
|
2024-02-16 16:39:19 +01:00
|
|
|
|
|
|
|
return config
|
2024-02-13 17:43:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newFile(dir string, filename string, contents string) *os.File {
|
|
|
|
path := filepath.Join(dir, filename)
|
2024-02-15 20:01:35 +01:00
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-02-13 17:43:20 +01:00
|
|
|
file.WriteString(contents)
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO move to assert package
|
|
|
|
func assert(t *testing.T, cond bool) {
|
|
|
|
t.Helper()
|
|
|
|
if !cond {
|
|
|
|
t.Fatalf("%v is false", cond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertEqual(t *testing.T, a interface{}, b interface{}) {
|
|
|
|
t.Helper()
|
|
|
|
if a != b {
|
|
|
|
t.Fatalf("%v != %v", a, b)
|
|
|
|
}
|
|
|
|
}
|