test.py (8906B)
1 #!/usr/bin/env python3 2 3 import os, subprocess, tempfile 4 5 # Empty 6 with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir: 7 os.mkdir(tempdir+'/test') 8 os.mkdir(tempdir+'/test/foo') 9 h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip() 10 if h == b'dc99f8161ccf245e178102a00264e4f4f43cd0048ea525b6c9e226777414352f': 11 print("test passed: empty\n", flush=True) 12 else: 13 print("TEST FAILED: empty: got hash " + repr(h) + "\n", flush=True) 14 exit(1) 15 16 # Plain text file 17 with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir: 18 os.mkdir(tempdir+'/test') 19 os.mkdir(tempdir+'/test/foo') 20 os.system('echo a > '+tempdir+'/test/foo/x') 21 h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip() 22 if h == b'6b393b2233479ccc54975f83f4de0d39592d5ab78cd02b19597e7bbe97f43cf1': 23 print("test passed: plain text file\n", flush=True) 24 else: 25 print("TEST FAILED: plain text file: got hash " + repr(h) + "\n", flush=True) 26 exit(1) 27 28 # Plain text file and empty folder in subdirectory 29 with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir: 30 os.mkdir(tempdir+'/test') 31 os.mkdir(tempdir+'/test/foo') 32 os.mkdir(tempdir+'/test/foo/bar') 33 os.mkdir(tempdir+'/test/foo/baz') 34 os.mkdir(tempdir+'/test/foo/baz/quux') 35 os.system('echo a > '+tempdir+'/test/foo/baz/titi') 36 h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip() 37 if h == b'0bb2f31bf05eb215ebef32abcc62cddbfad2d8b0a1221bb335da0acaf3455558': 38 print("test passed: plain text and empty folder in subdirectory\n", flush=True) 39 else: 40 print("TEST FAILED: plain text and empty folder in subdirectory: got hash " + repr(h) + "\n", flush=True) 41 exit(1) 42 43 # Git directories 44 with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir: 45 os.mkdir(tempdir+'/test') 46 os.mkdir(tempdir+'/test/foo') 47 os.mkdir(tempdir+'/test/foo/bar') 48 os.mkdir(tempdir+'/test/foo/baz') 49 os.mkdir(tempdir+'/test/foo/baz/quux') 50 os.system('git init '+tempdir+'/test/foo/baz/git_workdir -b branchname --quiet') 51 os.system('git init '+tempdir+'/test/foo/baz/git_workdir_empty -b branchname --quiet') 52 os.system('git init --bare '+tempdir+'/test/foo/baz/git_bare -b branchname --quiet') 53 os.system('git init --bare '+tempdir+'/test/foo/baz/git_bare_empty -b branchname --quiet') 54 os.system('cd '+tempdir+'/test/foo/baz/git_workdir && echo a > toto') 55 os.system('cd '+tempdir+'/test/foo/baz/git_workdir && git add toto') 56 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') 57 os.system('cd '+tempdir+'/test/foo/baz/git_workdir && git push ../git_bare branchname --quiet') 58 os.system('echo a > '+tempdir+'/test/foo/baz/titi') 59 h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip() 60 if h == b'f31eb7e1bcb25e79be0d1305d58eeadbe3fd9bf38ecbd0449789e8c91b5f4340': 61 print("test passed: git\n", flush=True) 62 else: 63 print("TEST FAILED: git: got hash " + repr(h) + "\n", flush=True) 64 exit(1) 65 66 # Sqlite 67 with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir: 68 os.mkdir(tempdir+'/test') 69 os.mkdir(tempdir+'/test/foo') 70 os.mkdir(tempdir+'/test/foo/bar') 71 os.mkdir(tempdir+'/test/foo/baz') 72 os.mkdir(tempdir+'/test/foo/baz/quux') 73 os.system('git init '+tempdir+'/test/foo/baz/git_workdir -b branchname --quiet') 74 os.system('git init '+tempdir+'/test/foo/baz/git_workdir_empty -b branchname --quiet') 75 os.system('git init --bare '+tempdir+'/test/foo/baz/git_bare -b branchname --quiet') 76 os.system('git init --bare '+tempdir+'/test/foo/baz/git_bare_empty -b branchname --quiet') 77 os.system('cd '+tempdir+'/test/foo/baz/git_workdir && echo a > toto') 78 os.system('cd '+tempdir+'/test/foo/baz/git_workdir && git add toto') 79 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') 80 os.system('cd '+tempdir+'/test/foo/baz/git_workdir && git push ../git_bare branchname --quiet') 81 # It seems that sqlite databases are quite reproducible; running the same command produces identical files! 82 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table digits(d);"') 83 for i in range(10): 84 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "insert into digits(d) values('+str(i)+');"') 85 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table tbl(x);"') 86 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;"') 87 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table rnd(x);"') 88 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "insert into rnd(x) select x from tbl order by random();"') 89 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table tbl2(x);"') 90 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "insert into tbl2(x) select x from rnd order by x;"') 91 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "drop table rnd;"') 92 #os.system('sqlite3 '+tempdir+'/test/foo/baz/db "vacuum;"') 93 os.system('echo a > '+tempdir+'/test/foo/baz/titi') 94 h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip() 95 if h == b'b775b5c3ad1b403c08fa88e43be42bd76143f93c26bf42cb8881c595161a5509': 96 print("test passed: sqlite\n", flush=True) 97 else: 98 print("TEST FAILED: sqlite got hash " + repr(h) + "\n", flush=True) 99 exit(1) 100 101 # Sqlite big table 102 with tempfile.TemporaryDirectory(prefix="test", dir="/tmp") as tempdir: 103 os.mkdir(tempdir+'/test') 104 os.mkdir(tempdir+'/test/foo') 105 os.mkdir(tempdir+'/test/foo/bar') 106 os.mkdir(tempdir+'/test/foo/baz') 107 os.mkdir(tempdir+'/test/foo/baz/quux') 108 os.system('git init '+tempdir+'/test/foo/baz/git_workdir -b branchname --quiet') 109 os.system('git init '+tempdir+'/test/foo/baz/git_workdir_empty -b branchname --quiet') 110 os.system('git init --bare '+tempdir+'/test/foo/baz/git_bare -b branchname --quiet') 111 os.system('git init --bare '+tempdir+'/test/foo/baz/git_bare_empty -b branchname --quiet') 112 os.system('cd '+tempdir+'/test/foo/baz/git_workdir && echo a > toto') 113 os.system('cd '+tempdir+'/test/foo/baz/git_workdir && git add toto') 114 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') 115 os.system('cd '+tempdir+'/test/foo/baz/git_workdir && git push ../git_bare branchname --quiet') 116 # It seems that sqlite databases are quite reproducible; running the same command produces identical files! 117 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table digits(d);"') 118 for i in range(10): 119 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "insert into digits(d) values('+str(i)+');"') 120 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table tbl(x);"') 121 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "insert into tbl(x) select d0.d * 1000000 + d1.d * 100000 + d2.d * 10000 + d3.d * 1000 + d4.d * 100 + d5.d * 10 + d6.d from digits d0, digits d1, digits d2, digits d3, digits d4, digits d5, digits d6;"') 122 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table rnd(x);"') 123 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "insert into rnd(x) select x from tbl order by random();"') 124 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "create table tbl2(x);"') 125 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "insert into tbl2(x) select x from rnd order by x;"') 126 os.system('sqlite3 '+tempdir+'/test/foo/baz/db "drop table rnd;"') 127 #os.system('sqlite3 '+tempdir+'/test/foo/baz/db "vacuum;"') 128 os.system('echo a > '+tempdir+'/test/foo/baz/titi') 129 h = subprocess.check_output([os.path.abspath('./hash-files.py'), 'test/foo'], cwd=tempdir).strip() 130 if h == b'7d6917fef222456552b6359ddc4eee235a0cdca089c0a6d9b4b2f6a747987eb9': 131 print("test passed: sqlite big table\n", flush=True) 132 else: 133 print("TEST FAILED: sqlite big table got hash " + repr(h) + "\n", flush=True) 134 exit(1)