Remove the sha256sum function for files and update tests
This commit is contained in:
@@ -25,7 +25,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
import hashlib
|
|
||||||
import logging
|
import logging
|
||||||
import unicodedata
|
import unicodedata
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
@@ -488,25 +487,6 @@ def makeFileNameSafe(text: str) -> str:
|
|||||||
return "".join(c for c in text if c.isalnum() or c in allowed)
|
return "".join(c for c in text if c.isalnum() or c in allowed)
|
||||||
|
|
||||||
|
|
||||||
def sha256sum(path: str | Path) -> str | None:
|
|
||||||
"""Make a shasum of a file using a buffer.
|
|
||||||
Based on: https://stackoverflow.com/a/44873382/5825851
|
|
||||||
"""
|
|
||||||
digest = hashlib.sha256()
|
|
||||||
bData = bytearray(65536)
|
|
||||||
mData = memoryview(bData)
|
|
||||||
try:
|
|
||||||
with open(path, mode="rb", buffering=0) as inFile:
|
|
||||||
for n in iter(lambda: inFile.readinto(mData), 0):
|
|
||||||
digest.update(mData[:n])
|
|
||||||
except Exception:
|
|
||||||
logger.error("Could not create sha256sum of: %s", path)
|
|
||||||
logException()
|
|
||||||
return None
|
|
||||||
|
|
||||||
return digest.hexdigest()
|
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================================== #
|
# =============================================================================================== #
|
||||||
# Other Functions
|
# Other Functions
|
||||||
# =============================================================================================== #
|
# =============================================================================================== #
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
|
|
||||||
import time
|
import time
|
||||||
import pytest
|
import pytest
|
||||||
import hashlib
|
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from xml.etree import ElementTree as ET
|
from xml.etree import ElementTree as ET
|
||||||
@@ -35,8 +34,8 @@ from novelwriter.common import (
|
|||||||
checkString, checkStringNone, checkUuid, formatInt, formatTime,
|
checkString, checkStringNone, checkUuid, formatInt, formatTime,
|
||||||
formatTimeStamp, fuzzyTime, getGuiItem, hexToInt, isHandle, isItemClass,
|
formatTimeStamp, fuzzyTime, getGuiItem, hexToInt, isHandle, isItemClass,
|
||||||
isItemLayout, isItemType, isTitleTag, jsonEncode, makeFileNameSafe, minmax,
|
isItemLayout, isItemType, isTitleTag, jsonEncode, makeFileNameSafe, minmax,
|
||||||
numberToRoman, NWConfigParser, readTextFile, sha256sum, simplified,
|
numberToRoman, NWConfigParser, readTextFile, simplified, transferCase,
|
||||||
transferCase, xmlIndent, yesNo
|
xmlIndent, yesNo
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -622,45 +621,6 @@ def testBaseCommon_makeFileNameSafe():
|
|||||||
# END Test testBaseCommon_makeFileNameSafe
|
# END Test testBaseCommon_makeFileNameSafe
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.base
|
|
||||||
def testBaseCommon_sha256sum(monkeypatch, fncPath, ipsumText):
|
|
||||||
"""Test the sha256sum function."""
|
|
||||||
longText = 50*(" ".join(ipsumText) + " ")
|
|
||||||
shortText = "This is a short file"
|
|
||||||
noneText = ""
|
|
||||||
|
|
||||||
assert len(longText) == 175650
|
|
||||||
|
|
||||||
longFile = fncPath / "long_file.txt"
|
|
||||||
shortFile = fncPath / "short_file.txt"
|
|
||||||
noneFile = fncPath / "none_file.txt"
|
|
||||||
|
|
||||||
writeFile(longFile, longText)
|
|
||||||
writeFile(shortFile, shortText)
|
|
||||||
writeFile(noneFile, noneText)
|
|
||||||
|
|
||||||
# Taken with sha256sum command on command line
|
|
||||||
longHash = "9b22aee35660da4fae204acbe96aec7f563022746ca2b7a3831f5e44544765eb"
|
|
||||||
shortHash = "6d7c9b2722364c471b8a8666bcb35d18500272d05b23b3427288e2e34c6618f0"
|
|
||||||
noneHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
|
||||||
|
|
||||||
assert sha256sum(longFile) == longHash
|
|
||||||
assert sha256sum(shortFile) == shortHash
|
|
||||||
assert sha256sum(noneFile) == noneHash
|
|
||||||
|
|
||||||
assert hashlib.sha256(longText.encode("utf-8")).hexdigest() == longHash
|
|
||||||
assert hashlib.sha256(shortText.encode("utf-8")).hexdigest() == shortHash
|
|
||||||
assert hashlib.sha256(noneText.encode("utf-8")).hexdigest() == noneHash
|
|
||||||
|
|
||||||
with monkeypatch.context() as mp:
|
|
||||||
mp.setattr("builtins.open", causeOSError)
|
|
||||||
assert sha256sum(longFile) is None
|
|
||||||
assert sha256sum(shortFile) is None
|
|
||||||
assert sha256sum(noneFile) is None
|
|
||||||
|
|
||||||
# END Test testBaseCommon_sha256sum
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.base
|
@pytest.mark.base
|
||||||
def testBaseCommon_getGuiItem(nwGUI):
|
def testBaseCommon_getGuiItem(nwGUI):
|
||||||
"""Check the GUI item function."""
|
"""Check the GUI item function."""
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncPath, mockRnd):
|
|||||||
assert theDoc.writeDocument(theText, forceWrite=True) is True
|
assert theDoc.writeDocument(theText, forceWrite=True) is True
|
||||||
|
|
||||||
# Force no meta data
|
# Force no meta data
|
||||||
theDoc._theItem = None
|
theDoc._item = None
|
||||||
assert theDoc.writeDocument(theText) is True
|
assert theDoc.writeDocument(theText) is True
|
||||||
assert readFile(docPath) == theText
|
assert readFile(docPath) == theText
|
||||||
|
|
||||||
@@ -137,7 +137,7 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncPath, mockRnd):
|
|||||||
assert theDoc.getError() == ""
|
assert theDoc.getError() == ""
|
||||||
|
|
||||||
# Saving with no handle
|
# Saving with no handle
|
||||||
theDoc._docHandle = None
|
theDoc._handle = None
|
||||||
assert theDoc.writeDocument(theText) is False
|
assert theDoc.writeDocument(theText) is False
|
||||||
|
|
||||||
# Delete Document
|
# Delete Document
|
||||||
|
|||||||
Reference in New Issue
Block a user