2021-05-04 20:26:41 +00:00
|
|
|
package zord_tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
cp "github.com/otiai10/copy"
|
2021-05-12 04:56:36 +00:00
|
|
|
"io/ioutil"
|
2021-05-04 20:26:41 +00:00
|
|
|
"os"
|
2021-05-12 04:56:36 +00:00
|
|
|
"strings"
|
2021-05-04 20:26:41 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFromNotExistingDirectory(t *testing.T) {
|
2021-05-09 18:17:07 +00:00
|
|
|
_, err := BuildTree("./testing/i_dont_exist", []string{})
|
2021-05-04 20:26:41 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Error("Not existing directory should return error, got NIL.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSunny(t *testing.T) {
|
|
|
|
err := os.Remove("./testdata/sunny1")
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
err = cp.Copy("./testdata/sunny", "./testdata/sunny1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Couldn't prepare data for testing")
|
|
|
|
}
|
2021-05-09 18:17:07 +00:00
|
|
|
tree, err := BuildTree("./testdata/sunny1", []string{})
|
2021-05-04 20:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-05-12 05:17:43 +00:00
|
|
|
if len(tree.Dirs) != 1 {
|
|
|
|
t.Errorf("Should be 1 subdirectory, got %v", len(tree.Dirs))
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 05:17:43 +00:00
|
|
|
if tree.Dirs[0].Files[0].Name != "file3.md" {
|
|
|
|
t.Errorf("File should be 'file3.md', got %v", tree.Dirs[0].Files[0].Name)
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
2021-05-12 05:17:43 +00:00
|
|
|
if tree.Files[0].Name != "file1.md" {
|
|
|
|
t.Errorf("File should be 'file1.md', got %v", tree.Files[0].Name)
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
2021-05-12 05:17:43 +00:00
|
|
|
if len(tree.Files[0].Tags) != 2 {
|
|
|
|
t.Errorf("File 'file1.md' should have 2 tags, got %v", len(tree.Files[0].Tags))
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
2021-05-12 05:17:43 +00:00
|
|
|
if tree.Files[0].Tags[1] != "t2" {
|
|
|
|
t.Errorf("File 'file1.md' 2nd tag 't2', got %v", tree.Files[0].Tags[1])
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
t.Log(tree)
|
2022-05-08 09:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixFormat(t *testing.T) {
|
|
|
|
err := os.Remove("./testdata/sunny1")
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
err = os.Remove("./testdata/sunny2")
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
err = cp.Copy("./testdata/sunny", "./testdata/sunny1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Couldn't prepare data for testing")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Population ended in err:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadFile("./testdata/sunny2/file1.md")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("No destination file 'file1.md'.")
|
|
|
|
}
|
|
|
|
str := string(b)
|
|
|
|
if !strings.Contains(str, "tags: t1,t2\ndescription: Če test file __va__\n") {
|
|
|
|
t.Fatal("Empty lines has not been removed in file 'file1.md'.")
|
|
|
|
}
|
|
|
|
if !strings.Contains(str, "\n\n---\n") {
|
|
|
|
t.Fatal("Split line has not been formatted in file 'file1.md'.")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log(tree)
|
2021-05-09 18:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMeta(t *testing.T) {
|
|
|
|
err := os.Remove("./testdata/sunny1")
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
err = cp.Copy("./testdata/sunny", "./testdata/sunny1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Couldn't prepare data for testing")
|
|
|
|
}
|
|
|
|
meta := []string{"option1", "option2"}
|
|
|
|
tree, err := BuildTree("./testdata/sunny1", meta)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-05-12 05:17:43 +00:00
|
|
|
if tree.Files[0].Name != "file1.md" {
|
|
|
|
t.Errorf("File should be 'file1.md', got %v", tree.Files[0].Name)
|
2021-05-09 18:17:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 05:17:43 +00:00
|
|
|
if tree.Files[0].Meta["option1"] != "test option" {
|
2021-05-09 18:17:07 +00:00
|
|
|
t.Error("File 'file1.md' should have 'option1' with value 'test option'")
|
|
|
|
}
|
2021-05-11 22:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestId(t *testing.T) {
|
|
|
|
err := os.Remove("./testdata/sunny1")
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
err = os.Remove("./testdata/sunny2")
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
err = cp.Copy("./testdata/sunny", "./testdata/sunny1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Couldn't prepare data for testing")
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:56:36 +00:00
|
|
|
err = PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Population ended in err:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadFile("./testdata/sunny2/file1.md")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("No destination file 'file1.md'.")
|
|
|
|
}
|
|
|
|
str := string(b)
|
|
|
|
if !strings.Contains(str, "\nid: 1\n") {
|
|
|
|
t.Fatal("No id in file 'file1.md'.")
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = ioutil.ReadFile("./testdata/sunny2/subcategory/file3.md")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("No destination file 'substring/file3.md'.")
|
|
|
|
}
|
|
|
|
str = string(b)
|
|
|
|
if !strings.Contains(str, "\nid: 3\n") {
|
|
|
|
t.Fatal("No id in file 'file3.md'.")
|
|
|
|
}
|
2021-05-11 22:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMissingOptions(t *testing.T) {
|
|
|
|
err := os.Remove("./testdata/sunny1")
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
err = os.Remove("./testdata/sunny2")
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
err = cp.Copy("./testdata/sunny", "./testdata/sunny1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Couldn't prepare data for testing")
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:56:36 +00:00
|
|
|
err = PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{"option1", "option2"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Population ended in err:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadFile("./testdata/sunny2/file1.md")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("No destination file 'file1.md'.")
|
|
|
|
}
|
|
|
|
str := string(b)
|
|
|
|
if !strings.Contains(str, "\noption1: test option\n") {
|
|
|
|
t.Fatal("Changed old value for 'option1' in file 'file1.md'.")
|
|
|
|
}
|
|
|
|
if !strings.Contains(str, "\noption2: \n") {
|
|
|
|
t.Fatal("'option2' has not been added to file 'file1.md'.")
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = ioutil.ReadFile("./testdata/sunny2/file2.md")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("No destination file 'file2.md'.")
|
|
|
|
}
|
|
|
|
str = string(b)
|
|
|
|
if !strings.Contains(str, "\noption1: \n") {
|
|
|
|
t.Fatal("'option1' has not been added to file 'file2.md'.")
|
|
|
|
}
|
|
|
|
if !strings.Contains(str, "\noption2: \n") {
|
|
|
|
t.Fatal("'option2' has not been added to file 'file2.md'.")
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = ioutil.ReadFile("./testdata/sunny2/subcategory/file3.md")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("No destination file 'substring/file3.md'.")
|
|
|
|
}
|
|
|
|
str = string(b)
|
|
|
|
if !strings.Contains(str, "\noption1: \n") {
|
|
|
|
t.Fatal("'option1' has not been added to file 'file3.md'.")
|
|
|
|
}
|
|
|
|
if !strings.Contains(str, "\noption2: it exists\n") {
|
|
|
|
t.Fatal("Changed old value for 'option2' in file 'file3.md'.")
|
|
|
|
}
|
2021-05-11 22:10:18 +00:00
|
|
|
}
|