This commit is contained in:
Suzanne Soy 2021-02-21 21:25:09 +00:00
parent f7499c55e6
commit b62032e931
3 changed files with 82 additions and 22 deletions

View File

@ -25,5 +25,9 @@ jobs:
run: PATH="$PWD/result/bin" ./test.py
- uses: actions/upload-artifact@v2
with:
name: debug.tar.gz
path: /tmp/debug.tar.gz
name: debug-git.tar.gz
path: /tmp/debug-git.tar.gz
- uses: actions/upload-artifact@v2
with:
name: debug-sql.tar.gz
path: /tmp/debug-sql.tar.gz

View File

@ -37,14 +37,8 @@ def hashGit(path):
HEAD = "HEAD" if ref_exists(path, "HEAD") else ''
return subprocess.check_output(['sh', '-c', git_command.format(HEAD=HEAD, FETCH_HEAD=FETCH_HEAD)], cwd=path)
sqlite3_command='''
(
sqlite3 file.db .dump | sort | sha256sum --binary --zero
)
'''
def hashSqlite3(path):
pass # TODO…
return subprocess.check_output(['sh', '-c', 'sqlite3 "$1" .dump | sort | sha256sum --binary --zero', '--', os.path.abspath(path)])
def ignore_exitcode(cmd, **kwargs):
try:
@ -69,7 +63,9 @@ def recur(x):
return hash1(b'git-versioned folder\0' + hashGit(x))
# directory
elif os.path.isdir(x):
return hash1(b'directory\0' + b''.join(recur(os.path.join(x, entry)) + b' ' + entry.encode('utf-8') + b'\0' for entry in os.listdir(x)))
return hash1(b'directory\0' + b''.join(recur(os.path.join(x, entry)) + b' ' + entry.encode('utf-8') + b'\0' for entry in os.listdir(x)))
elif b'SQLite 3.x database' in subprocess.check_output(["file", x]):
return hashSqlite3(x)
# Just a file
elif os.path.isfile(x):
return hashFile(x)

84
test.py
View File

