diff --git a/nw/core/project.py b/nw/core/project.py index 414c4f18..5bead8f5 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -41,7 +41,7 @@ from nw.core.document import NWDoc from nw.core.status import NWStatus from nw.core.options import OptionState from nw.common import ( - checkString, checkBool, checkInt, formatTimeStamp, makeFileNameSafe + checkString, checkBool, checkInt, isHandle, formatTimeStamp, makeFileNameSafe ) from nw.constants import ( nwFiles, nwItemType, nwItemClass, nwItemLayout, nwLabels, nwAlert @@ -1288,10 +1288,13 @@ class NWProject(): logger.warning("Skipping file %s" % fileItem) continue fHandle = fileItem[:13] + if not isHandle(fHandle): + logger.warning("Skipping file %s" % fileItem) + continue if fHandle in self.projTree: logger.debug("Checking file %s, handle %s: OK" % (fileItem, fHandle)) else: - logger.debug("Checking file %s, handle %s: Orphaned" % (fileItem, fHandle)) + logger.warning("Checking file %s, handle %s: Orphaned" % (fileItem, fHandle)) orphanFiles.append(fHandle) # Report status diff --git a/tests/nwdummy.py b/tests/nwdummy.py index 41b25907..313af767 100644 --- a/tests/nwdummy.py +++ b/tests/nwdummy.py @@ -10,6 +10,7 @@ class DummyMain(): return def makeAlert(self, theMessage, theLevel): + print("%s: %s" % (str(theLevel), theMessage)) return def setStatus(self, theMessage): diff --git a/tests/test_project.py b/tests/test_project.py index 54d44d6a..17eae3b0 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -12,7 +12,7 @@ from nw.core.project import NWProject from nw.core.document import NWDoc from nw.core.index import NWIndex from nw.core.spellcheck import NWSpellEnchant, NWSpellSimple -from nw.constants import nwItemClass, nwItemLayout, nwFiles +from nw.constants import nwItemClass, nwItemType, nwItemLayout, nwFiles @pytest.mark.project def testProjectNewOpenSave(nwFuncTemp, nwTempProj, nwRef, nwTemp, nwDummy): @@ -431,3 +431,64 @@ def testProjectOptions(nwDummy, nwLipsum): assert theOpts.getFloat("GuiWritingStats", "NoName", False) is False assert theOpts.setValue("GuiWritingStats", "winWidth", "True") assert theOpts.getFloat("GuiWritingStats", "winWidth", False) is False + +@pytest.mark.project +def testOrphanedFiles(nwDummy, nwLipsum): + theProject = NWProject(nwDummy) + assert theProject.openProject(nwLipsum) + assert theProject.projTree["636b6aa9b697b"] is None + assert theProject.closeProject() + + # First Item with Meta Data + orphPath = path.join(nwLipsum, "content", "636b6aa9b697b.nwd") + with open(orphPath, mode="w", encoding="utf8") as outFile: + outFile.write(r"%%~ 5eaea4e8cdee8:15c4492bd5107:WORLD:NOTE:Mars") + outFile.write("\n") + + # Second Item without Meta Data + orphPath = path.join(nwLipsum, "content", "736b6aa9b697b.nwd") + with open(orphPath, mode="w", encoding="utf8") as outFile: + outFile.write("\n") + + # Invalid File Name + dummyPath = path.join(nwLipsum, "content", "636b6aa9b697b.txt") + with open(dummyPath, mode="w", encoding="utf8") as outFile: + outFile.write("\n") + + # Invalid File Name + dummyPath = path.join(nwLipsum, "content", "636b6aa9b697bb.nwd") + with open(dummyPath, mode="w", encoding="utf8") as outFile: + outFile.write("\n") + + # Invalid File Name + dummyPath = path.join(nwLipsum, "content", "abcdefghijklm.nwd") + with open(dummyPath, mode="w", encoding="utf8") as outFile: + outFile.write("\n") + + assert theProject.openProject(nwLipsum) + assert theProject.projPath is not None + assert theProject.projTree["636b6aa9b697bb"] is None + assert theProject.projTree["abcdefghijklm"] is None + + # First Item with Meta Data + oItem = theProject.projTree["636b6aa9b697b"] + assert oItem is not None + assert oItem.itemName == "Mars" + assert oItem.itemHandle == "636b6aa9b697b" + assert oItem.parHandle is None + assert oItem.itemClass == nwItemClass.WORLD + assert oItem.itemType == nwItemType.FILE + assert oItem.itemLayout == nwItemLayout.NOTE + + # Second Item without Meta Data + oItem = theProject.projTree["736b6aa9b697b"] + assert oItem is not None + assert oItem.itemName == "Orphaned File 1" + assert oItem.itemHandle == "736b6aa9b697b" + assert oItem.parHandle is None + assert oItem.itemClass == nwItemClass.NO_CLASS + assert oItem.itemType == nwItemType.FILE + assert oItem.itemLayout == nwItemLayout.NO_LAYOUT + + assert theProject.saveProject(nwLipsum) + assert theProject.closeProject()