diff --git a/firefox/firefox.go b/firefox/firefox.go index 1e7f2cd..bfa6fa5 100644 --- a/firefox/firefox.go +++ b/firefox/firefox.go @@ -131,6 +131,7 @@ func (ff *Firefox) scanBookmarks() ([]*MozBookmark, error) { * the node tree. */ for _, tagName := range strings.Split(bkEntry.Tags, ",") { + if tagName == "" { continue } seen, tagNode := ff.addTagNode(tagName) if !seen { log.Infof("tag <%s> already in tag map", tagNode.Name) @@ -143,11 +144,10 @@ func (ff *Firefox) scanBookmarks() ([]*MozBookmark, error) { // Parent will be a folder or nothing? tree.AddChild(ff.tagMap[tagNode.Name], urlNode) - ff.Stats.CurrentUrlCount++ + ff.CurrentUrlCount++ } // Link this URL node to its corresponding folder node if it exists. - //FIX: sql query wront parentFolderId ?? (indian cooking) //TODO: add all parent folders in the tags list of this url node folderNode, fOk := ff.folderMap[bkEntry.ParentId] if fOk { @@ -276,7 +276,7 @@ func (f *Firefox) Load() error { f.CurrentUrlCount, f.CurrentNodeCount, f.LastFullTreeParseTime) - f.Stats.Reset() + f.Reset() // Sync the URLIndex to the buffer // We do not use the NodeTree here as firefox tags are represented @@ -425,7 +425,7 @@ func (f *Firefox) Run() { // urlNode.Parent = f.tagMap[bk.parent] // tree.Insert(f.tagMap[bk.parent].Children, urlNode) - f.Stats.CurrentUrlCount++ + f.CurrentUrlCount++ } } } @@ -442,7 +442,7 @@ func (f *Firefox) Run() { log.Error(err) } - f.Stats.LastWatchRunTime = time.Since(startRun) + f.LastWatchRunTime = time.Since(startRun) // log.Debugf("execution time %s", time.Since(startRun)) // tree.PrintTree(f.NodeTree) // debugging @@ -500,7 +500,7 @@ func (f *Firefox) addUrlNode(url, title, desc string) (bool, *tree.Node) { log.Debugf("inserting url %s in url index", url) f.URLIndex.Insert(url, urlNode) f.URLIndexList = append(f.URLIndexList, url) - f.Stats.CurrentNodeCount++ + f.CurrentNodeCount++ return true, urlNode } else { @@ -545,7 +545,7 @@ func (ff *Firefox) addTagNode(tagName string) (bool, *tree.Node) { tree.AddChild(tagsBranch, tagNode) ff.tagMap[tagName] = tagNode - ff.Stats.CurrentNodeCount++ + ff.CurrentNodeCount++ return true, tagNode } @@ -674,7 +674,7 @@ func loadBookmarks(f *Firefox) { // Set tag as parent to urlnode tree.AddChild(f.tagMap[tagTitle], urlNode) - f.Stats.CurrentUrlCount++ + f.CurrentUrlCount++ } log.Debugf("root tree children len is %d", len(f.NodeTree.Children)) @@ -752,7 +752,7 @@ func (f *Firefox) fetchUrlChanges(rows *sql.Rows, func (f *Firefox) initPlacesCopy() error { err := f.copyPlacesToTmp() if err != nil { - return fmt.Errorf("Could not copy places.sqlite to tmp folder: %s", + return fmt.Errorf("could not copy places.sqlite to tmp folder: %s", err) } diff --git a/firefox/firefox_test.go b/firefox/firefox_test.go index 1cc1f97..fdca7a8 100644 --- a/firefox/firefox_test.go +++ b/firefox/firefox_test.go @@ -56,12 +56,15 @@ func runPlacesTest(name string, t *testing.T, test func(t *testing.T)) { t.Error(err) } - t.Cleanup(func() { + defer func() { err = ff.places.Handle.Close() if err != nil { t.Error(err) } - }) + // Run the wal_checkpoint command to clean up the WAL file + ff.places.Handle.Exec("PRAGMA wal_checkpoint(TRUNCATE)") + + }() t.Run(name, test) @@ -423,7 +426,31 @@ func Test_scanBookmarks(t *testing.T) { t.Run("url node is child of the right tag nodes", func(t *testing.T){ // Every URL node should be a child of the right tag node - t.Error() + + // Go through each tag node + for _, bk:= range bookmarks { + + urlNode, urlNodeExists := ff.URLIndex.Get(bk.Url) + assert.True(t, urlNodeExists, "url missing in URLIndex") + + // only check bookmarks with tags + if len(bk.Tags) == 0 { continue } + + var foundTagNodeForUrl bool + for _, tagName := range strings.Split(bk.Tags, ",") { + tagNode, tagNodeExists := ff.tagMap[tagName] + if !tagNodeExists { + t.Errorf("missing tag <%s>", tagName) + } + // Check that the URL node is a direct child of the tag node + if urlNode.(*tree.Node).DirectChildOf(tagNode) { + foundTagNodeForUrl = true + } + } + + assert.True(t, foundTagNodeForUrl) + + } }) t.Run("url underneath the right folders", func(t *testing.T){ @@ -442,8 +469,15 @@ func Test_scanBookmarks(t *testing.T) { // URL node has the right parent folder node // If Parent is nil, it means no folder was assigned to this url node + parentFolder := bk.ParentFolder + switch parentFolder { + case "unfiled": + parentFolder = mozilla.RootFolders[mozilla.OtherID] + case "mobile": + parentFolder = mozilla.RootFolders[mozilla.MobileID] + } if urlNode.(*tree.Node).Parent != nil { - assert.Equal(t, urlNode.(*tree.Node).Parent.Name, bk.ParentFolder, + assert.Equal(t, urlNode.(*tree.Node).Parent.Name, parentFolder, "wrong folder for <%s>", bk.Url) } diff --git a/tree/tree.go b/tree/tree.go index 364d22a..33e12de 100644 --- a/tree/tree.go +++ b/tree/tree.go @@ -57,7 +57,13 @@ func Ancestor(node *Node) *Node { } func (node *Node) DirectChildOf(parent *Node) bool { - return node.Parent == parent + if len(parent.Children) == 0 { return false } + var found bool + for _, child := range parent.Children { + if node == child { found = true } + } + + return found }