Skip to content

Commit

Permalink
test(tidy-up): forbidden pages, running tasks, double spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Dec 28, 2024
1 parent 9c63b1b commit be08a1e
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: "1.21"
- name: Install richgo
run: |
go install github.com/kyoh86/richgo@latest
- name: Run tests and generate coverage data
run: |
make test-go-coverage
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ repos:
hooks:
- id: prettier
stages:
- commit
- pre-commit
- repo: https://github.com/doublify/pre-commit-rust
rev: v1.0
hooks:
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
ACTIVATE_VENV = source ~/.pyenv/versions/logseq-doctor/bin/activate
GO_TEST = go test -v ./... -race -covermode=atomic

# To install richgo, run:
# go install github.com/kyoh86/richgo@latest
GO_TEST = $(shell command -v richgo &> /dev/null && echo "richgo" || echo "go") test -v ./... -race -covermode=atomic

help: # Display this help
@cat Makefile | egrep '^[a-z0-9 ./-]*:.*#' | sed -E -e 's/:.+# */@ /g' -e 's/ .+@/@/g' | sort | awk -F@ '{printf "\033[1;34m%-15s\033[0m %s\n", $$1, $$2}'
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/andreoliwa/logseq-go v0.0.0-20241226195503-ec30920a9c1c
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.8.1
)

require (
Expand All @@ -21,16 +22,19 @@ require (
github.com/blugelabs/ice v1.0.0 // indirect
github.com/blugelabs/ice/v2 v2.0.1 // indirect
github.com/caio/go-tdigest v3.1.0+incompatible // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-metro v0.0.0-20180109044635-280f6062b5bc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.15.2 // indirect
github.com/mschoch/smat v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/yuin/goldmark v1.6.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
Expand Down Expand Up @@ -133,8 +137,10 @@ gonum.org/v1/gonum v0.7.0 h1:Hdks0L0hgznZLG9nzXb8vZ0rRvqNvAcgAp84y7Mwkgw=
gonum.org/v1/gonum v0.7.0/go.mod h1:L02bwd0sqlsvRv41G7wGWFCsVNZFv/k1xzGIxeANHGM=
gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw=
gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b/go.mod h1:Wt8AAjI+ypCyYX3nZBvf6cAIx93T+c/OS2HFAYskSZc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
Expand Down
2 changes: 2 additions & 0 deletions go/cmd/testdata/graph/logseq/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
;; Empty config for now
{}
5 changes: 5 additions & 0 deletions go/cmd/testdata/graph/pages/forbidden.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- some [[quick capture]] on top level
- nested [[Inbox]]
- top level #Inbox
- child
- deep nested #[[quick capture]] as a tag
4 changes: 4 additions & 0 deletions go/cmd/testdata/graph/pages/running.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- DOING this is a running task
- nested
- top level
- IN-PROGRESS running child task
7 changes: 7 additions & 0 deletions go/cmd/testdata/graph/pages/spaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- Regular text with spaces
- child
- nested [Link With Spaces ](https://example.com)
- top level
- child 2
- child 3
- Here we have #[[some tag with spaces]]
7 changes: 7 additions & 0 deletions go/cmd/testdata/graph/pages/valid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- no forbidden pages [[here]]
- nested
- top level
- child
- deep nested #[[bla bla]] as a tag
- no running tasks
- no double spaces
19 changes: 11 additions & 8 deletions go/cmd/tidy_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var tidyUpCmd = &cobra.Command{

changes := make([]string, 0)

functions := []func(logseq.Page) string{checkForbiddenReferences, checkRunningTasks, checkConsecutiveSpaces}
functions := []func(logseq.Page) string{checkForbiddenReferences, checkRunningTasks, checkDoubleSpaces}
for _, f := range functions {
if msg := f(page); msg != "" {
changes = append(changes, msg)
Expand Down Expand Up @@ -129,9 +129,10 @@ func checkForbiddenReferences(page logseq.Page) string {
return false
})
}
if len(all) > 0 {
count := len(all)
if count > 0 {
unique := sortAndRemoveDuplicates(all)
return fmt.Sprintf("remove these forbidden references to pages/tags: %s", strings.Join(unique, ", "))
return fmt.Sprintf("remove %d forbidden references to pages/tags: %s", count, strings.Join(unique, ", "))
}
return ""
}
Expand Down Expand Up @@ -169,14 +170,15 @@ func checkRunningTasks(page logseq.Page) string {
return false
})
}
if len(all) > 0 {
count := len(all)
if count > 0 {
unique := sortAndRemoveDuplicates(all)
return fmt.Sprintf("stop the running tasks: %s", strings.Join(unique, ", "))
return fmt.Sprintf("stop %d running task(s): %s", count, strings.Join(unique, ", "))
}
return ""
}

func checkConsecutiveSpaces(page logseq.Page) string {
func checkDoubleSpaces(page logseq.Page) string {
all := make([]string, 0)
for _, block := range page.Blocks() {
block.Children().FilterDeep(func(n content.Node) bool {
Expand All @@ -194,9 +196,10 @@ func checkConsecutiveSpaces(page logseq.Page) string {
return false
})
}
if len(all) > 0 {
count := len(all)
if count > 0 {
unique := sortAndRemoveDuplicates(all)
return fmt.Sprintf("double spaces: %s", strings.Join(unique, ", "))
return fmt.Sprintf("%d double spaces: %s", count, strings.Join(unique, ", "))
}
return ""
}
42 changes: 42 additions & 0 deletions go/cmd/tidy_up_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"context"
"github.com/andreoliwa/logseq-go"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -65,3 +68,42 @@ func TestIsValidMarkdownFile(t *testing.T) {
})
}
}

func setupPage(t *testing.T, name string) logseq.Page {
ctx := context.Background()
graph, err := logseq.Open(ctx, filepath.Join("testdata", "graph"))
if err != nil {
t.Fatal(err)
}

page, err := graph.OpenPage(name)
if err != nil {
t.Fatal(err)
}

return page
}

func TestCheckForbiddenReferences(t *testing.T) {
invalid := setupPage(t, "forbidden")
assert.Equal(t, "remove 4 forbidden references to pages/tags: Inbox, quick capture", checkForbiddenReferences(invalid))

valid := setupPage(t, "valid")
assert.Equal(t, "", checkForbiddenReferences(valid))
}

func TestCheckRunningTasks(t *testing.T) {
invalid := setupPage(t, "running")
assert.Equal(t, "stop 2 running task(s): DOING, IN-PROGRESS", checkRunningTasks(invalid))

valid := setupPage(t, "valid")
assert.Equal(t, "", checkRunningTasks(valid))
}

func TestCheckDoubleSpaces(t *testing.T) {
invalid := setupPage(t, "spaces")
assert.Equal(t, "3 double spaces: 'Link With Spaces ', 'Regular text with spaces', 'some tag with spaces'", checkDoubleSpaces(invalid))

valid := setupPage(t, "valid")
assert.Equal(t, "", checkDoubleSpaces(valid))
}

0 comments on commit be08a1e

Please sign in to comment.