@ -2,7 +2,30 @@
import os, subprocess, tempfile
# Plain text file and empty folder
# Empty
with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir:
os.mkdir(tempdir+'/test')
os.mkdir(tempdir+'/test/foo')
h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip()
if h == b'dc99f8161ccf245e178102a00264e4f4f43cd0048ea525b6c9e226777414352f':
print("test passed: empty")
else:
print("TEST FAILED: empty: got hash " + repr(h))
exit(1)
# Plain text file
with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir:
os.mkdir(tempdir+'/test')
os.mkdir(tempdir+'/test/foo')
os.system('echo a > '+tempdir+'/test/foo/x')
h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip()
if h == b'6b393b2233479ccc54975f83f4de0d39592d5ab78cd02b19597e7bbe97f43cf1':
print("test passed: plain text file")
else:
print("TEST FAILED: plain text file: got hash " + repr(h))
exit(1)
# Plain text file and empty folder in subdirectory
with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir:
os.mkdir(tempdir+'/test')
os.mkdir(tempdir+'/test/foo')
@ -12,9 +35,9 @@ with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir:
os.system('echo a > '+tempdir+'/test/foo/baz/titi')
h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip()
if h == b'7421373b28f6a1929228a3bd7ecb23123d25da36c9bbe41518c7a6252f351712':
print("test passed")
print("test passed: plain text and empty folder in subdirectory")
else:
print("TEST FAILED: got hash " + repr(h))
print("TEST FAILED: plain text and empty folder in subdirectory: got hash " + repr(h))
exit(1)
# Git directories
@ -35,14 +58,51 @@ with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir:
os.system('echo a > '+tempdir+'/test/foo/baz/titi')
h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip()
if h == b'8a84206ece36f07d2c408e565ec506bab407d6e1c645eb4a5c7d057049956110':
print("test passed")
print(subprocess.check_output("tar -zcf /tmp/debug-git.tar.gz .", cwd=tempdir, shell=True))
print("test passed: git")
subprocess.check_output("tar -zcf /tmp/debug-git.tar.gz .", cwd=tempdir, shell=True)
else:
print("TEST FAILED: got hash " + repr(h))
print(subprocess.check_output("tar -zcf /tmp/debug-git.tar.gz .", cwd=tempdir, shell=True))
print("TEST FAILED: git: got hash " + repr(h))
subprocess.check_output("tar -zcf /tmp/debug-git.tar.gz .", cwd=tempdir, shell=True)
exit(0)
# Sqlite
with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir:
os.mkdir(tempdir+'/test')
os.mkdir(tempdir+'/test/foo')
os.mkdir(tempdir+'/test/foo/bar')
os.mkdir(tempdir+'/test/foo/baz')
os.mkdir(tempdir+'/test/foo/baz/quux')
os.system('git init '+tempdir+'/test/foo/baz/git_workdir -b branchname --quiet')
os.system('git init '+tempdir+'/test/foo/baz/git_workdir_empty -b branchname --quiet')
os.system('git init --bare '+tempdir+'/test/foo/baz/git_bare -b branchname --quiet')
os.system('git init --bare '+tempdir+'/test/foo/baz/git_bare_empty -b branchname --quiet')
os.system('cd '+tempdir+'/test/foo/baz/git_workdir && echo a > toto')
os.system('cd '+tempdir+'/test/foo/baz/git_workdir && git add toto')
os.system('cd '+tempdir+'/test/foo/baz/git_workdir&& GIT_COMMITTER_DATE="Sun Feb 21 18:00 2020 +0000" GIT_AUTHOR_NAME="Suzanne Soy" GIT_AUTHOR_EMAIL="example@suzanne.soy" GIT_COMMITTER_NAME="Suzanne Soy" GIT_COMMITTER_EMAIL="example@suzanne.soy" git commit -m "example commit for tests" --date="Sun Feb 21 18:00 2020 +0000" --quiet')
os.system('cd '+tempdir+'/test/foo/baz/git_workdir && git push ../git_bare branchname --quiet')
# It seems that sqlite databases are quite reproducible; running the same command produces identical files!
os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table digits(d);"')
for i in range(10):
os.system('sqlite3 '+tempdir+'/test/foo/baz/db "insert into digits(d) values('+str(i)+');"')
os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table tbl(x);"')
os.system('sqlite3 '+tempdir+'/test/foo/baz/db "insert into tbl(x) select d4.d * 100 + d5.d * 10 + d6.d from digits d4, digits d5, digits d6;"')
os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table rnd(x);"')
os.system('sqlite3 '+tempdir+'/test/foo/baz/db "insert into rnd(x) select x from tbl order by random();"')
os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table tbl2(x);"')
os.system('sqlite3 '+tempdir+'/test/foo/baz/db "insert into tbl2(x) select x from rnd order by x;"')
os.system('sqlite3 '+tempdir+'/test/foo/baz/db "drop table rnd;"')
#os.system('sqlite3 '+tempdir+'/test/foo/baz/db "vacuum;"')
os.system('echo a > '+tempdir+'/test/foo/baz/titi')
h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip()
if h == b'c04f602dcb7eec19433c12981f58d653c61ac7453da60e375f2f5587dc57f474':
print("test passed: sqlite")
subprocess.check_output("tar -zcf /tmp/debug-sql.tar.gz .", cwd=tempdir, shell=True)
else:
print("TEST FAILED: sqlite got hash " + repr(h))
subprocess.check_output("tar -zcf /tmp/debug-sql.tar.gz .", cwd=tempdir, shell=True)
exit(0)
# Sqlite big table
with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir:
os.mkdir(tempdir+'/test')
os.mkdir(tempdir+'/test/foo')
@ -71,10 +131,10 @@ with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir:
#os.system('sqlite3 '+tempdir+'/test/foo/baz/db "vacuum;"')
os.system('echo a > '+tempdir+'/test/foo/baz/titi')
h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip()
if h == b'e8e0e538fa2a79a6c03d5575734bb77ee8c8734b07201d3d7dfc289c118d81a4':
print("test passed")
print(subprocess.check_output("tar -zcf /tmp/debug-sql.tar.gz .", cwd=tempdir, shell=True))
if h == b'b0aae011d0e438a78d6e2652478ec667b7650b9c29dc6d7235ccce26a75c95cb':
print("test passed: sqlite big table")
subprocess.check_output("tar -zcf /tmp/debug-sql.tar.gz .", cwd=tempdir, shell=True)
else:
print("TEST FAILED: got hash " + repr(h))
print(subprocess.check_output("tar -zcf /tmp/debug-sql.tar.gz .", cwd=tempdir, shell=True))
print("TEST FAILED: sqlite big table got hash " + repr(h))
subprocess.check_output("tar -zcf /tmp/debug-sql.tar.gz .", cwd=tempdir, shell=True)
exit(0)