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
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fileExists(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
// recursive list of all files in a directory
|
// recursive list of all files in a directory
|
||||||
func walkDir(path string) []string {
|
func walkDir(path string) []string {
|
||||||
files := make([]string, 0)
|
files := make([]string, 0)
|
||||||
|
|
10
user.go
10
user.go
|
@ -5,7 +5,6 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
@ -47,11 +46,14 @@ func (user User) Name() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user User) Posts() ([]string, error) {
|
func (user User) Posts() ([]string, error) {
|
||||||
postFiles := walkDir(path.Join(user.Dir(), "public"))
|
postFiles := listDir(path.Join(user.Dir(), "public"))
|
||||||
posts := make([]string, 0)
|
posts := make([]string, 0)
|
||||||
for _, id := range postFiles {
|
for _, id := range postFiles {
|
||||||
if strings.HasSuffix(id, "/index.md") {
|
// if is a directory and has index.md, add to posts
|
||||||
posts = append(posts, id[:len(id)-9])
|
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
|
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
|
// Create a new user
|
||||||
repo, _ := owl.CreateRepository(testRepoName())
|
repo, _ := owl.CreateRepository(testRepoName())
|
||||||
user, _ := repo.CreateUser(randomUserName())
|
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)
|
os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644)
|
||||||
posts, _ := user.Posts()
|
posts, _ := user.Posts()
|
||||||
if contains(posts, "foo") {
|
if contains(posts, "foo") {
|
||||||
t.Error("Contains non-post name: foo")
|
t.Error("Contains invalid post: foo. Found:")
|
||||||
for _, p := range posts {
|
|
||||||
t.Error("\t" + p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !contains(posts, "foo/bar") {
|
|
||||||
t.Error("Post not found. Found: ")
|
|
||||||
for _, p := range posts {
|
for _, p := range posts {
|
||||||
t.Error("\t" + p)
|
t.Error("\t" + p)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue