limit posts to single depth directory
This commit is contained in:
parent
3b67e7731d
commit
70936659da
|
@ -18,6 +18,11 @@ func listDir(path string) []string {
|
|||
return files
|
||||
}
|
||||
|
||||
func fileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// recursive list of all files in a directory
|
||||
func walkDir(path string) []string {
|
||||
files := make([]string, 0)
|
||||
|
|
10
user.go
10
user.go
|
@ -5,7 +5,6 @@ import (
|
|||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
|
@ -47,11 +46,14 @@ func (user User) Name() string {
|
|||
}
|
||||
|
||||
func (user User) Posts() ([]string, error) {
|
||||
postFiles := walkDir(path.Join(user.Dir(), "public"))
|
||||
postFiles := listDir(path.Join(user.Dir(), "public"))
|
||||
posts := make([]string, 0)
|
||||
for _, id := range postFiles {
|
||||
if strings.HasSuffix(id, "/index.md") {
|
||||
posts = append(posts, id[:len(id)-9])
|
||||
// if is a directory and has index.md, add to posts
|
||||
if dirExists(path.Join(user.Dir(), "public", id)) {
|
||||
if fileExists(path.Join(user.Dir(), "public", id, "index.md")) {
|
||||
posts = append(posts, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
return posts, nil
|
||||
|
|
44
user_test.go
44
user_test.go
|
@ -69,7 +69,40 @@ func TestCanListUserPosts(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCanListUserPostsWithSubdirectories(t *testing.T) {
|
||||
func TestCannotListUserPostsInSubdirectories(t *testing.T) {
|
||||
// Create a new user
|
||||
repo, _ := owl.CreateRepository(testRepoName())
|
||||
user, _ := repo.CreateUser(randomUserName())
|
||||
// Create a new post
|
||||
user.CreateNewPost("testpost")
|
||||
os.Mkdir(path.Join(user.PostDir(), "foo"), 0755)
|
||||
os.Mkdir(path.Join(user.PostDir(), "foo/bar"), 0755)
|
||||
content := ""
|
||||
content += "---\n"
|
||||
content += "title: test\n"
|
||||
content += "---\n"
|
||||
content += "\n"
|
||||
content += "Write your post here.\n"
|
||||
|
||||
os.WriteFile(path.Join(user.PostDir(), "foo/index.md"), []byte(content), 0644)
|
||||
os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644)
|
||||
posts, _ := user.Posts()
|
||||
if !contains(posts, "foo") {
|
||||
t.Error("Does not contain post: foo. Found:")
|
||||
for _, p := range posts {
|
||||
t.Error("\t" + p)
|
||||
}
|
||||
}
|
||||
|
||||
if contains(posts, "foo/bar") {
|
||||
t.Error("Invalid post found: foo/bar. Found:")
|
||||
for _, p := range posts {
|
||||
t.Error("\t" + p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCannotListUserPostsWithoutIndexMd(t *testing.T) {
|
||||
// Create a new user
|
||||
repo, _ := owl.CreateRepository(testRepoName())
|
||||
user, _ := repo.CreateUser(randomUserName())
|
||||
|
@ -87,14 +120,7 @@ func TestCanListUserPostsWithSubdirectories(t *testing.T) {
|
|||
os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644)
|
||||
posts, _ := user.Posts()
|
||||
if contains(posts, "foo") {
|
||||
t.Error("Contains non-post name: foo")
|
||||
for _, p := range posts {
|
||||
t.Error("\t" + p)
|
||||
}
|
||||
}
|
||||
|
||||
if !contains(posts, "foo/bar") {
|
||||
t.Error("Post not found. Found: ")
|
||||
t.Error("Contains invalid post: foo. Found:")
|
||||
for _, p := range posts {
|
||||
t.Error("\t" + p)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue